Created
September 25, 2019 08:44
-
-
Save frimik/e14abe3214402342d8ad27ea3a647e3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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