infra/codestorage/hg-ssh

Paddy 2015-10-15 Parent:2f4a2a20ad6d Child:4c6afe37e83a

5:bf7b66df555f Go to Latest

infra/codestorage/hg-ssh/pullkeys.sh

This time with bonus host key pinning. You know what's _super obnoxious_? Getting a MITM warning just because a Docker container restarted. So, basically, every time you set up an SSH server, it generated its own public/private keypair. And the first time you connect, you'd get that public key from it, and store it locally. Then if you tried connecting later, and a different public key was used, OpenSSH would vomit an error in your face and stop the connection, because it thought you were running into a man-in-the-middle attack. Which is generally a Good Thing. But when we apply this to Docker, it becomes problematic. I'm not subject to a MITM attack, my container just restarted, you silly machine. The correct way around this problem was to reuse the public/private key pair as part of the image. So I put the public keys in the image itself, and used Kubernetes secrets to mount the private keys at /data/ssh/, and then a script to copy them all over into the /etc/ssh directory, where they'd override the generated ones. This means that every instance of this container has the same public/private key combo, meaning no more warning. That solves the problem in a reasonably secure (I think) way when the image is only used by us, but if anyone else wants to use the image, suddenly they need to edit it. They need to change the public keys, because they don't have our private keys.

History
1 #!/bin/bash
2 DOMAIN=${DOMAIN:-code.secondbit.org}
3 SSH_KEYS_BUCKET=${SSH_KEYS_BUCKET:-sshkeys.$DOMAIN}
5 mkdir -p /tmp/sshkeys
7 echo "Cleaning up..."
8 rm -rf /tmp/sshkeys/*
10 echo "Downloading keys from gs://${SSH_KEYS_BUCKET}/"
12 output=$(gsutil cp -R gs://$SSH_KEYS_BUCKET/\* /tmp/sshkeys/ 2>&1)
13 echo $output
15 keys=$(find /tmp/sshkeys -name '*.pub')
17 for key in $keys
18 do
19 dir=$(dirname $key)
20 stripped=${dir#.}
21 stripped=${stripped#/tmp/sshkeys}
22 target=${key#/tmp/sshkeys}
23 target=${target%.pub}
24 target=${target#/}
25 IFS='-' read -ra USERSPEC <<< $target
26 if [ -d "/home${USERSPEC[0]}" ]
27 then
28 echo "User ${USERSPEC[0]} already exists, skipping."
29 else
30 echo "Creating user ${USERSPEC[0]} with ID ${USERSPEC[1]}."
31 /bin/bash /usr/local/bin/helpers/create_user.sh "${USERSPEC[0]}" "${USERSPEC[1]}"
32 cat $key > /home/${USERSPEC[0]}/.ssh/authorized_keys
33 fi
34 done
36 echo "Cleaning up..."
37 rm -rf /tmp/sshkeys/*
39 echo "SSH key pull complete."