Skip to content

fix(roles/firewall)!: make the iptables and nftables rulesets mandatory - #748

Open
NavidSassan wants to merge 1 commit into
mainfrom
fix/firewall-mandatory-ruleset
Open

NavidSassan wants to merge 1 commit into
mainfrom
fix/firewall-mandatory-ruleset

Conversation

@NavidSassan

Copy link
Copy Markdown
Member

Closes #721

What changes

  • firewall__iptables_ruleset and firewall__nftables_ruleset hold the ruleset as a string. The one for the selected firewall__firewall is mandatory; the preflight aborts without it, before the role stops any firewall.
  • The role deploys the ruleset to /etc/sysconfig/iptables, or to the file nftables.service loads (/etc/sysconfig/nftables.conf on the Red Hat family, /etc/nftables.conf on Debian and Ubuntu), and restarts the service after a change.
  • The copy tasks validate with iptables-restore --test and nft --check, and run before the other firewalls are stopped, so a broken ruleset aborts the run with the old firewall still running. For this, the package installation of both firewalls moved up in tasks/main.yml.
  • The implicit copy of host_files/<host>/etc/sysconfig/iptables is gone. The README example shows the lookup("ansible.builtin.file", ...) that replaces it.

Breaking

Every host with firewall__firewall: 'iptables' or 'nftables' needs its variable set, otherwise the run aborts. The CHANGELOG entry describes the migration.

Deviation from the issue

The issue suggests a check for a ruleset on the host. Detecting whether an nftables file "filters" needed a load in a network namespace and a JSON evaluation, and iptables had no check at all. Making the ruleset an inventory variable for both reduces the check to "is the variable set", the way other roles take whole files (blocky__config_yml, the certificates of opensearch).

Testing

molecule test --scenario-name firewall passes on Rocky 9 and Debian 13 (converge, idempotence, verify). The scenario covers:

  • the switch from fwbuilder to nftables, with the ruleset from the inventory loaded in the kernel
  • the aborts on an unset iptables ruleset, a blank nftables ruleset and a ruleset nft rejects, after which the original ruleset is still loaded
  • on Rocky 9, the switch to iptables and back

Not run: Rocky 8 and 10, Debian 12, Ubuntu. iptables is not tested on Debian and Ubuntu, where the iptables-services package the role installs does not exist; that is unchanged.

…ry (fix #721)

The role deployed no nftables ruleset, and what the packages ship filters
nothing: comments only on the Red Hat family, three chains with policy accept
on Debian and Ubuntu. Since the role stops every other firewall, the host
ended up unfiltered. For iptables the ruleset was optional and came from a
fixed path in host_files.

Both now come from a variable, firewall__iptables_ruleset and
firewall__nftables_ruleset, and the preflight aborts if the one for the
selected firewall is unset. The ruleset is validated and deployed before the
other firewalls are stopped, so a broken one aborts the run with the old
firewall still running.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

firewall: nftables leaves the host with an empty ruleset

1 participant