Skip to content

Instantly share code, notes, and snippets.

@peter-mcconnell
Last active December 19, 2020 09:19
Show Gist options
  • Save peter-mcconnell/2b37cb2609b61d324fcbd48aea6e64a6 to your computer and use it in GitHub Desktop.
Save peter-mcconnell/2b37cb2609b61d324fcbd48aea6e64a6 to your computer and use it in GitHub Desktop.
docker ssh with github pubkey auth

docker (ubuntu) with openssh - SSH auth using github keys

If you're already using your host for ssh communication with git (git clone / push to github using ssh) then you should be set:

docker build -t=test .
docker run --rm -v "$SSH_AUTH_SOCK:/ssh-agent" -e SSH_AUTH_SOCK=/ssh-agent -p 2222:22 -ti test

ssh -X -p 2222 root@localhost

If you don't want to bother building it, for convenience I have pushed this to pemcconnell/sshdemo on github:

docker run --rm -v "$SSH_AUTH_SOCK:/ssh-agent" -e SSH_AUTH_SOCK=/ssh-agent -p 2222:22 -ti pemcconnell/sshdemo

ssh -X -p 2222 root@localhost
FROM ubuntu:20.04
LABEL "MAINTAINER" "Peter McConnell <[email protected]>"
SHELL ["/bin/bash", "-eo", "pipefail", "-c"]
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive \
apt-get install -yq --no-install-recommends \
xauth \
openssh-server \
ca-certificates \
openssl \
curl
COPY ./entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
RUN mkdir -p /run/sshd
COPY sshd_config /etc/ssh/sshd_config
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
#!/usr/bin/env bash
set -e
trap cleanup 0
# allow bypassing of user detection
GITHUB_USERNAME="${GITHUB_USERNAME:-False}"
# ensure we have an authorized_keys
AUTH_KEYS="${HOME}/.ssh/authorized_keys"
mkdir -p "${HOME}/.ssh"
touch "$AUTH_KEYS"
chmod 0600 "$AUTH_KEYS"
function cleanup() {
echo "cleaning up!"
}
function getuser() {
if [ "$GITHUB_USERNAME" = "False" ]; then
ssh-keyscan -H github.com >> "${HOME}/.ssh/known_hosts" 2> /dev/null
GITHUB_USERNAME="$(ssh -T [email protected] 2>&1 | grep -o "^Hi .*!" | sed -e "s/^Hi //" -e "s/\!$//")"
fi
echo "$GITHUB_USERNAME"
}
function getpubs() {
local user
user="$1"
echo "fetching public keys from github.com/${user}.keys ..."
curl -s "https://github.com/${user}.keys" >> "$AUTH_KEYS"
}
echo "configuring ssh access ..."
getpubs "$(getuser)"
echo "executing command ..."
"${@}" &
wait $!
Include /etc/ssh/sshd_config.d/*.conf
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
#LogLevel INFO
# Authentication:
LoginGraceTime 60m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
PubkeyAuthentication yes
# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
X11UseLocalhost no
PermitTTY yes
PrintMotd yes
#PrintLastLog yes
TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# Banner /etc/ssh/banner
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
# override default of no subsystems
Subsystem sftp /usr/lib/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
X11Forwarding yes
AllowTcpForwarding yes
PermitTTY yes
# ForceCommand cvs server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment