The role listed every service unit file to answer one question: is each unit in its disable and mask lists installed? The listing keeps PID 1 busy for as long as the host has unit files. Ask about the listed units only, with one `systemctl show --property=LoadState`; a unit with no unit file reports `not-found` and is skipped as before. An instance name such as [email protected] is now matched when its template exists. A template name such as [email protected] makes the lookup fail, where the listing matched it. Closes #283 Assisted-by: Claude Code
77 lines
2.9 KiB
YAML
77 lines
2.9 KiB
YAML
---
|
|
# 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 }}.service"
|
|
loop: [rpcbind, telnet]
|
|
register: systemd_hardening_verify_units
|
|
check_mode: true
|
|
|
|
- name: The disabled unit is disabled and the masked unit is masked
|
|
ansible.builtin.assert:
|
|
that:
|
|
- systemd_hardening_verify_units.results[0].status.UnitFileState == "disabled"
|
|
- systemd_hardening_verify_units.results[1].status.UnitFileState == "masked"
|
|
quiet: true
|
|
|
|
- name: Ask systemd about a listed unit that is not installed
|
|
ansible.builtin.command: systemctl show --property=LoadState rsh.service # noqa: command-instead-of-module
|
|
register: systemd_hardening_verify_missing
|
|
changed_when: false
|
|
|
|
- name: The missing unit was skipped, not masked
|
|
ansible.builtin.assert:
|
|
that:
|
|
- systemd_hardening_verify_missing.stdout == "LoadState=not-found"
|
|
quiet: true
|
|
|
|
- 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
|