From 2e70980b4473cbd6e2bd9e39021e6c6dce31f51c Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Fri, 27 Nov 2026 14:13:31 +0000 Subject: [PATCH] test: check refusals once on the controller in verify converge.yml is replayed by check, converge, check and idempotence on both platforms, so the validation-only plays in it ran eight times per scenario. They need no container: move them to the top of verify.yml, on localhost in check mode, so they run once and a regressed refusal cannot change the controller. Applies to the default scenarios of system_baseline, docker, sshd and systemd_hardening. The systemd_hardening refusal of a drop-in entry that is not a mapping comes after the disable and mask tasks, so it moves to verify.yml but stays on the container. The README Tests sections say where each check now runs. Assisted-by: Claude Code:claude-opus-5-5 --- changelogs/fragments/refusals-in-verify.yml | 6 + roles/docker/molecule/default/converge.yml | 113 ------ roles/docker/molecule/default/verify.yml | 129 +++++++ roles/sshd/README.md | 4 +- roles/sshd/molecule/default/converge.yml | 70 ---- roles/sshd/molecule/default/verify.yml | 79 ++++- roles/system_baseline/README.md | 8 +- .../molecule/default/converge.yml | 304 ---------------- .../molecule/default/verify.yml | 334 ++++++++++++++++++ .../molecule/default/converge.yml | 88 ----- .../molecule/default/verify.yml | 95 +++++ 11 files changed, 648 insertions(+), 582 deletions(-) create mode 100644 changelogs/fragments/refusals-in-verify.yml diff --git a/changelogs/fragments/refusals-in-verify.yml b/changelogs/fragments/refusals-in-verify.yml new file mode 100644 index 0000000..b5791aa --- /dev/null +++ b/changelogs/fragments/refusals-in-verify.yml @@ -0,0 +1,6 @@ +--- +trivial: + - >- + docker, sshd, system_baseline, systemd_hardening - The Molecule scenarios check the refusals and the + accepted inputs once, on the controller, in verify instead of in every converge run; the README Tests + sections say so. diff --git a/roles/docker/molecule/default/converge.yml b/roles/docker/molecule/default/converge.yml index d0cb6e8..652e5bd 100644 --- a/roles/docker/molecule/default/converge.yml +++ b/roles/docker/molecule/default/converge.yml @@ -20,116 +20,3 @@ max-concurrent-downloads: 3 roles: - role: mortis.kamino.docker - -# The plays below are refusals: argument_specs rejects the wrong types before any task runs, -# so the idempotence run stays clean. A sentinel in the failure message tells "the role -# refused" from "the fail task after the role ran", which a bare rescue cannot. -- name: A docker_users value that is not a list is refused - hosts: all - gather_facts: false - vars: - docker_users: - not: a-list - tasks: - - name: Apply the role with a mapping for docker_users - block: - - name: Include the role - ansible.builtin.include_role: - name: mortis.kamino.docker - - name: Fail when the wrong type was accepted - ansible.builtin.fail: - msg: MOLECULE-ACCEPTED - rescue: - - name: The argument check stopped the play - ansible.builtin.assert: - that: - - "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')" - - "'docker_users' in ansible_failed_result.msg | default('')" - quiet: true - -- name: A docker_remove value that is not a boolean is refused - hosts: all - gather_facts: false - vars: - docker_remove: maybe - tasks: - - name: Apply the role with a non-boolean docker_remove - block: - - name: Include the role - ansible.builtin.include_role: - name: mortis.kamino.docker - - name: Fail when the wrong type was accepted - ansible.builtin.fail: - msg: MOLECULE-ACCEPTED - rescue: - - name: The argument check stopped the play - ansible.builtin.assert: - that: - - "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')" - - "'docker_remove' in ansible_failed_result.msg | default('')" - quiet: true - -# The architecture is written into the one-line apt source, so a value with a space or a bracket is refused before anything -# changes. The derived default is covered by the converge above and verify.yml (the container is x86_64, so the source says amd64). -- name: A docker_apt_arch value that would corrupt the apt source is refused - hosts: all - gather_facts: false - vars: - docker_apt_arch: "bad arch" - tasks: - - name: Apply the role with a bad architecture - block: - - name: Include the role - ansible.builtin.include_role: - name: mortis.kamino.docker - - name: Fail when the bad architecture was accepted - ansible.builtin.fail: - msg: MOLECULE-ACCEPTED - rescue: - - name: The validation stopped the play - ansible.builtin.assert: - that: - - "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')" - - "'docker_apt_arch' in ansible_failed_result.msg | default('')" - quiet: true - -# The unit timings are checked before anything changes. -- name: A docker_stop_at_shutdown_timeout of 0 is refused - hosts: all - gather_facts: false - vars: - docker_stop_at_shutdown_timeout: 0 - tasks: - - name: Apply the role with a timeout of 0 - block: - - name: Include the role - ansible.builtin.include_role: - name: mortis.kamino.docker - - name: Fail when the timeout was accepted - ansible.builtin.fail: - msg: MOLECULE-ACCEPTED - rescue: - - name: The validation stopped the play - ansible.builtin.assert: - that: - - "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')" - - "'docker_stop_at_shutdown_timeout' in ansible_failed_result.msg | default('')" - quiet: true - -# A key that docker_daemon_options and a dedicated input both set, and a log value dockerd would reject, stop the run before any change. -- name: A bad daemon setting is refused - hosts: all - gather_facts: false - tasks: - - name: Apply the role with each bad setting - ansible.builtin.include_tasks: tasks/refuse_daemon.yml - vars: - docker_refused: "{{ item }}" - loop: - - {docker_live_restore: true, docker_daemon_options: {live-restore: false}} - - {docker_log_driver: local, docker_daemon_options: {log-driver: json-file}} - - {docker_log_max_size: 10m, docker_daemon_options: {log-opts: {max-size: 1m}}} - - {docker_log_driver: Local} - - {docker_log_max_size: ten} - - {docker_log_max_file: "0"} - - {docker_log_max_file: "-1"} diff --git a/roles/docker/molecule/default/verify.yml b/roles/docker/molecule/default/verify.yml index 522fc5d..70bef8a 100644 --- a/roles/docker/molecule/default/verify.yml +++ b/roles/docker/molecule/default/verify.yml @@ -1,4 +1,133 @@ --- +# The refusals need no container, so they run once, on the controller, in check mode so that a +# refusal that regressed cannot change it. The controller has no gathered facts, so a play that +# does not test the architecture sets it. A sentinel in the failure message tells "the role +# refused" from "the fail task after the role ran", which a bare rescue cannot. +- name: A docker_users value that is not a list is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + docker_apt_arch: amd64 + docker_users: + not: a-list + tasks: + - name: Apply the role with a mapping for docker_users + block: + - name: Include the role + ansible.builtin.include_role: + name: mortis.kamino.docker + - name: Fail when the wrong type was accepted + ansible.builtin.fail: + msg: MOLECULE-ACCEPTED + rescue: + - name: The argument check stopped the play + ansible.builtin.assert: + that: + - "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')" + - "'docker_users' in ansible_failed_result.msg | default('')" + quiet: true + +- name: A docker_remove value that is not a boolean is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + docker_apt_arch: amd64 + docker_remove: maybe + tasks: + - name: Apply the role with a non-boolean docker_remove + block: + - name: Include the role + ansible.builtin.include_role: + name: mortis.kamino.docker + - name: Fail when the wrong type was accepted + ansible.builtin.fail: + msg: MOLECULE-ACCEPTED + rescue: + - name: The argument check stopped the play + ansible.builtin.assert: + that: + - "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')" + - "'docker_remove' in ansible_failed_result.msg | default('')" + quiet: true + +# The architecture is written into the one-line apt source, so a value with a space or a bracket is refused before anything +# changes. The derived default is covered by converge.yml and the plays below (the container is x86_64, so the source says amd64). +- name: A docker_apt_arch value that would corrupt the apt source is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + docker_apt_arch: "bad arch" + tasks: + - name: Apply the role with a bad architecture + block: + - name: Include the role + ansible.builtin.include_role: + name: mortis.kamino.docker + - name: Fail when the bad architecture was accepted + ansible.builtin.fail: + msg: MOLECULE-ACCEPTED + rescue: + - name: The validation stopped the play + ansible.builtin.assert: + that: + - "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')" + - "'docker_apt_arch' in ansible_failed_result.msg | default('')" + quiet: true + +# The unit timings are checked before anything changes. +- name: A docker_stop_at_shutdown_timeout of 0 is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + docker_apt_arch: amd64 + docker_stop_at_shutdown_timeout: 0 + tasks: + - name: Apply the role with a timeout of 0 + block: + - name: Include the role + ansible.builtin.include_role: + name: mortis.kamino.docker + - name: Fail when the timeout was accepted + ansible.builtin.fail: + msg: MOLECULE-ACCEPTED + rescue: + - name: The validation stopped the play + ansible.builtin.assert: + that: + - "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')" + - "'docker_stop_at_shutdown_timeout' in ansible_failed_result.msg | default('')" + quiet: true + +# A key that docker_daemon_options and a dedicated input both set, and a log value dockerd would reject, stop the run before any change. +- name: A bad daemon setting is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + docker_apt_arch: amd64 + tasks: + - name: Apply the role with each bad setting + ansible.builtin.include_tasks: tasks/refuse_daemon.yml + vars: + docker_refused: "{{ item }}" + loop: + - {docker_live_restore: true, docker_daemon_options: {live-restore: false}} + - {docker_log_driver: local, docker_daemon_options: {log-driver: json-file}} + - {docker_log_max_size: 10m, docker_daemon_options: {log-opts: {max-size: 1m}}} + - {docker_log_driver: Local} + - {docker_log_max_size: ten} + - {docker_log_max_file: "0"} + - {docker_log_max_file: "-1"} + # What the role is for, as far as stand-in packages can show it: the repository key and the # source line are in place and apt really used them, the packages are installed from that # repository, the unit is enabled and running, and the user joined the group. The second play diff --git a/roles/sshd/README.md b/roles/sshd/README.md index 7b20144..720ae26 100644 --- a/roles/sshd/README.md +++ b/roles/sshd/README.md @@ -61,8 +61,8 @@ The role defines none. The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). Both images ship openssh-server. It runs `check` before the converge (a fresh host) and after it (a configured host). -- Converge applies the `ubuntu_lxc` profile. It then loads the defaults through `tasks_from: validate.yml` without changing the host: every profile (`pve`, `ubuntu_lxc`, `ubuntu_vm`) passes validation and keeps its root login policy and key algorithms, a consumer `sshd_settings` override wins over the profile, and the `pve` guard accepts each value that allows root key login. -- Verify first checks the refusals: a run without `sshd_profile`, `PubkeyAuthentication no` (lock-out), a `pve` profile with `PermitRootLogin no` or `forced-commands-only`, a `pve` profile without `rsa-sha2-512`, SHA-1 `ssh-rsa`, and an unknown profile. Each but the missing profile is matched on its message. +- Converge applies the `ubuntu_lxc` profile. Verify first loads the defaults on the controller through `tasks_from: validate.yml`: every profile (`pve`, `ubuntu_lxc`, `ubuntu_vm`) passes validation and keeps its root login policy and key algorithms, a consumer `sshd_settings` override wins over the profile, and the `pve` guard accepts each value that allows root key login. +- Verify then checks the refusals: a run without `sshd_profile`, `PubkeyAuthentication no` (lock-out), a `pve` profile with `PermitRootLogin no` or `forced-commands-only`, a `pve` profile without `rsa-sha2-512`, SHA-1 `ssh-rsa`, and an unknown profile. Each but the missing profile is matched on its message. - Verify then checks the converged host: the drop-in exists, `sshd -t` passes, the `ssh` unit is active, and `sshd -T` shows key login on and password and root login off. - The `pve` profile is checked by rendering its drop-in to a scratch file and parsing it with `sshd -T -f`: root logs in with keys only and passwords stay off. diff --git a/roles/sshd/molecule/default/converge.yml b/roles/sshd/molecule/default/converge.yml index 05d21b2..dc5401a 100644 --- a/roles/sshd/molecule/default/converge.yml +++ b/roles/sshd/molecule/default/converge.yml @@ -7,73 +7,3 @@ ansible_user: root roles: - role: mortis.kamino.sshd - -# The plays below only load the role's defaults and run validate.yml, which changes nothing -# on the container. They cover the profiles the converge above cannot run and the values -# the guards accept. -- name: Every profile passes validation and keeps its root login policy - hosts: all - gather_facts: false - tasks: - - name: Validate each profile - ansible.builtin.include_role: - name: mortis.kamino.sshd - tasks_from: validate.yml - public: true - vars: - sshd_profile: "{{ item }}" - loop: [pve, ubuntu_lxc, ubuntu_vm] - - name: Assert the profile data - ansible.builtin.assert: - that: - - sshd_profiles.pve.PermitRootLogin == "prohibit-password" - - sshd_profiles.ubuntu_lxc.PermitRootLogin == "no" - - sshd_profiles.ubuntu_vm.PermitRootLogin == "no" - quiet: true - # Proxmox cluster SSH needs RSA with SHA-2, never SHA-1 ssh-rsa; the others stay ed25519 only. - - name: Assert the key algorithms per profile - ansible.builtin.assert: - that: - - sshd_profiles.pve.HostKeyAlgorithms == "ssh-ed25519,rsa-sha2-512,rsa-sha2-256" - - sshd_profiles.pve.PubkeyAcceptedAlgorithms == "ssh-ed25519,rsa-sha2-512,rsa-sha2-256" - - sshd_settings_defaults.HostKeyAlgorithms == "ssh-ed25519" - - sshd_settings_defaults.PubkeyAcceptedAlgorithms == "ssh-ed25519" - - "'HostKeyAlgorithms' not in sshd_profiles.ubuntu_lxc" - - "'HostKeyAlgorithms' not in sshd_profiles.ubuntu_vm" - quiet: true - -# Play vars outrank the role defaults, so the override reaches the merged settings. -- name: A consumer override wins and keeps the rest - hosts: all - gather_facts: false - vars: - sshd_profile: ubuntu_vm - sshd_settings: - MaxAuthTries: "5" - tasks: - - name: Load the role defaults - ansible.builtin.include_role: - name: mortis.kamino.sshd - tasks_from: validate.yml - public: true - - name: Assert the override - ansible.builtin.assert: - that: - - sshd_settings_effective.MaxAuthTries == "5" - - sshd_settings_effective.PermitRootLogin == "no" - - sshd_settings_effective.PubkeyAuthentication == "yes" - quiet: true - -- name: The Proxmox guard accepts every value that allows root key login - hosts: all - gather_facts: false - tasks: - - name: Validate the pve profile with each accepted value - ansible.builtin.include_role: - name: mortis.kamino.sshd - tasks_from: validate.yml - vars: - sshd_profile: pve - sshd_settings: - PermitRootLogin: "{{ item }}" - loop: [prohibit-password, without-password, "yes", Prohibit-Password] diff --git a/roles/sshd/molecule/default/verify.yml b/roles/sshd/molecule/default/verify.yml index 67001d3..e20e9be 100644 --- a/roles/sshd/molecule/default/verify.yml +++ b/roles/sshd/molecule/default/verify.yml @@ -1,6 +1,83 @@ --- +# The plays below only load the role's defaults and run validate.yml, which needs no container, +# so they run once, on the controller, in check mode so that a regressed guard cannot change it. +# They cover the profiles converge.yml cannot run and the values the guards accept. +- name: Every profile passes validation and keeps its root login policy + hosts: localhost + connection: local + check_mode: true + gather_facts: false + tasks: + - name: Validate each profile + ansible.builtin.include_role: + name: mortis.kamino.sshd + tasks_from: validate.yml + public: true + vars: + sshd_profile: "{{ item }}" + loop: [pve, ubuntu_lxc, ubuntu_vm] + - name: Assert the profile data + ansible.builtin.assert: + that: + - sshd_profiles.pve.PermitRootLogin == "prohibit-password" + - sshd_profiles.ubuntu_lxc.PermitRootLogin == "no" + - sshd_profiles.ubuntu_vm.PermitRootLogin == "no" + quiet: true + # Proxmox cluster SSH needs RSA with SHA-2, never SHA-1 ssh-rsa; the others stay ed25519 only. + - name: Assert the key algorithms per profile + ansible.builtin.assert: + that: + - sshd_profiles.pve.HostKeyAlgorithms == "ssh-ed25519,rsa-sha2-512,rsa-sha2-256" + - sshd_profiles.pve.PubkeyAcceptedAlgorithms == "ssh-ed25519,rsa-sha2-512,rsa-sha2-256" + - sshd_settings_defaults.HostKeyAlgorithms == "ssh-ed25519" + - sshd_settings_defaults.PubkeyAcceptedAlgorithms == "ssh-ed25519" + - "'HostKeyAlgorithms' not in sshd_profiles.ubuntu_lxc" + - "'HostKeyAlgorithms' not in sshd_profiles.ubuntu_vm" + quiet: true + +# Play vars outrank the role defaults, so the override reaches the merged settings. +- name: A consumer override wins and keeps the rest + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + sshd_profile: ubuntu_vm + sshd_settings: + MaxAuthTries: "5" + tasks: + - name: Load the role defaults + ansible.builtin.include_role: + name: mortis.kamino.sshd + tasks_from: validate.yml + public: true + - name: Assert the override + ansible.builtin.assert: + that: + - sshd_settings_effective.MaxAuthTries == "5" + - sshd_settings_effective.PermitRootLogin == "no" + - sshd_settings_effective.PubkeyAuthentication == "yes" + quiet: true + +- name: The Proxmox guard accepts every value that allows root key login + hosts: localhost + connection: local + check_mode: true + gather_facts: false + tasks: + - name: Validate the pve profile with each accepted value + ansible.builtin.include_role: + name: mortis.kamino.sshd + tasks_from: validate.yml + vars: + sshd_profile: pve + sshd_settings: + PermitRootLogin: "{{ item }}" + loop: [prohibit-password, without-password, "yes", Prohibit-Password] + # The refusal cases: here, not in converge.yml, because idempotence reruns converge. Each -# fails in validate.yml; they run first, so the assertions below also show the host unchanged. +# fails in validate.yml; they run before the host checks, so the assertions below also show the +# host unchanged. # A play without sshd_profile: the role must refuse it. The block proves the refusal # (not its wording). diff --git a/roles/system_baseline/README.md b/roles/system_baseline/README.md index a5f2dd9..f3cef36 100644 --- a/roles/system_baseline/README.md +++ b/roles/system_baseline/README.md @@ -128,12 +128,12 @@ The two task files besides `main` are a supported interface, for a caller that d The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It installs packages from the Ubuntu mirrors, so the runner needs outbound network. It runs `check` before the converge (a fresh host) and after it (a configured host). -- Converge first checks that the role refuses a bad input with its own message before any change: an unknown `system_baseline_system_type`, an unknown `system_baseline_apt_upgrade` (main flow and `update` entry point), a negative reboot throttle or a negative or boolean post-reboot delay (main flow and `reboot` entry point), a line break in `system_baseline_apt_daily_timeout`, a bad `system_baseline_journal_max_use` (a bare number, a space, a line break, a number), and a bad package blacklist (a quote, a null item, a bare string). -- Converge then applies the `ubuntu_lxc` profile, with tmux autostart and `system_baseline_apt_daily_timeout: 1h`, and only resolves the `pve` and `ubuntu_vm` profiles and a consumer override, asserting the resolved inputs. +- Verify first checks, on the controller, that the role refuses a bad input with its own message before any change: an unknown `system_baseline_system_type`, an unknown `system_baseline_apt_upgrade` (main flow and `update` entry point), a negative reboot throttle or a negative or boolean post-reboot delay (main flow and `reboot` entry point), a line break in `system_baseline_apt_daily_timeout`, a bad `system_baseline_journal_max_use` (a bare number, a space, a line break, a number), and a bad package blacklist (a quote, a null item, a bare string). +- Converge applies the `ubuntu_lxc` profile, with tmux autostart and `system_baseline_apt_daily_timeout: 1h`. Verify only resolves the `pve` and `ubuntu_vm` profiles and a consumer override, on the controller, asserting the resolved inputs. - Verify checks the installed and removed packages, the journal size cap, the apt settings in `20auto-upgrades` and `50unattended-upgrades`, the locale, and that systemd loaded the `apt-daily.service` timeout. - Verify checks the tmux snippet: root-owned, valid shell, the session and quoted window command, no effect without a terminal, a session name or window command ending in a newline refused, and the file removed when autostart is off. -- Converge refuses bad items in `system_baseline_extra_packages` and `system_baseline_extra_remove_packages` (whitespace, a null, an empty string, a trailing minus, upper case, a bare string) and a package that one input installs and another purges, then applies the `ubuntu_lxc` profile with `jq` as an extra package and `ed` as an extra removal; verify checks that `jq` is installed, `ed` is gone and the profile's packages are still there, and that an extra `tmux` installs the snippet where a flat list without it installs none. -- Converge also checks that a bad `system_baseline_apt_tmpdir` (under `/tmp` or `/var/tmp`, relative, with `..`, empty) is refused in the main flow and in the `update` entry point. Verify reads `tasks/update.yml` and checks that the scratch directory is created with mode `0700`, that both apt tasks set `TMPDIR` to it, and that the connection is reset afterwards: the scenario skips the `update` tag, so the upgrade itself never runs there. +- Verify refuses bad items in `system_baseline_extra_packages` and `system_baseline_extra_remove_packages` (whitespace, a null, an empty string, a trailing minus, upper case, a bare string) and a package that one input installs and another purges; converge applies the `ubuntu_lxc` profile with `jq` as an extra package and `ed` as an extra removal; verify checks that `jq` is installed, `ed` is gone and the profile's packages are still there, and that an extra `tmux` installs the snippet where a flat list without it installs none. +- Verify also checks that a bad `system_baseline_apt_tmpdir` (under `/tmp` or `/var/tmp`, relative, with `..`, empty) is refused in the main flow and in the `update` entry point. It reads `tasks/update.yml` and checks that the scratch directory is created with mode `0700`, that both apt tasks set `TMPDIR` to it, and that the connection is reset afterwards: the scenario skips the `update` tag, so the upgrade itself never runs there. - Verify ends by running the role once with `system_baseline_journal_max_use: ""` after removing the drop-in, and asserts that no drop-in is written and that `systemd-journald` was not restarted. - Verify runs the `reboot` entry point with `system_baseline_reboot_if_required: false`, without and with `/var/run/reboot-required`, and checks the fact and that the host did not reboot. - The `pve` origins pin is applied to the Ubuntu container and read back with `apt-config dump`, then removed again. The package blacklist is written, read back as a list, left alone by a second run and removed by an empty list. diff --git a/roles/system_baseline/molecule/default/converge.yml b/roles/system_baseline/molecule/default/converge.yml index 11bb7b4..be9aa95 100644 --- a/roles/system_baseline/molecule/default/converge.yml +++ b/roles/system_baseline/molecule/default/converge.yml @@ -1,181 +1,4 @@ --- -- name: Refuse an unknown system_baseline_system_type - hosts: all - gather_facts: false - tasks: - - name: Run the role with a bad system_baseline_system_type - block: - - name: Include system_baseline - ansible.builtin.include_role: - name: mortis.kamino.system_baseline - vars: - system_baseline_system_type: not_a_type - - name: Unreachable when validation works - ansible.builtin.fail: - msg: system_baseline ran with an invalid system_baseline_system_type - rescue: - - name: The validation stopped the play - ansible.builtin.assert: - that: "'is invalid' in ansible_failed_result.msg | default('')" - quiet: true - -# The apt upgrade mode is safe or dist: anything else (here `full`, an apt module value the role does not offer) stops the run before -# any change, with a message that names the input. -- name: An unknown apt upgrade mode is refused - hosts: all - gather_facts: false - tasks: - - name: Run the role with a bad system_baseline_apt_upgrade - block: - - name: Include system_baseline - ansible.builtin.include_role: - name: mortis.kamino.system_baseline - vars: - system_baseline_system_type: ubuntu_lxc - system_baseline_apt_upgrade: full - - name: Unreachable when validation works - ansible.builtin.fail: - msg: system_baseline ran with an invalid system_baseline_apt_upgrade - rescue: - - name: The validation stopped the play - ansible.builtin.assert: - that: "'system_baseline_apt_upgrade is' in ansible_failed_result.msg | default('')" - quiet: true - -# The update entry point does not run validate.yml, so it checks the mode itself and stops before it calls the package module. -- name: The update entry point refuses an unknown apt upgrade mode - hosts: all - gather_facts: false - tasks: - - name: Run the update entry point with a bad system_baseline_apt_upgrade - block: - - name: Include system_baseline, tasks_from update - ansible.builtin.include_role: - name: mortis.kamino.system_baseline - tasks_from: update - vars: - system_baseline_apt_upgrade: full - - name: Unreachable when validation works - ansible.builtin.fail: - msg: the update entry point ran with an invalid system_baseline_apt_upgrade - rescue: - - name: The validation stopped the play - ansible.builtin.assert: - that: "'system_baseline_apt_upgrade is' in ansible_failed_result.msg | default('')" - quiet: true - -# The apt scratch directory becomes TMPDIR of the upgrade: a path under /tmp or /var/tmp (which a restart of ssh.service can delete), a relative -# path and a `..` each stop the run before any change, in the main flow and in the update entry point (which does not run validate.yml). -- name: A bad apt scratch directory is refused - hosts: all - gather_facts: false - tasks: - - name: Refuse each bad value - ansible.builtin.include_tasks: tasks/refuse_apt_tmpdir_entries.yml - vars: - system_baseline_refused_tmpdir: "{{ item }}" - loop: - - /tmp - - /var/tmp/apt - - /var/cache/../../tmp/x - - var/cache/apt-tmp - - "" - -# The reboot inputs are whole numbers, 0 or more: a negative number and a boolean each stop the run before a host reboots, -# in the main flow and in the reboot entry point (which does not run validate.yml). -- name: A bad reboot throttle or post-reboot delay is refused - hosts: all - gather_facts: false - tasks: - - name: Refuse each bad value - ansible.builtin.include_tasks: tasks/refuse_reboot.yml - vars: - system_baseline_refused_reboot: "{{ item }}" - loop: - - {system_baseline_reboot_throttle: -1} - - {system_baseline_reboot_post_delay: -5} - - {system_baseline_reboot_post_delay: true} - -# The value is written into a unit file, so a line break (a second directive) is refused. -- name: A bad apt-daily timeout is refused - hosts: all - gather_facts: false - tasks: - - name: Run the role with a line break in system_baseline_apt_daily_timeout - block: - - name: Include system_baseline - ansible.builtin.include_role: - name: mortis.kamino.system_baseline - vars: - system_baseline_system_type: ubuntu_lxc - system_baseline_apt_daily_timeout: "1h\nExecStart=/bin/true" - - name: Unreachable when validation works - ansible.builtin.fail: - msg: system_baseline ran with an invalid system_baseline_apt_daily_timeout - rescue: - - name: The validation stopped the play - ansible.builtin.assert: - that: "'system_baseline_apt_daily_timeout must be' in ansible_failed_result.msg | default('')" - quiet: true - -# The journal cap is a number and a unit letter, or empty: a bare number (journald would read bytes), a zero, a space, a line break, a -# lower-case unit and a number that is not a string each stop the run before any change. -- name: A bad journal size cap is refused - hosts: all - gather_facts: false - tasks: - - name: Refuse each bad value - ansible.builtin.include_tasks: tasks/refuse_journal_max_use.yml - vars: - system_baseline_refused_journal: "{{ item }}" - loop: - - "300" - - "0" - - 10 M - - "10m" - - "200M\nStorage=none" - - 300 - -# A quote, a null item and a bare string (not a list) each stop the run: the bare string would otherwise be split into one-character -# patterns, and the null item would become an empty pattern that matches every package. -- name: A bad package blacklist is refused - hosts: all - gather_facts: false - tasks: - - name: Refuse each bad value - ansible.builtin.include_tasks: tasks/refuse_blacklist.yml - vars: - system_baseline_refused_blacklist: "{{ item }}" - loop: - - ['bad"name'] - - [null] - - linux-image-.* - -# Items that are not Debian package names (whitespace, null, empty, a trailing minus that apt reads as a removal, an option, upper case, a -# version pin), and a package that one input installs and another purges, each stop the run before any change. -# The overlaps are: an extra install against the profile's remove list (telnet), an extra install against an extra remove, and an extra -# remove against the profile's install list (tmux). -- name: A bad extra package list is refused - hosts: all - gather_facts: false - tasks: - - name: Refuse each bad value - ansible.builtin.include_tasks: tasks/refuse_extra_packages.yml - vars: - system_baseline_refused_case: "{{ item }}" - loop: - - { install: ['bad name'], expect: 'must be lists of package names' } - - { install: [null], expect: 'must be lists of package names' } - - { install: jq, expect: 'must be lists of package names' } - - { remove: [''], expect: 'must be lists of package names' } - - { install: ['jq-'], expect: 'must be lists of package names' } - - { install: ['-y'], expect: 'must be lists of package names' } - - { remove: ['Telnet'], expect: 'must be lists of package names' } - - { remove: ['ed=1.0'], expect: 'must be lists of package names' } - - { install: [telnet], expect: 'is both installed and purged' } - - { install: [jq], remove: [jq], expect: 'is both installed and purged' } - - { remove: [tmux], expect: 'is both installed and purged' } - - name: Converge the LXC profile hosts: all gather_facts: true @@ -190,130 +13,3 @@ - { name: logs, command: "echo 'a b'; true" } roles: - role: mortis.kamino.system_baseline - -# The remaining plays only load the role's defaults (validate.yml changes nothing) and assert -# the resolved inputs. They cover the profiles the converge above cannot run. -- name: The pve profile resolves - hosts: all - gather_facts: false - vars: - system_baseline_system_type: pve - tasks: - - name: Load the role defaults - ansible.builtin.include_role: - name: mortis.kamino.system_baseline - tasks_from: validate.yml - public: true - - name: Assert the pve profile - ansible.builtin.assert: - that: - - "'ufw' not in system_baseline_essential_packages" - - "'tmux' not in system_baseline_essential_packages" - - "'unattended-upgrades' in system_baseline_essential_packages" - - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot'] == false - - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot-Time'] == '03:00' - - system_baseline_secure_tmp_mounts == ['/dev/shm', '/tmp', '/var/tmp'] - - system_baseline_update - - system_baseline_apt_upgrade == 'dist' - - system_baseline_unattended_upgrade_origins | length == 3 - - system_baseline_unattended_upgrade_origins | select('search', 'Proxmox') | list == [] - - __system_baseline_install_packages == system_baseline_essential_packages - - __system_baseline_purge_packages == system_baseline_remove_packages - quiet: true - -- name: The ubuntu_vm profile resolves - hosts: all - gather_facts: false - vars: - system_baseline_system_type: ubuntu_vm - tasks: - - name: Load the role defaults - ansible.builtin.include_role: - name: mortis.kamino.system_baseline - tasks_from: validate.yml - public: true - - name: Assert the ubuntu_vm profile - ansible.builtin.assert: - that: - - "'qemu-guest-agent' in system_baseline_essential_packages" - - "'tmux' in system_baseline_essential_packages" - - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot'] == false - - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot-Time'] == '04:00' - - system_baseline_sysctl_hardening | selectattr('name', 'equalto', 'net.ipv4.conf.all.log_martians') | list | length == 1 - - system_baseline_apt_upgrade == 'safe' - - system_baseline_unattended_upgrade_origins == [] - - __system_baseline_install_packages == system_baseline_essential_packages - - __system_baseline_purge_packages == system_baseline_remove_packages - quiet: true - -# Consumer play vars override the profile. -- name: Consumer overrides win over the profile - hosts: all - gather_facts: false - vars: - system_baseline_system_type: ubuntu_vm - system_baseline_remove_packages: [telnet] - system_baseline_apt_upgrade: dist - system_baseline_profile_overrides: - ubuntu_vm: - unattended_upgrade_settings: - "Unattended-Upgrade::Automatic-Reboot": true - tasks: - - name: Load the role defaults - ansible.builtin.include_role: - name: mortis.kamino.system_baseline - tasks_from: validate.yml - public: true - - name: Assert the overrides - ansible.builtin.assert: - that: - - system_baseline_remove_packages == ['telnet'] - - system_baseline_apt_upgrade == 'dist' - - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot'] == true - - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot-Time'] == '04:00' - - "'qemu-guest-agent' in system_baseline_essential_packages" - quiet: true - -# The extras append to the resolved lists, whether the profile's list or a flat input supplies them, and a name that is in both is listed once. -- name: Extra packages append to the resolved lists - hosts: all - gather_facts: false - vars: - system_baseline_system_type: ubuntu_lxc - system_baseline_essential_packages: [vim, curl] - system_baseline_extra_packages: [jq, vim, unattended-upgrades] - system_baseline_extra_remove_packages: [ed, telnet] - tasks: - - name: Load the role defaults - ansible.builtin.include_role: - name: mortis.kamino.system_baseline - tasks_from: validate.yml - public: true - - name: Assert the lists the role acts on - ansible.builtin.assert: - that: - - __system_baseline_install_packages == ['vim', 'curl', 'jq', 'unattended-upgrades'] - - "'ed' in __system_baseline_purge_packages" - - __system_baseline_purge_packages | select('equalto', 'telnet') | list | length == 1 - - system_baseline_remove_packages | difference(__system_baseline_purge_packages) | length == 0 - quiet: true - -# A flat input that is a plain string (the package module accepts one) still counts as a one-item list, with and without extras. -- name: A flat string package list still resolves - hosts: all - gather_facts: false - vars: - system_baseline_system_type: ubuntu_lxc - system_baseline_essential_packages: vim - system_baseline_extra_packages: [jq] - tasks: - - name: Load the role defaults - ansible.builtin.include_role: - name: mortis.kamino.system_baseline - tasks_from: validate.yml - public: true - - name: Assert the lists the role acts on - ansible.builtin.assert: - that: - - __system_baseline_install_packages == ['vim', 'jq'] - quiet: true diff --git a/roles/system_baseline/molecule/default/verify.yml b/roles/system_baseline/molecule/default/verify.yml index 7afc4b2..d4ccacb 100644 --- a/roles/system_baseline/molecule/default/verify.yml +++ b/roles/system_baseline/molecule/default/verify.yml @@ -1,4 +1,338 @@ --- +# Validation-only plays: they need no container, so they run once, on the controller, in +# check mode so that a refusal that regressed cannot change the controller. +- name: Refuse an unknown system_baseline_system_type + hosts: localhost + connection: local + check_mode: true + gather_facts: false + tasks: + - name: Run the role with a bad system_baseline_system_type + block: + - name: Include system_baseline + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + vars: + system_baseline_system_type: not_a_type + - name: Unreachable when validation works + ansible.builtin.fail: + msg: system_baseline ran with an invalid system_baseline_system_type + rescue: + - name: The validation stopped the play + ansible.builtin.assert: + that: "'is invalid' in ansible_failed_result.msg | default('')" + quiet: true + +# The apt upgrade mode is safe or dist: anything else (here `full`, an apt module value the role does not offer) stops the run before +# any change, with a message that names the input. +- name: An unknown apt upgrade mode is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + tasks: + - name: Run the role with a bad system_baseline_apt_upgrade + block: + - name: Include system_baseline + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + vars: + system_baseline_system_type: ubuntu_lxc + system_baseline_apt_upgrade: full + - name: Unreachable when validation works + ansible.builtin.fail: + msg: system_baseline ran with an invalid system_baseline_apt_upgrade + rescue: + - name: The validation stopped the play + ansible.builtin.assert: + that: "'system_baseline_apt_upgrade is' in ansible_failed_result.msg | default('')" + quiet: true + +# The update entry point does not run validate.yml, so it checks the mode itself and stops before it calls the package module. +- name: The update entry point refuses an unknown apt upgrade mode + hosts: localhost + connection: local + check_mode: true + gather_facts: false + tasks: + - name: Run the update entry point with a bad system_baseline_apt_upgrade + block: + - name: Include system_baseline, tasks_from update + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + tasks_from: update + vars: + system_baseline_apt_upgrade: full + - name: Unreachable when validation works + ansible.builtin.fail: + msg: the update entry point ran with an invalid system_baseline_apt_upgrade + rescue: + - name: The validation stopped the play + ansible.builtin.assert: + that: "'system_baseline_apt_upgrade is' in ansible_failed_result.msg | default('')" + quiet: true + +# The apt scratch directory becomes TMPDIR of the upgrade: a path under /tmp or /var/tmp (which a restart of ssh.service can delete), a relative +# path and a `..` each stop the run before any change, in the main flow and in the update entry point (which does not run validate.yml). +- name: A bad apt scratch directory is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + tasks: + - name: Refuse each bad value + ansible.builtin.include_tasks: tasks/refuse_apt_tmpdir_entries.yml + vars: + system_baseline_refused_tmpdir: "{{ item }}" + loop: + - /tmp + - /var/tmp/apt + - /var/cache/../../tmp/x + - var/cache/apt-tmp + - "" + +# The reboot inputs are whole numbers, 0 or more: a negative number and a boolean each stop the run before a host reboots, +# in the main flow and in the reboot entry point (which does not run validate.yml). +- name: A bad reboot throttle or post-reboot delay is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + tasks: + - name: Refuse each bad value + ansible.builtin.include_tasks: tasks/refuse_reboot.yml + vars: + system_baseline_refused_reboot: "{{ item }}" + loop: + - {system_baseline_reboot_throttle: -1} + - {system_baseline_reboot_post_delay: -5} + - {system_baseline_reboot_post_delay: true} + +# The value is written into a unit file, so a line break (a second directive) is refused. +- name: A bad apt-daily timeout is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + tasks: + - name: Run the role with a line break in system_baseline_apt_daily_timeout + block: + - name: Include system_baseline + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + vars: + system_baseline_system_type: ubuntu_lxc + system_baseline_apt_daily_timeout: "1h\nExecStart=/bin/true" + - name: Unreachable when validation works + ansible.builtin.fail: + msg: system_baseline ran with an invalid system_baseline_apt_daily_timeout + rescue: + - name: The validation stopped the play + ansible.builtin.assert: + that: "'system_baseline_apt_daily_timeout must be' in ansible_failed_result.msg | default('')" + quiet: true + +# The journal cap is a number and a unit letter, or empty: a bare number (journald would read bytes), a zero, a space, a line break, a +# lower-case unit and a number that is not a string each stop the run before any change. +- name: A bad journal size cap is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + tasks: + - name: Refuse each bad value + ansible.builtin.include_tasks: tasks/refuse_journal_max_use.yml + vars: + system_baseline_refused_journal: "{{ item }}" + loop: + - "300" + - "0" + - 10 M + - "10m" + - "200M\nStorage=none" + - 300 + +# A quote, a null item and a bare string (not a list) each stop the run: the bare string would otherwise be split into one-character +# patterns, and the null item would become an empty pattern that matches every package. +- name: A bad package blacklist is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + tasks: + - name: Refuse each bad value + ansible.builtin.include_tasks: tasks/refuse_blacklist.yml + vars: + system_baseline_refused_blacklist: "{{ item }}" + loop: + - ['bad"name'] + - [null] + - linux-image-.* + +# Items that are not Debian package names (whitespace, null, empty, a trailing minus that apt reads as a removal, an option, upper case, a +# version pin), and a package that one input installs and another purges, each stop the run before any change. +# The overlaps are: an extra install against the profile's remove list (telnet), an extra install against an extra remove, and an extra +# remove against the profile's install list (tmux). +- name: A bad extra package list is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + tasks: + - name: Refuse each bad value + ansible.builtin.include_tasks: tasks/refuse_extra_packages.yml + vars: + system_baseline_refused_case: "{{ item }}" + loop: + - { install: ['bad name'], expect: 'must be lists of package names' } + - { install: [null], expect: 'must be lists of package names' } + - { install: jq, expect: 'must be lists of package names' } + - { remove: [''], expect: 'must be lists of package names' } + - { install: ['jq-'], expect: 'must be lists of package names' } + - { install: ['-y'], expect: 'must be lists of package names' } + - { remove: ['Telnet'], expect: 'must be lists of package names' } + - { remove: ['ed=1.0'], expect: 'must be lists of package names' } + - { install: [telnet], expect: 'is both installed and purged' } + - { install: [jq], remove: [jq], expect: 'is both installed and purged' } + - { remove: [tmux], expect: 'is both installed and purged' } + +# The remaining plays only load the role's defaults (validate.yml changes nothing) and assert +# the resolved inputs. They cover the profiles converge.yml cannot run. +- name: The pve profile resolves + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + system_baseline_system_type: pve + tasks: + - name: Load the role defaults + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + tasks_from: validate.yml + public: true + - name: Assert the pve profile + ansible.builtin.assert: + that: + - "'ufw' not in system_baseline_essential_packages" + - "'tmux' not in system_baseline_essential_packages" + - "'unattended-upgrades' in system_baseline_essential_packages" + - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot'] == false + - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot-Time'] == '03:00' + - system_baseline_secure_tmp_mounts == ['/dev/shm', '/tmp', '/var/tmp'] + - system_baseline_update + - system_baseline_apt_upgrade == 'dist' + - system_baseline_unattended_upgrade_origins | length == 3 + - system_baseline_unattended_upgrade_origins | select('search', 'Proxmox') | list == [] + - __system_baseline_install_packages == system_baseline_essential_packages + - __system_baseline_purge_packages == system_baseline_remove_packages + quiet: true + +- name: The ubuntu_vm profile resolves + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + system_baseline_system_type: ubuntu_vm + tasks: + - name: Load the role defaults + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + tasks_from: validate.yml + public: true + - name: Assert the ubuntu_vm profile + ansible.builtin.assert: + that: + - "'qemu-guest-agent' in system_baseline_essential_packages" + - "'tmux' in system_baseline_essential_packages" + - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot'] == false + - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot-Time'] == '04:00' + - system_baseline_sysctl_hardening | selectattr('name', 'equalto', 'net.ipv4.conf.all.log_martians') | list | length == 1 + - system_baseline_apt_upgrade == 'safe' + - system_baseline_unattended_upgrade_origins == [] + - __system_baseline_install_packages == system_baseline_essential_packages + - __system_baseline_purge_packages == system_baseline_remove_packages + quiet: true + +# Consumer play vars override the profile. +- name: Consumer overrides win over the profile + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + system_baseline_system_type: ubuntu_vm + system_baseline_remove_packages: [telnet] + system_baseline_apt_upgrade: dist + system_baseline_profile_overrides: + ubuntu_vm: + unattended_upgrade_settings: + "Unattended-Upgrade::Automatic-Reboot": true + tasks: + - name: Load the role defaults + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + tasks_from: validate.yml + public: true + - name: Assert the overrides + ansible.builtin.assert: + that: + - system_baseline_remove_packages == ['telnet'] + - system_baseline_apt_upgrade == 'dist' + - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot'] == true + - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot-Time'] == '04:00' + - "'qemu-guest-agent' in system_baseline_essential_packages" + quiet: true + +# The extras append to the resolved lists, whether the profile's list or a flat input supplies them, and a name that is in both is listed once. +- name: Extra packages append to the resolved lists + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + system_baseline_system_type: ubuntu_lxc + system_baseline_essential_packages: [vim, curl] + system_baseline_extra_packages: [jq, vim, unattended-upgrades] + system_baseline_extra_remove_packages: [ed, telnet] + tasks: + - name: Load the role defaults + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + tasks_from: validate.yml + public: true + - name: Assert the lists the role acts on + ansible.builtin.assert: + that: + - __system_baseline_install_packages == ['vim', 'curl', 'jq', 'unattended-upgrades'] + - "'ed' in __system_baseline_purge_packages" + - __system_baseline_purge_packages | select('equalto', 'telnet') | list | length == 1 + - system_baseline_remove_packages | difference(__system_baseline_purge_packages) | length == 0 + quiet: true + +# A flat input that is a plain string (the package module accepts one) still counts as a one-item list, with and without extras. +- name: A flat string package list still resolves + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + system_baseline_system_type: ubuntu_lxc + system_baseline_essential_packages: vim + system_baseline_extra_packages: [jq] + tasks: + - name: Load the role defaults + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + tasks_from: validate.yml + public: true + - name: Assert the lists the role acts on + ansible.builtin.assert: + that: + - __system_baseline_install_packages == ['vim', 'jq'] + quiet: true + - name: Verify hosts: all gather_facts: false diff --git a/roles/systemd_hardening/molecule/default/converge.yml b/roles/systemd_hardening/molecule/default/converge.yml index c389edc..68c6ab7 100644 --- a/roles/systemd_hardening/molecule/default/converge.yml +++ b/roles/systemd_hardening/molecule/default/converge.yml @@ -12,91 +12,3 @@ LimitNOFILE: 1024 roles: - role: mortis.kamino.systemd_hardening - -# The plays below are refusals. The missing and unknown system types stop before the role -# changes anything. The malformed drop-in plays fail at dropins.yml, after the disable, mask -# and manager drop-in tasks ran, but those are no-ops after the converge above, so the -# idempotence run stays clean. A sentinel in the failure message tells "the role refused" -# from "the fail task after the role ran", which a bare rescue cannot. -- name: A run without systemd_hardening_system_type is refused - hosts: all - gather_facts: false - tasks: - - name: Apply the role without a system type - block: - - name: Include the role - ansible.builtin.include_role: - name: mortis.kamino.systemd_hardening - - name: Fail when the missing type was accepted - ansible.builtin.fail: - msg: MOLECULE-ACCEPTED - rescue: - - name: The role refused the run - ansible.builtin.assert: - that: "'missing required arguments' in ansible_failed_result.msg | default('')" - quiet: true - -- name: An unknown system type is refused - hosts: all - gather_facts: false - vars: - systemd_hardening_system_type: not_a_type - tasks: - - name: Apply the role with an unknown type - block: - - name: Include the role - ansible.builtin.include_role: - name: mortis.kamino.systemd_hardening - - name: Fail when the unknown type was accepted - ansible.builtin.fail: - msg: MOLECULE-ACCEPTED - rescue: - - name: The profile check stopped the play - ansible.builtin.assert: - that: "'is invalid' in ansible_failed_result.msg | default('')" - quiet: true - -- name: A drop-in entry that is not a mapping is refused - hosts: all - gather_facts: false - vars: - systemd_hardening_system_type: ubuntu_lxc - systemd_hardening_services: - testsvc: not-a-mapping - tasks: - - name: Apply the role with a malformed drop-in - block: - - name: Include the role - ansible.builtin.include_role: - name: mortis.kamino.systemd_hardening - - name: Fail when the malformed drop-in was accepted - ansible.builtin.fail: - msg: MOLECULE-ACCEPTED - rescue: - - name: The drop-in validation stopped the play - ansible.builtin.assert: - that: "'must be a mapping' in ansible_failed_result.msg | default('')" - quiet: true - -- name: A drop-in list that is not a mapping is refused - hosts: all - gather_facts: false - vars: - systemd_hardening_system_type: ubuntu_lxc - systemd_hardening_services: [testsvc] - tasks: - - name: Apply the role with a list instead of a mapping - block: - - name: Include the role - ansible.builtin.include_role: - name: mortis.kamino.systemd_hardening - - name: Fail when the list was accepted - ansible.builtin.fail: - msg: MOLECULE-ACCEPTED - rescue: - - name: The role refused the run - ansible.builtin.assert: - that: - - "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')" - - "'systemd_hardening_services' in ansible_failed_result.msg | default('')" - quiet: true diff --git a/roles/systemd_hardening/molecule/default/verify.yml b/roles/systemd_hardening/molecule/default/verify.yml index 4348901..1d686c7 100644 --- a/roles/systemd_hardening/molecule/default/verify.yml +++ b/roles/systemd_hardening/molecule/default/verify.yml @@ -1,4 +1,99 @@ --- +# The refusals that stop before the role changes anything need no container, so they run once, +# on the controller, in check mode so that a refusal that regressed cannot change it. A sentinel +# in the failure message tells "the role refused" from "the fail task after the role ran", +# which a bare rescue cannot. +- name: A run without systemd_hardening_system_type is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + tasks: + - name: Apply the role without a system type + block: + - name: Include the role + ansible.builtin.include_role: + name: mortis.kamino.systemd_hardening + - name: Fail when the missing type was accepted + ansible.builtin.fail: + msg: MOLECULE-ACCEPTED + rescue: + - name: The role refused the run + ansible.builtin.assert: + that: "'missing required arguments' in ansible_failed_result.msg | default('')" + quiet: true + +- name: An unknown system type is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + systemd_hardening_system_type: not_a_type + tasks: + - name: Apply the role with an unknown type + block: + - name: Include the role + ansible.builtin.include_role: + name: mortis.kamino.systemd_hardening + - name: Fail when the unknown type was accepted + ansible.builtin.fail: + msg: MOLECULE-ACCEPTED + rescue: + - name: The profile check stopped the play + ansible.builtin.assert: + that: "'is invalid' in ansible_failed_result.msg | default('')" + quiet: true + +- name: A drop-in list that is not a mapping is refused + hosts: localhost + connection: local + check_mode: true + gather_facts: false + vars: + systemd_hardening_system_type: ubuntu_lxc + systemd_hardening_services: [testsvc] + tasks: + - name: Apply the role with a list instead of a mapping + block: + - name: Include the role + ansible.builtin.include_role: + name: mortis.kamino.systemd_hardening + - name: Fail when the list was accepted + ansible.builtin.fail: + msg: MOLECULE-ACCEPTED + rescue: + - name: The role refused the run + ansible.builtin.assert: + that: + - "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')" + - "'systemd_hardening_services' in ansible_failed_result.msg | default('')" + quiet: true + +# This refusal comes at dropins.yml, after the disable, mask and manager drop-in tasks, so it +# runs on the container, where converge.yml already made those tasks no-ops. +- name: A drop-in entry that is not a mapping is refused + hosts: all + gather_facts: false + vars: + systemd_hardening_system_type: ubuntu_lxc + systemd_hardening_services: + testsvc: not-a-mapping + tasks: + - name: Apply the role with a malformed drop-in + block: + - name: Include the role + ansible.builtin.include_role: + name: mortis.kamino.systemd_hardening + - name: Fail when the malformed drop-in was accepted + ansible.builtin.fail: + msg: MOLECULE-ACCEPTED + rescue: + - name: The drop-in validation stopped the play + ansible.builtin.assert: + that: "'must be a mapping' in ansible_failed_result.msg | default('')" + quiet: true + # What the role is for: the installed risky units are disabled or masked, the manager # drop-in is in place, and the opt-in drop-in renders and is loaded by systemd. The # directives restate the converge vars, not the role's whole policy.