nixosTests.firewall{,-nftables}: handleTest -> runTest

This commit is contained in:
Sizhe Zhao 2025-06-07 21:39:30 +08:00
parent ab94f9518a
commit bb76321386
No known key found for this signature in database
GPG Key ID: ED1807251A7DA08F
2 changed files with 104 additions and 100 deletions

View File

@ -520,8 +520,14 @@ in
}; };
firefoxpwa = runTest ./firefoxpwa.nix; firefoxpwa = runTest ./firefoxpwa.nix;
firejail = runTest ./firejail.nix; firejail = runTest ./firejail.nix;
firewall = handleTest ./firewall.nix { nftables = false; }; firewall = runTest {
firewall-nftables = handleTest ./firewall.nix { nftables = true; }; imports = [ ./firewall.nix ];
_module.args.nftables = false;
};
firewall-nftables = runTest {
imports = [ ./firewall.nix ];
_module.args.nftables = true;
};
fish = runTest ./fish.nix; fish = runTest ./fish.nix;
firezone = runTest ./firezone/firezone.nix; firezone = runTest ./firezone/firezone.nix;
flannel = handleTestOn [ "x86_64-linux" ] ./flannel.nix { }; flannel = handleTestOn [ "x86_64-linux" ] ./flannel.nix { };

View File

@ -1,119 +1,117 @@
# Test the firewall module. # Test the firewall module.
import ./make-test-python.nix ( { lib, nftables, ... }:
{ pkgs, nftables, ... }: {
{ name = "firewall" + lib.optionalString nftables "-nftables";
name = "firewall" + pkgs.lib.optionalString nftables "-nftables"; meta = with lib.maintainers; {
meta = with pkgs.lib.maintainers; { maintainers = [
maintainers = [ rvfg
rvfg garyguo
garyguo ];
]; };
};
nodes = { nodes = {
walled = walled =
{ ... }: { ... }:
{ {
networking.firewall = { networking.firewall = {
enable = true; enable = true;
logRefusedPackets = true; logRefusedPackets = true;
# Syntax smoke test, not actually verified otherwise # Syntax smoke test, not actually verified otherwise
allowedTCPPorts = [ allowedTCPPorts = [
25 25
993 993
8005 8005
]; ];
allowedTCPPortRanges = [
{
from = 980;
to = 1000;
}
{
from = 990;
to = 1010;
}
{
from = 8000;
to = 8010;
}
];
interfaces.eth0 = {
allowedTCPPorts = [ 10003 ];
allowedTCPPortRanges = [ allowedTCPPortRanges = [
{ {
from = 980; from = 10000;
to = 1000; to = 10005;
}
{
from = 990;
to = 1010;
}
{
from = 8000;
to = 8010;
} }
]; ];
interfaces.eth0 = {
allowedTCPPorts = [ 10003 ];
allowedTCPPortRanges = [
{
from = 10000;
to = 10005;
}
];
};
interfaces.eth3 = {
allowedUDPPorts = [ 10003 ];
allowedUDPPortRanges = [
{
from = 10000;
to = 10005;
}
];
};
}; };
networking.nftables.enable = nftables; interfaces.eth3 = {
services.httpd.enable = true; allowedUDPPorts = [ 10003 ];
services.httpd.adminAddr = "foo@example.org"; allowedUDPPortRanges = [
{
specialisation.different-config.configuration = { from = 10000;
networking.firewall.rejectPackets = true; to = 10005;
}
];
}; };
}; };
networking.nftables.enable = nftables;
services.httpd.enable = true;
services.httpd.adminAddr = "foo@example.org";
attacker = specialisation.different-config.configuration = {
{ ... }: networking.firewall.rejectPackets = true;
{
services.httpd.enable = true;
services.httpd.adminAddr = "foo@example.org";
networking.firewall.enable = false;
}; };
}; };
testScript = attacker =
{ nodes, ... }: { ... }:
let {
unit = if nftables then "nftables" else "firewall"; services.httpd.enable = true;
in services.httpd.adminAddr = "foo@example.org";
'' networking.firewall.enable = false;
start_all() };
};
walled.wait_for_unit("${unit}") testScript =
walled.wait_for_unit("httpd") { nodes, ... }:
attacker.wait_for_unit("network.target") let
unit = if nftables then "nftables" else "firewall";
in
''
start_all()
# Local connections should still work. walled.wait_for_unit("${unit}")
walled.succeed("curl -v http://localhost/ >&2") walled.wait_for_unit("httpd")
attacker.wait_for_unit("network.target")
# Connections to the firewalled machine should fail, but ping should succeed. # Local connections should still work.
attacker.fail("curl --fail --connect-timeout 2 http://walled/ >&2") walled.succeed("curl -v http://localhost/ >&2")
attacker.succeed("ping -c 1 walled >&2")
# Outgoing connections/pings should still work. # Connections to the firewalled machine should fail, but ping should succeed.
walled.succeed("curl -v http://attacker/ >&2") attacker.fail("curl --fail --connect-timeout 2 http://walled/ >&2")
walled.succeed("ping -c 1 attacker >&2") attacker.succeed("ping -c 1 walled >&2")
# Open tcp port 80 at runtime # Outgoing connections/pings should still work.
walled.succeed("nixos-firewall-tool open tcp 80") walled.succeed("curl -v http://attacker/ >&2")
attacker.succeed("curl -v http://walled/ >&2") walled.succeed("ping -c 1 attacker >&2")
# Reset the firewall # Open tcp port 80 at runtime
walled.succeed("nixos-firewall-tool reset") walled.succeed("nixos-firewall-tool open tcp 80")
attacker.fail("curl --fail --connect-timeout 2 http://walled/ >&2") attacker.succeed("curl -v http://walled/ >&2")
# If we stop the firewall, then connections should succeed. # Reset the firewall
walled.stop_job("${unit}") walled.succeed("nixos-firewall-tool reset")
attacker.succeed("curl -v http://walled/ >&2") attacker.fail("curl --fail --connect-timeout 2 http://walled/ >&2")
# Check whether activation of a new configuration reloads the firewall. # If we stop the firewall, then connections should succeed.
walled.succeed( walled.stop_job("${unit}")
"/run/booted-system/specialisation/different-config/bin/switch-to-configuration test 2>&1 | grep -qF ${unit}.service" attacker.succeed("curl -v http://walled/ >&2")
)
''; # Check whether activation of a new configuration reloads the firewall.
} walled.succeed(
) "/run/booted-system/specialisation/different-config/bin/switch-to-configuration test 2>&1 | grep -qF ${unit}.service"
)
'';
}