2013-10-31 13:26:06 +01:00
|
|
|
# This module defines a systemd service that obtains the SSH key and
|
|
|
|
# host name of virtual machines running on Amazon EC2, Eucalyptus and
|
2011-04-06 17:09:34 +02:00
|
|
|
# OpenStack Compute (Nova).
|
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2011-04-06 17:09:34 +02:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2013-09-04 13:05:09 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
config = {
|
|
|
|
|
2015-09-23 00:03:13 +02:00
|
|
|
systemd.services.fetch-ec2-data =
|
2013-09-04 13:05:09 +02:00
|
|
|
{ description = "Fetch EC2 Data";
|
|
|
|
|
2014-07-30 17:55:30 +02:00
|
|
|
wantedBy = [ "multi-user.target" "sshd.service" ];
|
2013-09-04 13:05:09 +02:00
|
|
|
before = [ "sshd.service" ];
|
2014-07-30 17:55:30 +02:00
|
|
|
wants = [ "ip-up.target" ];
|
|
|
|
after = [ "ip-up.target" ];
|
2013-09-04 13:05:09 +02:00
|
|
|
|
2014-07-30 17:55:30 +02:00
|
|
|
path = [ pkgs.wget pkgs.iproute ];
|
2013-09-04 13:05:09 +02:00
|
|
|
|
|
|
|
script =
|
|
|
|
''
|
2014-07-30 17:55:30 +02:00
|
|
|
wget="wget -q --retry-connrefused -O -"
|
2013-09-04 13:05:09 +02:00
|
|
|
|
|
|
|
${optionalString (config.networking.hostName == "") ''
|
2015-05-04 16:56:46 +02:00
|
|
|
echo "setting host name..."
|
2014-07-30 17:55:30 +02:00
|
|
|
${pkgs.nettools}/bin/hostname $($wget http://169.254.169.254/1.0/meta-data/hostname)
|
2013-09-04 13:05:09 +02:00
|
|
|
''}
|
|
|
|
|
|
|
|
# Don't download the SSH key if it has already been injected
|
|
|
|
# into the image (a Nova feature).
|
|
|
|
if ! [ -e /root/.ssh/authorized_keys ]; then
|
|
|
|
echo "obtaining SSH key..."
|
2015-01-15 18:36:38 +01:00
|
|
|
mkdir -m 0700 -p /root/.ssh
|
2014-07-30 17:55:30 +02:00
|
|
|
$wget http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key > /root/key.pub
|
2013-09-04 13:05:09 +02:00
|
|
|
if [ $? -eq 0 -a -e /root/key.pub ]; then
|
2015-09-28 13:31:28 +02:00
|
|
|
cat /root/key.pub >> /root/.ssh/authorized_keys
|
|
|
|
echo "new key added to authorized_keys"
|
2013-09-04 13:05:09 +02:00
|
|
|
chmod 600 /root/.ssh/authorized_keys
|
|
|
|
rm -f /root/key.pub
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Extract the intended SSH host key for this machine from
|
|
|
|
# the supplied user data, if available. Otherwise sshd will
|
|
|
|
# generate one normally.
|
2014-07-30 17:55:30 +02:00
|
|
|
$wget http://169.254.169.254/2011-01-01/user-data > /root/user-data || true
|
2015-09-23 00:03:13 +02:00
|
|
|
|
|
|
|
mkdir -m 0755 -p /etc/ssh
|
|
|
|
|
2013-09-04 13:05:09 +02:00
|
|
|
key="$(sed 's/|/\n/g; s/SSH_HOST_DSA_KEY://; t; d' /root/user-data)"
|
|
|
|
key_pub="$(sed 's/SSH_HOST_DSA_KEY_PUB://; t; d' /root/user-data)"
|
|
|
|
if [ -n "$key" -a -n "$key_pub" -a ! -e /etc/ssh/ssh_host_dsa_key ]; then
|
|
|
|
(umask 077; echo "$key" > /etc/ssh/ssh_host_dsa_key)
|
|
|
|
echo "$key_pub" > /etc/ssh/ssh_host_dsa_key.pub
|
|
|
|
fi
|
2015-09-23 00:03:13 +02:00
|
|
|
|
|
|
|
key="$(sed 's/|/\n/g; s/SSH_HOST_ED25519_KEY://; t; d' /root/user-data)"
|
|
|
|
key_pub="$(sed 's/SSH_HOST_ED25519_KEY_PUB://; t; d' /root/user-data)"
|
|
|
|
if [ -n "$key" -a -n "$key_pub" -a ! -e /etc/ssh/ssh_host_ed25519_key ]; then
|
|
|
|
(umask 077; echo "$key" > /etc/ssh/ssh_host_ed25519_key)
|
|
|
|
echo "$key_pub" > /etc/ssh/ssh_host_ed25519_key.pub
|
|
|
|
fi
|
2013-09-04 13:05:09 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services."print-host-key" =
|
|
|
|
{ description = "Print SSH Host Key";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "sshd.service" ];
|
|
|
|
script =
|
|
|
|
''
|
|
|
|
# Print the host public key on the console so that the user
|
|
|
|
# can obtain it securely by parsing the output of
|
|
|
|
# ec2-get-console-output.
|
|
|
|
echo "-----BEGIN SSH HOST KEY FINGERPRINTS-----" > /dev/console
|
2015-09-24 10:36:50 +02:00
|
|
|
for i in /etc/ssh/ssh_host_*_key.pub; do
|
|
|
|
${config.programs.ssh.package}/bin/ssh-keygen -l -f $i > /dev/console
|
|
|
|
done
|
2013-09-04 13:05:09 +02:00
|
|
|
echo "-----END SSH HOST KEY FINGERPRINTS-----" > /dev/console
|
|
|
|
'';
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2011-04-06 17:09:34 +02:00
|
|
|
}
|