Skip to content

Instantly share code, notes, and snippets.

@AriaFallah
Last active November 5, 2023 02:59
Show Gist options
  • Save AriaFallah/fe7b651ba2652bd301334e011749e4b2 to your computer and use it in GitHub Desktop.
Save AriaFallah/fe7b651ba2652bd301334e011749e4b2 to your computer and use it in GitHub Desktop.
MacOS security CLI wrapper
#!/usr/bin/env bash
KEYCHAIN="secrets.keychain"
main () {
if [[ -z "$1" ]]; then
print_usage
fi
case "$1" in
ls) list_secrets ;;
get) get_secret "$2" ;;
set) set_secret "$2" "$3" ;;
rm) delete_secret "$2" ;;
*) print_usage ;;
esac
}
list_secrets() {
security dump-keychain $KEYCHAIN | grep 0x00000007 | awk -F= '{print $2}' | tr -d \"
}
get_secret() {
if [[ -z "$1" ]]; then
print_usage
fi
security find-generic-password -a $USER -s "$1" -w $KEYCHAIN
}
set_secret() {
if [[ -z "$1" ]] || [[ -z "$2" ]]; then
print_usage
fi
security add-generic-password -D secret -U -a $USER -s "$1" -w "$2" $KEYCHAIN
}
delete_secret() {
if [[ -z "$1" ]]; then
print_usage
fi
security delete-generic-password -a $USER -s "$1" $KEYCHAIN
}
print_usage() {
cat << EOF
Usage:
sec set <name> <value>
sec get <name>
sec rm <name>
sec ls
EOF
exit 0
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment