diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/qemu/debug.nix | 18 | ||||
| -rw-r--r-- | tests/qemu/default.nix | 25 | ||||
| -rw-r--r-- | tests/qemu/firecracker.sh | 27 | ||||
| -rw-r--r-- | tests/vm-host.nix | 118 |
4 files changed, 188 insertions, 0 deletions
diff --git a/tests/qemu/debug.nix b/tests/qemu/debug.nix new file mode 100644 index 0000000..a8790de --- /dev/null +++ b/tests/qemu/debug.nix @@ -0,0 +1,18 @@ +# A layer for testing in qemu (see default.nix): a root shell script run +# at the end of boot, whose output goes to the console (the test's +# stdout). (virtio_pci, which qemu needs, is in the base system.) +script: { pkgs, config, lib, ... }: { + systemd.services.fc-test = { + wantedBy = [ "multi-user.target" ]; + after = [ "multi-user.target" ]; + path = [ pkgs.util-linux pkgs.coreutils "/run/current-system/sw" ]; + environment = lib.mkIf config.fc.nix { + NIX_REMOTE = "daemon"; + NIX_PATH = "nixpkgs=/nixpkgs"; + }; + serviceConfig.StandardOutput = "tty"; + serviceConfig.StandardError = "tty"; + serviceConfig.TTYPath = "/dev/console"; + inherit script; + }; +} diff --git a/tests/qemu/default.nix b/tests/qemu/default.nix new file mode 100644 index 0000000..82c7de6 --- /dev/null +++ b/tests/qemu/default.nix @@ -0,0 +1,25 @@ +# The run script with a qemu stand-in for Firecracker (firecracker.sh), +# for testing without Firecracker (fast with /dev/kvm, else under TCG). +# +# nix-build tests/qemu --argstr script 'echo TEST $(nproc); systemctl poweroff' +# ./result/bin/run-firecracker-vm --secret ssh_host_ed25519_key=KEY [OPTION...] | tee log +# +# script: a shell script run as root at the end of boot (see debug.nix); +# end it with `systemctl poweroff` for the guest to exit, or leave it out +# to log in via ssh (127.0.0.1:2222; authorize a key with another layer). +# layers: further layers, below the test's (e.g. [ ../../layers/nix.nix ], +# which needs --store). +# The run script still takes a tap, so this needs modules/host on the +# machine it runs on, and a free tap of the user's; qemu doesn't use it. +{ pkgs ? import <nixpkgs> {}, layers ? [ ], script ? "", ... }@args: +let + firecracker = pkgs.writeShellApplication { + name = "firecracker"; + runtimeInputs = [ pkgs.jq pkgs.gnused pkgs.qemu_kvm pkgs.vhost-device-vsock ]; + text = builtins.readFile ./firecracker.sh; + }; +in +import ../../pkgs/run-firecracker-vm (builtins.removeAttrs args [ "script" "layers" ] // { + pkgs = pkgs.extend (_: _: { inherit firecracker; }); + layers = layers ++ [ (import ./debug.nix script) ]; +}) diff --git a/tests/qemu/firecracker.sh b/tests/qemu/firecracker.sh new file mode 100644 index 0000000..08f766d --- /dev/null +++ b/tests/qemu/firecracker.sh @@ -0,0 +1,27 @@ +# A stand-in for firecracker --no-api --config-file FILE, for testing the +# run script where there's no Firecracker: boots the same kernel, initrd, +# drives, vsock (vhost-device-vsock does the same uds_path_PORT +# convention) and network interface (qemu's user networking; guest port 22 +# forwarded to 127.0.0.1:${FC_TEST_SSH_PORT:-2222}) in qemu, with +# virtio-pci instead of virtio-mmio (see debug.nix). +set -efu +config=$3 +get() { jq -r "$1" "$config"; } +uds=$(get .vsock.uds_path) +vhost=$uds.vhost +vhost-device-vsock --guest-cid 3 --socket "$vhost" --uds-path "$uds" > "$uds.vhost.log" 2>&1 & +vhost_pid=$! +trap 'kill $vhost_pid 2> /dev/null || :' EXIT +while ! test -S "$vhost"; do sleep 0.1; done +mem=$(get '."machine-config".mem_size_mib') +args=$(get '."boot-source".boot_args') +# shellcheck disable=SC2046 +qemu-system-x86_64 -machine accel=kvm:tcg -nographic -no-reboot \ + -m "$mem" -smp "$(get '."machine-config".vcpu_count')" \ + -object memory-backend-memfd,id=mem,size="$mem"M,share=on -numa node,memdev=mem \ + -kernel "$(printf %s "$args" | sed -n 's|.*init=\([^ ]*\)/init.*|\1|p')/kernel" \ + -initrd "$(get '."boot-source".initrd_path')" -append "$args" \ + -chardev socket,id=vs,path="$vhost" -device vhost-user-vsock-pci,chardev=vs \ + $(jq -r '.drives[] | "-drive file=\(.path_on_host),if=virtio,format=raw" + (if .is_read_only then ",readonly=on" else "" end)' "$config") \ + -netdev user,id=n,hostfwd=tcp:127.0.0.1:"${FC_TEST_SSH_PORT:-2222}"-:22 \ + -device virtio-net-pci,netdev=n,mac="$(get '."network-interfaces"[0].guest_mac')" 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") + ''; +} |
