summaryrefslogtreecommitdiffstats
path: root/nixos/tests/yggdrasil.nix
blob: 9086d1108fe2664d86aaf6aa0a78e1fe6b782780 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
let
  privateKeys = {
    alice = ''
      -----BEGIN PRIVATE KEY-----
      MC4CAQAwBQYDK2VwBCIEIKhn+eB45M5Y0xDPWs1GItdZ4qId8H4db8OAoqJkiUgN
      -----END PRIVATE KEY-----
    '';
    bob = ''
      -----BEGIN PRIVATE KEY-----
      MC4CAQAwBQYDK2VwBCIEIAxKJKzTQCcizpJ37RefSgS4lbSVhkk8Jfuu1gZT2FfW
      -----END PRIVATE KEY-----
    '';
  };

  aliceIp6 = "202:b70:9b0b:cf34:f93c:8f18:bbfd:7034";
  aliceKeys = {
    PublicKey = "3e91ec9e861960d86e1ce88051f97c435bdf2859640ab681dfa906eb45ad5182";
  };
  # Frank has a legacy keys.json that should be migrated
  # This is the same key as Alice but in the old hex format
  frankIp6 = aliceIp6; # Should get same IP after migration
  frankLegacyKeys = {
    # The old format stored PrivateKey as 128 hex chars (64 bytes = seed + pubkey)
    # This corresponds to Alice's key
    PrivateKey =
      "a867f9e078e4ce58d310cf5acd4622d759e2a21df07e1d6fc380a2a26489480d" + aliceKeys.PublicKey;
    PublicKey = aliceKeys.PublicKey;
  };
  bobIp6 = "202:a483:73a4:9f2d:a559:4a19:bc9:8458";
  bobPrefix = "302:a483:73a4:9f2d";
  bobConfig = {
    InterfacePeers = {
      eth1 = [ "tcp://192.168.1.200:12345" ];
    };
    MulticastInterfaces = [
      {
        Regex = ".*";
        Beacon = true;
        Listen = true;
        Port = 54321;
        Priority = 0;
      }
    ];
    PublicKey = "2b6f918b6c1a4b54d6bcde86cf74e074fb32ead4ee439b7930df2aa60c825186";
  };
  danIp6 = bobPrefix + "::2";

in
{ pkgs, ... }:
{
  name = "yggdrasil";
  meta = with pkgs.lib.maintainers; {
    maintainers = [ gazally ];
  };

  nodes = {
    # Alice is listening for peerings on a specified port,
    # but has multicast peering disabled.  Alice has part of her
    # yggdrasil config in Nix and part of it in a file.
    alice =
      { ... }:
      {
        networking = {
          interfaces.eth1.ipv4.addresses = [
            {
              address = "192.168.1.200";
              prefixLength = 24;
            }
          ];
          firewall.allowedTCPPorts = [
            80
            12345
          ];
        };
        services.httpd.enable = true;
        services.httpd.adminAddr = "foo@example.org";

        services.yggdrasil = {
          enable = true;
          settings = {
            Listen = [ "tcp://0.0.0.0:12345" ];
            MulticastInterfaces = [ ];
            PrivateKeyPath = "${pkgs.writeText "private" (privateKeys.alice)}";
          }
          // aliceKeys;
        };
      };

    # Bob is set up to peer with Alice, and also to do local multicast
    # peering.  Bob's yggdrasil config is in a file.
    bob =
      { ... }:
      {
        networking.firewall.allowedTCPPorts = [ 54321 ];
        services.yggdrasil = {
          enable = true;
          openMulticastPort = true;
          settings = bobConfig // {
            PrivateKeyPath = pkgs.writeText "private" (privateKeys.bob);
          };
        };

        boot.kernel.sysctl."net.ipv6.conf.all.forwarding" = 1;

        networking = {
          bridges.br0.interfaces = [ ];
          interfaces.br0 = {
            ipv6.addresses = [
              {
                address = bobPrefix + "::1";
                prefixLength = 64;
              }
            ];
          };
        };

        # dan is a node inside a container running on bob's host.
        containers.dan = {
          autoStart = true;
          privateNetwork = true;
          hostBridge = "br0";
          config = {
            networking.interfaces.eth0.ipv6 = {
              addresses = [
                {
                  address = bobPrefix + "::2";
                  prefixLength = 64;
                }
              ];
              routes = [
                {
                  address = "200::";
                  prefixLength = 7;
                  via = bobPrefix + "::1";
                }
              ];
            };
            services.httpd.enable = true;
            services.httpd.adminAddr = "foo@example.org";
            networking.firewall.allowedTCPPorts = [ 80 ];
            nix.enable = false; # disabled by default on the test's host. See all-tests.nix / tag(no-nix-by-default)
          };
        };
      };

    # Carol only does local peering.  Carol's yggdrasil config is all Nix.
    carol =
      { ... }:
      {
        networking.firewall.allowedTCPPorts = [ 43210 ];
        services.yggdrasil = {
          enable = true;
          extraArgs = [
            "-loglevel"
            "error"
          ];
          denyDhcpcdInterfaces = [ "ygg0" ];
          settings = {
            IfTAPMode = true;
            IfName = "ygg0";
            MulticastInterfaces = [
              {
                Port = 43210;
              }
            ];
          };
          openMulticastPort = true;
          persistentKeys = true;
        };
      };

    # Eve uses persistentKeys for automatic key generation.
    eve =
      { ... }:
      {
        networking.firewall.allowedTCPPorts = [ 43211 ];
        services.yggdrasil = {
          enable = true;
          persistentKeys = true;
          openMulticastPort = true;
          settings = {
            IfName = "ygg0";
            MulticastInterfaces = [
              {
                Regex = ".*";
                Beacon = true;
                Listen = true;
                Port = 43211;
              }
            ];
          };
        };
      };

    # Frank tests migration from legacy keys.json format
    frank =
      { pkgs, ... }:
      {
        networking.firewall.allowedTCPPorts = [ 43212 ];

        # Pre-populate the legacy keys.json file before the service starts
        systemd.services.yggdrasil-legacy-keys = {
          wantedBy = [ "yggdrasil-persistent-keys.service" ];
          before = [ "yggdrasil-persistent-keys.service" ];

          serviceConfig = {
            Type = "oneshot";
            RemainAfterExit = true;
          };

          script = ''
            mkdir -p /var/lib/yggdrasil
            cat > /var/lib/yggdrasil/keys.json << 'EOF'
            ${builtins.toJSON frankLegacyKeys}
            EOF
            chmod 600 /var/lib/yggdrasil/keys.json
          '';
        };

        services.yggdrasil = {
          enable = true;
          persistentKeys = true;
          openMulticastPort = true;
          settings = {
            IfName = "ygg0";
            MulticastInterfaces = [
              {
                Regex = ".*";
                Beacon = true;
                Listen = true;
                Port = 43212;
              }
            ];
          };
        };
      };
  };

  testScript =
    # python
    ''
      import re

      # Give Alice a head start so she is ready when Bob calls.
      alice.start()
      alice.wait_for_unit("yggdrasil.service")

      bob.start()
      carol.start()
      eve.start()
      bob.wait_for_unit("default.target")
      carol.wait_for_unit("yggdrasil.service")

      # Eve uses persistentKeys - verify the key generation service ran
      eve.wait_for_unit("yggdrasil-persistent-keys.service")
      eve.wait_for_unit("yggdrasil.service")
      eve.succeed("test -f /var/lib/yggdrasil/private.pem")
      eve.succeed("grep -q 'BEGIN PRIVATE KEY' /var/lib/yggdrasil/private.pem")

      ip_addr_show = "ip -o -6 addr show dev ygg0 scope global"
      carol.wait_until_succeeds(f"[ `{ip_addr_show} | grep -v tentative | wc -l` -ge 1 ]")
      carol_ip6 = re.split(" +|/", carol.succeed(ip_addr_show))[3]

      eve.wait_until_succeeds(f"[ `{ip_addr_show} | grep -v tentative | wc -l` -ge 1 ]")
      eve_ip6 = re.split(" +|/", eve.succeed(ip_addr_show))[3]

      # If Alice can talk to Carol, then Bob's outbound peering and Carol's
      # local peering have succeeded and everybody is connected.
      alice.wait_until_succeeds(f"ping -c 1 {carol_ip6}")
      alice.succeed("ping -c 1 ${bobIp6}")

      bob.succeed("ping -c 1 ${aliceIp6}")
      bob.succeed(f"ping -c 1 {carol_ip6}")

      carol.succeed("ping -c 1 ${aliceIp6}")
      carol.succeed("ping -c 1 ${bobIp6}")
      carol.succeed("ping -c 1 ${bobPrefix}::1")
      carol.succeed("ping -c 8 ${danIp6}")

      carol.fail("journalctl -u dhcpcd | grep ygg0")

      # Eve should be able to communicate with the network via multicast peering
      eve.wait_until_succeeds(f"ping -c 1 {carol_ip6}")
      carol.wait_until_succeeds(f"ping -c 1 {eve_ip6}")

      alice.wait_for_unit("httpd.service")
      carol.succeed("curl --fail -g http://[${aliceIp6}]")
      carol.succeed("curl --fail -g http://[${danIp6}]")

      with subtest("legacy key migration"):
        frank.start()
        # Frank tests migration from legacy keys.json format
        frank.wait_for_unit("yggdrasil-persistent-keys.service")
        frank.wait_for_unit("yggdrasil.service")
        # Verify migration happened: private.pem should exist
        frank.succeed("test -f /var/lib/yggdrasil/private.pem")
        frank.succeed("grep -q 'BEGIN PRIVATE KEY' /var/lib/yggdrasil/private.pem")
        # Legacy file should still exist (not deleted, user should verify and remove)
        frank.succeed("test -f /var/lib/yggdrasil/keys.json")

        # Verify Frank got the expected IP after migration (same key as Alice = same IP)
        frank.wait_until_succeeds(f"[ `{ip_addr_show} | grep -v tentative | wc -l` -ge 1 ]")
        frank_ip6 = re.split(" +|/", frank.succeed(ip_addr_show))[3]
        assert frank_ip6 == "${frankIp6}", f"Frank's IP {frank_ip6} doesn't match expected ${frankIp6} after migration"
    '';
}