feat(fail2ban): reload instead of restart when jail.local changes #332
No files matched your search
@@ -0,0 +1,7 @@
|
||||
---
|
||||
minor_changes:
|
||||
- >-
|
||||
fail2ban - the main entry point now reloads Fail2Ban when ``jail.local`` changes instead of restarting it, as ``service_jail``
|
||||
already did. A restart stops every jail, which flushes its bans out of the firewall until they are restored from Fail2Ban's
|
||||
database, and forgets failures counted toward ``maxretry``; a reload applies the new settings to the running jails. The
|
||||
``Restart Fail2Ban`` handler is removed: nothing in the role notifies it any more.
|
||||
@@ -68,10 +68,12 @@ Main entry point: `fail2ban` (everything), `fail2ban_install`, `fail2ban_validat
|
||||
## Notes
|
||||
|
||||
- Each service owns `jail.d/<name>.local`, so one play cannot overwrite another service's jail. `jail.local` only holds `fail2ban_jails`.
|
||||
- `fail2ban_ignoreip` lives in the `[DEFAULT]` section of `jail.local`, so it also applies to the jails that `service_jail` writes to `jail.d`. The exemption only exists once the main entry point has written `jail.local` on that host: `service_jail` does not read `fail2ban_ignoreip`, so a host that uses only `service_jail` has none. The first run after upgrading to a release with it adds the `[DEFAULT]` section and restarts Fail2Ban once.
|
||||
- `fail2ban_ignoreip` lives in the `[DEFAULT]` section of `jail.local`, so it also applies to the jails that `service_jail` writes to `jail.d`. The exemption only exists once the main entry point has written `jail.local` on that host: `service_jail` does not read `fail2ban_ignoreip`, so a host that uses only `service_jail` has none.
|
||||
- `fail2ban_ignoreip` replaces an `ignoreip` set in `jail.conf` or `jail.d/*.conf`, which Fail2Ban reads before `jail.local`; a `jail.d/*.local` file is read after it and can still override it for one jail. Entries must not contain whitespace, `;` or `%` (a `;` after whitespace starts a comment in Fail2Ban's config files and a `%` is interpolation syntax).
|
||||
- Prefer IP addresses and CIDR networks in `fail2ban_ignoreip`. Fail2Ban resolves a hostname when the jail starts, and a failed lookup leaves that entry ineffective with only a log warning. The validation of `fail2ban_ignoreip` runs under the `fail2ban_validate` tag, so `--tags fail2ban_configure` skips it.
|
||||
- Changing `jail.local` restarts Fail2Ban, which drops the in-memory ban list. Service jails only trigger a reload.
|
||||
- Changing `jail.local` or a service jail reloads Fail2Ban instead of restarting it. A restart stops every jail, which flushes its bans out of the firewall until they are restored from the database, and forgets failures counted toward `maxretry`; a reload applies the new settings to the running jails. When the whole role runs, the reload comes after `fail2ban-client -t`.
|
||||
- What a reload does not do, compared with the restart it replaces (read from the Fail2Ban 1.1.0 source, not run here): it does not unban an address that a widened `fail2ban_ignoreip` now covers (only a restart skips ignored addresses when it restores bans, so run `fail2ban-client unban <address>` to release one at once, or wait for the ban to expire); it does not change bans that already exist when `bantime` changes; and it does not apply server-level settings (`loglevel`, `dbfile`, `socket`, `pidfile`), which this role does not write. A changed `backend` restarts that jail by itself.
|
||||
- With `--tags fail2ban_configure` only, `fail2ban-client -t` does not run before the reload; the reload (`fail2ban-client reload` in the upstream unit) still rejects a bad configuration and leaves the running daemon alone.
|
||||
- `fail2ban-client -t` runs on every invocation of both entry points, not only when a file changed, so a broken file fails every run until fixed. It also runs in check mode.
|
||||
- `fail2ban_service_jail` and `fail2ban_service_filter` must be passed as a single templated expression so they stay mappings, not strings or lists.
|
||||
- `no_log` is not used: the role handles no secrets.
|
||||
@@ -1,15 +1,11 @@
|
||||
---
|
||||
- name: Restart Fail2Ban
|
||||
become: true
|
||||
ansible.builtin.service:
|
||||
name: fail2ban
|
||||
state: restarted
|
||||
|
||||
# Service jails only add a jail.d file, so reload instead of restarting the
|
||||
# daemon (a restart drops the in-memory ban list). state=reloaded starts the
|
||||
# service if it is not running. The config test runs as a task in
|
||||
# service_jail.yml, so a broken file fails every run instead of only the one
|
||||
# that wrote it.
|
||||
# The main entry point and service_jail both reload instead of restarting the daemon. A restart stops every jail, which flushes
|
||||
# its bans out of the firewall (they stay in Fail2Ban's database and are restored when the jail starts again), so for a moment
|
||||
# banned addresses are not blocked, and failures counted toward maxretry are forgotten. A reload applies the new settings to the
|
||||
# running jails and does neither. state=reloaded starts the service if it is not running. The configuration test
|
||||
# (`fail2ban-client -t`) runs as a task in service.yml and service_jail.yml, so a broken file fails every run of the full role
|
||||
# instead of only the one that wrote it, and when the whole role runs it comes before this handler (with only the configure tag it
|
||||
# does not, and the reload itself rejects a bad configuration).
|
||||
- name: Reload Fail2Ban
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
|
||||
@@ -41,7 +41,7 @@ argument_specs:
|
||||
An empty list exempts nothing, loopback included. It must be a list of non-empty strings without whitespace,
|
||||
semicolons or percent signs (entries are joined with spaces, a semicolon starts a comment and a percent sign is
|
||||
interpolation syntax in Fail2Ban's config files); a plain string, a number or null is refused.
|
||||
- Changing it rewrites jail.local, which restarts Fail2Ban.
|
||||
- Changing it rewrites jail.local, which reloads Fail2Ban.
|
||||
fail2ban_service:
|
||||
type: dict
|
||||
description:
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
---
|
||||
# Runs after the idempotence check and before verify. Record the daemon's PID, ban an address, change a jail setting so jail.local
|
||||
# is rewritten and the handler fires, and let verify prove the daemon was reloaded, not restarted. The PID is the discriminator:
|
||||
# a restart starts a new process, a reload does not. The ban alone would not tell them apart, because Fail2Ban restores unexpired
|
||||
# bans from its database when a jail starts. The PID goes to a file because verify is a separate play.
|
||||
- name: Change a jail setting while an address is banned
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Read the unit state of the Fail2Ban daemon
|
||||
ansible.builtin.systemd:
|
||||
name: fail2ban
|
||||
register: fail2ban_side_effect_unit
|
||||
|
||||
- name: Keep the main PID for the verify step
|
||||
ansible.builtin.copy:
|
||||
content: "{{ fail2ban_side_effect_unit.status.MainPID }}\n"
|
||||
dest: /run/fail2ban-molecule-mainpid
|
||||
mode: "0644"
|
||||
|
||||
# 198.51.100.0/24 is TEST-NET-2 (RFC 5737), outside the 192.0.2.0/24 the converge step ignores.
|
||||
- name: Ban a documentation address on the sshd jail
|
||||
ansible.builtin.command: fail2ban-client set sshd banip 198.51.100.77
|
||||
changed_when: true
|
||||
|
||||
- name: Rewrite jail.local with a different maxretry
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.fail2ban
|
||||
vars:
|
||||
fail2ban_ignoreip:
|
||||
- 127.0.0.1/8
|
||||
- "::1"
|
||||
- 192.0.2.0/24
|
||||
fail2ban_jails:
|
||||
- name: sshd
|
||||
enabled: true
|
||||
port: ssh
|
||||
filter: sshd
|
||||
backend: systemd
|
||||
maxretry: 4
|
||||
bantime: "1h"
|
||||
findtime: "10m"
|
||||
@@ -83,3 +83,33 @@
|
||||
loop: "{{ fail2ban_verify_ignoreip.results }}"
|
||||
loop_control:
|
||||
label: "{{ item.item }}"
|
||||
|
||||
# side_effect.yml recorded the daemon's PID, banned an address and rewrote jail.local with maxretry 4. The new maxretry must be
|
||||
# active and the ban still in force. The PID must be unchanged: that is what tells a reload from a restart (a restart would
|
||||
# have restored the ban from the database too, so the ban alone proves nothing about it).
|
||||
- name: Ask the daemon for the sshd maxretry and its banned addresses
|
||||
ansible.builtin.command: "fail2ban-client get sshd {{ item }}"
|
||||
loop:
|
||||
- maxretry
|
||||
- banip
|
||||
register: fail2ban_verify_reload
|
||||
changed_when: false
|
||||
|
||||
- name: Read the PID recorded before the rewrite
|
||||
ansible.builtin.slurp:
|
||||
src: /run/fail2ban-molecule-mainpid
|
||||
register: fail2ban_verify_pid_before
|
||||
|
||||
- name: Read the unit state of the Fail2Ban daemon now
|
||||
ansible.builtin.systemd:
|
||||
name: fail2ban
|
||||
register: fail2ban_verify_unit
|
||||
|
||||
- name: The new maxretry is active, the ban is in force and the daemon was not restarted
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "fail2ban_verify_reload.results[0].stdout | trim == '4'"
|
||||
- "'198.51.100.77' in fail2ban_verify_reload.results[1].stdout"
|
||||
- "(fail2ban_verify_pid_before.content | b64decode | trim) != '0'"
|
||||
- "(fail2ban_verify_pid_before.content | b64decode | trim) == (fail2ban_verify_unit.status.MainPID | string)"
|
||||
quiet: true
|
||||
@@ -7,4 +7,4 @@
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0600"
|
||||
notify: Restart Fail2Ban
|
||||
notify: Reload Fail2Ban
|
||||
@@ -16,8 +16,8 @@
|
||||
enabled: "{{ fail2ban_service.enabled | default(true) }}"
|
||||
state: "{{ fail2ban_service.state | default('started') }}"
|
||||
|
||||
# Handlers run at the end of the play; flush so the restart happens before the
|
||||
# jail check below.
|
||||
# Handlers run at the end of the play; flush so the reload happens after the
|
||||
# configuration test above and before the jail check below.
|
||||
- name: Apply pending Fail2Ban handlers
|
||||
ansible.builtin.meta: flush_handlers
|
||||
|
||||
|
||||
Reference in new issue
Block a user