Files
Kamino/roles/ufw/molecule/default/prepare.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

85 lines
4.3 KiB
YAML

---
# Rules that must already be stored before the role runs, so converge stays
# idempotent while still exercising the purge: a second converge finds them
# deleted (or kept, for the report-only owner) and changes nothing. ufw is
# installed here; it stays inactive until the role enables it.
- name: Prepare
hosts: all
gather_facts: false
tasks:
- name: Install ufw
ansible.builtin.package:
name: ufw
state: present
# An application profile whose name holds openssh, for the orphan seeds below.
- name: Define an application profile named like SSH
ansible.builtin.copy:
dest: /etc/ufw/applications.d/orphan-openssh
mode: "0644"
content: |
[OrphanOpenSSH]
title=Orphan test
description=Test profile for the orphan_rules molecule seeds
ports=22/tcp
- name: Store a manual rule without a tag
community.general.ufw:
rule: allow
port: "7777"
proto: tcp
comment: manual
# converge.yml replaces this one through a delete entry of the base list that has no
# comment, so the delete must match the rule without it.
- name: Store a manual rule that the base list deletes
community.general.ufw:
rule: allow
port: "9091"
proto: tcp
comment: replaced
# Owners svc_a, svc_c and svc_d keep rules in converge.yml that these entries are
# not part of: a stale rule that is only reported, stale rules to purge, a rule of
# a removed service.
- name: Store tagged rules that are no longer wanted
community.general.ufw:
rule: allow
port: "{{ ufw_prepare_rule.port | default(omit) }}"
proto: "{{ 'tcp' if ufw_prepare_rule.port is defined else omit }}"
from: "{{ ufw_prepare_rule.from | default(omit) }}"
interface: "{{ ufw_prepare_rule.interface | default(omit) }}"
direction: "{{ ufw_prepare_rule.direction | default(omit) }}"
name: "{{ ufw_prepare_rule.name | default(omit) }}"
comment: "{{ ufw_prepare_rule.comment }}"
loop:
# svc_c: the old spec of a rule whose port changed (key of allow|7001|tcp||||in)
- {port: 7001, comment: "ansible-ufw[svc_c:128c086248]: old"}
- {port: 7002, comment: "ansible-ufw[svc_d:abcdef0123]: gone"}
# svc_a: a stale rule kept because svc_a does not purge, while svc_c's purge runs
- {port: 7003, comment: "ansible-ufw[svc_a:aaaaaaaaaa]: kept"}
# svc_c: a stale rule that looks like an SSH rule, which a purge must skip
- {port: 22, from: 10.9.0.0/16, comment: "ansible-ufw[svc_c:bbbbbbbbbb]: old"}
# orphan_rules: svc_o1 and svc_o2 are never listed as managed owners in converge.yml.
# svc_o2's rule looks like the SSH rule and must survive a purge; svc_m is managed
# but its role is never called, so its rule must survive too.
- {port: 7401, comment: "ansible-ufw[svc_o1:1111111111]: orphan one"}
- {port: 7402, comment: "ansible-ufw[svc_o1:2222222222]: orphan two"}
- {port: 22, from: 10.9.1.0/24, comment: "ansible-ufw[svc_o2:3333333333]: orphan ssh lookalike"}
- {port: 7501, comment: "ansible-ufw[svc_m:4444444444]: managed, never called"}
# orphan_rules, owner svc_o3: tagged orphans that may carry SSH and must be kept by a
# purge: no port (interface only), no port (source only), a range spanning 22, an
# application profile named like SSH.
- {interface: tailscale0, direction: in, comment: "ansible-ufw[svc_o3:5555555551]: no port"}
- {from: 10.20.0.0/24, comment: "ansible-ufw[svc_o3:5555555552]: source only"}
- {port: "20:30", comment: "ansible-ufw[svc_o3:5555555553]: range over 22"}
- {name: OrphanOpenSSH, comment: "ansible-ufw[svc_o3:5555555554]: openssh profile"}
# svc_c again: 7601 is stale and deleted by its purge, the other three are stale and
# may carry SSH, so they stay.
- {port: 7601, comment: "ansible-ufw[svc_c:6666666664]: stale numeric"}
- {interface: wg0, direction: in, comment: "ansible-ufw[svc_c:6666666661]: no port"}
- {from: 10.30.0.0/24, comment: "ansible-ufw[svc_c:6666666662]: source only"}
- {port: "40:50", comment: "ansible-ufw[svc_c:6666666663]: range"}
loop_control:
loop_var: ufw_prepare_rule