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
329 lines
12 KiB
YAML
329 lines
12 KiB
YAML
---
|
|
# 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 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).
|
|
- name: The role refuses a run without a profile
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Missing sshd_profile is rejected
|
|
block:
|
|
- name: Apply the role without sshd_profile
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.sshd
|
|
- name: Fail when the missing profile was accepted
|
|
ansible.builtin.fail:
|
|
msg: the sshd role accepted a run without sshd_profile
|
|
rescue:
|
|
- name: The role refused the run
|
|
ansible.builtin.debug:
|
|
msg: Rejected as expected
|
|
|
|
- name: The lock-out guard refuses a lock-out
|
|
hosts: all
|
|
gather_facts: false
|
|
vars:
|
|
sshd_profile: ubuntu_lxc
|
|
sshd_settings:
|
|
PubkeyAuthentication: "no"
|
|
tasks:
|
|
- name: Apply a lock-out configuration
|
|
block:
|
|
- name: Include the validation
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.sshd
|
|
tasks_from: validate.yml
|
|
- name: Fail when the lock-out was accepted
|
|
ansible.builtin.fail:
|
|
msg: the sshd role accepted a lock-out configuration
|
|
rescue:
|
|
- name: The lock-out guard stopped the play
|
|
ansible.builtin.assert:
|
|
that: "'lock you out' in ansible_failed_result.msg | default('')"
|
|
quiet: true
|
|
|
|
- name: The Proxmox guard refuses a pve profile without root login
|
|
hosts: all
|
|
gather_facts: false
|
|
vars:
|
|
sshd_profile: pve
|
|
sshd_settings:
|
|
PermitRootLogin: "no"
|
|
tasks:
|
|
- name: Apply a pve configuration without root login
|
|
block:
|
|
- name: Include the validation
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.sshd
|
|
tasks_from: validate.yml
|
|
- name: Fail when the Proxmox guard was bypassed
|
|
ansible.builtin.fail:
|
|
msg: the sshd role accepted a pve profile without root login
|
|
rescue:
|
|
- name: The Proxmox guard stopped the play
|
|
ansible.builtin.assert:
|
|
that: "'needs root public-key login' in ansible_failed_result.msg | default('')"
|
|
quiet: true
|
|
|
|
- name: The Proxmox guard refuses forced-commands-only
|
|
hosts: all
|
|
gather_facts: false
|
|
vars:
|
|
sshd_profile: pve
|
|
sshd_settings:
|
|
PermitRootLogin: forced-commands-only
|
|
tasks:
|
|
- name: Apply a pve configuration with forced commands only
|
|
block:
|
|
- name: Include the validation
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.sshd
|
|
tasks_from: validate.yml
|
|
- name: Fail when the Proxmox guard was bypassed
|
|
ansible.builtin.fail:
|
|
msg: the sshd role accepted forced-commands-only for the pve profile
|
|
rescue:
|
|
- name: The Proxmox guard stopped the play
|
|
ansible.builtin.assert:
|
|
that: "'needs root public-key login' in ansible_failed_result.msg | default('')"
|
|
quiet: true
|
|
|
|
- name: The Proxmox guard refuses a pve profile without RSA keys
|
|
hosts: all
|
|
gather_facts: false
|
|
vars:
|
|
sshd_profile: pve
|
|
sshd_settings:
|
|
PubkeyAcceptedAlgorithms: "ssh-ed25519"
|
|
tasks:
|
|
- name: Apply a pve configuration without RSA keys
|
|
block:
|
|
- name: Include the validation
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.sshd
|
|
tasks_from: validate.yml
|
|
- name: Fail when the RSA guard was bypassed
|
|
ansible.builtin.fail:
|
|
msg: the sshd role accepted a pve profile without RSA keys
|
|
rescue:
|
|
- name: The RSA guard stopped the play
|
|
ansible.builtin.assert:
|
|
that: "'must both include rsa-sha2-512' in ansible_failed_result.msg | default('')"
|
|
quiet: true
|
|
|
|
- name: The guard refuses SHA-1 ssh-rsa
|
|
hosts: all
|
|
gather_facts: false
|
|
vars:
|
|
sshd_profile: pve
|
|
sshd_settings:
|
|
HostKeyAlgorithms: "ssh-ed25519, rsa-sha2-512, ssh-rsa"
|
|
tasks:
|
|
- name: Apply a configuration that allows ssh-rsa
|
|
block:
|
|
- name: Include the validation
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.sshd
|
|
tasks_from: validate.yml
|
|
- name: Fail when the ssh-rsa guard was bypassed
|
|
ansible.builtin.fail:
|
|
msg: the sshd role accepted ssh-rsa
|
|
rescue:
|
|
- name: The ssh-rsa guard stopped the play
|
|
ansible.builtin.assert:
|
|
that: "'must not list ssh-rsa' in ansible_failed_result.msg | default('')"
|
|
quiet: true
|
|
|
|
- name: An unknown profile is refused
|
|
hosts: all
|
|
gather_facts: false
|
|
vars:
|
|
sshd_profile: not_a_profile
|
|
tasks:
|
|
- name: Apply the role with an unknown profile
|
|
block:
|
|
- name: Include the validation
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.sshd
|
|
tasks_from: validate.yml
|
|
- name: Fail when the unknown profile was accepted
|
|
ansible.builtin.fail:
|
|
msg: the sshd role accepted an unknown profile
|
|
rescue:
|
|
- name: The profile check stopped the play
|
|
ansible.builtin.assert:
|
|
that: "'is invalid' in ansible_failed_result.msg | default('')"
|
|
quiet: true
|
|
|
|
# Lock-out signals only: the drop-in is in place, the daemon accepts the config and
|
|
# runs, and the effective settings keep key authentication on and passwords and
|
|
# root login off. The remaining hardening values restate the role's own defaults
|
|
# and would only break on a policy change.
|
|
- name: Verify
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Stat the drop-in
|
|
ansible.builtin.stat:
|
|
path: /etc/ssh/sshd_config.d/99-ansible-hardening.conf
|
|
register: sshd_verify_dropin_stat
|
|
|
|
- name: The drop-in exists
|
|
ansible.builtin.assert:
|
|
that: sshd_verify_dropin_stat.stat.exists
|
|
quiet: true
|
|
|
|
- name: Test the configuration sshd will load
|
|
ansible.builtin.command: sshd -t
|
|
changed_when: false
|
|
|
|
- name: Ask systemd about the ssh unit
|
|
ansible.builtin.systemd:
|
|
name: ssh
|
|
register: sshd_verify_unit
|
|
check_mode: true
|
|
|
|
- name: The ssh unit is active
|
|
ansible.builtin.assert:
|
|
that: sshd_verify_unit.status.ActiveState == "active"
|
|
quiet: true
|
|
|
|
- name: Dump the effective configuration
|
|
ansible.builtin.command: sshd -T
|
|
register: sshd_verify_effective
|
|
changed_when: false
|
|
|
|
- name: Key login stays on, password and root login stay off
|
|
ansible.builtin.assert:
|
|
that:
|
|
- sshd_verify_effective.stdout_lines is contains('pubkeyauthentication yes')
|
|
- sshd_verify_effective.stdout_lines is contains('passwordauthentication no')
|
|
- sshd_verify_effective.stdout_lines is contains('permitrootlogin no')
|
|
quiet: true
|
|
|
|
# The pve profile cannot be converged here (it is not an LXC), so render its drop-in
|
|
# to a scratch file and let sshd parse it: proves sshd accepts the value and the
|
|
# profile's algorithm lists, and what it resolves them to.
|
|
- name: Verify the pve profile parses
|
|
hosts: all
|
|
gather_facts: false
|
|
vars:
|
|
sshd_profile: pve
|
|
tasks:
|
|
- name: Load the role defaults for the pve profile
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.sshd
|
|
tasks_from: validate.yml
|
|
public: true
|
|
|
|
- name: Create a scratch file for the pve drop-in
|
|
ansible.builtin.tempfile:
|
|
suffix: .conf
|
|
register: sshd_verify_pve_file
|
|
|
|
- name: Render the pve drop-in
|
|
ansible.builtin.copy:
|
|
content: "{{ lookup('ansible.builtin.template', playbook_dir ~ '/../../templates/99-ansible-hardening.conf.j2') }}"
|
|
dest: "{{ sshd_verify_pve_file.path }}"
|
|
mode: "0600"
|
|
|
|
- name: Dump the effective pve configuration
|
|
ansible.builtin.command: sshd -T -f {{ sshd_verify_pve_file.path }}
|
|
register: sshd_verify_pve_effective
|
|
changed_when: false
|
|
|
|
- name: Root logs in with keys only, passwords stay off
|
|
ansible.builtin.assert:
|
|
that:
|
|
# sshd -T prints prohibit-password under one of its names, by OpenSSH version.
|
|
- >-
|
|
'permitrootlogin prohibit-password' in sshd_verify_pve_effective.stdout_lines
|
|
or 'permitrootlogin without-password' in sshd_verify_pve_effective.stdout_lines
|
|
- sshd_verify_pve_effective.stdout_lines is contains('passwordauthentication no')
|
|
- sshd_verify_pve_effective.stdout_lines is contains('pubkeyauthentication yes')
|
|
quiet: true
|
|
|
|
- name: Remove the scratch file
|
|
ansible.builtin.file:
|
|
path: "{{ sshd_verify_pve_file.path }}"
|
|
state: absent
|