Merge pull request 'test(molecule): move refusal plays from converge to verify' (#302) from test/refusal-plays-to-verify into main
Build / build (push) Successful in 19s
Lint / lint (push) Successful in 1m8s
Molecule / molecule (push) Successful in 22m5s

Reviewed-on: #302
This commit was merged in pull request #302.
This commit is contained in:
Defame1297 committed 2026-11-23 06:45:16 +00:00
commit fabbe7d977
11 files changed
+657 -630

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
---
# Run by converge.yml in a child ansible-playbook process, because the mint guard
# Run by verify.yml in a child ansible-playbook process, because the mint guard
# reads the controller's environment (lookup env), which a play cannot change; the
# child gets its environment from the task that starts it, so the result does not
# depend on what the runner exports. With the CI variable set it fails at the CI
+3 -114
View File
@@ -1,118 +1,7 @@
---
# Guard and no-op paths of the mint entry point (nothing is installed). Each
# failure case wraps the include in block/rescue, so the play passes when the
# guard fires, and fails when the include returns or fails with another message.
# Gitea is never installed in this scenario, so converge applies nothing; the mint guards and
# every check are in verify.yml, which runs once.
- name: Converge
hosts: all
gather_facts: false
tasks:
- name: Mint without confirmation
block:
- name: Include the mint entry point with gitea_mint_confirm unset
ansible.builtin.include_role:
name: mortis.kamino.gitea
tasks_from: mint
# Only reached when the include returned, so the guard did not fire. The message
# must not contain the guard's text, so the rescue below cannot mistake it for it.
- name: Fail because the include returned
ansible.builtin.fail:
msg: The include returned without the confirm guard firing
rescue:
- name: The confirm guard fired, and nothing else failed first
ansible.builtin.assert:
that:
- "'mint is opt-in' in ansible_failed_result.msg"
- "'gitea_mint_confirm=true' in ansible_failed_result.msg"
quiet: true
- name: Mint under each CI variable, run as a child process with only that one set
delegate_to: localhost
run_once: true
ansible.builtin.command:
argv:
- ansible-playbook
- -i
- localhost,
- "{{ playbook_dir }}/ci_guard.yml"
environment: "{{ {'GITHUB_ACTIONS': '', 'GITEA_ACTIONS': '', 'CI': '', 'ANSIBLE_NOCOLOR': '1'} | combine({item: 'true'}) }}"
loop:
- GITHUB_ACTIONS
- GITEA_ACTIONS
- CI
register: gitea_test_mint_ci
# The child is expected to fail (the CI guard fires). Its rc and output are asserted in the next task.
failed_when: false
changed_when: false
# Ansible 2.19 prints task failures to stderr, so both streams are checked.
- name: The CI guard fired for every variable
ansible.builtin.assert:
that:
- item.rc != 0
- "'refuses to run under CI' in (item.stdout ~ item.stderr)"
- "'mint is opt-in' not in (item.stdout ~ item.stderr)"
quiet: true
loop: "{{ gitea_test_mint_ci.results }}"
loop_control:
label: "{{ item.item }}"
- name: Mint outside CI with the token already defined, run as a child process
delegate_to: localhost
run_once: true
ansible.builtin.command:
argv:
- ansible-playbook
- -i
- localhost,
- "{{ playbook_dir }}/ci_guard.yml"
- -e
- gitea_api_token=molecule-not-a-token
environment:
GITHUB_ACTIONS: ""
GITEA_ACTIONS: ""
CI: ""
ANSIBLE_NOCOLOR: "1"
register: gitea_test_mint_noop
# A child failure must not abort here; the next task asserts rc and output.
failed_when: false
changed_when: false
- name: The mint was a no-op that registered no token fact
ansible.builtin.assert:
that:
- gitea_test_mint_noop.rc == 0
- "'nothing to mint' in (gitea_test_mint_noop.stdout ~ gitea_test_mint_noop.stderr)"
quiet: true
# An empty token counts as missing (configure.yml refuses it), so the mint must not report
# "nothing to mint". In check mode it creates nothing and registers no fact, and ends green
# (ci_guard.yml asserts that gitea_minted_api_token is not defined).
- name: Mint outside CI with an empty token in check mode, run as a child process
delegate_to: localhost
run_once: true
ansible.builtin.command:
argv:
- ansible-playbook
- -i
- localhost,
- "{{ playbook_dir }}/ci_guard.yml"
- --check
- -e
- gitea_api_token=
environment:
GITHUB_ACTIONS: ""
GITEA_ACTIONS: ""
CI: ""
ANSIBLE_NOCOLOR: "1"
register: gitea_test_mint_empty
# A child failure must not abort here; the next task asserts rc and output.
failed_when: false
changed_when: false
- name: An empty token was treated as missing and nothing was minted
ansible.builtin.assert:
that:
- gitea_test_mint_empty.rc == 0
- "'nothing to mint' not in (gitea_test_mint_empty.stdout ~ gitea_test_mint_empty.stderr)"
quiet: true
tasks: []
+119
View File
@@ -1,4 +1,123 @@
---
# Guard and no-op paths of the mint entry point (nothing is installed). Each failure case
# wraps the include in block/rescue, or runs ci_guard.yml as a child process, so the play
# passes when the guard fires and fails when the include returns or fails with another message.
# First, so the assertions below also show the guards changed nothing.
- name: Verify the mint guards
hosts: all
gather_facts: false
tasks:
- name: Mint without confirmation
block:
- name: Include the mint entry point with gitea_mint_confirm unset
ansible.builtin.include_role:
name: mortis.kamino.gitea
tasks_from: mint
# Only reached when the include returned, so the guard did not fire. The message
# must not contain the guard's text, so the rescue below cannot mistake it for it.
- name: Fail because the include returned
ansible.builtin.fail:
msg: The include returned without the confirm guard firing
rescue:
- name: The confirm guard fired, and nothing else failed first
ansible.builtin.assert:
that:
- "'mint is opt-in' in ansible_failed_result.msg"
- "'gitea_mint_confirm=true' in ansible_failed_result.msg"
quiet: true
- name: Mint under each CI variable, run as a child process with only that one set
delegate_to: localhost
run_once: true
ansible.builtin.command:
argv:
- ansible-playbook
- -i
- localhost,
- "{{ playbook_dir }}/ci_guard.yml"
environment: "{{ {'GITHUB_ACTIONS': '', 'GITEA_ACTIONS': '', 'CI': '', 'ANSIBLE_NOCOLOR': '1'} | combine({item: 'true'}) }}"
loop:
- GITHUB_ACTIONS
- GITEA_ACTIONS
- CI
register: gitea_test_mint_ci
# The child is expected to fail (the CI guard fires). Its rc and output are asserted in the next task.
failed_when: false
changed_when: false
# Ansible 2.19 prints task failures to stderr, so both streams are checked.
- name: The CI guard fired for every variable
ansible.builtin.assert:
that:
- item.rc != 0
- "'refuses to run under CI' in (item.stdout ~ item.stderr)"
- "'mint is opt-in' not in (item.stdout ~ item.stderr)"
quiet: true
loop: "{{ gitea_test_mint_ci.results }}"
loop_control:
label: "{{ item.item }}"
- name: Mint outside CI with the token already defined, run as a child process
delegate_to: localhost
run_once: true
ansible.builtin.command:
argv:
- ansible-playbook
- -i
- localhost,
- "{{ playbook_dir }}/ci_guard.yml"
- -e
- gitea_api_token=molecule-not-a-token
environment:
GITHUB_ACTIONS: ""
GITEA_ACTIONS: ""
CI: ""
ANSIBLE_NOCOLOR: "1"
register: gitea_test_mint_noop
# A child failure must not abort here; the next task asserts rc and output.
failed_when: false
changed_when: false
- name: The mint was a no-op that registered no token fact
ansible.builtin.assert:
that:
- gitea_test_mint_noop.rc == 0
- "'nothing to mint' in (gitea_test_mint_noop.stdout ~ gitea_test_mint_noop.stderr)"
quiet: true
# An empty token counts as missing (configure.yml refuses it), so the mint must not report
# "nothing to mint". In check mode it creates nothing and registers no fact, and ends green
# (ci_guard.yml asserts that gitea_minted_api_token is not defined).
- name: Mint outside CI with an empty token in check mode, run as a child process
delegate_to: localhost
run_once: true
ansible.builtin.command:
argv:
- ansible-playbook
- -i
- localhost,
- "{{ playbook_dir }}/ci_guard.yml"
- --check
- -e
- gitea_api_token=
environment:
GITHUB_ACTIONS: ""
GITEA_ACTIONS: ""
CI: ""
ANSIBLE_NOCOLOR: "1"
register: gitea_test_mint_empty
# A child failure must not abort here; the next task asserts rc and output.
failed_when: false
changed_when: false
- name: An empty token was treated as missing and nothing was minted
ansible.builtin.assert:
that:
- gitea_test_mint_empty.rc == 0
- "'nothing to mint' not in (gitea_test_mint_empty.stdout ~ gitea_test_mint_empty.stderr)"
quiet: true
- name: Verify
hosts: all
gather_facts: false
+2 -157
View File
@@ -8,28 +8,9 @@
roles:
- role: mortis.kamino.sshd
# A play without sshd_profile: the role must refuse it and leave the config
# converged above untouched. The block proves the refusal (not its wording); the play
# still passes, so the idempotence run stays clean.
- name: Converge the role 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
# 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 guards.
# 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
@@ -83,29 +64,6 @@
- sshd_settings_effective.PubkeyAuthentication == "yes"
quiet: true
- 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 accepts every value that allows root key login
hosts: all
gather_facts: false
@@ -119,116 +77,3 @@
sshd_settings:
PermitRootLogin: "{{ item }}"
loop: [prohibit-password, without-password, "yes", Prohibit-Password]
- 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
+158
View File
@@ -1,4 +1,162 @@
---
# 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.
# 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
+2 -47
View File
@@ -1,6 +1,6 @@
---
# Role variables come from the inventory group_vars in molecule.yml, so every play
# renders identical configuration (idempotence).
# Role variables come from the inventory group_vars in molecule.yml, so the idempotence step
# reruns converge with identical configuration; verify's plays override only what they test.
# The base ufw play comes first: traefik passes its rules through the mortis.kamino.ufw
# service_rules entry point, which needs ufw installed and active. ufw_rules comes from the
# same group_vars (the SSH allow rule the lock-out guard requires).
@@ -10,48 +10,3 @@
roles:
- role: mortis.kamino.ufw
- role: mortis.kamino.traefik
- name: Refuse an enabled dashboard without basicAuth users
hosts: all
gather_facts: false
tasks:
- name: Run the role with the dashboard on and no users
block:
- name: Include traefik
ansible.builtin.include_role:
name: mortis.kamino.traefik
vars:
traefik_dashboard_enabled: true
traefik_dashboard_basicauth_users: []
- name: Unreachable when the guard works
ansible.builtin.fail:
msg: traefik accepted an enabled dashboard without users
rescue:
- name: The dashboard guard, not something else, stopped the play
ansible.builtin.assert:
that: "'unauthenticated Traefik dashboard' in ansible_failed_result.msg | default('')"
quiet: true
- name: Refuse a dashboard-auth middleware collision
hosts: all
gather_facts: false
tasks:
- name: Run the role with a colliding middleware
block:
- name: Include traefik
ansible.builtin.include_role:
name: mortis.kamino.traefik
vars:
traefik_dashboard_basicauth_users:
- "admin:$apr1$x$y"
traefik_dynamic_middlewares:
dashboard-auth:
basicAuth: {users: []}
- name: Unreachable when the check works
ansible.builtin.fail:
msg: traefik accepted a colliding dashboard-auth middleware
rescue:
- name: The collision check, not something else, stopped the play
ansible.builtin.assert:
that: "'collides with the middleware' in ansible_failed_result.msg | default('')"
quiet: true
+3 -3
View File
@@ -23,9 +23,9 @@ platforms:
- apparmor=unconfined
provisioner:
name: ansible
# One source for every play in converge.yml. The role renders traefik.yml before
# its dashboard guard and its dashboard-auth collision check, so a play that included it with other
# values would re-render the file and break the idempotence step.
# One source for every play in converge.yml and verify.yml. The role renders traefik.yml before
# its dashboard-auth collision check, so that guard play with other values would change it (and
# restart Traefik) before verify's assertions; converge's values must also stay fixed for idempotence.
inventory:
group_vars:
all:
+49
View File
@@ -1,4 +1,53 @@
---
# The input guards. Both fail after the role's prereqs (packages already present), and the
# collision case re-renders traefik.yml from the same group_vars with the dashboard still off,
# so neither changes the host. They run first, so every assertion below also shows that; the
# remove path stays last, since after it the role would install Traefik again.
- name: Refuse an enabled dashboard without basicAuth users
hosts: all
gather_facts: false
tasks:
- name: Run the role with the dashboard on and no users
block:
- name: Include traefik
ansible.builtin.include_role:
name: mortis.kamino.traefik
vars:
traefik_dashboard_enabled: true
traefik_dashboard_basicauth_users: []
- name: Unreachable when the guard works
ansible.builtin.fail:
msg: traefik accepted an enabled dashboard without users
rescue:
- name: The dashboard guard, not something else, stopped the play
ansible.builtin.assert:
that: "'unauthenticated Traefik dashboard' in ansible_failed_result.msg | default('')"
quiet: true
- name: Refuse a dashboard-auth middleware collision
hosts: all
gather_facts: false
tasks:
- name: Run the role with a colliding middleware
block:
- name: Include traefik
ansible.builtin.include_role:
name: mortis.kamino.traefik
vars:
traefik_dashboard_basicauth_users:
- "admin:$apr1$x$y"
traefik_dynamic_middlewares:
dashboard-auth:
basicAuth: {users: []}
- name: Unreachable when the check works
ansible.builtin.fail:
msg: traefik accepted a colliding dashboard-auth middleware
rescue:
- name: The collision check, not something else, stopped the play
ansible.builtin.assert:
that: "'collides with the middleware' in ansible_failed_result.msg | default('')"
quiet: true
- name: Verify
hosts: all
gather_facts: false
+1 -302
View File
@@ -1,117 +1,5 @@
---
# Lock-out guard: default-deny without an SSH allow rule must be refused before
# any policy is touched.
- name: Refuse default-deny without an SSH allow rule
hosts: all
gather_facts: false
tasks:
- name: Run the role with only a non-SSH rule
block:
- name: Include ufw
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: 8080, proto: tcp}
- name: Unreachable when the guard works
ansible.builtin.fail:
msg: ufw ran without an SSH allow rule
rescue:
- name: The guard, not something else, stopped the play
ansible.builtin.assert:
that: "'would lock this host out' in ansible_failed_result.msg | default('')"
quiet: true
# A delete entry removes the SSH rule it names, so it must not satisfy the guard.
- name: Run the role with an SSH rule that is only deleted
block:
- name: Include ufw
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: 8080, proto: tcp}
- {rule: allow, port: 22, proto: tcp, delete: true}
- name: Unreachable when the guard ignores delete entries
ansible.builtin.fail:
msg: ufw counted a deleted SSH rule as the SSH allow rule
rescue:
- name: The guard stopped the play
ansible.builtin.assert:
that: "'would lock this host out' in ansible_failed_result.msg | default('')"
quiet: true
# The same rule added and deleted: the delete would win (deletes run last) while the
# guard counted the add. The comment, the port's type and the written default
# direction and source do not make them different.
- name: Run the role with a rule that is both added and deleted
block:
- name: Include ufw
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: "22", proto: tcp, direction: in, from: any, comment: ssh}
- {rule: allow, port: 22, proto: tcp, delete: true}
- name: Unreachable when the conflict check works
ansible.builtin.fail:
msg: ufw accepted a rule that is both added and deleted
rescue:
- name: The conflict check stopped the play
ansible.builtin.assert:
that: "'both added and deleted' in ansible_failed_result.msg | default('')"
quiet: true
# Stray variable guard: variables named like <role>_ufw_rules must fail the play
# before anything changes, naming the variable. The vars sit on the include only, so they
# do not reach the plays below (the base ufw_rules and ufw_before_rules never match).
- name: Refuse stray variables named like the old *_ufw_rules
hosts: all
gather_facts: false
tasks:
- name: A stray *_ufw_rules variable
block:
- name: Include ufw with a stray rules variable
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: 22, proto: tcp, comment: ssh}
stray_ufw_rules: # noqa: var-naming[no-role-prefix]
- {rule: allow, port: 7999, proto: tcp, comment: stray}
- name: Unreachable when the stray variable guard works
ansible.builtin.fail:
msg: a stray *_ufw_rules variable was accepted
rescue:
- name: The stray guard, not something else, stopped the play
ansible.builtin.assert:
that:
- "'stray_ufw_rules' in ansible_failed_result.msg | default('')"
- "'stray_ufw_before_rules' not in ansible_failed_result.msg | default('')"
- "'service_rules' in ansible_failed_result.msg | default('')"
quiet: true
- name: A stray *_ufw_before_rules variable
block:
- name: Include ufw with a stray before-rules variable
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: 22, proto: tcp, comment: ssh}
stray_ufw_before_rules: # noqa: var-naming[no-role-prefix]
- {proto: udp, port: 7998, comment: stray}
- name: Unreachable when the stray variable guard works
ansible.builtin.fail:
msg: a stray *_ufw_before_rules variable was accepted
rescue:
- name: The stray guard, not something else, stopped the play
ansible.builtin.assert:
that:
- "'stray_ufw_before_rules' in ansible_failed_result.msg | default('')"
- "'stray_ufw_rules' not in ansible_failed_result.msg | default('')"
quiet: true
# Only runs that apply the role; the refusal cases are in verify.yml.
- name: Converge with an SSH rule and service rules
hosts: all
gather_facts: true
@@ -218,171 +106,6 @@
ufw_service_rules:
- {rule: allow, port: 7602, proto: tcp, comment: desired}
# Inputs the entry point must refuse, each before it changes anything. Every block
# proves the refusal and its message; the play still passes, so the idempotence run
# stays clean and verify.yml proves no rule was left behind.
- name: Refuse invalid service rule inputs
hosts: all
gather_facts: false
vars:
# A failed looped task reports "One or more items failed" in msg and the item
# messages in results; an unlooped one has the message in msg.
ufw_converge_failure: >-
{{ ansible_failed_result.msg | default('') }}
{{ ansible_failed_result.results | default([]) | map(attribute='msg', default='') | join(' ') }}
tasks:
- name: An owner name outside the allowed characters
block:
- name: Include with an invalid owner name
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: Bad-Name
ufw_service_rules:
- {rule: allow, port: 8101, proto: tcp}
- name: Unreachable when the owner name is checked
ansible.builtin.fail:
msg: an invalid owner name was accepted
rescue:
- name: The owner check stopped the play
ansible.builtin.assert:
that: "'ufw_service_name must be a string matching' in ufw_converge_failure"
quiet: true
- name: The reserved owner base
block:
- name: Include as base
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: base
ufw_service_rules:
- {rule: allow, port: 8102, proto: tcp}
- name: Unreachable when base is reserved
ansible.builtin.fail:
msg: the reserved owner base was accepted
rescue:
- name: The reserved name stopped the play
ansible.builtin.assert:
that: "'reserved for the base play' in ufw_converge_failure"
quiet: true
- name: A single quote in a comment
block:
- name: Include with a quote in the comment
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_f
ufw_service_rules:
- {rule: allow, port: 8103, proto: tcp, comment: "it's"}
- name: Unreachable when comments are checked
ansible.builtin.fail:
msg: a quote in a comment was accepted
rescue:
- name: The comment check stopped the play
ansible.builtin.assert:
that: "'single quote' in ufw_converge_failure"
quiet: true
- name: A rule another owner already holds
block:
- name: Include with the spec of a svc_a rule
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_f
ufw_service_rules:
- {rule: allow, port: 8001, proto: tcp, comment: other text}
- name: Unreachable when collisions are checked
ansible.builtin.fail:
msg: a rule owned by svc_a was accepted for svc_f
rescue:
- name: The collision check stopped the play
ansible.builtin.assert:
that: "'already owned by another tag' in ufw_converge_failure"
quiet: true
- name: A rule that would retag the SSH rule
block:
- name: Include with an allow rule for the SSH port
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_f
ufw_service_rules:
- {rule: allow, port: 22, proto: tcp}
- name: Unreachable when the SSH port is protected
ansible.builtin.fail:
msg: a service rule for the SSH port was accepted
rescue:
- name: The SSH check stopped the play
ansible.builtin.assert:
that: "'allows the SSH port' in ufw_converge_failure"
quiet: true
- name: An unknown rule key
block:
- name: Include with a to_port key
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_f
ufw_service_rules:
- {rule: allow, to_port: 8104, proto: tcp}
- name: Unreachable when keys are checked
ansible.builtin.fail:
msg: an unknown rule key was accepted
rescue:
- name: The key check stopped the play
ansible.builtin.assert:
that: "'has an unknown key' in ufw_converge_failure"
quiet: true
- name: An interface without a direction
block:
- name: Include with an interface and no direction
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_f
ufw_service_rules:
- {rule: allow, port: 8105, proto: tcp, interface: eth0}
- name: Unreachable when the direction is required
ansible.builtin.fail:
msg: an interface without a direction was accepted
rescue:
- name: The direction check stopped the play
ansible.builtin.assert:
that: "'interface without direction' in ufw_converge_failure"
quiet: true
- name: Purging with an empty rule list
block:
- name: Include with an empty list and ufw_purge_stale
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_a
ufw_purge_stale: true
ufw_service_rules: []
- name: Unreachable when an empty list is refused
ansible.builtin.fail:
msg: an empty list with ufw_purge_stale was accepted
rescue:
- name: The empty list stopped the play
ansible.builtin.assert:
that: "'use ufw_service_state: absent' in ufw_converge_failure"
quiet: true
# The orphan_rules entry point. prepare.yml stored two rules of svc_o1, a rule of
# svc_o2 that looks like the SSH rule, and a rule of svc_m. The managed owners below are
# the ones used above plus svc_m (listed but never called), so svc_o1 and svc_o2 are
@@ -403,9 +126,6 @@
- svc_h
- svc_m
- svc_p
ufw_converge_failure: >-
{{ ansible_failed_result.msg | default('') }}
{{ ansible_failed_result.results | default([]) | map(attribute='msg', default='') | join(' ') }}
tasks:
# The marker is written after the purge, so the expected counts are absolute on both
# passes: svc_o1 holds 2 rules before the purge and 0 once the marker exists.
@@ -442,27 +162,6 @@
- name: Nothing was deleted with an empty list
ansible.builtin.include_tasks: tasks/orphan_check.yml
- name: An invalid managed owner
block:
- name: Include with an owner name outside the allowed characters
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: orphan_rules
vars:
ufw_managed_owners: [svc_a, Bad-Name]
ufw_purge_orphans: true
- name: Unreachable when the owners are checked
ansible.builtin.fail:
msg: an invalid managed owner was accepted
rescue:
- name: The owner check stopped the play
ansible.builtin.assert:
that: "'ufw_managed_owners must be a list of strings matching' in ufw_converge_failure"
quiet: true
- name: Nothing was deleted after the invalid owner
ansible.builtin.include_tasks: tasks/orphan_check.yml
- name: Report the orphans only
ansible.builtin.include_role:
name: mortis.kamino.ufw
@@ -1,9 +1,9 @@
---
# Included by converge.yml after each orphan_rules case. The counts are absolute: svc_o1
# holds ufw_conv_o1_want rules (default ufw_conv_o1_initial: 2 before the purge, 0 after),
# and the tagged total is 20 plus those. The 20: svc_a 2, svc_b 2, svc_c 1, svc_e 2, svc_g 1,
# svc_h 2, svc_m 1, svc_o2 1, svc_o3 4 (kept orphans that may carry SSH), svc_p 4 (its
# desired rule and three kept stale rules).
# Included by converge.yml and verify.yml after each orphan_rules case. The counts are
# absolute: svc_o1 holds ufw_conv_o1_want rules (default ufw_conv_o1_initial: 2 before the
# purge, 0 after), and the tagged total is 20 plus those. The 20: svc_a 2, svc_b 2, svc_c 1,
# svc_e 2, svc_g 1, svc_h 2, svc_m 1, svc_o2 1, svc_o3 4 (kept orphans that may carry SSH),
# svc_p 4 (its desired rule and three kept stale rules).
- name: Read the rules ufw would load
ansible.builtin.command: ufw show added
register: ufw_conv_added
+314 -1
View File
@@ -1,4 +1,317 @@
---
# The refusal cases run first, so every assertion below also proves a refused run left the
# host unchanged. They are not in converge.yml because idempotence reruns it.
# Lock-out guard: default-deny without an SSH allow rule must be refused before
# any policy is touched.
- name: Refuse default-deny without an SSH allow rule
hosts: all
gather_facts: false
tasks:
- name: Run the role with only a non-SSH rule
block:
- name: Include ufw
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: 8080, proto: tcp}
- name: Unreachable when the guard works
ansible.builtin.fail:
msg: ufw ran without an SSH allow rule
rescue:
- name: The guard, not something else, stopped the play
ansible.builtin.assert:
that: "'would lock this host out' in ansible_failed_result.msg | default('')"
quiet: true
# A delete entry removes the SSH rule it names, so it must not satisfy the guard.
- name: Run the role with an SSH rule that is only deleted
block:
- name: Include ufw
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: 8080, proto: tcp}
- {rule: allow, port: 22, proto: tcp, delete: true}
- name: Unreachable when the guard ignores delete entries
ansible.builtin.fail:
msg: ufw counted a deleted SSH rule as the SSH allow rule
rescue:
- name: The guard stopped the play
ansible.builtin.assert:
that: "'would lock this host out' in ansible_failed_result.msg | default('')"
quiet: true
# The same rule added and deleted: the delete would win (deletes run last) while the
# guard counted the add. The comment, the port's type and the written default
# direction and source do not make them different.
- name: Run the role with a rule that is both added and deleted
block:
- name: Include ufw
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: "22", proto: tcp, direction: in, from: any, comment: ssh}
- {rule: allow, port: 22, proto: tcp, delete: true}
- name: Unreachable when the conflict check works
ansible.builtin.fail:
msg: ufw accepted a rule that is both added and deleted
rescue:
- name: The conflict check stopped the play
ansible.builtin.assert:
that: "'both added and deleted' in ansible_failed_result.msg | default('')"
quiet: true
# Stray variable guard: variables named like <role>_ufw_rules must fail the play
# before anything changes, naming the variable. The vars sit on the include only, so they
# do not reach the plays below (the base ufw_rules and ufw_before_rules never match).
- name: Refuse stray variables named like the old *_ufw_rules
hosts: all
gather_facts: false
tasks:
- name: A stray *_ufw_rules variable
block:
- name: Include ufw with a stray rules variable
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: 22, proto: tcp, comment: ssh}
stray_ufw_rules: # noqa: var-naming[no-role-prefix]
- {rule: allow, port: 7999, proto: tcp, comment: stray}
- name: Unreachable when the stray variable guard works
ansible.builtin.fail:
msg: a stray *_ufw_rules variable was accepted
rescue:
- name: The stray guard, not something else, stopped the play
ansible.builtin.assert:
that:
- "'stray_ufw_rules' in ansible_failed_result.msg | default('')"
- "'stray_ufw_before_rules' not in ansible_failed_result.msg | default('')"
- "'service_rules' in ansible_failed_result.msg | default('')"
quiet: true
- name: A stray *_ufw_before_rules variable
block:
- name: Include ufw with a stray before-rules variable
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: 22, proto: tcp, comment: ssh}
stray_ufw_before_rules: # noqa: var-naming[no-role-prefix]
- {proto: udp, port: 7998, comment: stray}
- name: Unreachable when the stray variable guard works
ansible.builtin.fail:
msg: a stray *_ufw_before_rules variable was accepted
rescue:
- name: The stray guard, not something else, stopped the play
ansible.builtin.assert:
that:
- "'stray_ufw_before_rules' in ansible_failed_result.msg | default('')"
- "'stray_ufw_rules' not in ansible_failed_result.msg | default('')"
quiet: true
# Inputs the entry point must refuse, each before it changes anything. Every block
# proves the refusal and its message; the Verify play proves no rule was left behind.
- name: Refuse invalid service rule inputs
hosts: all
gather_facts: false
vars:
# A failed looped task reports "One or more items failed" in msg and the item
# messages in results; an unlooped one has the message in msg.
ufw_verify_failure: >-
{{ ansible_failed_result.msg | default('') }}
{{ ansible_failed_result.results | default([]) | map(attribute='msg', default='') | join(' ') }}
tasks:
- name: An owner name outside the allowed characters
block:
- name: Include with an invalid owner name
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: Bad-Name
ufw_service_rules:
- {rule: allow, port: 8101, proto: tcp}
- name: Unreachable when the owner name is checked
ansible.builtin.fail:
msg: an invalid owner name was accepted
rescue:
- name: The owner check stopped the play
ansible.builtin.assert:
that: "'ufw_service_name must be a string matching' in ufw_verify_failure"
quiet: true
- name: The reserved owner base
block:
- name: Include as base
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: base
ufw_service_rules:
- {rule: allow, port: 8102, proto: tcp}
- name: Unreachable when base is reserved
ansible.builtin.fail:
msg: the reserved owner base was accepted
rescue:
- name: The reserved name stopped the play
ansible.builtin.assert:
that: "'reserved for the base play' in ufw_verify_failure"
quiet: true
- name: A single quote in a comment
block:
- name: Include with a quote in the comment
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_f
ufw_service_rules:
- {rule: allow, port: 8103, proto: tcp, comment: "it's"}
- name: Unreachable when comments are checked
ansible.builtin.fail:
msg: a quote in a comment was accepted
rescue:
- name: The comment check stopped the play
ansible.builtin.assert:
that: "'single quote' in ufw_verify_failure"
quiet: true
- name: A rule another owner already holds
block:
- name: Include with the spec of a svc_a rule
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_f
ufw_service_rules:
- {rule: allow, port: 8001, proto: tcp, comment: other text}
- name: Unreachable when collisions are checked
ansible.builtin.fail:
msg: a rule owned by svc_a was accepted for svc_f
rescue:
- name: The collision check stopped the play
ansible.builtin.assert:
that: "'already owned by another tag' in ufw_verify_failure"
quiet: true
- name: A rule that would retag the SSH rule
block:
- name: Include with an allow rule for the SSH port
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_f
ufw_service_rules:
- {rule: allow, port: 22, proto: tcp}
- name: Unreachable when the SSH port is protected
ansible.builtin.fail:
msg: a service rule for the SSH port was accepted
rescue:
- name: The SSH check stopped the play
ansible.builtin.assert:
that: "'allows the SSH port' in ufw_verify_failure"
quiet: true
- name: An unknown rule key
block:
- name: Include with a to_port key
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_f
ufw_service_rules:
- {rule: allow, to_port: 8104, proto: tcp}
- name: Unreachable when keys are checked
ansible.builtin.fail:
msg: an unknown rule key was accepted
rescue:
- name: The key check stopped the play
ansible.builtin.assert:
that: "'has an unknown key' in ufw_verify_failure"
quiet: true
- name: An interface without a direction
block:
- name: Include with an interface and no direction
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_f
ufw_service_rules:
- {rule: allow, port: 8105, proto: tcp, interface: eth0}
- name: Unreachable when the direction is required
ansible.builtin.fail:
msg: an interface without a direction was accepted
rescue:
- name: The direction check stopped the play
ansible.builtin.assert:
that: "'interface without direction' in ufw_verify_failure"
quiet: true
- name: Purging with an empty rule list
block:
- name: Include with an empty list and ufw_purge_stale
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_a
ufw_purge_stale: true
ufw_service_rules: []
- name: Unreachable when an empty list is refused
ansible.builtin.fail:
msg: an empty list with ufw_purge_stale was accepted
rescue:
- name: The empty list stopped the play
ansible.builtin.assert:
that: "'use ufw_service_state: absent' in ufw_verify_failure"
quiet: true
# An orphan_rules input the entry point must refuse before it reads or deletes a rule.
- name: Refuse an invalid managed owner
hosts: all
gather_facts: false
vars:
ufw_verify_failure: >-
{{ ansible_failed_result.msg | default('') }}
{{ ansible_failed_result.results | default([]) | map(attribute='msg', default='') | join(' ') }}
tasks:
- name: An invalid managed owner
block:
- name: Include with an owner name outside the allowed characters
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: orphan_rules
vars:
ufw_managed_owners: [svc_a, Bad-Name]
ufw_purge_orphans: true
- name: Unreachable when the owners are checked
ansible.builtin.fail:
msg: an invalid managed owner was accepted
rescue:
- name: The owner check stopped the play
ansible.builtin.assert:
that: "'ufw_managed_owners must be a list of strings matching' in ufw_verify_failure"
quiet: true
# Converge's last play purged svc_o1 (want 0); svc_o2, svc_m and the tagged total carry the proof.
- name: Nothing was deleted after the invalid owner
ansible.builtin.include_tasks: tasks/orphan_check.yml
vars:
ufw_conv_o1_want: 0
- name: Verify
hosts: all
gather_facts: false
@@ -41,7 +354,7 @@
vars:
ufw_verify_untagged: "{{ ufw_verify_added.stdout_lines | select('match', '^ufw ') | reject('search', 'ansible-ufw') | list }}"
# converge.yml proves the guard's message; the 7999 and 7998 absence checks are redundant confirmation that the stray rules never reached the host.
# The refusal play proves the guard's message; the 7999 and 7998 absence checks are redundant confirmation that the stray rules never reached the host.
- name: A stray *_ufw_rules variable did not reach the firewall
ansible.builtin.assert:
that: