Skip to content

Instantly share code, notes, and snippets.

@fuzzy
Last active January 8, 2021 15:42
Show Gist options
  • Save fuzzy/268300fa5a6bafbb4919482273463fc9 to your computer and use it in GitHub Desktop.
Save fuzzy/268300fa5a6bafbb4919482273463fc9 to your computer and use it in GitHub Desktop.
Keybase secret management tool
#!/bin/sh
KBPW_IS_USER=0
KBPW_TEAMNAME=
KBPW_NAMESPACE="kbpw"
test -z "$(which jq 2>/dev/null)" && (
printf "\033[1;31mERROR\033[0m: This scripts depends on jq being installed."
exit 1
)
while getopts ":hUT:" opt; do
case ${opt} in
h)
cat >/dev/stderr <<EOF
Usage:
$(basename ${0}) -h Display this help message.
$(basename ${0}) (ARGS) <CMD> (ARGS)
Arguments:
-T TEAMNAME Specify the team or username.
-U Specify the name is a username not a team.
Commands:
list List the keys for the given user/team.
set Set a key's value.
get Get a key's value.
del Delete a key.
Examples:
$(basename ${0}) set mail_yahoo_com_pw supersecretvalue
$(basename ${0}) get mail_yahoo_com_pw
EOF
exit 0
;;
U)
KBPW_IS_USER=1
;;
T):
KBPW_TEAMNAME="${OPTARG}"
;;
esac
done
shift $((${OPTIND} - 1))
if test -z "${KBPW_TEAMNAME}"; then
KBPW_TEAMNAME="${USER},${USER}"
KBPW_IS_USER=1
fi
if test ${KBPW_IS_USER} -eq 1; then
if test -z "$(echo ${KBPW_TEAMNAME}|grep ',')"; then
KBPW_TEAMNAME="${KBPW_TEAMNAME},${KBPW_TEAMNAME}"
fi
fi
api_exec() {
fn=$(mktemp)
printf "${1}" >${fn}
echo $(eval "$(which keybase) kvstore api -m $(cat ${fn}) && rm -f ${fn}")
}
case "${1}" in
list)
api_exec "'{\"method\": \"list\", \"params\": {\"options\": {\"team\": \"${KBPW_TEAMNAME}\", \"namespace\": \"kbpw\"}}}'"|$(which jq) .result.entryKeys|awk '/entryKey/ {print $2}'|awk -F'"' '{print "\033[1;32m*\033[0m "$2}'
;;
set)
test -z "${2}" || test -z "${3}" && exec $(basename ${0}) -h
api_exec "'{\"method\": \"put\", \"params\": {\"options\": {\"team\": \"${KBPW_TEAMNAME}\", \"namespace\": \"kbpw\", \"entryKey\": \"${2}\", \"entryValue\": \"${3}\"}}}'" >/dev/null
;;
get)
test -z "${2}" && exec $(basename ${0}) -h
api_exec "'{\"method\": \"get\", \"params\": {\"options\": {\"team\": \"${KBPW_TEAMNAME}\", \"namespace\": \"kbpw\", \"entryKey\": \"${2}\"}}}'"|jq .result.entryValue|awk -F'"' '{print $2}'
;;
del)
test -z "${2}" && exec $(basename ${0}) -h
api_exec "'{\"method\": \"del\", \"params\": {\"options\": {\"team\": \"${KBPW_TEAMNAME}\", \"namespace\": \"kbpw\", \"entryKey\": \"${2}\"}}}'" >/dev/null
;;
*)
exec ${0} -h
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment