Skip to content

Instantly share code, notes, and snippets.

@shinji257
Forked from Speedy37/gpg-agent-relay.sh
Created August 18, 2023 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinji257/3e18b3e9254e69ebbfbe92ea7128a8ff to your computer and use it in GitHub Desktop.
Save shinji257/3e18b3e9254e69ebbfbe92ea7128a8ff to your computer and use it in GitHub Desktop.
WSL2 gpg agent relay (Yubikey)
#!/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"
USERNAME=Vincent
# I use the same username for wsl and windows, but feel free to modify the paths below if that isn't the case
WIN_GPGDIR="C:/Users/${USERNAME}/AppData/Roaming/gnupg"
NPIPERELAY="${HOME}/npiperelay.exe"
PIDFILE="${GPGDIR}/.gpg-agent-relay.pid"
OLDPID=$(cat "${PIDFILE}")
if [ ! -z "${OLDPID}" ]; then
ps -p "${OLDPID}" >/dev/null && exit 0
fi
rm -f "${GPGDIR}/S.gpg-agent" "${GPGDIR}/S.gpg-agent.ssh"
# 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=$!
echo $AGENTPID > ${PIDFILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment