Created
April 11, 2022 09:09
-
-
Save abeluck/b915b244612196967f0ad9e3c4946ace to your computer and use it in GitHub Desktop.
A script for forwarding your gpg-agent to gitpod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
opt=${1:-} | |
usage() { | |
echo "$0 - ssh into a remote gitpod with your gpg-agent forwarded" | |
echo | |
echo "usage: $0 [option]" | |
echo | |
echo "Options are one of:" | |
echo " --help / -h This help" | |
echo " --ssh SSH directly into the gitpod instance with your gpg-agent forwarded" | |
echo " --configure Output the ssh configuration file to STDOUT" | |
echo | |
echo "Before running this script you should have a local vscode instance open" | |
echo "and connected to a remote gitpod workspace." | |
echo "This script will attempt to find the latest gitpod ssh config that the vscode" | |
echo "extension writes to /tmp/gitpod_ssh_config*" | |
} | |
if [[ "$opt" == "--help" || "$opt" == "-h" ]]; then | |
usage | |
exit 0 | |
fi | |
function generate_config() { | |
set +e | |
if ! gpg --card-status &> /dev/null; then | |
echo "Error: No gpg smartcard detected" | |
exit 1 | |
fi | |
set -e | |
GPG_AGENT_EXTRA=$(gpgconf --list-dir agent-extra-socket) | |
GITPOD_SSH_CONFIG=$(find /tmp -maxdepth 1 -name "gitpod_ssh_config*" -print0 |\ | |
xargs -r -0 ls -1 -t | \ | |
head -1) | |
if [ ! -f "$GITPOD_SSH_CONFIG" ]; then | |
echo "Error: No gitpod ssh config found in /tmp" | |
echo "Are you connected to a gitpod?" | |
exit 1 | |
fi | |
PORT_IDFILE=$(cat $GITPOD_SSH_CONFIG | awk -v RS= -v FS=\\n -v IGNORECASE=1 ' | |
{ | |
ip = "" | |
alias = "" | |
id_file = "" | |
port = "" | |
for (j = 1; j <= NF; ++j) { | |
split($j, tmp, " ") | |
if (tmp[1] == "Host") { alias = tmp[2] } | |
if (tmp[1] == "Hostname") { ip = tmp[2] } | |
if (tmp[1] == "IdentityFile") { id_file = tmp[2] } | |
if (tmp[1] == "Port") { port = tmp[2] } | |
} | |
if (ip || alias && alias != "*") { | |
print port "\t" id_file | |
} | |
} | |
'); | |
read port idfile <<< "$PORT_IDFILE" | |
cat << EOF | |
# | |
# To use this config: | |
# 1. Connect to a remote gitpod workspace with vscode | |
# 2. Inside the gitpod run: | |
gpgconf --kill all | |
rm /home/gitpod/.gnupg/S.gpg-agent | |
# 3. Locally run 'ssh -F gitpod.ssh gitpod' where gitpod.ssh is the output of the script | |
# | |
Host gitpod | |
HostName 127.0.0.1 | |
User gitpod | |
Port $port | |
IdentityFile $idfile | |
RemoteForward /home/gitpod/.gnupg/S.gpg-agent $GPG_AGENT_EXTRA | |
IdentitiesOnly yes | |
EOF | |
} | |
if [[ "$opt" == "--ssh" ]]; then | |
ssh_config="$(generate_config)" | |
tdir=$(mktemp -d "${TMPDIR:-/tmp/}$(basename $0).XXXXXXXXXXXX") | |
echo "$ssh_config" > "$tdir/gitpod.ssh" | |
ssh -t -F "$tdir/gitpod.ssh" gitpod "gpgconf --kill all && rm /home/gitpod/.gnupg/S.gpg-agent" &> /dev/null | |
set +e | |
ssh -F "$tdir/gitpod.ssh" gitpod | |
rm -rf "$tdir" | |
elif [[ "$opt" == "--configure" ]]; then | |
generate_config | |
else | |
usage | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment