Skip to content

Instantly share code, notes, and snippets.

@charles-dyfis-net
Last active April 8, 2022 15:07
Show Gist options
  • Save charles-dyfis-net/750da4513b781526d5fc673e8b7f06cb to your computer and use it in GitHub Desktop.
Save charles-dyfis-net/750da4513b781526d5fc673e8b7f06cb to your computer and use it in GitHub Desktop.
GnuPG/SSH agent setup for inclusion in bashrc on MacOS
do_gpg_setup() {
local ssh_add_err
if gpg-connect-agent /bye &>/dev/null && [[ -e "$HOME/.ssh_auth_sock" ]] && { ssh_add_err=$(SSH_AUTH_SOCK=$HOME/.ssh_auth_sock ssh-add -l 2>&1) || [[ $ssh_add_err = "The agent has no identities." ]]; }; then
export SSH_AUTH_SOCK=$HOME/.ssh_auth_sock
return # Nothing to do
else
[[ $orig_GPG_AGENT_INFO ]] && GPG_AGENT_INFO=$orig_GPG_AGENT_INFO gpg-connect-agent killagent /bye
[[ $GPG_AGENT_INFO ]] && GPG_AGENT_INFO=$GPG_AGENT_INFO gpg-connect-agent killagent /bye
fi
local cmds
cmds="$(gpg-agent --daemon --enable-ssh-support)" || return
eval "$cmds"
if [[ $SSH_AUTH_SOCK ]]; then
export ssh_agent_is_gpg_agent=1
orig_SSH_AUTH_SOCK=$SSH_AUTH_SOCK
ln -s -f -- "$orig_SSH_AUTH_SOCK" $HOME/.ssh_auth_sock
SSH_AUTH_SOCK=$HOME/.ssh_auth_sock
export orig_SSH_AUTH_SOCK
fi
if [[ $GPG_AGENT_INFO ]]; then
: "${orig_GPG_AGENT_INFO:=$GPG_AGENT_INFO}"
IFS=: read -r gpg_agent_file gpg_agent_pid _ <<<"$GPG_AGENT_INFO"
ln -s -f -- "$gpg_agent_file" "$HOME/.gpg_agent_lock"
GPG_AGENT_INFO="$HOME/.gpg_agent_lock:${GPG_AGENT_INFO#*:}"
export orig_GPG_AGENT_INFO
fi
}
do_gpg_setup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment