feat(fail2ban): add the fail2ban role
Copied from the environment repository's roles/fail2ban and scrubbed for the Collection: galaxy namespace mortis, the molecule scenario calls the role by its FQCN (and takes its paths from the workflow, like docker's), README examples use mortis.kamino.fail2ban, repository-local issue references are dropped. mock_roles in .ansible-lint lists the new FQCN. Not verified locally: the molecule scenario (runs on the kamino-molecule runner in CI) and ansible-test sanity. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
8ff66fb4e8
commit
1ea478b124
19 files changed
+560
No files matched your search
@@ -0,0 +1,57 @@
|
||||
---
|
||||
- name: Converge the base role (jail.local, sshd jail on the systemd backend)
|
||||
hosts: all
|
||||
roles:
|
||||
- role: mortis.kamino.fail2ban
|
||||
|
||||
# The entry point service roles use. Two services must not overwrite each other:
|
||||
# each gets its own jail.d file.
|
||||
- name: Converge two service jails through the entry point
|
||||
hosts: all
|
||||
tasks:
|
||||
- name: Service jail with a logpath
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.fail2ban
|
||||
tasks_from: service_jail
|
||||
vars:
|
||||
fail2ban_service_name: svc-a
|
||||
fail2ban_service_jail:
|
||||
port: "8080"
|
||||
logpath: /var/log/svc-a.log
|
||||
maxretry: 4
|
||||
fail2ban_service_filter:
|
||||
failregex: "^.*auth failure from <HOST>.*$"
|
||||
|
||||
- name: Service jail with a journalmatch
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.fail2ban
|
||||
tasks_from: service_jail
|
||||
vars:
|
||||
fail2ban_service_name: svc-b
|
||||
fail2ban_service_jail:
|
||||
port: "9090"
|
||||
journalmatch: "_SYSTEMD_UNIT=ssh.service"
|
||||
fail2ban_service_filter:
|
||||
failregex: "^.*denied for <HOST>.*$"
|
||||
|
||||
# An input the entry point must reject. The block proves it is refused (the
|
||||
# verify step proves no file was left behind); the play still passes, so the
|
||||
# idempotence run stays clean.
|
||||
- name: Missing failregex is rejected
|
||||
block:
|
||||
- name: Service jail without a failregex
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.fail2ban
|
||||
tasks_from: service_jail
|
||||
vars:
|
||||
fail2ban_service_name: svc-bad
|
||||
fail2ban_service_jail:
|
||||
logpath: /var/log/svc-a.log
|
||||
fail2ban_service_filter: {}
|
||||
- name: Fail when the invalid input was accepted
|
||||
ansible.builtin.fail:
|
||||
msg: service_jail accepted a filter without a failregex
|
||||
rescue:
|
||||
- name: The entry point refused the input
|
||||
ansible.builtin.debug:
|
||||
msg: Rejected as expected
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404 image
|
||||
# (MOLECULE_IMAGE, the pin in .gitea/workflows/molecule.yml). No privileged mode: systemd needs a
|
||||
# writable cgroup and the firewall tools need NET_ADMIN, nothing else.
|
||||
driver:
|
||||
name: docker
|
||||
platforms:
|
||||
- name: fail2ban-instance
|
||||
image: ${MOLECULE_IMAGE}
|
||||
pre_build_image: true
|
||||
pull: true
|
||||
command: /sbin/init
|
||||
cgroupns_mode: host
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
tmpfs:
|
||||
- /run
|
||||
- /run/lock
|
||||
capabilities:
|
||||
- NET_ADMIN
|
||||
security_opts:
|
||||
- apparmor=unconfined
|
||||
provisioner:
|
||||
name: ansible
|
||||
verifier:
|
||||
name: ansible
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
# The log file a service jail watches must exist, or `fail2ban-client -t` fails.
|
||||
- name: Prepare
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Create the log file the logpath jail watches
|
||||
ansible.builtin.file:
|
||||
path: /var/log/svc-a.log
|
||||
state: touch
|
||||
mode: "0644"
|
||||
access_time: preserve
|
||||
modification_time: preserve
|
||||
@@ -0,0 +1,55 @@
|
||||
---
|
||||
# What matters: the daemon accepts the whole configuration, loads the base jail and
|
||||
# both service jails (each from its own jail.d file, #433), the sshd jail does not
|
||||
# fall back to the empty auth.log, and a rejected input left nothing behind. The
|
||||
# file contents are the templates' own output and are not restated here.
|
||||
- name: Verify
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Read jail.local
|
||||
ansible.builtin.slurp:
|
||||
src: /etc/fail2ban/jail.local
|
||||
register: fail2ban_verify_jail_local
|
||||
|
||||
- name: The sshd jail reads the journal, not auth.log
|
||||
ansible.builtin.assert:
|
||||
that: "'/var/log/auth.log' not in (fail2ban_verify_jail_local.content | b64decode)"
|
||||
quiet: true
|
||||
|
||||
- name: Stat the service jails and the rejected one
|
||||
ansible.builtin.stat:
|
||||
path: "{{ item }}"
|
||||
loop:
|
||||
- /etc/fail2ban/jail.d/svc-a.local
|
||||
- /etc/fail2ban/jail.d/svc-b.local
|
||||
- /etc/fail2ban/jail.d/svc-bad.local
|
||||
- /etc/fail2ban/filter.d/svc-bad.conf
|
||||
register: fail2ban_verify_files
|
||||
|
||||
- name: Each service has its own file and the rejected input left nothing behind
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- fail2ban_verify_files.results[0].stat.exists
|
||||
- fail2ban_verify_files.results[1].stat.exists
|
||||
- not fail2ban_verify_files.results[2].stat.exists
|
||||
- not fail2ban_verify_files.results[3].stat.exists
|
||||
quiet: true
|
||||
|
||||
- name: Test the configuration the daemon will load
|
||||
ansible.builtin.command: fail2ban-client -t
|
||||
changed_when: false
|
||||
|
||||
- name: Ask the running daemon for its jails
|
||||
ansible.builtin.command: fail2ban-client status
|
||||
register: fail2ban_verify_status
|
||||
changed_when: false
|
||||
|
||||
- name: The daemon loaded the base and both service jails
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "'sshd' in fail2ban_verify_status.stdout"
|
||||
- "'svc-a' in fail2ban_verify_status.stdout"
|
||||
- "'svc-b' in fail2ban_verify_status.stdout"
|
||||
- "'svc-bad' not in fail2ban_verify_status.stdout"
|
||||
quiet: true
|
||||
Reference in new issue
Block a user