feat(fail2ban): reload instead of restart when jail.local changes
The main entry point notified a restart. A restart stops every jail, which flushes its bans out of the firewall until they are restored from Fail2Ban's database when the jail starts again, and forgets failures counted toward maxretry. A reload applies the new settings to the running jails. service_jail already reloads. The Restart Fail2Ban handler is removed: nothing notifies it any more, and a handler name is not part of the role's interface (#257). A new side_effect.yml bans an address, records the daemon's PID and rewrites jail.local; verify asserts the new maxretry is active, the ban is in force and the PID is unchanged (the PID is what separates a reload from a restart, since bans are restored from the database). Assisted-by: Claude Code
This commit is contained in:
1 parent
597590c2f1
commit
e0e9fa1b17
8 files changed
+94
-17
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user