From 3cc8cc1f84692846eccc8c7bc3308d998f2106b6 Mon Sep 17 00:00:00 2001 From: Adam Hellberg Date: Fri, 14 Feb 2020 02:59:42 +0100 Subject: [PATCH] Add files for WSL GPG forwarding --- bin/wsl-gpg-agent-relay.sh | 32 ++++++++++++++++++++++++++++++++ zsh/.zshrc | 23 ++++++++++++++++++++--- 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100755 bin/wsl-gpg-agent-relay.sh diff --git a/bin/wsl-gpg-agent-relay.sh b/bin/wsl-gpg-agent-relay.sh new file mode 100755 index 0000000..9613111 --- /dev/null +++ b/bin/wsl-gpg-agent-relay.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# Launches socat+npiperelay to relay the gpg-agent socket file for use in WSL +# See https://justyn.io/blog/using-a-yubikey-for-gpg-in-windows-10-wsl-windows-subsystem-for-linux/ for details + +GPGDIR="${HOME}/.gnupg" +WINDOWS_USERNAME=adamh +WSL_USERNAME=sharparam +WIN_GPGDIR="C:/Users/${WINDOWS_USERNAME}/AppData/Roaming/gnupg" +NPIPERELAY="/mnt/e/apps/npiperelay.exe" +PIDFILE="${GPGDIR}/.gpg-agent-relay.pid" +OLDPID=$(cat "${PIDFILE}" 2>/dev/null) + +# Launch socat+npiperelay for the regular gpg-agent +if [ ! -z "${OLDPID}" ]; then + ps -p "${OLDPID}" >/dev/null && \ + echo "gpg-agent-relay.sh already running with process id $(cat ${PIDFILE})" && \ + exit 0 +fi + +rm -f "${GPGDIR}/S.gpg-agent*" +echo $$ > ${PIDFILE} + +# Relay the regular gpg-agent socket for gpg operations +socat UNIX-LISTEN:"${GPGDIR}/S.gpg-agent,fork" EXEC:"${NPIPERELAY} -ep -ei -s -a '${WIN_GPGDIR}/S.gpg-agent'",nofork & +AGENTPID=$! + +# Relay the gpg ssh-agent +socat UNIX-LISTEN:"${GPGDIR}/S.gpg-agent.ssh,fork" EXEC:"${NPIPERELAY} -ep -ei -s -a '${WIN_GPGDIR}/S.gpg-agent.ssh'",nofork & +SSHPID=$! + +wait ${AGENTPID} +wait ${SSHPID} diff --git a/zsh/.zshrc b/zsh/.zshrc index d6146fd..330401b 100644 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -196,9 +196,26 @@ if [[ -d "$HOME/.poetry/bin" ]]; then fi export GPG_TTY="$(tty)" -unset SSH_AGENT_PID -export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket) -gpgconf --launch gpg-agent +if [[ $is_wsl ]]; then + wsl_gpg_agent_relay="$HOME/.local/bin/wsl-gpg-agent-relay.sh" + if [[ -f "$wsl_gpg_agent_relay" ]]; then + echo '[WSL] Launching WSL GPG agent relay' + $wsl_gpg_agent_relay & + else + echo "[WSL] Missing: ${wsl_gpg_agent_relay}" + fi + unset SSH_AGENT_PID + export SSH_AUTH_SOCK=$HOME/.local/wsl-ssh-pageant/ssh-agent.sock +else + unset SSH_AGENT_PID + export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket) + gpgconf --launch gpg-agent +fi + +if [[ -d '/usr/local/go/bin' ]]; then + path=("$path[@]" /usr/local/go/bin) +fi +#export GOPATH="$HOME/go" # tmux helpers ts() { -- 2.51.2