vuls: init at 0.27.0
[NixPkgs.git] / nixos / tests / systemd-networkd.nix
bloba595fb9cba4ac93619afdd054c6dc14aaa27466e
1 let generateNodeConf = { lib, pkgs, config, privk, pubk, peerId, nodeId, ...}: {
2       imports = [ common/user-account.nix ];
3       systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug";
4       networking.useNetworkd = true;
5       networking.useDHCP = false;
6       networking.firewall.enable = false;
7       virtualisation.vlans = [ 1 ];
8       environment.systemPackages = with pkgs; [ wireguard-tools ];
9       systemd.network = {
10         enable = true;
11         config = {
12           routeTables.custom = 23;
13         };
14         netdevs = {
15           "90-wg0" = {
16             netdevConfig = { Kind = "wireguard"; Name = "wg0"; };
17             wireguardConfig = {
18               # NOTE: we're storing the wireguard private key in the
19               #       store for this test. Do not do this in the real
20               #       world. Keep in mind the nix store is
21               #       world-readable.
22               PrivateKeyFile = pkgs.writeText "wg0-priv" privk;
23               ListenPort = 51820;
24               FirewallMark = 42;
25             };
26             wireguardPeers = [ {
27               Endpoint = "192.168.1.${peerId}:51820";
28               PublicKey = pubk;
29               PresharedKeyFile = pkgs.writeText "psk.key" "yTL3sCOL33Wzi6yCnf9uZQl/Z8laSE+zwpqOHC4HhFU=";
30               AllowedIPs = [ "10.0.0.${peerId}/32" ];
31               PersistentKeepalive = 15;
32             } ];
33           };
34         };
35         networks = {
36           "99-nope" = {
37             matchConfig.Name = "eth*";
38             linkConfig.Unmanaged = true;
39           };
40           "90-wg0" = {
41             matchConfig = { Name = "wg0"; };
42             address = [ "10.0.0.${nodeId}/32" ];
43             routes = [
44               { Gateway = "10.0.0.${nodeId}"; Destination = "10.0.0.0/24"; }
45               { Gateway = "10.0.0.${nodeId}"; Destination = "10.0.0.0/24"; Table = "custom"; }
46             ];
47           };
48           "30-eth1" = {
49             matchConfig = { Name = "eth1"; };
50             address = [
51               "192.168.1.${nodeId}/24"
52               "fe80::${nodeId}/64"
53             ];
54             routingPolicyRules = [
55               { Table = 10; IncomingInterface = "eth1"; Family = "both"; }
56               { Table = 20; OutgoingInterface = "eth1"; }
57               { Table = 30; From = "192.168.1.1"; To = "192.168.1.2"; SourcePort = 666 ; DestinationPort = 667; }
58               { Table = 40; IPProtocol = "tcp"; InvertRule = true; }
59               { Table = 50; IncomingInterface = "eth1"; Family = "ipv4"; }
60               { Table = 60; FirewallMark = 4; }
61               { Table = 70; FirewallMark = "16/0x1f"; }
62             ];
63           };
64         };
65       };
66     };
67 in import ./make-test-python.nix ({pkgs, ... }: {
68   name = "networkd";
69   meta = with pkgs.lib.maintainers; {
70     maintainers = [ picnoir ];
71   };
72   nodes = {
73     node1 = { pkgs, ... }@attrs:
74     let localConf = {
75         privk = "GDiXWlMQKb379XthwX0haAbK6hTdjblllpjGX0heP00=";
76         pubk = "iRxpqj42nnY0Qz8MAQbSm7bXxXP5hkPqWYIULmvW+EE=";
77         nodeId = "1";
78         peerId = "2";
79     };
80     in generateNodeConf (attrs // localConf);
82     node2 = { pkgs, ... }@attrs:
83     let localConf = {
84         privk = "eHxSI2jwX/P4AOI0r8YppPw0+4NZnjOxfbS5mt06K2k=";
85         pubk = "27s0OvaBBdHoJYkH9osZpjpgSOVNw+RaKfboT/Sfq0g=";
86         nodeId = "2";
87         peerId = "1";
88     };
89     in generateNodeConf (attrs // localConf);
90   };
91 testScript = ''
92     start_all()
93     node1.wait_for_unit("systemd-networkd-wait-online.service")
94     node2.wait_for_unit("systemd-networkd-wait-online.service")
96     # ================================
97     # Networkd Config
98     # ================================
99     node1.succeed("grep RouteTable=custom:23 /etc/systemd/networkd.conf")
100     node1.succeed("sudo ip route show table custom | grep '10.0.0.0/24 via 10.0.0.1 dev wg0 proto static'")
102     # ================================
103     # Wireguard
104     # ================================
105     node1.succeed("ping -c 5 10.0.0.2")
106     node2.succeed("ping -c 5 10.0.0.1")
107     # Is the fwmark set?
108     node2.succeed("wg | grep -q 42")
110     # ================================
111     # Routing Policies
112     # ================================
113     # Testing all the routingPolicyRuleConfig members:
114     # Table + IncomingInterface
115     node1.succeed("sudo ip rule | grep 'from all iif eth1 lookup 10'")
116     # OutgoingInterface
117     node1.succeed("sudo ip rule | grep 'from all oif eth1 lookup 20'")
118     # From + To + SourcePort + DestinationPort
119     node1.succeed(
120         "sudo ip rule | grep 'from 192.168.1.1 to 192.168.1.2 sport 666 dport 667 lookup 30'"
121     )
122     # IPProtocol + InvertRule
123     node1.succeed("sudo ip rule | grep 'not from all ipproto tcp lookup 40'")
124     # FirewallMark without a mask
125     node1.succeed("sudo ip rule | grep 'from all fwmark 0x4 lookup 60'")
126     # FirewallMark with a mask
127     node1.succeed("sudo ip rule | grep 'from all fwmark 0x10/0x1f lookup 70'")