Skip to content

Commit cf94467

Browse files
author
Matthew Fisher
committed
fix(builder): generate key on boot
This change generates keys on boot, as well as storing the keys in etcd such that the key will persist between builder reboots. BREAKING CHANGE: since the key is no longer generated on `docker build`, the first time the user runs this they may receive a key conflict warning with the old builder keys. Since this change now saves it to etcd, the user should remove the old key from their known_hosts file and accept the new key. On reboots, the newly generated key should stay the same.
1 parent 1c9d34c commit cf94467

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

builder/rootfs/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ RUN curl -sSL https://get.docker.com/builds/Linux/x86_64/docker-1.5.0 -o /usr/bi
3131

3232
# configure ssh server
3333
RUN mkdir -p /var/run/sshd && rm -rf /etc/ssh/ssh_host*
34-
RUN /usr/bin/ssh-keygen -A
3534

3635
# install git and configure gituser
3736
ENV GITHOME /home/git

builder/rootfs/bin/boot

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ function etcd_safe_mkdir {
3939
set -e
4040
}
4141

42+
function etcd_set_default_stdin {
43+
set +e
44+
ERROR=$(etcdctl --no-sync -C $ETCD mk $ETCD_PATH/$1 2>&1 >/dev/null)
45+
if [[ $? -ne 0 && $(echo $ERROR | grep -ive "key already exists") ]]; then
46+
echo "etcd_set_default_stdin: an etcd error occurred ($ERROR)"
47+
echo "aborting..."
48+
exit 1
49+
fi
50+
set -e
51+
}
52+
53+
function etcd_get {
54+
etcdctl --no-sync -C $ETCD get $ETCD_PATH/$1
55+
}
56+
4257
etcd_safe_mkdir $ETCD_PATH/users
4358

4459
# wait for confd to run once and install initial templates
@@ -67,13 +82,36 @@ DOCKER_PID=$!
6782

6883
# wait for docker to start
6984
while [[ ! -e /var/run/docker.sock ]]; do
70-
sleep 1
85+
sleep 1
7186
done
7287

7388
# build required images
7489
docker build -t deis/slugbuilder /usr/local/src/slugbuilder/
7590
docker build -t deis/slugrunner /usr/local/src/slugrunner/
7691

92+
function gen_host_keys {
93+
if ! etcd_get sshHostKey; then
94+
# generate the keys, then set them up in etcd
95+
/usr/bin/ssh-keygen -A
96+
for type in dsa ecdsa ed25519 rsa; do
97+
cat "/etc/ssh/ssh_host_${type}_key" | etcd_set_default_stdin "sshHost${type}Key"
98+
cat "/etc/ssh/ssh_host_${type}_key.pub" | etcd_set_default_stdin "sshHost${type}PubKey"
99+
done
100+
cat "etc/ssh/ssh_host_key" | etcd_set_default_stdin sshHostKey
101+
cat "/etc/ssh/ssh_host_key.pub" | etcd_set_default_stdin sshHostPubKey
102+
else
103+
# pull the keys from etcd
104+
for type in dsa ecdsa ed25519 rsa; do
105+
etcd_get "sshHost${type}Key" > "/etc/ssh/ssh_host_${type}_key"
106+
etcd_get "sshHost${type}PubKey" > "/etc/ssh/ssh_host_${type}_key.pub"
107+
done
108+
etcd_get sshHostKey > /etc/ssh/ssh_host_key
109+
etcd_get sshHostPubKey > /etc/ssh/ssh_host_key.pub
110+
fi
111+
}
112+
113+
gen_host_keys
114+
77115
# start an SSH daemon to process `git push` requests
78116
/usr/sbin/sshd -D -e &
79117
SSHD_PID=$!

0 commit comments

Comments
 (0)