1{ system ? builtins.currentSystem }:
2
3with import ../lib/testing.nix { inherit system; };
4with import ../lib/qemu-flags.nix;
5with pkgs.lib;
6
7let
8 image =
9 (import ../lib/eval-config.nix {
10 inherit system;
11 modules = [
12 ../maintainers/scripts/ec2/amazon-hvm-config.nix
13 ../../nixos/modules/testing/test-instrumentation.nix
14 { boot.initrd.kernelModules = [ "virtio" "virtio_blk" "virtio_pci" "virtio_ring" ]; }
15 ];
16 }).config.system.build.amazonImage;
17
18 makeEc2Test = { name, userData, script, hostname ? "ec2-instance", sshPublicKey ? null }:
19 let
20 metaData = pkgs.stdenv.mkDerivation {
21 name = "metadata";
22 buildCommand = ''
23 mkdir -p $out/2011-01-01
24 ln -s ${pkgs.writeText "userData" userData} $out/2011-01-01/user-data
25 mkdir -p $out/1.0/meta-data
26 echo "${hostname}" > $out/1.0/meta-data/hostname
27 '' + optionalString (sshPublicKey != null) ''
28 mkdir -p $out/1.0/meta-data/public-keys/0
29 ln -s ${pkgs.writeText "sshPublicKey" sshPublicKey} $out/1.0/meta-data/public-keys/0/openssh-key
30 '';
31 };
32 in makeTest {
33 name = "ec2-" + name;
34 nodes = {};
35 testScript =
36 ''
37 use File::Temp qw/ tempfile /;
38 my ($fh, $filename) = tempfile();
39
40 `qemu-img create -f qcow2 -o backing_file=${image}/nixos.img $filename`;
41
42 my $startCommand = "qemu-kvm -m 768 -net nic -net 'user,net=169.254.0.0/16,guestfwd=tcp:169.254.169.254:80-cmd:${pkgs.micro-httpd}/bin/micro_httpd ${metaData}'";
43 $startCommand .= " -drive file=" . Cwd::abs_path($filename) . ",if=virtio,werror=report";
44 $startCommand .= " \$QEMU_OPTS";
45
46 my $machine = createMachine({ startCommand => $startCommand });
47 ${script}
48 '';
49 };
50
51 snakeOilPrivateKey = [
52 "-----BEGIN EC PRIVATE KEY-----"
53 "MHcCAQEEIHQf/khLvYrQ8IOika5yqtWvI0oquHlpRLTZiJy5dRJmoAoGCCqGSM49"
54 "AwEHoUQDQgAEKF0DYGbBwbj06tA3fd/+yP44cvmwmHBWXZCKbS+RQlAKvLXMWkpN"
55 "r1lwMyJZoSGgBHoUahoYjTh9/sJL7XLJtA=="
56 "-----END EC PRIVATE KEY-----"
57 ];
58
59 snakeOilPublicKey = pkgs.lib.concatStrings [
60 "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHA"
61 "yNTYAAABBBChdA2BmwcG49OrQN33f/sj+OHL5sJhwVl2Qim0vkUJQCry1zFpKTa"
62 "9ZcDMiWaEhoAR6FGoaGI04ff7CS+1yybQ= snakeoil"
63 ];
64in {
65 boot-ec2-nixops = makeEc2Test {
66 name = "nixops-userdata";
67 sshPublicKey = snakeOilPublicKey; # That's right folks! My user's key is also the host key!
68
69 userData = ''
70 SSH_HOST_DSA_KEY_PUB:${snakeOilPublicKey}
71 SSH_HOST_DSA_KEY:${pkgs.lib.concatStringsSep "|" snakeOilPrivateKey}
72 '';
73 script = ''
74 $machine->start;
75 $machine->waitForFile("/root/user-data");
76 $machine->waitForUnit("sshd.service");
77
78 # We have no keys configured on the client side yet, so this should fail
79 $machine->fail("ssh -o BatchMode=yes localhost exit");
80
81 # Let's install our client private key
82 $machine->succeed("mkdir -p ~/.ssh");
83 ${concatMapStrings (s: "$machine->succeed('echo ${s} >> ~/.ssh/id_ecdsa');") snakeOilPrivateKey}
84 $machine->succeed("chmod 600 ~/.ssh/id_ecdsa");
85
86 # We haven't configured the host key yet, so this should still fail
87 $machine->fail("ssh -o BatchMode=yes localhost exit");
88
89 # Add the host key; ssh should finally succeed
90 $machine->succeed("echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts");
91 $machine->succeed("ssh -o BatchMode=yes localhost exit");
92
93 $machine->shutdown;
94 '';
95 };
96
97 boot-ec2-config = makeEc2Test {
98 name = "config-userdata";
99 sshPublicKey = snakeOilPublicKey;
100
101 userData = ''
102 ### http://nixos.org/channels/nixos-unstable nixos
103 {
104 imports = [
105 <nixpkgs/nixos/modules/virtualisation/amazon-image.nix>
106 <nixpkgs/nixos/modules/testing/test-instrumentation.nix>
107 ];
108 environment.etc.testFile = {
109 text = "whoa";
110 };
111 }
112 '';
113 script = ''
114 $machine->start;
115 $machine->waitForFile("/etc/testFile");
116 $machine->succeed("cat /etc/testFile | grep -q 'whoa'");
117 '';
118 };
119}