Files
Kamino/roles/ufw/tasks/validate.yml
T
Defame1297 166e0eb3e0
Build / build (pull_request) Successful in 33s
Lint / renovate-config (pull_request) Successful in 11s
Molecule / select (pull_request) Successful in 10s
Lint / lint (pull_request) Successful in 2m32s
Molecule / scenarios (pull_request) Successful in 13m34s
Molecule / Result (pull_request) Successful in 1s
docs(ufw): describe the base rule comment check where readers look
Name the single-quote limit in the README's schema note, give the new
verify case a lead comment like its neighbours, and say why ufw refuses
the quote in the validate.yml comment. The fragment now says the check
runs before any rule is applied, since install and sysctl run earlier.

Refs: #221
Assisted-by: Claude Code
2026-12-06 21:53:58 +00:00

110 lines
5.2 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
# The module wraps the comment in unescaped single quotes, which ufw's parser refuses, so the rules
# loop would fail part-way. Delete entries are skipped: rules.yml never sends their comment.
- name: Validate the ufw_rules comments
ansible.builtin.assert:
that: "\"'\" not in (ufw_rule.comment | default('', true) | string)"
fail_msg: >-
UFW rule comment contains a single quote, which the ufw module cannot pass to ufw: {{ ufw_rule }}
quiet: true
loop: "{{ ufw_aggregated_rules | default([]) }}"
loop_control:
loop_var: ufw_rule
when:
- ufw_manage_rules
- ufw_aggregated_rules | length > 0
- not ufw_rule.delete | default(false)
# 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']