-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nixos-firewall-tool: add nftables support #324615
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7608,6 +7608,12 @@ | |
githubId = 91987; | ||
name = "Jim Garrison"; | ||
}; | ||
garyguo = { | ||
email = "[email protected]"; | ||
github = "nbdd0121"; | ||
githubId = 4065244; | ||
name = "Gary Guo"; | ||
}; | ||
gavin = { | ||
email = "[email protected]"; | ||
github = "gavinrogers"; | ||
|
@@ -18709,6 +18715,12 @@ | |
githubId = 141248; | ||
name = "Ramses"; | ||
}; | ||
rvfg = { | ||
email = "[email protected]"; | ||
github = "duament"; | ||
githubId = 30264485; | ||
name = "Rvfg"; | ||
}; | ||
rvl = { | ||
email = "[email protected]"; | ||
github = "rvl"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,31 @@ | |
import ./make-test-python.nix ( { pkgs, nftables, ... } : { | ||
name = "firewall" + pkgs.lib.optionalString nftables "-nftables"; | ||
meta = with pkgs.lib.maintainers; { | ||
maintainers = [ ]; | ||
maintainers = [ rvfg garyguo ]; | ||
}; | ||
|
||
nodes = | ||
{ walled = | ||
{ ... }: | ||
{ networking.firewall.enable = true; | ||
networking.firewall.logRefusedPackets = true; | ||
{ networking.firewall = { | ||
enable = true; | ||
logRefusedPackets = true; | ||
# Syntax smoke test, not actually verified otherwise | ||
allowedTCPPorts = [ 25 993 8005 ]; | ||
allowedTCPPortRanges = [ | ||
{ from = 980; to = 1000; } | ||
{ 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; | ||
services.httpd.enable = true; | ||
services.httpd.adminAddr = "[email protected]"; | ||
|
@@ -48,6 +65,14 @@ import ./make-test-python.nix ( { pkgs, nftables, ... } : { | |
walled.succeed("curl -v http://attacker/ >&2") | ||
walled.succeed("ping -c 1 attacker >&2") | ||
|
||
# Open tcp port 80 at runtime | ||
walled.succeed("nixos-firewall-tool open tcp 80") | ||
attacker.succeed("curl -v http://walled/ >&2") | ||
|
||
# Reset the firewall | ||
walled.succeed("nixos-firewall-tool reset") | ||
attacker.fail("curl --fail --connect-timeout 2 http://walled/ >&2") | ||
|
||
# If we stop the firewall, then connections should succeed. | ||
walled.stop_job("${unit}") | ||
attacker.succeed("curl -v http://walled/ >&2") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
{ writeShellApplication, iptables, lib }: | ||
{ writeShellApplication, lib }: | ||
|
||
writeShellApplication { | ||
name = "nixos-firewall-tool"; | ||
|
||
text = builtins.readFile ./nixos-firewall-tool.sh; | ||
runtimeInputs = [ | ||
iptables | ||
]; | ||
|
||
meta = with lib; { | ||
description = "Temporarily manipulate the NixOS firewall"; | ||
license = licenses.mit; | ||
maintainers = with maintainers; [ clerie ]; | ||
maintainers = with maintainers; [ clerie rvfg garyguo ]; | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This implementation differs from the one with iptables. With iptables, all rules are being reset to the definitions done with nix. With nftables only temporarily opened ports are flushed.
I appreciate the nftables implementation more, as it respects dynamic rules done by other tools like Docker, but maybe this should be highlighted, or the subcommands for nftables should be renamed, and a notice added that reset only works with iptables and for nftables the unit should be restarted manually.
Alternatively, reset could be implemented for nftables with the same functionality, and the current implementation gets moved into another separate subcommand.
Although I am also fine with the implementation as it is right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's intentional, since otherwise it's very disruptive. It's especially so when you uses nft sets, everything added there will also be lost.
Arguably the same should be done for iptables by setting up a separate chain, but I don't have bandwidth for it now, I just want to get nftables functionality to work on par with iptables so more people can switch over.