Files
Kamino/roles/ufw/molecule/default/converge.yml
T
Defame1297 0779e8b5c7
Build / build (pull_request) Successful in 37s
Lint / lint (pull_request) Successful in 2m46s
Molecule / molecule (pull_request) Successful in 18m9s
test(ufw): cover the service_rules cases with fewer owners
The scenario called service_rules once per owner, eight times, about 30 tasks each, on every
check, converge and idempotence pass. Three owners keep every case.

- svc_a (no purge): the old svc_a (8001, 8002 with from), svc_b (8003, the 8004 deny with no
  comment or proto), svc_g (8022 with a comment mentioning 22), svc_e (7201 and the stale
  7003 kept because purging is off).
- svc_c (purge): the old svc_c (7101, stale 7001 deleted), svc_h (7301, the stale SSH
  lookalike kept), svc_p (7602, stale 7601 deleted, no-port, source-only and range rules
  kept). svc_a's stale 7003 surviving svc_c's purge proves owner isolation.
- svc_d (absent): unchanged.

The tagged total stays at 20 (svc_a 7, svc_c 7, svc_m 1, svc_o2 1, svc_o3 4) and the untagged
at 5. Converge keeps two orphan checks, one after the three cases without a purge and one after
the purge; the check after the invalid managed owner in verify goes, as the Verify play asserts
the same state. Converge gathers no facts: the role reads none.

Closes #457

Assisted-by: Claude Code
2026-11-28 21:52:36 +00:00

137 lines
5.6 KiB
YAML

---
# Only runs that apply the role; the refusal cases are in verify.yml.
- name: Converge with an SSH rule and service rules
hosts: all
# The role reads no facts; ufw_ssh_port defaults from ansible_port.
gather_facts: false
roles:
- role: mortis.kamino.ufw
vars:
ufw_rules:
# Listed first on purpose: deletes run after every other rule. The second run
# deletes a rule that is gone, which must not report a change.
# The comment differs from the stored one and is not sent, so it still matches.
- {rule: allow, port: 9091, proto: tcp, comment: other, delete: true}
- {rule: allow, port: 22, proto: tcp, comment: ssh}
- {rule: allow, port: 8080, proto: tcp, from: 10.0.0.0/8, comment: web}
- {rule: allow, port: 53, proto: udp, comment: dns}
# no proto: both protocols, and the guard must cope with the key missing
- {rule: allow, port: 9090, comment: metrics}
# The service_rules entry point. prepare.yml stored a stale rule for svc_a, stale rules for
# svc_c and a rule of the removed service svc_d, so every pass here is a no-op after the
# first one, purge included. svc_c purges after svc_a ran, so svc_a's stale rule surviving
# proves a purge touches only its own owner.
- name: Converge the rules of several services through the entry point
hosts: all
gather_facts: false
tasks:
- name: Keep the stale rule of svc_a because purging is off
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_a
ufw_service_rules:
- {rule: allow, port: 8001, proto: tcp, comment: alpha web}
- {rule: allow, port: 8002, proto: udp, from: 10.1.0.0/16, comment: alpha dns}
- {rule: allow, port: 8003, proto: tcp, comment: beta}
# No comment and no proto: the tag alone, and both protocols.
- {rule: deny, port: 8004}
# A comment that mentions the SSH port on another port.
- {rule: allow, port: 8022, proto: tcp, comment: forward to 22 here}
- {rule: allow, port: 7201, proto: tcp, comment: epsilon}
# Of the stale rules, only 7001 and 7601 are single numeric ports; the SSH lookalike, the
# rule with no port, the source-only rule and the range are kept as possible SSH paths.
- name: Purge the stale rules of svc_c except those that may carry SSH
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_c
ufw_purge_stale: true
ufw_service_rules:
- {rule: allow, port: 7101, proto: tcp, comment: gamma}
- {rule: allow, port: 7301, proto: tcp, comment: eta}
- {rule: allow, port: 7602, proto: tcp, comment: desired}
- name: Remove svc_d, which deletes without ufw_purge_stale
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_d
ufw_service_state: absent
# 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
# orphans. The cases that must change nothing run first and the purge last, so the
# idempotence run finds nothing left to delete. Under --check the role's own skipped probe
# keeps the purge from deleting, and orphan_check.yml skips its counts.
- name: Report and purge the rules of unmanaged owners
hosts: all
gather_facts: false
vars:
ufw_converge_managed:
- svc_a
- svc_c
- svc_d
- svc_m
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.
- name: Look for the marker of an earlier purge
ansible.builtin.stat:
path: /var/tmp/ufw-orphans-purged
register: ufw_conv_marker
- name: Work out the expected number of rules of svc_o1
ansible.builtin.set_fact:
ufw_conv_o1_initial: "{{ 0 if ufw_conv_marker.stat.exists else 2 }}"
- name: No managed owners given, purge requested
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: orphan_rules
vars:
ufw_purge_orphans: true
- name: An empty list of managed owners, purge requested
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: orphan_rules
vars:
ufw_managed_owners: []
ufw_purge_orphans: true
- name: Report the orphans only
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: orphan_rules
vars:
ufw_managed_owners: "{{ ufw_converge_managed }}"
ufw_purge_orphans: false
# The counts are absolute, so one check proves none of the three cases above deleted.
- name: The cases without a purge deleted nothing
ansible.builtin.include_tasks: tasks/orphan_check.yml
- name: Purge the orphans
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: orphan_rules
vars:
ufw_managed_owners: "{{ ufw_converge_managed }}"
ufw_purge_orphans: true
- name: Only svc_o1 is gone, the kept orphans and svc_m stay
ansible.builtin.include_tasks: tasks/orphan_check.yml
vars:
ufw_conv_o1_want: 0
- name: Mark the purge as done
ansible.builtin.command: touch /var/tmp/ufw-orphans-purged
changed_when: false