diff options
| author | tv <tv@krebsco.de> | 2026-11-21 16:17:35 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-25 22:53:22 +0100 |
| commit | 595f6e3f69702aa50f17d37eb49d9050257307a0 (patch) | |
| tree | fdd2a8ffe357e7dd425a564193c024d7abb089ec /tests/vm-host.nix | |
| parent | fe46f7703e6145af9d3c9eb43d6ded19b316c83c (diff) | |
tests: the run script in qemu, and a NixOS VM test of modules/host
Both were only in the sandbox the project was developed in so far.
tests/qemu builds the run script with a qemu stand-in for Firecracker
(same kernel, initrd, drives, vsock via vhost-device-vsock; network via
qemu's user networking, the guest's port 22 on 127.0.0.1:2222) and a
test layer running a script at the end of boot, its output on the
console. The run script still takes a tap, so it needs modules/host
where it runs.
tests/vm-host.nix is a NixOS VM test of modules/host, with network
namespaces on veths in place of taps as guests: a VM's static address
by name, tcp:// rules over IPv4 and IPv6 and by name, DNS, anti-spoofing,
POST /check, the path units and the timer.
Diffstat (limited to 'tests/vm-host.nix')
| -rw-r--r-- | tests/vm-host.nix | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/vm-host.nix b/tests/vm-host.nix new file mode 100644 index 0000000..04f66de --- /dev/null +++ b/tests/vm-host.nix @@ -0,0 +1,118 @@ +# NixOS VM test of modules/host: the module for real (systemd units, path +# units, timer, nftables, dnsmasq, nginx's checks), with network +# namespaces on veths in place of tv's taps fctap1 and fctap2 as guests: +# g1 as the VM box, with box's address (172.16.0.10, MAC 06:00:ac:10:00:0a), +# g2 without a name, with fctap2's own (172.16.0.202, ...:ca); SLAAC gives +# them the IPv6 addresses the host expects from those MACs. Another netns +# is an upstream with 42::1 and 198.51.100.1. Covers static addresses by +# name, the tcp:// rules (IPv4, IPv6, names), DNS, IPv6 port 443, +# anti-spoofing, POST /check; not the HTTPS proxy. +# +# nix-build tests/vm-host.nix # needs kvm +# nix-build -E '(import ./tests/vm-host.nix {}).driver' -o driver +# ./driver/bin/nixos-test-driver -o out # without kvm: TCG, slow +{ pkgs ? import <nixpkgs> {} }: +pkgs.testers.runNixOSTest { + name = "fc-host"; + nodes.host = { pkgs, ... }: { + # alice: fctap0, tv: fctap1 and fctap2 + imports = [ (import ../modules/host { + users = { tv = 2; alice = 1; }; + vms.box = { user = "tv"; ip = "172.16.0.10"; }; + }) ]; + users.users.tv.isNormalUser = true; + users.users.alice.isNormalUser = true; + networking.useNetworkd = true; + networking.nameservers = [ "127.0.0.1" ]; + networking.hosts."42::1" = [ "sshhost6.test" ]; + networking.hosts."198.51.100.1" = [ "sshhost.test" ]; + # As the host's firewall has to (see README.md). + networking.firewall.trustedInterfaces = [ "fcbr0" ]; + environment.systemPackages = [ pkgs.socat pkgs.dig pkgs.curl ]; + virtualisation.memorySize = 1024; + }; + testScript = '' + g1_ip6 = "fdfc:fc::400:acff:fe10:a" + g2_ip6 = "fdfc:fc::400:acff:fe10:ca" + + host.wait_for_unit("multi-user.target") + host.wait_for_unit("fcbr0-dnsmasq.service") + host.wait_for_unit("fcbr0-firewall.service") + # box on fctap1, as the run script does it + host.succeed("su tv -c 'echo box > /run/fc-egress/fctap1.name'") + host.wait_until_succeeds("test \"$(head -n 1 /run/fcbr0-addr/fctap1)\" = 172.16.0.10", timeout=60) + host.succeed(""" + set -e + guest() { # N TAP IP MAC + ip link del $2 + ip netns add g$1 + ip link add $2 type veth peer name eth0 netns g$1 + ip link set $2 master fcbr0 up + ip netns exec g$1 sysctl -qw net.ipv6.conf.all.use_tempaddr=0 net.ipv6.conf.default.use_tempaddr=0 + ip netns exec g$1 ip link set eth0 address $4 + ip netns exec g$1 ip link set lo up + ip netns exec g$1 ip addr add $3/24 dev eth0 + ip netns exec g$1 ip link set eth0 up + ip netns exec g$1 ip route add default via 172.16.0.1 + } + guest 1 fctap1 172.16.0.10 06:00:ac:10:00:0a + guest 2 fctap2 172.16.0.202 06:00:ac:10:00:ca + ip netns add U + ip link add up0 type veth peer name eth0 netns U + ip link set up0 up + ip addr add 42::fe/16 dev up0 nodad + ip addr add 198.51.100.254/24 dev up0 + ip netns exec U ip link set lo up + ip netns exec U ip link set eth0 up + ip netns exec U ip addr add 42::1/16 dev eth0 nodad + ip netns exec U ip addr add 198.51.100.1/24 dev eth0 + ip netns exec U ip -6 route add default via 42::fe + ip netns exec U ip route add default via 198.51.100.254 + """) + host.succeed("ip netns exec U socat TCP6-LISTEN:22,fork,reuseaddr SYSTEM:'echo ssh-ok' >&2 &") + host.succeed("ip netns exec U socat TCP6-LISTEN:443,fork,reuseaddr SYSTEM:'echo https-direct' >&2 &") + # networkd can take minutes under TCG + host.wait_until_succeeds(f"ip netns exec g1 ip -6 addr show dev eth0 | grep -q '{g1_ip6}/64'", timeout=300) + host.wait_until_succeeds(f"ip netns exec g2 ip -6 addr show dev eth0 | grep -q '{g2_ip6}/64'", timeout=60) + + def tcp(g, dst, port, extra=""): + return host.succeed(f"ip netns exec {g} timeout 5 socat -T3 - TCP:{dst}:{port}{extra} </dev/null 2>/dev/null || echo blocked").strip() + + def dig(g, t, name): + return host.execute(f"ip netns exec {g} dig +short {t} {name} @172.16.0.1")[1].strip() + + def check_uris(g, *uris): + body = "\\n".join(uris) + return host.succeed(f"printf '{body}\\n' | ip netns exec {g} curl -sS --data-binary @- http://172.16.0.1/check").split() + + def check(what, got, want): + print(f"{what}: {got}") + assert got == want, f"{what}: got {got!r}, want {want!r}" + + # rules via fc-egress and the path units + host.succeed("su tv -c 'fc-egress -l box allow tcp://[42::1]:22 tcp://[42::1]:443 tcp://sshhost6.test:22 tcp://198.51.100.1:22'") + host.fail("su tv -c 'fc-egress -l box allow tcp://[42::1/129]:22'") + host.succeed("su tv -c 'echo box > /run/fc-egress/fctap1'") + host.wait_until_succeeds("nft list set inet firecracker allow6_fctap1 | grep -q '42::1'", timeout=60) + + check("host reaches the upstream", host.succeed("timeout 5 socat -T3 - TCP6:[42::1]:22 </dev/null").strip(), "ssh-ok") + check("g1 [42::1]:22, allowed", tcp("g1", "[42::1]", 22), "ssh-ok") + check("g2 [42::1]:22, not allowed", tcp("g2", "[42::1]", 22), "blocked") + check("g1 [42::1]:443, IPv6 HTTPS refused", tcp("g1", "[42::1]", 443), "blocked") + check("g1 198.51.100.1:22, IPv4", tcp("g1", "198.51.100.1", 22), "ssh-ok") + check("g1 AAAA of a rule's name", dig("g1", "AAAA", "sshhost6.test"), "42::1") + check("g1 A of another name", dig("g1", "A", "other.test"), "172.16.0.1") + check("g1 AAAA of another name", dig("g1", "AAAA", "other.test"), "") + check("g1 checks", check_uris("g1", "tcp://[42::1]:22", "tcp://[42::1]:23", "tcp://[42::1]:443", "tcp://sshhost6.test:22", "tcp://198.51.100.1:22"), + ["true", "false", "false", "true", "true"]) + check("g2 checks", check_uris("g2", "tcp://[42::1]:22"), ["false"]) + host.succeed(f"ip netns exec g2 ip addr add {g1_ip6}/64 dev eth0 nodad") + check("g2 sending as g1", tcp("g2", "[42::1]", 22, f",bind=[{g1_ip6}]"), "blocked") + check("g1 to the host's IPv6 address", tcp("g1", "[fdfc:fc::1]", 22), "blocked") + host.succeed("su tv -c 'fc-egress -l box deny tcp://[42::1]:22 tcp://sshhost6.test:22'") + host.wait_until_fails("nft list set inet firecracker allow6_fctap1 | grep -q '42::1 . tcp . 22'", timeout=60) + check("g1 [42::1]:22 after deny", tcp("g1", "[42::1]", 22), "blocked") + check("g1 checks after deny", check_uris("g1", "tcp://[42::1]:22"), ["false"]) + host.succeed("systemctl list-timers fcbr0-firewall-refresh.timer --no-pager | grep -q fcbr0-firewall-refresh") + ''; +} |
