Cache credentials using the Linux keyring/MacOS security in Go
For a detailed blogpost see: https://www.davids-blog.gamba.ca/posts/caching-credentials-linux-keyring-golang.html
Password cache also comes as a handy CLI that works for both Linux and MacOS. When called from bash, it will handle the user prompt.
#!/bin/bash
# Allow interactive operation
./password-cache mykey -t 60
if [[ $? == 0 ]]; then
# Read from store
password=$(./password-cache mykey -t 60 --print)
# Use
echo "|$password|"
fi
Then we call it the first time:
$ bash bash-script.sh Enter 'mykey' password: |password|
The second time around it just retrieves the key as expected:
$ bash bash-script.sh |password|
-
Install using homebrew:
brew tap DavidGamba/dgtools https://github.com/DavidGamba/dgtools brew install DavidGamba/dgtools/password-cache
NoteCompletion is auto setup for bash.
For
zsh
completions, an additional step is required, add the following to your.zshrc
:export ZSHELL="true" source "$(brew --prefix)/share/zsh/site-functions/dgtools.password-cache.zsh"
Upgrade with:
brew update brew upgrade password-cache
-
Install using go:
Install the binary into your
~/go/bin
:go install github.com/DavidGamba/dgtools/password-cache@latest
Then setup the completion.
For bash:
complete -o default -C password-cache password-cache
For zsh:
export ZSHELL="true" autoload -U +X compinit && compinit autoload -U +X bashcompinit && bashcompinit complete -o default -C password-cache password-cache