Skip to content

Instantly share code, notes, and snippets.

@tolgahanakgun
Created March 28, 2023 17:24
Show Gist options
  • Save tolgahanakgun/4f66ad45774ad64817bc9c4a85751a65 to your computer and use it in GitHub Desktop.
Save tolgahanakgun/4f66ad45774ad64817bc9c4a85751a65 to your computer and use it in GitHub Desktop.
Relay forwarded ssh-agent to docker
#!/bin/bash
set -ex
set -o pipefail
# THIS MUST BE USED WITH AN SSH-AGENT THAT SUPPORTS CONFIRMATION/NOTIFICATION UPON A SIGNATURE REQUEST.
# Mount the sock to the docker
# docker run --user $(id -u):$(id -g) --rm -it -v "$HOME/.ssh/sshsock:/sshsock" -e SSH_AUTH_SOCK=/sshsock/sock ubuntu bash
ssh_sock_in_user_dir="$HOME/.ssh/sshsock/sock"
ssh_sock_in_tmp_dir="$(find /tmp ! -readable -prune -o -maxdepth 2 -type s -name "agent*" -user $USER -printf '%T@ %p\n' 2>/dev/null | sort -n | tail -1 | cut -d' ' -f2)"
[ -n "$ssh_sock_in_tmp_dir" ] || (echo "Cannot find agent, exiting"; exit 1)
mkdir -p "$(dirname $ssh_sock_in_user_dir)"
socat -dd UNIX-LISTEN:"$ssh_sock_in_user_dir",user=$(id -u),group=$(id -g),mode=0660,fork UNIX-CLIENT:"$ssh_sock_in_tmp_dir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment