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
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
656 lines
28 KiB
YAML
656 lines
28 KiB
YAML
---
|
|
# The refusal cases run first, so every assertion below also proves a refused run left the
|
|
# host unchanged. They are not in converge.yml because idempotence reruns it.
|
|
|
|
# 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
|
|
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_verify_failure: >-
|
|
{{ ansible_failed_result.msg | default('') }}
|
|
{{ ansible_failed_result.results | default([]) | map(attribute='msg', default='') | join(' ') }}
|
|
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
|
|
|
|
# ufw's parser refuses a quote in a comment; the check must stop the play before any rule is applied.
|
|
- name: A single quote in a base rule comment
|
|
block:
|
|
- name: Include ufw
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
vars:
|
|
ufw_rules:
|
|
- {rule: allow, port: 22, proto: tcp, comment: ssh}
|
|
- {rule: allow, port: 8080, proto: tcp, comment: "it's"}
|
|
- name: Unreachable when base rule comments are checked
|
|
ansible.builtin.fail:
|
|
msg: a quote in a base rule comment was accepted
|
|
rescue:
|
|
- name: The comment check stopped the play
|
|
ansible.builtin.assert:
|
|
that: "'single quote' in ufw_verify_failure"
|
|
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
|
|
|
|
# Inputs the entry point must refuse, each before it changes anything. Every block
|
|
# proves the refusal and its message; the Verify play 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_verify_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_verify_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_verify_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_verify_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_verify_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_verify_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_verify_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_verify_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_verify_failure"
|
|
quiet: true
|
|
|
|
# An orphan_rules input the entry point must refuse before it reads or deletes a rule.
|
|
- name: Refuse an invalid managed owner
|
|
hosts: all
|
|
gather_facts: false
|
|
vars:
|
|
ufw_verify_failure: >-
|
|
{{ ansible_failed_result.msg | default('') }}
|
|
{{ ansible_failed_result.results | default([]) | map(attribute='msg', default='') | join(' ') }}
|
|
tasks:
|
|
- 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_verify_failure"
|
|
quiet: true
|
|
|
|
- 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 }}"
|
|
|
|
# The refusal play 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/tcp') | list | length == 0
|
|
- ufw_verify_ufw.stdout is not search('9091/tcp')
|
|
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_a:[0-9a-f]{10}\]: beta") | list | length == 1'
|
|
- 'ufw_verify_tagged | select("search", "8004.*ansible-ufw\[svc_a:[0-9a-f]{10}\]:.$") | list | length == 1'
|
|
- 'ufw_verify_tagged | select("search", "7201.*ansible-ufw\[svc_a:[0-9a-f]{10}\]: epsilon") | list | length == 1'
|
|
- 'ufw_verify_tagged | select("search", "8022/tcp comment .ansible-ufw\[svc_a:[0-9a-f]{10}\]: forward to 22 here") | 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", "7301.*ansible-ufw\[svc_c:[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_a:aaaaaaaaaa') | list | length == 1
|
|
- ufw_verify_added.stdout_lines | select('search', 'svc_c:bbbbbbbbbb') | list | length == 1
|
|
- ufw_verify_added.stdout_lines | select('search', 'ansible-ufw') | list | length == 20
|
|
quiet: true
|
|
|
|
# Twenty tagged rules: svc_a 7 (six desired, one stale), svc_c 7 (three desired, four stale),
|
|
# svc_m 1, the SSH lookalike of svc_o2 and four orphans of svc_o3. Both rules of svc_o1 and
|
|
# the numeric stale rules of svc_c 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_c:6666666664') | list | length == 0
|
|
- ufw_verify_added.stdout_lines | select('search', '7601') | list | length == 0
|
|
- ufw_verify_added.stdout_lines | select('search', 'wg0.*svc_c:6666666661') | list | length == 1
|
|
- ufw_verify_added.stdout_lines | select('search', '10\.30\.0\.0/24.*svc_c:6666666662') | list | length == 1
|
|
- ufw_verify_added.stdout_lines | select('search', '40:50.*svc_c:6666666663') | list | length == 1
|
|
- 'ufw_verify_added.stdout_lines | select("search", "7602.*ansible-ufw\\[svc_c:[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 does
|
|
# not run 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 seven 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 == 7
|
|
- 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
|
|
|
|
# prepare.yml left nftables.service enabled and active. The package's postinst runs
|
|
# `deb-systemd-invoke try-restart nftables.service` on upgrade (and on a reinstall, which passes
|
|
# the old version too); the stock config and ExecStop flush the ruleset. The masked case runs
|
|
# first, then a control without the mask proves the reinstall reaches the unit here, then the
|
|
# absent-package case. Last, because the control flushes ufw's rules.
|
|
- name: The nftables mask keeps a package upgrade from flushing the rules
|
|
hosts: all
|
|
gather_facts: false
|
|
vars:
|
|
ufw_verify_nft_reinstall:
|
|
- apt-get
|
|
- install
|
|
- --reinstall
|
|
- -y
|
|
- -o
|
|
- Dpkg::Options::=--force-confold
|
|
- nftables
|
|
tasks:
|
|
# A mask request in check mode changes nothing and returns the unit's properties.
|
|
- name: Read the state of nftables.service
|
|
ansible.builtin.systemd_service:
|
|
name: nftables.service
|
|
masked: true
|
|
check_mode: true
|
|
register: ufw_verify_nft_unit
|
|
|
|
- name: The role masked the unit and did not stop it
|
|
ansible.builtin.assert:
|
|
that:
|
|
- ufw_verify_nft_unit is not changed
|
|
- ufw_verify_nft_unit.status.LoadState == 'masked'
|
|
- ufw_verify_nft_unit.status.ActiveState == 'active'
|
|
quiet: true
|
|
|
|
- name: Reinstall nftables while the unit is masked
|
|
ansible.builtin.command:
|
|
argv: "{{ ufw_verify_nft_reinstall }}"
|
|
environment: {DEBIAN_FRONTEND: noninteractive}
|
|
changed_when: true
|
|
|
|
- name: Read the iptables rules after the masked reinstall
|
|
ansible.builtin.command: iptables -S
|
|
register: ufw_verify_nft_masked_rules
|
|
changed_when: false
|
|
|
|
- name: The rules of ufw survived the reinstall
|
|
ansible.builtin.assert:
|
|
that:
|
|
- ufw_verify_nft_masked_rules.stdout_lines | select('search', '^-N ufw-user-input$') | list | length == 1
|
|
- ufw_verify_nft_masked_rules.stdout_lines | select('search', '^-A ufw-user-input .*--dport 22 ') | list | length > 0
|
|
quiet: true
|
|
|
|
- name: Remove the mask for the control case
|
|
ansible.builtin.systemd_service:
|
|
name: nftables.service
|
|
masked: false
|
|
|
|
- name: Reinstall nftables while the unit is enabled and active
|
|
ansible.builtin.command:
|
|
argv: "{{ ufw_verify_nft_reinstall }}"
|
|
environment: {DEBIAN_FRONTEND: noninteractive}
|
|
changed_when: true
|
|
|
|
- name: Read the iptables rules after the unmasked reinstall
|
|
ansible.builtin.command: iptables -S
|
|
register: ufw_verify_nft_unmasked_rules
|
|
changed_when: false
|
|
|
|
- name: Without the mask the reinstall flushed the rules of ufw
|
|
ansible.builtin.assert:
|
|
that: ufw_verify_nft_unmasked_rules.stdout_lines | select('search', '^-N ufw-user-input$') | list | length == 0
|
|
quiet: true
|
|
|
|
- name: Purge nftables
|
|
ansible.builtin.apt:
|
|
name: nftables
|
|
state: absent
|
|
purge: true
|
|
|
|
- name: Read the state of nftables.service with the package gone
|
|
ansible.builtin.systemd_service:
|
|
name: nftables.service
|
|
masked: true
|
|
check_mode: true
|
|
register: ufw_verify_nft_gone
|
|
|
|
- name: Run the role on a host without the nftables package
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
vars:
|
|
ufw_rules:
|
|
- {rule: allow, port: 22, proto: tcp, comment: ssh}
|
|
|
|
- name: Read the state of nftables.service after the role
|
|
ansible.builtin.systemd_service:
|
|
name: nftables.service
|
|
masked: true
|
|
check_mode: true
|
|
register: ufw_verify_nft_absent
|
|
|
|
- name: The role masked the absent unit without failing
|
|
ansible.builtin.assert:
|
|
that:
|
|
- ufw_verify_nft_gone is changed
|
|
- ufw_verify_nft_absent is not changed
|
|
quiet: true
|