Instantly share code, notes, and snippets.

@AriaFallah /sec.sh
Last active Jun 14, 2018

Embed
What would you like to do?
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