summary refs log tree commit diff
path: root/nixos/modules/virtualisation/ec2-data.nix
blob: 93a83a3e42af4dfda0dba2218833e414adcf1ee3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# This module defines a systemd service that obtains the SSH key and
# host name of virtual machines running on Amazon EC2, Eucalyptus and
# OpenStack Compute (Nova).

{ config, lib, pkgs, ... }:

with lib;

{
  options = {
    ec2.metadata = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Whether to allow access to EC2 metadata.
      '';
    };
  };

  config = {

    systemd.services."fetch-ec2-data" =
      { description = "Fetch EC2 Data";

        wantedBy = [ "multi-user.target" "sshd.service" ];
        before = [ "sshd.service" ];
        wants = [ "ip-up.target" ];
        after = [ "ip-up.target" ];

        path = [ pkgs.wget pkgs.iproute ];

        script =
          ''
            ip route del blackhole 169.254.169.254/32 || true

            wget="wget -q --retry-connrefused -O -"

            echo "setting host name..."
            ${optionalString (config.networking.hostName == "") ''
              ${pkgs.nettools}/bin/hostname $($wget http://169.254.169.254/1.0/meta-data/hostname)
            ''}

            # 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..."
                mkdir -p /root/.ssh
                $wget http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key > /root/key.pub
                if [ $? -eq 0 -a -e /root/key.pub ]; then
                    if ! grep -q -f /root/key.pub /root/.ssh/authorized_keys; then
                        cat /root/key.pub >> /root/.ssh/authorized_keys
                        echo "new key added to authorized_keys"
                    fi
                    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.
            $wget http://169.254.169.254/2011-01-01/user-data > /root/user-data || true
            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
                mkdir -m 0755 -p /etc/ssh
                (umask 077; echo "$key" > /etc/ssh/ssh_host_dsa_key)
                echo "$key_pub" > /etc/ssh/ssh_host_dsa_key.pub
            fi

            ${optionalString (! config.ec2.metadata) ''
            # Since the user data is sensitive, prevent it from being
            # accessed from now on.
            ip route add blackhole 169.254.169.254/32
            ''}
          '';

        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
            ${pkgs.openssh}/bin/ssh-keygen -l -f /etc/ssh/ssh_host_dsa_key.pub > /dev/console
            echo "-----END SSH HOST KEY FINGERPRINTS-----" > /dev/console
          '';
        serviceConfig.Type = "oneshot";
        serviceConfig.RemainAfterExit = true;
      };

  };
}