feat(ufw): add the ufw role
Copied from the environment repository's local role. The files are identical except: galaxy namespace mortis; comments, argument_specs descriptions and the README scrubbed of environment references (issue numbers, CI runs, playbooks, inventory paths and groups, scripts and caller roles by name), with the technical explanation kept and reworded generically; the stray-variable guard's fail message drops its issue reference; the molecule scenario calls the role by its FQCN (mortis.kamino.ufw), follows the Kamino scenario conventions (kamino-molecule header, scenario.test_sequence) and no longer sets ANSIBLE_COLLECTIONS_PATH or ANSIBLE_ROLES_PATH. No variable, default, argument_specs type or task logic changed; the test coverage is the same. The README follows the other Kamino roles: purpose, the main, service_rules and orphan_rules entry points, variables, and example plays with the FQCN and tasks_from. The meta licence stays GPL-3.0-or-later AND GPL-3.0-only (templates/before.rules.j2 is derived from ufw's own file, already annotated in REUSE.toml and NOTICE). galaxy.yml's dependency comment names ufw as a community.general user. Not verified locally: the molecule scenario (it runs on the kamino-molecule runner in CI) and ansible-test sanity. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
7fa6c6754d
commit
e18db424ec
25 files changed
+2271
-1
No files matched your search
@@ -0,0 +1,448 @@
|
||||
---
|
||||
# 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
|
||||
|
||||
# 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:
|
||||
- {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
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404 image
|
||||
# (MOLECULE_IMAGE, the pin in .gitea/workflows/molecule.yml). No privileged mode: systemd needs a
|
||||
# writable cgroup and the firewall tools need NET_ADMIN, nothing else.
|
||||
driver:
|
||||
name: docker
|
||||
platforms:
|
||||
- name: ufw-instance
|
||||
image: ${MOLECULE_IMAGE}
|
||||
pre_build_image: true
|
||||
pull: true
|
||||
command: /sbin/init
|
||||
cgroupns_mode: host
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
tmpfs:
|
||||
- /run
|
||||
- /run/lock
|
||||
capabilities:
|
||||
- NET_ADMIN
|
||||
security_opts:
|
||||
- apparmor=unconfined
|
||||
provisioner:
|
||||
name: ansible
|
||||
verifier:
|
||||
name: ansible
|
||||
scenario:
|
||||
# molecule's default sequence minus its first destroy: the workflow removes what a killed run
|
||||
# left before any scenario starts, so that destroy only waits out the
|
||||
# driver's 5 s poll on a container that is not there. After an aborted local run, run
|
||||
# `molecule destroy` yourself.
|
||||
test_sequence:
|
||||
- dependency
|
||||
- cleanup
|
||||
- syntax
|
||||
- create
|
||||
- prepare
|
||||
- converge
|
||||
- idempotence
|
||||
- side_effect
|
||||
- verify
|
||||
- cleanup
|
||||
- destroy
|
||||
@@ -0,0 +1,74 @@
|
||||
---
|
||||
# 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: mortis.kamino.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
|
||||
|
||||
# Owners svc_c, svc_d and svc_e keep rules in converge.yml that these entries are
|
||||
# not part of: a stale rule to purge, a rule of a removed service, a stale rule
|
||||
# that is only reported.
|
||||
- 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"}
|
||||
- {port: 7003, comment: "ansible-ufw[svc_e:aaaaaaaaaa]: kept"}
|
||||
# svc_h: 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_h: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"}
|
||||
# service_rules purge of svc_p (converge.yml keeps only a rule for 7602): 7601 is stale
|
||||
# and deleted, the other three are stale and may carry SSH, so they stay.
|
||||
- {port: 7601, comment: "ansible-ufw[svc_p:6666666664]: stale numeric"}
|
||||
- {interface: wg0, direction: in, comment: "ansible-ufw[svc_p:6666666661]: no port"}
|
||||
- {from: 10.30.0.0/24, comment: "ansible-ufw[svc_p:6666666662]: source only"}
|
||||
- {port: "40:50", comment: "ansible-ufw[svc_p:6666666663]: range"}
|
||||
loop_control:
|
||||
loop_var: ufw_prepare_rule
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
# Included by converge.yml after each orphan_rules case. The counts are absolute: svc_o1
|
||||
# holds ufw_conv_o1_want rules (default ufw_conv_o1_initial: 2 before the purge, 0 after),
|
||||
# and the tagged total is 20 plus those. The 20: svc_a 2, svc_b 2, svc_c 1, svc_e 2, svc_g 1,
|
||||
# svc_h 2, svc_m 1, svc_o2 1, svc_o3 4 (kept orphans that may carry SSH), svc_p 4 (its
|
||||
# desired rule and three kept stale rules).
|
||||
- name: Read the rules ufw would load
|
||||
ansible.builtin.command: ufw show added
|
||||
register: ufw_conv_added
|
||||
changed_when: false
|
||||
environment: {LC_ALL: C}
|
||||
|
||||
- name: The seeds are as this case leaves them
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- ufw_conv_added.stdout_lines | select('search', 'svc_o1:') | list | length == ufw_conv_o1_count | int
|
||||
- ufw_conv_added.stdout_lines | select('search', 'svc_o2:3333333333') | list | length == 1
|
||||
- ufw_conv_added.stdout_lines | select('search', 'svc_o3:555555555[1-4]') | list | length == 4
|
||||
- ufw_conv_added.stdout_lines | select('search', 'svc_m:4444444444') | list | length == 1
|
||||
- ufw_conv_added.stdout_lines | select('search', 'svc_p:666666666[123]') | list | length == 3
|
||||
- ufw_conv_added.stdout_lines | select('search', 'svc_p:6666666664') | list | length == 0
|
||||
- ufw_conv_added.stdout_lines | select('match', '^ufw ') | select('search', 'ansible-ufw') | list | length == 20 + ufw_conv_o1_count | int
|
||||
- ufw_conv_added.stdout_lines | select('match', '^ufw ') | reject('search', 'ansible-ufw') | list | length == 5
|
||||
fail_msg: "Unexpected rules after the orphan_rules case: {{ ufw_conv_added.stdout_lines }}"
|
||||
quiet: true
|
||||
vars:
|
||||
ufw_conv_o1_count: "{{ ufw_conv_o1_want | default(ufw_conv_o1_initial) }}"
|
||||
@@ -0,0 +1,209 @@
|
||||
---
|
||||
- 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 rule from prepare.yml. 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 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
|
||||
Reference in new issue
Block a user