1 # Test the firewall module.
3 import ./make-test-python.nix ( { pkgs, nftables, ... } : {
4 name = "firewall" + pkgs.lib.optionalString nftables "-nftables";
5 meta = with pkgs.lib.maintainers; {
6 maintainers = [ rvfg garyguo ];
12 { networking.firewall = {
14 logRefusedPackets = true;
15 # Syntax smoke test, not actually verified otherwise
16 allowedTCPPorts = [ 25 993 8005 ];
17 allowedTCPPortRanges = [
18 { from = 980; to = 1000; }
19 { from = 990; to = 1010; }
20 { from = 8000; to = 8010; }
23 allowedTCPPorts = [ 10003 ];
24 allowedTCPPortRanges = [ { from = 10000; to = 10005; } ];
27 allowedUDPPorts = [ 10003 ];
28 allowedUDPPortRanges = [ { from = 10000; to = 10005; } ];
31 networking.nftables.enable = nftables;
32 services.httpd.enable = true;
33 services.httpd.adminAddr = "foo@example.org";
35 specialisation.different-config.configuration = {
36 networking.firewall.rejectPackets = true;
42 { services.httpd.enable = true;
43 services.httpd.adminAddr = "foo@example.org";
44 networking.firewall.enable = false;
48 testScript = { nodes, ... }: let
49 unit = if nftables then "nftables" else "firewall";
53 walled.wait_for_unit("${unit}")
54 walled.wait_for_unit("httpd")
55 attacker.wait_for_unit("network.target")
57 # Local connections should still work.
58 walled.succeed("curl -v http://localhost/ >&2")
60 # Connections to the firewalled machine should fail, but ping should succeed.
61 attacker.fail("curl --fail --connect-timeout 2 http://walled/ >&2")
62 attacker.succeed("ping -c 1 walled >&2")
64 # Outgoing connections/pings should still work.
65 walled.succeed("curl -v http://attacker/ >&2")
66 walled.succeed("ping -c 1 attacker >&2")
68 # Open tcp port 80 at runtime
69 walled.succeed("nixos-firewall-tool open tcp 80")
70 attacker.succeed("curl -v http://walled/ >&2")
73 walled.succeed("nixos-firewall-tool reset")
74 attacker.fail("curl --fail --connect-timeout 2 http://walled/ >&2")
76 # If we stop the firewall, then connections should succeed.
77 walled.stop_job("${unit}")
78 attacker.succeed("curl -v http://walled/ >&2")
80 # Check whether activation of a new configuration reloads the firewall.
82 "/run/booted-system/specialisation/different-config/bin/switch-to-configuration test 2>&1 | grep -qF ${unit}.service"