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
94 lines
4.5 KiB
YAML
94 lines
4.5 KiB
YAML
---
|
|
- name: Validate ufw_rules schema
|
|
ansible.builtin.assert:
|
|
that:
|
|
- ufw_rule.rule is defined
|
|
- ufw_rule.rule in ['allow', 'deny', 'reject', 'limit']
|
|
- ufw_rule.port is not defined or ufw_rule.port | int > 0
|
|
- ufw_rule.proto is not defined or ufw_rule.proto in ['tcp', 'udp']
|
|
- ufw_rule.port is defined or ufw_rule.from is defined or ufw_rule.to is defined or ufw_rule.interface is defined
|
|
- ufw_rule.delete is not defined or ufw_rule.delete is boolean
|
|
fail_msg: "Invalid UFW rule: {{ ufw_rule }}"
|
|
loop: "{{ ufw_aggregated_rules | default([]) }}"
|
|
loop_control:
|
|
loop_var: ufw_rule
|
|
when:
|
|
- ufw_manage_rules
|
|
- ufw_aggregated_rules | length > 0
|
|
|
|
# A rule both added and deleted would be added, then deleted (deletes run last), and the
|
|
# lock-out guard below would still count the added one. Fields are normalised the way ufw
|
|
# stores them before comparing: no direction is in (incoming/outgoing are in/out), no from
|
|
# or to is any, the port is a string. The comment is not part of a rule's identity. A
|
|
# missing proto stays distinct from tcp, as ufw keeps the two as different rules.
|
|
- name: Refuse a rule that ufw_rules both adds and deletes
|
|
ansible.builtin.assert:
|
|
that: ufw_rules_conflicting | length == 0
|
|
fail_msg: >-
|
|
These UFW rules are both added and deleted in the same rule list (rule|port|proto|from|to|interface|direction):
|
|
{{ ufw_rules_conflicting }}. Remove one of the two entries.
|
|
vars:
|
|
ufw_rules_conflicting: >-
|
|
{%- set ns = namespace(added=[], deleted=[]) -%}
|
|
{%- for r in ufw_aggregated_rules | default([]) -%}
|
|
{%- set dir = r.direction | default('in', true) | string | lower -%}
|
|
{%- set spec = [r.rule | default(''), r.port | default('') | string, r.proto | default(''),
|
|
r.from | default('any', true), r.to | default('any', true), r.interface | default(''),
|
|
{'incoming': 'in', 'outgoing': 'out'}.get(dir, dir)] | join('|') -%}
|
|
{%- if r.delete | default(false) -%}
|
|
{%- set ns.deleted = ns.deleted + [spec] -%}
|
|
{%- else -%}
|
|
{%- set ns.added = ns.added + [spec] -%}
|
|
{%- endif -%}
|
|
{%- endfor -%}
|
|
{{ ns.deleted | select('in', ns.added) | unique | join(', ') }}
|
|
when:
|
|
- ufw_manage_rules
|
|
- ufw_aggregated_rules | length > 0
|
|
|
|
- name: Validate ufw before.rules schema
|
|
ansible.builtin.assert:
|
|
that:
|
|
- ufw_before_rule.port is not defined or ufw_before_rule.port | int > 0
|
|
- ufw_before_rule.proto is not defined or ufw_before_rule.proto in ['tcp', 'udp']
|
|
- ufw_before_rule.interface is not defined or ufw_before_rule.interface | length > 0
|
|
- ufw_before_rule.port is defined or ufw_before_rule.from is defined or ufw_before_rule.to is defined or ufw_before_rule.interface is defined
|
|
fail_msg: "Invalid UFW before-rule: {{ ufw_before_rule }}"
|
|
loop: "{{ ufw_aggregated_before_rules | default([]) }}"
|
|
loop_control:
|
|
loop_var: ufw_before_rule
|
|
when: ufw_aggregated_before_rules | length > 0
|
|
|
|
# defaults.yml sets the deny policy before rules.yml adds any rule, so a missing
|
|
# SSH rule would lock the host out mid-play. Checks the merged list only; it does
|
|
# not care which variable a rule came from. A missing proto means both protocols
|
|
# and a missing direction means incoming, as in ufw itself. Any `from`, `to` or
|
|
# `interface` restriction still counts: the guard catches a forgotten rule, it
|
|
# cannot know which source address the control host connects from. A rule marked
|
|
# delete: true removes a rule, so it never counts.
|
|
- name: Require an SSH allow rule when incoming traffic is denied by default
|
|
ansible.builtin.assert:
|
|
that: ufw_ssh_rule_count | int > 0
|
|
fail_msg: >-
|
|
No allow/limit rule for TCP port {{ ufw_ssh_port }} (SSH) in the UFW rules, and the
|
|
incoming default is {{ ufw_defaults.incoming }}: enabling it would lock this host out.
|
|
Add the rule, or set ufw_require_ssh_allow: false if SSH is reached another way.
|
|
vars:
|
|
ufw_ssh_rule_count: >-
|
|
{%- set ns = namespace(n=0) -%}
|
|
{%- for r in ufw_aggregated_rules | default([]) -%}
|
|
{%- if r.rule | default('') in ['allow', 'limit']
|
|
and r.port | default('') | string == ufw_ssh_port | string
|
|
and r.proto | default('tcp') == 'tcp'
|
|
and r.direction | default('in') == 'in'
|
|
and not r.delete | default(false) -%}
|
|
{%- set ns.n = ns.n + 1 -%}
|
|
{%- endif -%}
|
|
{%- endfor -%}
|
|
{{ ns.n }}
|
|
when:
|
|
- ufw_enabled
|
|
- ufw_manage_rules
|
|
- ufw_require_ssh_allow
|
|
- ufw_defaults.incoming in ['deny', 'reject']
|