feat(ufw): let ufw_rules entries delete a rule #200

Merged
Defame1297 merged 3 commits from feat/ufw-rule-delete into main 2026-11-15 22:44:05 +00:00
Owner

An entry of ufw_rules may now carry delete: true (a boolean) to remove the rule it describes, through community.general.ufw's delete option. Until now the role could only add rules, so a consumer could not replace a base rule, for example narrow an open allow 22/tcp to an address range and an interface.

Behaviour

  • tasks/rules.yml applies delete entries after every other rule, whatever their place in the list, so a replacement exists before the rule it replaces goes.
  • A delete is sent without its comment. ufw matches a delete with no comment against the rule whatever its comment; a delete with a different comment matches nothing and exits 0, which would leave the rule silently.
  • tasks/validate.yml:
    • the schema check requires delete to be a boolean;
    • a new check refuses a rule that the list both adds and deletes. Fields are normalised as ufw stores them (no direction is in, incoming/outgoing are in/out, no from/to is any, port as a string); a missing proto stays distinct from tcp, as in ufw;
    • the SSH lock-out guard no longer counts a delete entry.
  • service_rules keeps its own key list and refuses delete; it already purges its own stale rules.
  • README: a delete without an address removes the IPv4 and IPv6 rule; an IPv4 from allows IPv4 only.

Tests

Molecule: prepare stores a 9091/tcp rule with a comment; the base list deletes it with an entry that has a different comment and is listed first; verify checks it is gone and the untagged count is unchanged; the idempotence pass covers deleting a rule that is already gone. Two new refusals: an SSH rule that is only deleted (lock-out guard) and a rule both added and deleted, written with explicit direction: in / from: any.

Verified

  • All pre-commit hooks and the pre-push ansible-test sanity.
  • validate.yml against localhost in check mode: a sample replacement list (an SSH allow from a source range, an SSH allow on a named interface, and a delete of plain allow 22/tcp) passes; the conflicts (plain, direction: in, from: any, direction: incoming), a delete-only SSH list and a non-boolean delete are refused; the apply order puts deletes last.
  • A subagent review. It found the two bugs fixed in the second commit: the conflict check missed direction: in / from: any spellings, and a delete with a differing comment did nothing. It read ufw 0.36.2's matching code for the comment behaviour.

Not verified

  • The molecule scenario (CI only), including the commented-rule delete on a real ufw.

🤖 Generated with Claude Code

An entry of `ufw_rules` may now carry `delete: true` (a boolean) to remove the rule it describes, through `community.general.ufw`'s `delete` option. Until now the role could only add rules, so a consumer could not replace a base rule, for example narrow an open `allow 22/tcp` to an address range and an interface. ## Behaviour - `tasks/rules.yml` applies delete entries after every other rule, whatever their place in the list, so a replacement exists before the rule it replaces goes. - A delete is sent without its comment. ufw matches a delete with no comment against the rule whatever its comment; a delete with a different comment matches nothing and exits 0, which would leave the rule silently. - `tasks/validate.yml`: - the schema check requires `delete` to be a boolean; - a new check refuses a rule that the list both adds and deletes. Fields are normalised as ufw stores them (no `direction` is `in`, `incoming`/`outgoing` are `in`/`out`, no `from`/`to` is `any`, port as a string); a missing `proto` stays distinct from `tcp`, as in ufw; - the SSH lock-out guard no longer counts a delete entry. - `service_rules` keeps its own key list and refuses `delete`; it already purges its own stale rules. - README: a delete without an address removes the IPv4 and IPv6 rule; an IPv4 `from` allows IPv4 only. ## Tests Molecule: prepare stores a `9091/tcp` rule with a comment; the base list deletes it with an entry that has a different comment and is listed first; verify checks it is gone and the untagged count is unchanged; the idempotence pass covers deleting a rule that is already gone. Two new refusals: an SSH rule that is only deleted (lock-out guard) and a rule both added and deleted, written with explicit `direction: in` / `from: any`. ## Verified - All pre-commit hooks and the pre-push `ansible-test sanity`. - `validate.yml` against localhost in check mode: a sample replacement list (an SSH allow from a source range, an SSH allow on a named interface, and a delete of plain `allow 22/tcp`) passes; the conflicts (plain, `direction: in`, `from: any`, `direction: incoming`), a delete-only SSH list and a non-boolean `delete` are refused; the apply order puts deletes last. - A subagent review. It found the two bugs fixed in the second commit: the conflict check missed `direction: in` / `from: any` spellings, and a delete with a differing comment did nothing. It read ufw 0.36.2's matching code for the comment behaviour. ## Not verified - The molecule scenario (CI only), including the commented-rule delete on a real ufw. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Claude added 2 commits 2026-11-15 22:27:40 +00:00
An entry with delete: true (a boolean) removes the rule it describes,
passed to community.general.ufw's delete option. It lets a consumer
replace a base rule, such as an open SSH allow by narrower ones, which
the role could only add before.

- rules.yml applies the delete entries after every other rule, whatever
  their place in the list, so the replacement exists before the old rule
  goes.
- validate.yml: the schema check requires a boolean; a new check refuses
  a rule the list both adds and deletes (compared without the comment,
  port as a string), because the delete would win while the lock-out
  guard counted the add; the lock-out guard no longer counts a delete
  entry.
- service_rules keeps its own key list and does not accept delete.

Molecule: prepare stores a commented 9091 rule that the base list deletes
with an uncommented entry listed first; verify checks it is gone and the
untagged count is unchanged; the idempotence pass covers deleting a rule
that is gone. Two new refusals: an SSH rule that is only deleted (lock-out
guard) and a rule both added and deleted.

Verified locally: validate.yml against localhost in check mode (the
planned replacement list passes; the conflict, the delete-only SSH list
and a non-boolean delete are refused) and the apply order. Not verified
locally: the molecule scenario (CI only), including whether ufw matches
the commented rule from an uncommented delete.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
fix(ufw): normalise the conflict check and send deletes without a comment
Build / build (pull_request) Successful in 12s
Lint / lint (pull_request) Successful in 28s
Molecule / molecule (pull_request) Canceled after 1m25s
43bb2ce8dc
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
Defame1297 added 1 commit 2026-11-15 22:31:04 +00:00
Merge branch 'main' into feat/ufw-rule-delete
Build / build (pull_request) Successful in 11s
Lint / lint (pull_request) Successful in 30s
Molecule / molecule (pull_request) Successful in 2m38s
14a9a72ea2
Defame1297 scheduled this pull request to auto merge when all checks succeed 2026-11-15 22:31:25 +00:00
Defame1297 merged commit a4e80c1864 into main 2026-11-15 22:44:05 +00:00
Defame1297 deleted branch feat/ufw-rule-delete 2026-11-15 22:44:05 +00:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#200