Skip to content

Instantly share code, notes, and snippets.

@frimik
Created September 25, 2019 08:44
Show Gist options
  • Save frimik/e14abe3214402342d8ad27ea3a647e3b to your computer and use it in GitHub Desktop.
Save frimik/e14abe3214402342d8ad27ea3a647e3b to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# get_secret (c) 2019 Fulhack industries.
#
# Author: Mikael Fridh
#
# Half inspired half stolen from somewhere, can't remember exactly.
#
# Use in scripts like:
# get_secret vpn_password "VPN password" [username]
#
#
# Openconnect example:
#
# get_secret vpn_password "VPN Password" | exec sudo openconnect "${VPN_URL}" -u $(USER}" --passwd-on-stdin
#
# Requirements (Ubuntu):
#
# sudo apt install libsecret-tools
usage()
{
echo "usage: get_secret \"login\" \"Human readable label\" [username]"
exit 1
}
# Unique id for this login
LOGIN=$1
shift
# Human readable label
LABEL=$1
shift
# the username (optional) defaults to $USER
USERNAME=$1
shift
if [ -z "$LOGIN" -o -z "$LABEL" ]; then
usage
fi
USERNAME=${USERNAME:-${USER}}
ST=/usr/bin/secret-tool
get_password() {
$ST lookup "$LOGIN" "$USERNAME"
}
password=$( get_password )
if [ "$password" = "" ]; then
$ST store --label "$LABEL" "$LOGIN" "$USERNAME"
password=$( get_password )
fi
if [ "$password" = "" ]; then
echo "ERROR: Failed to fetch password!"
else
echo "$password"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment