diff options
| author | tv <tv@krebsco.de> | 2026-11-21 18:17:26 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-25 22:53:22 +0100 |
| commit | 00b15b27c2f4510c262474c8ae54be588c575476 (patch) | |
| tree | f22d3f4f097e40934df7f0b9c58481a5a9992892 /tests | |
| parent | 595f6e3f69702aa50f17d37eb49d9050257307a0 (diff) | |
tests/vm-host.nix: the HTTPS proxy
The proxy was only ever tested by hand, in network namespaces, with its
configuration taken out of the module. Now the VM test covers it with
the module for real: an upstream (openssl s_server) serving allowed.test
& co. with a certificate from a test CA the host trusts, and
untrusted.test with a self-signed one, and a dnsmasq on the host's lo
as nginx's resolver. Checked: whole domains passed through (with the
upstream's certificate, subdomains too), path rules decrypted (with the
proxy's), other paths, `..`, `%2e%2e` and a Host other than the server
name refused, names without a rule and other guests' rules refused, an
upstream certificate that doesn't verify a 502, POST /check for
https://, the log lines, and rules changing without a reload.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/vm-host.nix | 96 |
1 files changed, 89 insertions, 7 deletions
diff --git a/tests/vm-host.nix b/tests/vm-host.nix index 04f66de..569d874 100644 --- a/tests/vm-host.nix +++ b/tests/vm-host.nix @@ -1,17 +1,39 @@ # 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: +# units, timer, nftables, dnsmasq, nginx), 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. +# is an upstream with 42::1 and 198.51.100.1, there for the names *.test +# (a dnsmasq on the host's lo, as nginx's resolver), with an HTTPS server +# whose certificate the host trusts (but for untrusted.test's). Covers +# static addresses by name, the tcp:// rules (IPv4, IPv6, names), DNS, +# IPv6 port 443, anti-spoofing, POST /check, and the HTTPS proxy: passed +# through, decrypted with path rules, refused, upstream certificates +# verified, the log, rules effective without reload. # # 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> {} }: +let + # Test certificates (keys in the store, for the test only): a CA, the + # upstream's certificate for the names below from it, and a self-signed + # one for untrusted.test. + certs = pkgs.runCommand "fc-test-certs" { nativeBuildInputs = [ pkgs.openssl ]; } '' + mkdir $out && cd $out + openssl req -x509 -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \ + -days 3650 -subj /CN=fc-test-ca -keyout ca.key -out ca.pem \ + -addext basicConstraints=critical,CA:TRUE -addext keyUsage=critical,keyCertSign + openssl req -x509 -CA ca.pem -CAkey ca.key -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \ + -days 3650 -subj /CN=allowed.test -keyout server.key -out server.pem \ + -addext basicConstraints=CA:FALSE \ + -addext subjectAltName=DNS:allowed.test,DNS:sub.allowed.test,DNS:paths.test,DNS:denied.test + openssl req -x509 -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \ + -days 3650 -subj /CN=untrusted.test -keyout untrusted.key -out untrusted.pem \ + -addext subjectAltName=DNS:untrusted.test + ''; +in pkgs.testers.runNixOSTest { name = "fc-host"; nodes.host = { pkgs, ... }: { @@ -26,9 +48,20 @@ pkgs.testers.runNixOSTest { networking.nameservers = [ "127.0.0.1" ]; networking.hosts."42::1" = [ "sshhost6.test" ]; networking.hosts."198.51.100.1" = [ "sshhost.test" ]; + # nginx's resolver (from resolv.conf): the upstream for any *.test. + services.dnsmasq = { + enable = true; + settings = { + interface = "lo"; + bind-interfaces = true; + no-resolv = true; + address = "/test/198.51.100.1"; + }; + }; + security.pki.certificateFiles = [ "${certs}/ca.pem" ]; # As the host's firewall has to (see README.md). networking.firewall.trustedInterfaces = [ "fcbr0" ]; - environment.systemPackages = [ pkgs.socat pkgs.dig pkgs.curl ]; + environment.systemPackages = [ pkgs.socat pkgs.dig pkgs.curl pkgs.openssl ]; virtualisation.memorySize = 1024; }; testScript = '' @@ -70,7 +103,7 @@ pkgs.testers.runNixOSTest { 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 &") + host.succeed("ip netns exec U socat TCP6-LISTEN:443,fork,reuseaddr,ipv6only=1 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) @@ -114,5 +147,54 @@ pkgs.testers.runNixOSTest { 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") + + # The HTTPS proxy. The upstream serves files (s_server -WWW), as + # allowed.test & co. with a certificate from the test CA, which the + # host trusts, and as untrusted.test with a self-signed one. The + # guests dial any address: their port 443 goes to the proxy. + test_ca = "${certs}/ca.pem" + fc_ca = "/var/lib/fcbr0-egress/ca.pem" + host.succeed("mkdir -p /tmp/www/ok /tmp/www/no && echo hello > /tmp/www/hello && echo ok > /tmp/www/ok/file && echo no > /tmp/www/no/file") + host.succeed( + "cd /tmp/www && ip netns exec U openssl s_server -quiet -WWW -accept 198.51.100.1:443" + " -cert ${certs}/server.pem -key ${certs}/server.key" + " -servername untrusted.test -cert2 ${certs}/untrusted.pem -key2 ${certs}/untrusted.key >&2 &" + ) + host.wait_until_succeeds("ip netns exec U ss -ltn | grep -q '198.51.100.1:443'", timeout=30) + host.wait_for_unit("fcbr0-egress.service") + + def https(g, url, ca, extra=""): + name = url.split("/")[2] + out = host.succeed( + f"ip netns exec {g} curl -sS --max-time 10 --cacert {ca} --resolve {name}:443:203.0.113.9 {extra}" + f" -w '\\n%{{http_code}}' {url} 2>/dev/null || echo blocked" + ).split() + return "blocked" if out[-1] == "blocked" else f"{' '.join(out[:-1])} {out[-1]}" + + def status(g, url, extra=""): + return https(g, url, fc_ca, extra).split()[-1] + + host.succeed("su tv -c 'fc-egress -l box allow https://allowed.test https://paths.test/ok/ https://untrusted.test/ok/'") + check("g1 allowed.test, passed through", https("g1", "https://allowed.test/hello", test_ca), "hello 200") + check("g1 allowed.test, not with our CA", https("g1", "https://allowed.test/hello", fc_ca), "blocked") + check("g1 sub.allowed.test, subdomain", https("g1", "https://sub.allowed.test/hello", test_ca), "hello 200") + check("g1 paths.test/ok/, decrypted", https("g1", "https://paths.test/ok/file", fc_ca), "ok 200") + check("g1 paths.test/ok/, not the upstream's certificate", https("g1", "https://paths.test/ok/file", test_ca), "blocked") + check("g1 paths.test/no/", status("g1", "https://paths.test/no/file"), "403") + check("g1 paths.test/ok/../no/", status("g1", "https://paths.test/ok/../no/file", "--path-as-is"), "403") + check("g1 paths.test/ok/%2e%2e/no/", status("g1", "https://paths.test/ok/%2e%2e/no/file"), "403") + check("g1 paths.test, Host not the server name", status("g1", "https://paths.test/ok/file", "-H Host:allowed.test"), "403") + check("g1 denied.test, no rule", https("g1", "https://denied.test/hello", test_ca), "blocked") + check("g2 allowed.test, not its rule", https("g2", "https://allowed.test/hello", test_ca), "blocked") + check("g1 untrusted.test, upstream not verified", status("g1", "https://untrusted.test/ok/file"), "502") + check("g1 https checks", check_uris("g1", "https://allowed.test", "https://sub.allowed.test/x", "https://paths.test/ok/file", "https://paths.test/no/file", "https://denied.test"), + ["true", "true", "true", "false", "false"]) + host.wait_until_succeeds("journalctl -t fc_egress --no-pager | grep -q 'ALLOW tv fctap1 box https://allowed.test'", timeout=30) + host.wait_until_succeeds("journalctl -t fc_egress --no-pager | grep -q 'DENY tv fctap1 box https://denied.test'", timeout=30) + host.wait_until_succeeds("journalctl -t fc_egress --no-pager | grep -qE 'DENY .*GET https://paths.test/no/file'", timeout=30) + # rules apply to the next connection, without reloading anything + host.succeed("su tv -c 'fc-egress -l box deny https://allowed.test && fc-egress -l box allow https://denied.test'") + check("g1 allowed.test after deny", https("g1", "https://allowed.test/hello", test_ca), "blocked") + check("g1 denied.test after allow", https("g1", "https://denied.test/hello", test_ca), "hello 200") ''; } |
