Skip to content

Instantly share code, notes, and snippets.

@charles-dyfis-net
Created August 30, 2018 15:17
Show Gist options
  • Save charles-dyfis-net/ad0d6eb0729ee91ad3cd40a601b9bc2a to your computer and use it in GitHub Desktop.
Save charles-dyfis-net/ad0d6eb0729ee91ad3cd40a601b9bc2a to your computer and use it in GitHub Desktop.
gpg-agent yubikey / gpg tooling
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
enable-ssh-support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment