Files
Kamino/roles/ufw/molecule/default/verify.yml
T
Defame1297andClaude Opus 5.5 10a620a2d9 feat(ufw): let ufw_rules entries delete a rule
An entry with delete: true (a boolean) removes the rule it describes,
passed to community.general.ufw's delete option. It lets a consumer
replace a base rule, such as an open SSH allow by narrower ones, which
the role could only add before.

- rules.yml applies the delete entries after every other rule, whatever
  their place in the list, so the replacement exists before the old rule
  goes.
- validate.yml: the schema check requires a boolean; a new check refuses
  a rule the list both adds and deletes (compared without the comment,
  port as a string), because the delete would win while the lock-out
  guard counted the add; the lock-out guard no longer counts a delete
  entry.
- service_rules keeps its own key list and does not accept delete.

Molecule: prepare stores a commented 9091 rule that the base list deletes
with an uncommented entry listed first; verify checks it is gone and the
untagged count is unchanged; the idempotence pass covers deleting a rule
that is gone. Two new refusals: an SSH rule that is only deleted (lock-out
guard) and a rule both added and deleted.

Verified locally: validate.yml against localhost in check mode (the
planned replacement list passes; the conflict, the delete-only SSH list
and a non-boolean delete are refused) and the apply order. Not verified
locally: the molecule scenario (CI only), including whether ufw matches
the commented rule from an uncommented delete.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
2026-11-15 22:12:16 +00:00

218 lines
11 KiB
YAML

---
- name: Verify
hosts: all
gather_facts: false
tasks:
- name: Read the firewall state
ansible.builtin.command: ufw status verbose
register: ufw_verify_ufw
changed_when: false
environment: {LC_ALL: C}
- name: UFW is active and denies incoming by default
ansible.builtin.assert:
that:
- "'Status: active' in ufw_verify_ufw.stdout"
- "'deny (incoming)' in ufw_verify_ufw.stdout"
quiet: true
- name: The SSH rule and the service rules were rendered
ansible.builtin.assert:
that:
- ufw_verify_ufw.stdout is search('22/tcp\s+ALLOW IN\s+Anywhere')
- ufw_verify_ufw.stdout is search('8080/tcp\s+ALLOW IN\s+10\.0\.0\.0/8')
- ufw_verify_ufw.stdout is search('53/udp\s+ALLOW IN\s+Anywhere')
- ufw_verify_ufw.stdout is search('9090\s+ALLOW IN\s+Anywhere')
quiet: true
- name: Read the rules ufw would load
ansible.builtin.command: ufw show added
register: ufw_verify_added
changed_when: false
environment: {LC_ALL: C}
# The four base rules plus the manual 7777 rule from prepare.yml (the base list deleted
# its 9091 rule). The refused runs
# (guard, invalid service inputs) added none.
- name: Exactly the five untagged rules exist
ansible.builtin.assert:
that: ufw_verify_untagged | length == 5
quiet: true
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.
- name: A stray *_ufw_rules variable did not reach the firewall
ansible.builtin.assert:
that:
- ufw_verify_added.stdout_lines | select('search', '7999') | list | length == 0
- ufw_verify_ufw.stdout is not search('7999')
quiet: true
- name: The rule marked delete in the base list is gone
ansible.builtin.assert:
that:
- ufw_verify_added.stdout_lines | select('search', '9091') | list | length == 0
- ufw_verify_ufw.stdout is not search('9091')
quiet: true
- name: The manual rule and the SSH rule were not touched
ansible.builtin.assert:
that:
- ufw_verify_untagged | select('search', "7777/tcp comment 'manual'") | list | length == 1
- ufw_verify_untagged | select('search', "22/tcp comment 'ssh'") | list | length == 1
quiet: true
vars:
ufw_verify_untagged: "{{ ufw_verify_added.stdout_lines | select('match', '^ufw ') | reject('search', 'ansible-ufw') | list }}"
# Keys are the first ten hex characters of the sha1 of
# rule|port|proto|from|to|interface|direction.
- name: The service rules carry their owner tag
ansible.builtin.assert:
that:
- ufw_verify_tagged | select('search', ufw_verify_svc_a_web) | list | length == 1
- 'ufw_verify_tagged | select("search", "8002.*ansible-ufw\[svc_a:[0-9a-f]{10}\]: alpha dns") | list | length == 1'
- 'ufw_verify_tagged | select("search", "8003.*ansible-ufw\[svc_b:[0-9a-f]{10}\]: beta") | list | length == 1'
- 'ufw_verify_tagged | select("search", "8004.*ansible-ufw\[svc_b:[0-9a-f]{10}\]:.$") | list | length == 1'
- 'ufw_verify_tagged | select("search", "7101.*ansible-ufw\[svc_c:[0-9a-f]{10}\]: gamma") | list | length == 1'
- 'ufw_verify_tagged | select("search", "7201.*ansible-ufw\[svc_e:[0-9a-f]{10}\]: epsilon") | list | length == 1'
- 'ufw_verify_tagged | select("search", "8022/tcp comment .ansible-ufw\[svc_g:[0-9a-f]{10}\]: forward to 22 here") | list | length == 1'
- 'ufw_verify_tagged | select("search", "7301.*ansible-ufw\[svc_h:[0-9a-f]{10}\]: eta") | list | length == 1'
quiet: true
vars:
ufw_verify_tagged: "{{ ufw_verify_added.stdout_lines | select('match', '^ufw ') | select('search', 'ansible-ufw') | list }}"
ufw_verify_key_a_web: "{{ ('allow|8001|tcp||||in' | hash('sha1'))[:10] }}"
ufw_verify_svc_a_web: "8001/tcp comment 'ansible-ufw\\[svc_a:{{ ufw_verify_key_a_web }}\\]: alpha web'"
- name: Stale rules were purged only where purging was on, and svc_d is gone
ansible.builtin.assert:
that:
- ufw_verify_added.stdout_lines | select('search', 'svc_c:128c086248') | list | length == 0
- ufw_verify_added.stdout_lines | select('search', 'svc_d:') | list | length == 0
- ufw_verify_added.stdout_lines | select('search', '7003.*svc_e:aaaaaaaaaa') | list | length == 1
- ufw_verify_added.stdout_lines | select('search', 'svc_h:bbbbbbbbbb') | list | length == 1
- ufw_verify_added.stdout_lines | select('search', 'ansible-ufw') | list | length == 20
quiet: true
# Twenty tagged rules: svc_a 2, svc_b 2, svc_c 1, svc_e 2, svc_g 1, svc_h 2, svc_m 1, the
# SSH lookalike of svc_o2, four orphans of svc_o3 and four rules of svc_p (its desired
# rule and three stale ones). Both rules of svc_o1 and the numeric stale rule of svc_p were
# deleted; the kept ones may carry SSH.
- name: Orphans were purged except those that may carry SSH, a managed owner kept its rule
ansible.builtin.assert:
that:
- ufw_verify_added.stdout_lines | select('search', 'svc_o1:') | list | length == 0
- ufw_verify_added.stdout_lines | select('search', '10\.9\.1\.0/24.* port 22 .*svc_o2:3333333333') | list | length == 1
- ufw_verify_added.stdout_lines | select('search', 'tailscale0.*svc_o3:5555555551') | list | length == 1
- ufw_verify_added.stdout_lines | select('search', '10\.20\.0\.0/24.*svc_o3:5555555552') | list | length == 1
- ufw_verify_added.stdout_lines | select('search', '20:30.*svc_o3:5555555553') | list | length == 1
- ufw_verify_added.stdout_lines | select('search', 'OrphanOpenSSH.*svc_o3:5555555554') | list | length == 1
- ufw_verify_added.stdout_lines | select('search', '7501.*svc_m:4444444444') | list | length == 1
quiet: true
- name: The stale purge of service_rules kept the lines that may carry SSH
ansible.builtin.assert:
that:
- ufw_verify_added.stdout_lines | select('search', 'svc_p:6666666664') | list | length == 0
- ufw_verify_added.stdout_lines | select('search', '7601') | list | length == 0
- ufw_verify_added.stdout_lines | select('search', 'wg0.*svc_p:6666666661') | list | length == 1
- ufw_verify_added.stdout_lines | select('search', '10\.30\.0\.0/24.*svc_p:6666666662') | list | length == 1
- ufw_verify_added.stdout_lines | select('search', '40:50.*svc_p:6666666663') | list | length == 1
- 'ufw_verify_added.stdout_lines | select("search", "7602.*ansible-ufw\\[svc_p:[0-9a-f]{10}\\]: desired") | list | length == 1'
quiet: true
- name: No rejected input left a rule behind
ansible.builtin.assert:
that:
- 'ufw_verify_added.stdout_lines | select("search", "( 8101| 8102| 8103)([ /]|$)|svc_f") | list | length == 0'
quiet: true
- name: A stray *_ufw_before_rules variable did not reach before.rules
ansible.builtin.command: grep -q 7998 /etc/ufw/before.rules
register: ufw_verify_stray_before
changed_when: false
failed_when: ufw_verify_stray_before.rc != 1
- name: The before.rules file keeps the loopback accept
ansible.builtin.command: grep -q -- '-A ufw-before-input -i lo -j ACCEPT' /etc/ufw/before.rules
changed_when: false
- name: The before.rules file drops INVALID-state input packets
ansible.builtin.command: grep -q -- '-A ufw-before-input -m conntrack --ctstate INVALID -j DROP' /etc/ufw/before.rules
changed_when: false
- name: The INVALID drop logs through ufw-logging-deny first
ansible.builtin.command: grep -q -- '-A ufw-before-input -m conntrack --ctstate INVALID -j ufw-logging-deny' /etc/ufw/before.rules
changed_when: false
- name: Read the before.rules input rules in order
ansible.builtin.command: grep -n -- '^-A ufw-before-input ' /etc/ufw/before.rules
register: ufw_verify_input_rules
changed_when: false
- name: The INVALID drop sits after the established accept, behind its logging rule, and before the ICMP accepts
ansible.builtin.assert:
that:
- ufw_verify_idx_established < ufw_verify_idx_logging
- ufw_verify_idx_logging < ufw_verify_idx_invalid
- ufw_verify_idx_invalid < ufw_verify_idx_icmp
quiet: true
vars:
ufw_verify_lines: "{{ ufw_verify_input_rules.stdout_lines }}"
ufw_verify_idx_established: "{{ ufw_verify_lines | select('search', '--ctstate RELATED,ESTABLISHED') | first | split(':') | first | int }}"
ufw_verify_idx_logging: "{{ ufw_verify_lines | select('search', '--ctstate INVALID -j ufw-logging-deny') | first | split(':') | first | int }}"
ufw_verify_idx_invalid: "{{ ufw_verify_lines | select('search', '--ctstate INVALID -j DROP') | first | split(':') | first | int }}"
ufw_verify_idx_icmp: "{{ ufw_verify_lines | select('search', '-p icmp') | first | split(':') | first | int }}"
# Check mode of the entry point. It lives in verify, not in converge: a check-mode
# apply of community.general.ufw reports changed for the would-be rule, so molecule's
# idempotence run (a second converge) would fail on it. Verify runs once, outside that count.
# The play runs under the check_mode keyword, which does not set ansible_check_mode in the
# included role; the role detects check mode with a probe task instead (a command task is
# skipped under the keyword). A present call that would add a rule and an absent call for an
# owner that holds rules must change nothing. The two reads of the rule list are forced to
# run (check_mode: false) and compared as a whole.
- name: Check mode of service_rules changes nothing
hosts: all
gather_facts: false
check_mode: true
tasks:
- name: Read the rules before
ansible.builtin.command: ufw show added
register: ufw_verify_chk_before
changed_when: false
check_mode: false
environment: {LC_ALL: C}
- name: A present call that would add a rule for a new owner
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_chk
ufw_service_rules:
- {rule: allow, port: 7701, proto: tcp, comment: check mode}
- name: An absent call for svc_a, which holds two rules
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: svc_a
ufw_service_state: absent
- name: Read the rules after
ansible.builtin.command: ufw show added
register: ufw_verify_chk_after
changed_when: false
check_mode: false
environment: {LC_ALL: C}
- name: Check mode added and deleted nothing
ansible.builtin.assert:
that:
- ufw_verify_chk_before.stdout_lines | select('search', 'svc_a:') | list | length == 2
- ufw_verify_chk_after.stdout_lines == ufw_verify_chk_before.stdout_lines
- ufw_verify_chk_after.stdout_lines | select('search', 'svc_chk|7701') | list | length == 0
quiet: true