git.iko.soy/typeable/nixpkgs
on master help sign in
nixos: Fix ordering of firewall.service

Follow-up to the following commits: abdc5961c3cdf9f5893ea1e91ba08ff5089f53a4: Fix starting the firewall e090701e2d09aec3e8866ab9a8e53c37973ffeb4: Order before sysinit Solely use sysinit.target here instead of multi-user.target because we want to make sure that the iptables rules are applied *before* any socket units are started. The reason I've dropped the wantedBy on multi-user.target is that sysinit.target is already a part of the dependency chain of multi-user.target. To make sure that this holds true, I've added a small test case to ensure that during switch of the configuration the firewall.service is considered as well. Tested using the firewall NixOS test. Signed-off-by: aszlig <aszlig@redmoonstudios.org> Cc: @edolstra

commitfb46df8a9a4102e265f4b14af48a5df90d5b06c3 authoraszlig <aszlig@redmoonstudios.org> dateWed 7 Sep 2016, 14:18 (10 years ago) parent10b3359
2 files changed+36−20
nixos/modules/services/networking/firewall.nix+2−1 nixos/tests/firewall.nix+34−19
nixos/modules/services/networking/firewall.nix+2−1
@@ -490,7 +490,7 @@ in
490490
491491 systemd.services.firewall = {
492492 description = "Firewall";
493− wantedBy = [ "multi-user.target" "sysinit.target" ];
493+ wantedBy = [ "sysinit.target" ];
494494 wants = [ "network-pre.target" ];
495495 before = [ "network-pre.target" ];
496496 after = [ "systemd-modules-load.service" ];
@@ -501,6 +501,7 @@ in
501501 # containers don't have CAP_SYS_MODULE. So the host system had
502502 # better have all necessary modules already loaded.
503503 unitConfig.ConditionCapability = "CAP_NET_ADMIN";
504+ unitConfig.DefaultDependencies = false;
504505
505506 reloadIfChanged = true;
506507
nixos/tests/firewall.nix+34−19
@@ -15,6 +15,16 @@ import ./make-test.nix ( { pkgs, ... } : {
1515 services.httpd.adminAddr = "foo@example.org";
1616 };
1717
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+
1828 attacker =
1929 { config, pkgs, ... }:
2030 { services.httpd.enable = true;
@@ -23,28 +33,33 @@ import ./make-test.nix ( { pkgs, ... } : {
2333 };
2434 };
2535
26− testScript =
27− { nodes, ... }:
28− ''
29− startAll;
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");
3045
31− $walled->waitForUnit("firewall");
32− $walled->waitForUnit("httpd");
33− $attacker->waitForUnit("network.target");
46+ # Local connections should still work.
47+ $walled->succeed("curl -v http://localhost/ >&2");
3448
35− # Local connections should still work.
36− $walled->succeed("curl -v http://localhost/ >&2");
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");
3752
38− # Connections to the firewalled machine should fail, but ping should succeed.
39− $attacker->fail("curl --fail --connect-timeout 2 http://walled/ >&2");
40− $attacker->succeed("ping -c 1 walled >&2");
53+ # Outgoing connections/pings should still work.
54+ $walled->succeed("curl -v http://attacker/ >&2");
55+ $walled->succeed("ping -c 1 attacker >&2");
4156
42− # Outgoing connections/pings should still work.
43− $walled->succeed("curl -v http://attacker/ >&2");
44− $walled->succeed("ping -c 1 attacker >&2");
57+ # If we stop the firewall, then connections should succeed.
58+ $walled->stopJob("firewall");
59+ $attacker->succeed("curl -v http://walled/ >&2");
4560
46− # If we stop the firewall, then connections should succeed.
47− $walled->stopJob("firewall");
48− $attacker->succeed("curl -v http://walled/ >&2");
49− '';
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+ '';
5065})