Last active
November 5, 2023 02:59
-
-
Save AriaFallah/fe7b651ba2652bd301334e011749e4b2 to your computer and use it in GitHub Desktop.
MacOS security CLI wrapper
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
#!/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