Review findings on the delete support: - The both-added-and-deleted check compared the written fields, so direction: in or from: any on the added rule hid a conflict that ufw sees (it stores no direction as in and no address as any); the delete then removed the SSH rule the lock-out guard had counted. Fields are now normalised as ufw stores them (incoming/outgoing as in/out too). A missing proto stays distinct from tcp, as in ufw. - A delete sent with a comment that differs from the stored one matches nothing in ufw and exits 0, so the rule stayed silently. Deletes are now sent without their comment; ufw then matches the rule whatever its comment. The scenario's delete entry carries a different comment to cover it, and the conflict refusal uses the explicit defaults. - README: a delete without an address removes the IPv4 and IPv6 rules, an IPv4 from allows IPv4 only. - verify searches 9091/tcp, not a bare 9091 that an owner tag could hold. Verified locally in check mode against localhost: both bypass lists and an incoming/outgoing spelling are refused; the planned replacement list and a no-proto add with a tcp delete pass. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
493 lines
19 KiB
YAML
493 lines
19 KiB
YAML
---
|
|
# 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
|
|
|
|
- name: Converge with an SSH rule and service rules
|
|
hosts: all
|
|
gather_facts: true
|
|
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_c,
|
|
# a rule of the removed service svc_d and a stale rule for svc_e, so every pass here
|
|
# is a no-op after the first one, purge included.
|
|
- name: Converge the rules of several services through the entry point
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Two rules for svc_a
|
|
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}
|
|
|
|
- name: Two rules for svc_b, one without a comment
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_b
|
|
ufw_service_rules:
|
|
- {rule: allow, port: 8003, proto: tcp, comment: beta}
|
|
- {rule: deny, port: 8004}
|
|
|
|
- name: Keep one rule for svc_c and purge its stale rule
|
|
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}
|
|
|
|
- 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
|
|
|
|
- name: Keep the stale rule of svc_e because purging is off
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_e
|
|
ufw_service_rules:
|
|
- {rule: allow, port: 7201, proto: tcp, comment: epsilon}
|
|
|
|
- name: A comment that mentions the SSH port on another port
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_g
|
|
ufw_service_rules:
|
|
- {rule: allow, port: 8022, proto: tcp, comment: forward to 22 here}
|
|
|
|
- name: Purge svc_h without touching its stale rule that looks like the SSH rule
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_h
|
|
ufw_purge_stale: true
|
|
ufw_service_rules:
|
|
- {rule: allow, port: 7301, proto: tcp, comment: eta}
|
|
|
|
- name: Purge svc_p but keep its stale rules that may carry SSH
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
# prepare.yml stored four stale rules for svc_p: a numeric port (deleted), and a rule
|
|
# with no port, a source-only rule and a range, which are kept as possible SSH paths.
|
|
- name: Keep one rule for svc_p and purge its stale rules
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_p
|
|
ufw_purge_stale: true
|
|
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
|
|
# orphans. The cases that must change nothing run first and the purge last, so the
|
|
# idempotence run finds nothing left to delete. Check mode is not exercised: whether it
|
|
# reaches the tasks of an include_role is not verified.
|
|
- name: Report and purge the rules of unmanaged owners
|
|
hosts: all
|
|
gather_facts: false
|
|
vars:
|
|
ufw_converge_managed:
|
|
- svc_a
|
|
- svc_b
|
|
- svc_c
|
|
- svc_d
|
|
- svc_e
|
|
- svc_g
|
|
- 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.
|
|
- 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: Assert it before the first case
|
|
ansible.builtin.include_tasks: tasks/orphan_check.yml
|
|
|
|
- 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: Nothing was deleted without a list
|
|
ansible.builtin.include_tasks: tasks/orphan_check.yml
|
|
|
|
- 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: 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
|
|
tasks_from: orphan_rules
|
|
vars:
|
|
ufw_managed_owners: "{{ ufw_converge_managed }}"
|
|
ufw_purge_orphans: false
|
|
|
|
- name: Reporting 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
|