at 18.03-beta 2.2 kB view raw
1# Test the firewall module. 2 3import ./make-test.nix ( { pkgs, ... } : { 4 name = "firewall"; 5 meta = with pkgs.stdenv.lib.maintainers; { 6 maintainers = [ eelco chaoflow ]; 7 }; 8 9 nodes = 10 { walled = 11 { config, pkgs, nodes, ... }: 12 { networking.firewall.enable = true; 13 networking.firewall.logRefusedPackets = true; 14 services.httpd.enable = true; 15 services.httpd.adminAddr = "foo@example.org"; 16 }; 17 18 # Dummy configuration to check whether firewall.service will be honored 19 # during system activation. This only needs to be different to the 20 # original walled configuration so that there is a change in the service 21 # file. 22 walled2 = 23 { config, pkgs, nodes, ... }: 24 { networking.firewall.enable = true; 25 networking.firewall.rejectPackets = true; 26 }; 27 28 attacker = 29 { config, pkgs, ... }: 30 { services.httpd.enable = true; 31 services.httpd.adminAddr = "foo@example.org"; 32 networking.firewall.enable = false; 33 }; 34 }; 35 36 testScript = { nodes, ... }: let 37 newSystem = nodes.walled2.config.system.build.toplevel; 38 in '' 39 $walled->start; 40 $attacker->start; 41 42 $walled->waitForUnit("firewall"); 43 $walled->waitForUnit("httpd"); 44 $attacker->waitForUnit("network.target"); 45 46 # Local connections should still work. 47 $walled->succeed("curl -v http://localhost/ >&2"); 48 49 # Connections to the firewalled machine should fail, but ping should succeed. 50 $attacker->fail("curl --fail --connect-timeout 2 http://walled/ >&2"); 51 $attacker->succeed("ping -c 1 walled >&2"); 52 53 # Outgoing connections/pings should still work. 54 $walled->succeed("curl -v http://attacker/ >&2"); 55 $walled->succeed("ping -c 1 attacker >&2"); 56 57 # If we stop the firewall, then connections should succeed. 58 $walled->stopJob("firewall"); 59 $attacker->succeed("curl -v http://walled/ >&2"); 60 61 # Check whether activation of a new configuration reloads the firewall. 62 $walled->succeed("${newSystem}/bin/switch-to-configuration test 2>&1" . 63 " | grep -qF firewall.service"); 64 ''; 65})