# -*- mode: sh -*- # op-ssh-add will add SSH keys to the agent, but will fetch the # passphrase in 1Password rather than prompt you for it. # # To be found, keys must have the following three things: # # 1. Have the password field set to the passphrase of the key, # 2. Have an `SSH' tag set, and # 3. Have a `fingerprint' field set to the fingerprint of the key. # # You must also have the 1Password CLI tool (op) and jq installed. if [[ ${@[1]} =~ '-[DdKLlTXx]' ]]; then $(which -p ssh-add) $@ return $? fi local fp prv pub for prv (${@[-1]} ~/.ssh/id_{ecdsa,ed25519}{,_sk}); do if [[ -f ${prv}.pub ]]; then pub=${prv}.pub fp=$(cut -d' ' -f2 <<< $(ssh-keygen -f ${pub} -l)) break fi done if [[ -z ${pub} ]]; then $(which -p ssh-add) $@ return $? fi local session=$(op signin --raw) if [[ -z ${session} ]]; then return 1 fi zmodload zsh/zpty zpty ssh $(which -p ssh-add) $@ zpty -w ssh $(op list items --session ${session} --tags SSH \ | op get item \ --fields fingerprint,password \ --session ${session} - \ | jq -jr "select(.fingerprint == \"${fp}\") | .password") zpty -r ssh zpty -d ssh op signout --session ${session}