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
189 lines
7.0 KiB
YAML
189 lines
7.0 KiB
YAML
---
|
|
# 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.
|
|
- name: Verify
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Ask systemd about the stub units
|
|
ansible.builtin.systemd:
|
|
name: "{{ item }}"
|
|
loop: [rpcbind.socket, rpcbind.service, cups.service, bluetooth.socket, telnet.socket, telnet.service]
|
|
register: systemd_hardening_verify_units
|
|
check_mode: true
|
|
|
|
- name: The disabled units, sockets and services alike, are disabled and stopped
|
|
ansible.builtin.assert:
|
|
that:
|
|
- item.status.UnitFileState == "disabled"
|
|
- item.status.ActiveState == "inactive"
|
|
quiet: true
|
|
loop: "{{ systemd_hardening_verify_units.results | rejectattr('item', 'match', '^telnet[.]') | list }}"
|
|
loop_control:
|
|
label: "{{ item.item }}"
|
|
|
|
- name: The masked units, socket and service, are masked and stopped
|
|
ansible.builtin.assert:
|
|
that:
|
|
- item.status.UnitFileState == "masked"
|
|
- item.status.ActiveState == "inactive"
|
|
quiet: true
|
|
loop: "{{ systemd_hardening_verify_units.results | selectattr('item', 'match', '^telnet[.]') | list }}"
|
|
loop_control:
|
|
label: "{{ item.item }}"
|
|
|
|
- name: Ask systemd about the listed units that are not installed
|
|
ansible.builtin.command: systemctl show --property=LoadState {{ item }} # noqa: command-instead-of-module
|
|
loop: [cups.socket, bluetooth.service, rsh.service, rsh.socket]
|
|
register: systemd_hardening_verify_absent
|
|
changed_when: false
|
|
|
|
- name: The missing units were skipped, not masked or created
|
|
ansible.builtin.assert:
|
|
that:
|
|
- item.stdout == "LoadState=not-found"
|
|
quiet: true
|
|
loop: "{{ systemd_hardening_verify_absent.results }}"
|
|
loop_control:
|
|
label: "{{ item.item }}"
|
|
|
|
- name: Read the manager drop-in
|
|
ansible.builtin.slurp:
|
|
src: /etc/systemd/system.conf.d/hardening.conf
|
|
register: systemd_hardening_verify_manager
|
|
|
|
- name: The manager drop-in holds the limits
|
|
ansible.builtin.assert:
|
|
that:
|
|
- (systemd_hardening_verify_manager.content | b64decode).split('\n') is contains('[Manager]')
|
|
- (systemd_hardening_verify_manager.content | b64decode).split('\n') is contains('DefaultLimitNOFILE=65535')
|
|
- (systemd_hardening_verify_manager.content | b64decode).split('\n') is contains('DefaultLimitCORE=0')
|
|
quiet: true
|
|
|
|
- name: Read the service drop-in
|
|
ansible.builtin.slurp:
|
|
src: /etc/systemd/system/testsvc.service.d/hardening.conf
|
|
register: systemd_hardening_verify_dropin
|
|
|
|
- name: The drop-in renders booleans, lists and plain values
|
|
ansible.builtin.assert:
|
|
that:
|
|
- (systemd_hardening_verify_dropin.content | b64decode).split('\n') | select | list == systemd_hardening_verify_expected
|
|
quiet: true
|
|
vars:
|
|
systemd_hardening_verify_expected:
|
|
- "[Service]"
|
|
- ProtectSystem=strict
|
|
- NoNewPrivileges=yes
|
|
- ReadWritePaths=/var/tmp /run
|
|
- LimitNOFILE=1024
|
|
|
|
- name: Ask systemd about the drop-in service
|
|
ansible.builtin.systemd:
|
|
name: testsvc.service
|
|
register: systemd_hardening_verify_testsvc
|
|
check_mode: true
|
|
|
|
- name: The daemon reload made systemd load the drop-in
|
|
ansible.builtin.assert:
|
|
that:
|
|
- systemd_hardening_verify_testsvc.status.NoNewPrivileges == "yes"
|
|
- systemd_hardening_verify_testsvc.status.ProtectSystem == "strict"
|
|
quiet: true
|