diff --git a/docs/images/molecule-ubuntu2404.md b/docs/images/molecule-ubuntu2404.md index 98e8dec..8f0a78d 100644 --- a/docs/images/molecule-ubuntu2404.md +++ b/docs/images/molecule-ubuntu2404.md @@ -11,9 +11,10 @@ It is the release the hosts run today. - systemd as the init system (`systemd`, `systemd-sysv`), D-Bus, `sudo`, `iproute2`, `ca-certificates`. - Python for Ansible on the target: `python3`, `python3-apt`, `python3-systemd`. -- The packages the roles manage, preinstalled so a converge makes no network calls: - `iptables`, `ufw`, `fail2ban`, `openssh-server` (the `ssh` unit is enabled), and `gnupg` - (the docker scenario signs its stand-in APT repository with it). +- Test fixtures no role installs: `iptables`, `openssh-server` (the `ssh` unit is enabled), and + `gnupg` (the docker scenario signs its stand-in APT repository with it). +- No package a role installs, such as `ufw` or `fail2ban`: the role's scenario exercises that + install. The APT lists are removed, so such a scenario refreshes the APT cache in its prepare. - Throwaway SSH host keys and a machine-id generated at build time. They are not secrets and the image is a test target that must never be exposed to a network. diff --git a/docs/images/molecule-ubuntu2604.md b/docs/images/molecule-ubuntu2604.md index f53f2ff..c891f15 100644 --- a/docs/images/molecule-ubuntu2604.md +++ b/docs/images/molecule-ubuntu2604.md @@ -11,9 +11,10 @@ It is the release the hosts are moving to; every role scenario whose role declar - systemd as the init system (`systemd`, `systemd-sysv`), D-Bus, `sudo`, `iproute2`, `ca-certificates`. - Python for Ansible on the target: `python3`, `python3-apt`, `python3-systemd`. -- The packages the roles manage, preinstalled so a converge makes no network calls: - `iptables`, `ufw`, `fail2ban`, `openssh-server` (the `ssh` unit is enabled), and `gnupg` - (the docker scenario signs its stand-in APT repository with it). +- Test fixtures no role installs: `iptables`, `openssh-server` (the `ssh` unit is enabled), and + `gnupg` (the docker scenario signs its stand-in APT repository with it). +- No package a role installs, such as `ufw` or `fail2ban`: the role's scenario exercises that + install. The APT lists are removed, so such a scenario refreshes the APT cache in its prepare. - root's `sudo` skips the PAM account check (`pam_rootok`), so `become` works in the container. - Throwaway SSH host keys and a machine-id generated at build time. They are not secrets and the image is a test target that must never be exposed to a network. diff --git a/extensions/molecule/image-smoke-2604/verify.yml b/extensions/molecule/image-smoke-2604/verify.yml index a45cb09..710b69a 100644 --- a/extensions/molecule/image-smoke-2604/verify.yml +++ b/extensions/molecule/image-smoke-2604/verify.yml @@ -15,7 +15,7 @@ ansible.builtin.package_facts: manager: auto - - name: The packages the roles manage are preinstalled + - name: The fixtures the scenarios need are preinstalled ansible.builtin.assert: that: item in ansible_facts['packages'] quiet: true @@ -30,8 +30,6 @@ - python3-apt - python3-systemd - iptables - - ufw - - fail2ban - openssh-server - gnupg diff --git a/extensions/molecule/image-smoke/verify.yml b/extensions/molecule/image-smoke/verify.yml index 0d9e767..89d39e1 100644 --- a/extensions/molecule/image-smoke/verify.yml +++ b/extensions/molecule/image-smoke/verify.yml @@ -15,7 +15,7 @@ ansible.builtin.package_facts: manager: auto - - name: The packages the roles manage are preinstalled + - name: The fixtures the scenarios need are preinstalled ansible.builtin.assert: that: item in ansible_facts['packages'] quiet: true @@ -30,8 +30,6 @@ - python3-apt - python3-systemd - iptables - - ufw - - fail2ban - openssh-server - gnupg diff --git a/images/molecule-ubuntu2404/Dockerfile b/images/molecule-ubuntu2404/Dockerfile index 3946f10..a0f809e 100644 --- a/images/molecule-ubuntu2404/Dockerfile +++ b/images/molecule-ubuntu2404/Dockerfile @@ -1,7 +1,7 @@ # Molecule test target for the roles of this Collection: Ubuntu 24.04 with systemd -# as PID 1 and the packages the roles manage preinstalled, so a converge makes no -# network calls. It replaces the molecule test image that used to be built locally -# on the CI runner. +# as PID 1 and the fixtures the scenarios need preinstalled. A package a role installs +# is not baked in, so its scenario exercises the install (ADR 0002). It replaces the +# molecule test image that used to be built locally on the CI runner. # # Built by .gitea/workflows/image.yml with kaniko and pushed (never on pull # requests) to git.rkdr.net/mortis/molecule-ubuntu2404. Consumers reference it @@ -14,7 +14,7 @@ # scenario starts it with command /sbin/init, cgroupns_mode host, /sys/fs/cgroup # mounted rw, tmpfs on /run and /run/lock, capability NET_ADMIN. No privileged mode. # -# One RUN layer: about 30 MB compressed on top of the 29.8 MB base (estimated from +# One RUN layer: at most about 30 MB compressed on top of the 29.8 MB base (estimated from # the real .debs, with the .pyc files the postinst scripts compile), far below # Cloudflare's 100 MB request-body limit on git.rkdr.net (kaniko pushes a layer as # one request; gnupg adds a few MB, not measured). apt packages are not version-pinned, as in the original image; the @@ -28,7 +28,7 @@ FROM ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b LABEL org.opencontainers.image.title="molecule-ubuntu2404" \ - org.opencontainers.image.description="Disposable Ubuntu 24.04 test target for the molecule scenarios of the mortis.kamino Ansible Collection. systemd runs as PID 1 with D-Bus, sudo, python3 and the packages the roles manage (iptables, ufw, fail2ban, openssh-server) preinstalled, so a converge makes no network calls. Start it as a molecule platform (usage label); it carries throwaway SSH host keys and is a test target only, never to be exposed." \ + org.opencontainers.image.description="Disposable Ubuntu 24.04 test target for the molecule scenarios of the mortis.kamino Ansible Collection. systemd runs as PID 1 with D-Bus, sudo, python3 and the test fixtures (iptables, openssh-server, gnupg) preinstalled. A package a role installs is not baked in and no APT lists are kept, so a scenario refreshes the APT cache before a role installs one. Start it as a molecule platform (usage label); it carries throwaway SSH host keys and is a test target only, never to be exposed." \ org.opencontainers.image.source="https://git.rkdr.net/mortis/kamino" \ org.opencontainers.image.url="https://git.rkdr.net/mortis/kamino" \ org.opencontainers.image.documentation="https://git.rkdr.net/mortis/kamino/src/branch/main/docs/images/molecule-ubuntu2404.md" \ @@ -36,22 +36,20 @@ LABEL org.opencontainers.image.title="molecule-ubuntu2404" \ org.opencontainers.image.authors="Defame1297 and contributors" \ org.opencontainers.image.base.name="ubuntu:24.04" \ net.rkdr.mortis.purpose="Disposable Ubuntu 24.04 test target for the molecule scenarios of the mortis.kamino Ansible Collection." \ - net.rkdr.mortis.contents="Ubuntu 24.04, systemd, systemd-sysv, dbus, sudo, iproute2, ca-certificates, python3, python3-apt, python3-systemd, iptables, ufw, fail2ban, openssh-server (ssh enabled), gnupg" \ + net.rkdr.mortis.contents="Ubuntu 24.04, systemd, systemd-sysv, dbus, sudo, iproute2, ca-certificates, python3, python3-apt, python3-systemd, iptables, openssh-server (ssh enabled), gnupg" \ net.rkdr.mortis.usage="Molecule docker-driver platform with pre_build_image true, command /sbin/init, cgroupns_mode host, /sys/fs/cgroup mounted rw, tmpfs on /run and /run/lock, capability NET_ADMIN, security_opts apparmor=unconfined (not privileged). Pin git.rkdr.net/mortis/name:version@sha256:digest. See the documentation label." ENV DEBIAN_FRONTEND=noninteractive # systemd as PID 1 with D-Bus, sudo and networking tools; the Python Ansible needs on -# the target (python3-apt for the apt module); the firewall and intrusion-prevention -# packages the roles manage (python3-systemd is a dependency of fail2ban's systemd -# backend); sshd, enabled so a converge can test it under systemd; gnupg, which the docker -# scenario signs its stand-in APT repository with and which would otherwise cost it an -# apt update and install in every run. +# the target (python3-apt for the apt module, python3-systemd for the journal); iptables; +# sshd, enabled so a converge can test it under systemd; gnupg, which the docker scenario +# signs its stand-in APT repository with. ufw and fail2ban are left out: their roles install them. RUN apt-get update \ && apt-get install -y --no-install-recommends \ systemd systemd-sysv dbus sudo iproute2 ca-certificates \ python3 python3-apt python3-systemd \ - iptables ufw fail2ban \ + iptables \ openssh-server \ gnupg \ && rm -rf /var/lib/apt/lists/* \ diff --git a/images/molecule-ubuntu2604/Dockerfile b/images/molecule-ubuntu2604/Dockerfile index b495f2d..612c939 100644 --- a/images/molecule-ubuntu2604/Dockerfile +++ b/images/molecule-ubuntu2604/Dockerfile @@ -1,7 +1,7 @@ # Molecule test target for the roles of this Collection: Ubuntu 26.04 with systemd -# as PID 1 and the packages the roles manage preinstalled, so a converge makes no -# network calls. It replaces the molecule test image that used to be built locally -# on the CI runner. +# as PID 1 and the fixtures the scenarios need preinstalled. A package a role installs +# is not baked in, so its scenario exercises the install (ADR 0002). It replaces the +# molecule test image that used to be built locally on the CI runner. # # Built by .gitea/workflows/image.yml with kaniko and pushed (never on pull # requests) to git.rkdr.net/mortis/molecule-ubuntu2604. Consumers reference it @@ -14,7 +14,7 @@ # scenario starts it with command /sbin/init, cgroupns_mode host, /sys/fs/cgroup # mounted rw, tmpfs on /run and /run/lock, capability NET_ADMIN. No privileged mode. # -# One RUN layer: about 30 MB compressed on top of the 29.8 MB base (estimated from +# One RUN layer: at most about 30 MB compressed on top of the 29.8 MB base (estimated from # the real .debs, with the .pyc files the postinst scripts compile), far below # Cloudflare's 100 MB request-body limit on git.rkdr.net (kaniko pushes a layer as # one request; gnupg adds a few MB, not measured). apt packages are not version-pinned, as in the original image; the @@ -28,7 +28,7 @@ FROM ubuntu:26.04@sha256:d66b366dac61b575bee2aef07e9c26e8070096fbe84a3d4d3eb0da069b5d76db LABEL org.opencontainers.image.title="molecule-ubuntu2604" \ - org.opencontainers.image.description="Disposable Ubuntu 26.04 test target for the molecule scenarios of the mortis.kamino Ansible Collection. systemd runs as PID 1 with D-Bus, sudo, python3 and the packages the roles manage (iptables, ufw, fail2ban, openssh-server) preinstalled, so a converge makes no network calls. Start it as a molecule platform (usage label); it carries throwaway SSH host keys and is a test target only, never to be exposed." \ + org.opencontainers.image.description="Disposable Ubuntu 26.04 test target for the molecule scenarios of the mortis.kamino Ansible Collection. systemd runs as PID 1 with D-Bus, sudo, python3 and the test fixtures (iptables, openssh-server, gnupg) preinstalled. A package a role installs is not baked in and no APT lists are kept, so a scenario refreshes the APT cache before a role installs one. Start it as a molecule platform (usage label); it carries throwaway SSH host keys and is a test target only, never to be exposed." \ org.opencontainers.image.source="https://git.rkdr.net/mortis/kamino" \ org.opencontainers.image.url="https://git.rkdr.net/mortis/kamino" \ org.opencontainers.image.documentation="https://git.rkdr.net/mortis/kamino/src/branch/main/docs/images/molecule-ubuntu2604.md" \ @@ -36,24 +36,22 @@ LABEL org.opencontainers.image.title="molecule-ubuntu2604" \ org.opencontainers.image.authors="Defame1297 and contributors" \ org.opencontainers.image.base.name="ubuntu:26.04" \ net.rkdr.mortis.purpose="Disposable Ubuntu 26.04 test target for the molecule scenarios of the mortis.kamino Ansible Collection." \ - net.rkdr.mortis.contents="Ubuntu 26.04, systemd, systemd-sysv, dbus, sudo, iproute2, ca-certificates, python3, python3-apt, python3-systemd, iptables, ufw, fail2ban, openssh-server (ssh enabled), gnupg" \ + net.rkdr.mortis.contents="Ubuntu 26.04, systemd, systemd-sysv, dbus, sudo, iproute2, ca-certificates, python3, python3-apt, python3-systemd, iptables, openssh-server (ssh enabled), gnupg" \ net.rkdr.mortis.usage="Molecule docker-driver platform with pre_build_image true, command /sbin/init, cgroupns_mode host, /sys/fs/cgroup mounted rw, tmpfs on /run and /run/lock, capability NET_ADMIN, security_opts apparmor=unconfined (not privileged). Pin git.rkdr.net/mortis/name:version@sha256:digest. See the documentation label." ENV DEBIAN_FRONTEND=noninteractive # systemd as PID 1 with D-Bus, sudo and networking tools; the Python Ansible needs on -# the target (python3-apt for the apt module); the firewall and intrusion-prevention -# packages the roles manage (python3-systemd is a dependency of fail2ban's systemd -# backend); sshd, enabled so a converge can test it under systemd; gnupg, which the docker -# scenario signs its stand-in APT repository with and which would otherwise cost it an -# apt update and install in every run. +# the target (python3-apt for the apt module, python3-systemd for the journal); iptables; +# sshd, enabled so a converge can test it under systemd; gnupg, which the docker scenario +# signs its stand-in APT repository with. ufw and fail2ban are left out: their roles install them. # root's sudo (Ansible's become here) skips pam_unix's account check, whose unix_chkpwd # helper fails inside the container; non-root callers still go through pam_unix. RUN apt-get update \ && apt-get install -y --no-install-recommends \ systemd systemd-sysv dbus sudo iproute2 ca-certificates \ python3 python3-apt python3-systemd \ - iptables ufw fail2ban \ + iptables \ openssh-server \ gnupg \ && rm -rf /var/lib/apt/lists/* \ diff --git a/roles/fail2ban/README.md b/roles/fail2ban/README.md index 8ce1978..e1c6aff 100644 --- a/roles/fail2ban/README.md +++ b/roles/fail2ban/README.md @@ -82,7 +82,7 @@ Main entry point: `fail2ban` (everything), `fail2ban_install`, `fail2ban_validat ## Tests -The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It runs `check` before the converge (a fresh host, where the image already has fail2ban installed) and after it (a configured host). +The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It runs `check` before the converge (a fresh host, where the role installs fail2ban) and after it (a configured host). - Converge applies the main entry point with an extra `fail2ban_ignoreip` network and calls `service_jail` twice, one jail with a `logpath` and one with a `journalmatch`. - Converge also runs inputs that must be refused: a service filter without `failregex`, and a `fail2ban_ignoreip` entry with whitespace, a plain string instead of a list, and an entry starting with `;`. The `fail2ban_ignoreip` refusals are matched on their message. diff --git a/roles/fail2ban/molecule/check-absent/molecule.yml b/roles/fail2ban/molecule/check-absent/molecule.yml index 1ddf898..fcb89c0 100644 --- a/roles/fail2ban/molecule/check-absent/molecule.yml +++ b/roles/fail2ban/molecule/check-absent/molecule.yml @@ -3,7 +3,7 @@ # (MOLECULE_IMAGE) and molecule-ubuntu2604 (MOLECULE_IMAGE_2604) images, pinned in # .gitea/workflows/molecule.yml. No privileged mode: systemd needs a writable cgroup and the # firewall tools need NET_ADMIN, nothing else. Needs the network: -# prepare purges the fail2ban the image ships and refreshes the APT cache the image deletes. +# prepare purges any fail2ban and refreshes the APT cache the image deletes. driver: name: docker platforms: diff --git a/roles/fail2ban/molecule/check-absent/prepare.yml b/roles/fail2ban/molecule/check-absent/prepare.yml index 7e599de..ffd3d9f 100644 --- a/roles/fail2ban/molecule/check-absent/prepare.yml +++ b/roles/fail2ban/molecule/check-absent/prepare.yml @@ -1,6 +1,6 @@ --- -# The image ships fail2ban and no APT lists; the check step needs fail2ban gone and the lists back, -# or the install task cannot even preview the package. +# The purge keeps the scenario independent of what the image ships. The image keeps no APT lists, +# and the check step needs them, or the install task cannot even preview the package. - name: Prepare a host without fail2ban hosts: all gather_facts: false diff --git a/roles/fail2ban/molecule/default/prepare.yml b/roles/fail2ban/molecule/default/prepare.yml index 4a145df..22f174f 100644 --- a/roles/fail2ban/molecule/default/prepare.yml +++ b/roles/fail2ban/molecule/default/prepare.yml @@ -1,9 +1,15 @@ --- -# The log file a service jail watches must exist, or `fail2ban-client -t` fails. +# The image keeps no APT lists, and --check does not refresh them, so the fresh-host check needs +# them to preview the fail2ban install. The log file a service jail watches must exist, or +# `fail2ban-client -t` fails. - name: Prepare hosts: all gather_facts: false tasks: + - name: Refresh the APT cache + ansible.builtin.apt: + update_cache: true + - name: Create the log file the logpath jail watches ansible.builtin.file: path: /var/log/svc-a.log diff --git a/roles/gitea/molecule/default/molecule.yml b/roles/gitea/molecule/default/molecule.yml index 658e893..5b56634 100644 --- a/roles/gitea/molecule/default/molecule.yml +++ b/roles/gitea/molecule/default/molecule.yml @@ -7,7 +7,7 @@ # the service starts) is not covered. The mint tasks only assert, but the remove-path play at the # end of verify.yml needs systemd (it stops and disables a stub unit) and ufw (it deletes the Gitea # rules), so the container runs systemd as PID 1 with a writable cgroup and NET_ADMIN. No -# privileged mode. Both images have ufw and gnupg preinstalled, so the scenario downloads nothing. +# privileged mode. Both images have gnupg preinstalled; prepare.yml installs ufw from the archive. driver: name: docker platforms: diff --git a/roles/gitea/molecule/default/prepare.yml b/roles/gitea/molecule/default/prepare.yml new file mode 100644 index 0000000..046b365 --- /dev/null +++ b/roles/gitea/molecule/default/prepare.yml @@ -0,0 +1,12 @@ +--- +# The remove-path play in verify.yml needs ufw, which the image does not ship. It keeps no APT +# lists, so the install refreshes them. +- name: Prepare + hosts: all + gather_facts: false + tasks: + - name: Install ufw + ansible.builtin.apt: + name: ufw + state: present + update_cache: true diff --git a/roles/ufw/molecule/check-absent/molecule.yml b/roles/ufw/molecule/check-absent/molecule.yml index 1ca88cf..5a49e9f 100644 --- a/roles/ufw/molecule/check-absent/molecule.yml +++ b/roles/ufw/molecule/check-absent/molecule.yml @@ -3,7 +3,7 @@ # (MOLECULE_IMAGE) and molecule-ubuntu2604 (MOLECULE_IMAGE_2604) images, pinned in # .gitea/workflows/molecule.yml. No privileged mode: systemd needs a writable cgroup and the # firewall tools need NET_ADMIN, nothing else. Needs the network: -# prepare purges the ufw the image ships and refreshes the APT cache the image deletes. +# prepare purges any ufw and refreshes the APT cache the image deletes. driver: name: docker platforms: diff --git a/roles/ufw/molecule/check-absent/prepare.yml b/roles/ufw/molecule/check-absent/prepare.yml index 6c98ec3..bea1759 100644 --- a/roles/ufw/molecule/check-absent/prepare.yml +++ b/roles/ufw/molecule/check-absent/prepare.yml @@ -1,6 +1,6 @@ --- -# The image ships ufw and no APT lists; the check step needs ufw gone and the lists back, or the -# install task cannot even preview the package. +# The purge keeps the scenario independent of what the image ships. The image keeps no APT lists, +# and the check step needs them, or the install task cannot even preview the package. - name: Prepare a host without ufw hosts: all gather_facts: false diff --git a/roles/ufw/molecule/default/prepare.yml b/roles/ufw/molecule/default/prepare.yml index c16fff8..3dd444e 100644 --- a/roles/ufw/molecule/default/prepare.yml +++ b/roles/ufw/molecule/default/prepare.yml @@ -7,10 +7,12 @@ hosts: all gather_facts: false tasks: + # The image keeps no APT lists, so the install refreshes them. - name: Install ufw - ansible.builtin.package: + ansible.builtin.apt: name: ufw state: present + update_cache: true # An application profile whose name holds openssh, for the orphan seeds below. - name: Define an application profile named like SSH