Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Breaking Changes

* **role:firewall**: With `firewall__firewall: 'iptables'` or `'nftables'`, the ruleset is mandatory and comes from `firewall__iptables_ruleset` or `firewall__nftables_ruleset`; without it, the run aborts before the role stops any firewall. Until now an nftables host ended up without any filtering unless a ruleset was deployed by other means, and iptables hosts kept whatever `/etc/sysconfig/iptables` held. The role no longer picks up `host_files/<host>/etc/sysconfig/iptables` on its own: set `firewall__iptables_ruleset` to a `lookup("ansible.builtin.file", ...)` of that file, as the role README shows, and on nftables hosts move the ruleset into `firewall__nftables_ruleset`.
* **role:sshd, playbook:setup_basic**: sshd turns new connections away earlier while many logins are pending (`MaxStartups 10:30:60` instead of the OpenSSH default `10:30:100`), as the CIS Benchmarks require, and `MaxStartups` is configurable with `sshd__max_startups__host_var` / `sshd__max_startups__group_var`. A `MaxStartups` set through `sshd__raw` no longer takes effect, move it to the variable. Hosts with many simultaneous SSH logins need a higher value, for example `'100:30:200'` on GitLab hosts ([#723](https://github.com/Linuxfabrik/lfops/issues/723)).
* **role:grav, playbook:setup_grav**: The code of Grav, its configuration, pages, plugins and themes belong to `root`, so a hole in Grav or a plugin can no longer plant code in the site. The web server only writes `assets`, `backup`, `cache`, `images`, `logs`, `tmp`, `user/accounts` and `user/data`, and only these keep the SELinux type `httpd_sys_rw_content_t`. Grav, its plugins and themes are updated as `root` by `grav-selfupgrade-<instance>.timer` and `grav-update-<instance>.timer`, which replace `grav-selfupgrade.timer` and `grav-update.timer`, so several Grav sites on one host are each updated. Run other `bin/gpm` commands with `grav-gpm <installation directory> <arguments>` instead of `sudo -u apache bin/gpm`, which now fails, and deploy content below `user/` as `root`. The role no longer installs the Admin Panel, which could not save anything anymore, and drops `grav__skip_admin` and `grav__users`; it lists the Admin Panel plugins still installed at the end of the play, for you to remove. The vHost no longer accepts `POST`, and `setup_grav` no longer sets the SELinux boolean `httpd_can_network_connect`, which keeps its current value.
* **role:apache_httpd**: The `proxy` vHost template waits 30 instead of 5 seconds for the backend, so slower responses no longer end in a 502, and a hanging backend holds the connection longer. Set `conf_proxy_timeout: 5` on the vHost to keep the previous value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@
# has to stop, disable and mask fwb.service, and the idempotence step proves that masking the
# unit the role deployed itself does not report a change on every run.
firewall__firewall: 'nftables'

# The policy stays accept, so the VM remains reachable. The rule on a port nothing uses is what
# verify.yml looks for in the kernel.
firewall__nftables_ruleset: |-
flush ruleset
table inet lfops_molecule {
chain input {
type filter hook input priority filter; policy accept;
tcp dport 65000 counter drop
}
}
23 changes: 23 additions & 0 deletions extensions/molecule/firewall/tasks/abort.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Included by verify.yml once per case in which the role has to abort: runs the role with the
# firewall and ruleset of `__molecule__abort` and checks which task failed.
- block:

- name: 'Run the firewall role'
ansible.builtin.include_role:
name: 'linuxfabrik.lfops.firewall'
vars:
firewall__firewall: '{{ __molecule__abort["firewall"] }}'
firewall__iptables_ruleset: '{{ __molecule__abort["ruleset"] }}'
firewall__nftables_ruleset: '{{ __molecule__abort["ruleset"] }}'

- name: 'Fail, since the role did not abort'
ansible.builtin.fail:
msg: 'The firewall role did not abort.'

rescue:

- name: 'Assert that the role failed where expected, not on the task above'
ansible.builtin.assert:
that:
- 'ansible_failed_task["name"] is search(__molecule__abort["failed_task"])'
fail_msg: '{{ ansible_failed_task["name"] }}: {{ ansible_failed_result["msg"] | d("") }}'
92 changes: 90 additions & 2 deletions extensions/molecule/firewall/verify.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- name: 'Verify the switch from fwbuilder to nftables, and the abort on a repository without a compiled firewall'
- name: 'Verify the switch from fwbuilder to nftables, the way to iptables and back, and the aborts without a ruleset and on a repository without a compiled firewall'
hosts: 'systems_under_test'
gather_facts: false
gather_facts: true # the role needs the os_family

tasks:

Expand All @@ -27,6 +27,94 @@
- '__molecule__fwb_is_enabled_result["stdout"] == "masked"'
fail_msg: '{{ __molecule__is_active_result["stdout_lines"] + [__molecule__fwb_is_enabled_result["stdout"]] }}'

# nftables.service has to load the ruleset from the inventory into the kernel.
- name: 'nft list table inet lfops_molecule'
ansible.builtin.command: 'nft list table inet lfops_molecule'
changed_when: false
register: '__molecule__nft_list_result'

- name: 'Assert that the ruleset is loaded'
ansible.builtin.assert:
that:
- '"tcp dport 65000 counter" in __molecule__nft_list_result["stdout"]'
fail_msg: '{{ __molecule__nft_list_result["stdout_lines"] }}'

# Without a ruleset in the inventory the role must abort in its preflight checks.
- name: 'Run the firewall role without a ruleset'
ansible.builtin.include_tasks: 'tasks/abort.yml'
loop:
- firewall: 'iptables'
ruleset: ''
failed_task: 'has a ruleset for'
- firewall: 'nftables'
ruleset: " \n"
failed_task: 'has a ruleset for'
# a ruleset nft rejects must not replace the file nftables.service loads
- firewall: 'nftables'
ruleset: 'table inet lfops_molecule_broken {'
failed_task: 'Deploy '
loop_control:
loop_var: '__molecule__abort'
label: '{{ __molecule__abort["firewall"] }}: {{ __molecule__abort["ruleset"] | trim }}'

- name: 'nft list ruleset (after the aborted runs)'
ansible.builtin.command: 'nft list ruleset'
changed_when: false
register: '__molecule__nft_list_after_abort_result'

- name: 'Assert that the ruleset from the inventory is still the one loaded'
ansible.builtin.assert:
that:
- '"tcp dport 65000 counter" in __molecule__nft_list_after_abort_result["stdout"]'
- '"lfops_molecule_broken" not in __molecule__nft_list_after_abort_result["stdout"]'
fail_msg: '{{ __molecule__nft_list_after_abort_result["stdout_lines"] }}'

# The way to iptables and back, on the Red Hat family, which has the iptables-services
# package: iptables.service has to load the ruleset from the inventory, and nftables has to be
# stopped. The second run restores the state the idempotence step and this play start from.
- name: 'stat /etc/redhat-release'
ansible.builtin.stat:
path: '/etc/redhat-release'
register: '__molecule__redhat_release_result'

- block:

- name: 'Run the firewall role with iptables'
ansible.builtin.include_role:
name: 'linuxfabrik.lfops.firewall'
vars:
firewall__firewall: 'iptables'
firewall__iptables_ruleset: |-
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -p tcp -m tcp --dport 65001 -j DROP
COMMIT

- name: 'systemctl is-active iptables.service nftables.service; iptables-save' # noqa command-instead-of-module (read-only state query)
ansible.builtin.shell: 'systemctl is-active iptables.service nftables.service; iptables-save'
changed_when: false
register: '__molecule__iptables_result'

- name: 'Assert that iptables.service loaded the ruleset and nftables.service is stopped'
ansible.builtin.assert:
that:
- '__molecule__iptables_result["stdout_lines"][:2] == ["active", "inactive"]'
- '__molecule__iptables_result["stdout"] is search("--dport 65001 -j DROP")'
fail_msg: '{{ __molecule__iptables_result["stdout_lines"] }}'

- name: 'Run the firewall role with nftables again'
ansible.builtin.include_role:
name: 'linuxfabrik.lfops.firewall'

- name: 'nft list table inet lfops_molecule (back on nftables)'
ansible.builtin.command: 'nft list table inet lfops_molecule'
changed_when: false

when:
- '__molecule__redhat_release_result["stat"]["exists"]'

# A Firewall Builder repository without a compiled firewall for the host must abort the run
# before the role stops the firewall that is running, here nftables.
- name: 'mktemp -d (empty Firewall Builder repository on the controller)'
Expand Down
29 changes: 28 additions & 1 deletion roles/firewall/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This role configures a firewall on the system. For the currently supported firew
Manual steps:

* When using `firewall__firewall == fwbuilder`, either manually deploy a Firewall Builder file to `/etc/fwb.sh` or use the ``firewall__fwbuilder_repo_url`` variable to clone the Firewall Builder files automatically. Without either, or if the repository has no compiled file for the host, the run aborts before the role stops any firewall.
* When using `firewall__firewall == iptables`, optionally place an iptables config file in your inventory, which will be deployed to the system. The file has to be placed into `{{ inventory_dir }}/host_files/{{ inventory_hostname }}/etc/sysconfig/iptables`.
* When using `firewall__firewall == iptables` or `firewall__firewall == nftables`, set the ruleset in `firewall__iptables_ruleset` or `firewall__nftables_ruleset`. Without it, the run aborts before the role stops any firewall.


## Tags
Expand Down Expand Up @@ -90,6 +90,18 @@ Manual steps:
* Type: String.
* Default: unset

`firewall__iptables_ruleset`

* The iptables ruleset in `iptables-save` format, deployed to `/etc/sysconfig/iptables`. Mandatory if `firewall__firewall` is `'iptables'`. The role checks it with `iptables-restore --test` before it replaces the file, and restarts `iptables.service` after a change.
* Type: String.
* Default: unset

`firewall__nftables_ruleset`

* The nftables ruleset, deployed to the file `nftables.service` loads: `/etc/sysconfig/nftables.conf` on the Red Hat family, `/etc/nftables.conf` on Debian and Ubuntu. Mandatory if `firewall__firewall` is `'nftables'`. The role checks it with `nft --check` before it replaces the file, and restarts `nftables.service` after a change.
* Type: String.
* Default: unset

Example:
```yaml
# optional
Expand All @@ -104,6 +116,21 @@ firewall__firewalld_services__host_var:
state: 'enabled'
firewall__fwbuilder_fw_file: 'example'
firewall__fwbuilder_repo_url: 'git@git.example.com:fwbuilder/fwb.git'
# from a file in the inventory
firewall__iptables_ruleset: '{{ lookup("ansible.builtin.file",
inventory_dir ~ "/host_files/" ~ inventory_hostname ~ "/etc/sysconfig/iptables")
}}'
firewall__nftables_ruleset: |-
flush ruleset
table inet filter {
chain input {
type filter hook input priority filter; policy drop;
ct state established,related accept
iif "lo" accept
meta l4proto { icmp, ipv6-icmp } accept
tcp dport 22 accept
}
}
```


Expand Down
89 changes: 61 additions & 28 deletions roles/firewall/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,63 @@
ansible.builtin.import_tasks: 'preflight.yml'


# Deployed before the other firewalls are stopped: `validate` rejects a broken ruleset, and the
# run then aborts with the firewall the host uses still running. Installing the packages does
# not start their services.
- block:

- name: 'Make sure iptables-services is installed'
ansible.builtin.package:
name: 'iptables-services'
state: 'present'
retries: 3 # ride out a brief outage of the package repository
delay: 10

# the block scalar adds the newline iptables-restore needs after the last COMMIT, and which
# a file lookup strips
- name: 'Deploy /etc/sysconfig/iptables'
ansible.builtin.copy:
backup: true
content: |
{{ firewall__iptables_ruleset | trim }}
dest: '/etc/sysconfig/iptables'
owner: 'root'
group: 'root'
mode: 0o600
validate: 'iptables-restore --test %s'
register: 'firewall__iptables_definition_result'

when: 'firewall__firewall == "iptables"'
tags:
- 'firewall'


- block:

- name: 'Make sure nftables is installed'
ansible.builtin.package:
name: 'nftables'
state: 'present'
retries: 3 # ride out a brief outage of the package repository
delay: 10

- name: 'Deploy {{ __firewall__nftables_conf }}'
ansible.builtin.copy:
backup: true
content: |
{{ firewall__nftables_ruleset | trim }}
dest: '{{ __firewall__nftables_conf }}'
owner: 'root'
group: 'root'
mode: 0o600
validate: 'nft --check --file %s'
register: '__firewall__nftables_ruleset_result'

when: 'firewall__firewall == "nftables"'
tags:
- 'firewall'


- block:

- name: 'Get the list of services'
Expand Down Expand Up @@ -208,26 +265,6 @@

- block:

- name: 'Make sure iptables-services is installed'
ansible.builtin.package:
name: 'iptables-services'
state: 'present'
retries: 3 # ride out a brief outage of the package repository
delay: 10

- name: 'Copy iptables definition to /etc/sysconfig/iptables'
ansible.builtin.copy:
backup: true
src: "{{ inventory_dir }}/host_files/{{ inventory_hostname }}/etc/sysconfig/iptables"
dest: '/etc/sysconfig/'
owner: 'root'
group: 'root'
mode: 0o600
# the parentheses are needed: `is` binds tighter than `~`, and without them the condition is a
# non-empty string that is always true
when: '(inventory_dir ~ "/host_files/" ~ inventory_hostname ~ "/etc/sysconfig/iptables") is file'
register: 'firewall__iptables_definition_result'

- name: 'systemctl restart iptables.service'
ansible.builtin.systemd:
name: 'iptables.service'
Expand All @@ -243,17 +280,13 @@

- block:

- name: 'Make sure nftables is installed'
ansible.builtin.package:
name: 'nftables'
state: 'present'
retries: 3 # ride out a brief outage of the package repository
delay: 10

# Restarted rather than reloaded after a change: stopping the unit flushes the ruleset on every
# platform, while the reload on Debian and Ubuntu only loads the file again, which adds to the
# loaded ruleset unless the file starts with `flush ruleset`.
- name: 'Be sure nftables is started, enabled and not masked'
ansible.builtin.systemd:
name: 'nftables.service'
state: 'started'
state: '{{ __firewall__nftables_ruleset_result is changed | bool | ternary("restarted", "started") }}'
enabled: true
masked: false

Expand Down
18 changes: 18 additions & 0 deletions roles/firewall/tasks/preflight.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,21 @@
tags:
- 'firewall'
- 'firewall:deploy_fwb_sh'


# The role stops and masks every other firewall. iptables.service and nftables.service only load
# the ruleset they find on the host, and what the nftables packages ship filters nothing, so the
# ruleset has to come from the inventory.
- name: 'Assert that the inventory has a ruleset for {{ firewall__firewall }}'
ansible.builtin.assert:
that:
- 'lookup("ansible.builtin.vars", "firewall__" ~ firewall__firewall ~ "_ruleset", default="") | trim | length > 0'
quiet: true
fail_msg: >-
firewall__firewall is '{{ firewall__firewall }}', but firewall__{{ firewall__firewall }}_ruleset
is not set, so the host would run without the rules it needs. Set the variable to the
ruleset, or set firewall__firewall to the firewall this host uses.
when:
- 'firewall__firewall in ["iptables", "nftables"]'
tags:
- 'firewall'
4 changes: 4 additions & 0 deletions roles/firewall/vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# The controller-side clone of firewall__fwbuilder_repo_url, one per repository.
__firewall__fwbuilder_repo_dir: '/tmp/ansible.fwb-{{ firewall__fwbuilder_repo_url | d("") | hash("sha1") }}'

# The file nftables.service loads, as its unit says (`ExecStart=... nft -f <file>`). Verified with
# nftables 1.0.4 to 1.1.5 on Rocky 8, 9 and 10, Debian 12 and 13 and Ubuntu 24.04.
__firewall__nftables_conf: '{{ (ansible_facts["os_family"] == "RedHat") | ternary("/etc/sysconfig/nftables.conf", "/etc/nftables.conf") }}'
Loading