RouterOS 005: Firewall rule ordering convention #67

Closed
opened 2026-11-14 01:41:04 +00:00 by ginjiruu · 3 comments
Owner

Part of #43

Refs: lab/iac#45
Migrated from router-as-code tracker 005 (docs/routeros/tracker in this repo history). Research notes: docs/routeros/notes/.

Ordered menus (firewall filter/nat/mangle, bridge filter) are the one place
where Terraform's object-set model and RouterOS's ordered-list model grind.
Decide the lab convention before writing the first rule, because it also
constrains the boot-script compiler (#007). Findings in note §2, verified in
provider source.

The tools and their teeth

  • place_before: real attribute on those resources, but ForceNew + write-only
    • "does not work as you think" per its own description; intended to be used
      with the firewall data source. Reordering churn = rule destroy/create, and a
      created-into-position rule that later gets a reordered neighbour re-creates.
  • routeros_move_items: reorders existing items by ID sequence; read-back
    carries an author TODO warning it misses cases. Treat as a janitor, not the
    mechanism.

Candidate conventions

  1. place_before chains off a data source (provider-native, churn-prone).
  2. One address-list/prefix-per-purpose + coarse rule count: order matters less
    when chains are small and purpose-built.
  3. Numbered comments (comment="010-allow-wireguard-mgmt") as the human-order
    intent, rules otherwise independent; compiler (#007) sorts by the number and
    emits in that order — order guaranteed at boot, and day-2 edits to rule
    bodies don't reorder anything.

Default lean: 3 for the compiler's determinism plus 2 to keep chains small;
decide hands-on in sim (#001) with a 15-rule chain that must be reordered, and
record the winner as an ADR-lite section here.

Done when

  • Convention chosen, demonstrated in sim (reorder costs nothing surprising on
    live apply), written into the terraform tree's README.
Part of #43 Refs: lab/iac#45 Migrated from router-as-code tracker 005 (docs/routeros/tracker in this repo history). Research notes: docs/routeros/notes/. Ordered menus (firewall filter/nat/mangle, bridge filter) are the one place where Terraform's object-set model and RouterOS's ordered-list model grind. Decide the lab convention before writing the first rule, because it also constrains the boot-script compiler (#007). Findings in note §2, verified in provider source. ## The tools and their teeth - `place_before`: real attribute on those resources, but ForceNew + write-only + "does not work as you think" per its own description; intended to be used with the firewall data source. Reordering churn = rule destroy/create, and a created-into-position rule that later gets a reordered neighbour re-creates. - `routeros_move_items`: reorders existing items by ID sequence; read-back carries an author TODO warning it misses cases. Treat as a janitor, not the mechanism. ## Candidate conventions 1. `place_before` chains off a data source (provider-native, churn-prone). 2. One address-list/prefix-per-purpose + coarse rule count: order matters less when chains are small and purpose-built. 3. Numbered comments (`comment="010-allow-wireguard-mgmt"`) as the human-order intent, rules otherwise independent; compiler (#007) sorts by the number and emits in that order — order guaranteed at boot, and day-2 edits to rule bodies don't reorder anything. Default lean: 3 for the compiler's determinism plus 2 to keep chains small; decide hands-on in sim (#001) with a 15-rule chain that must be reordered, and record the winner as an ADR-lite section here. ## Done when - Convention chosen, demonstrated in sim (reorder costs nothing surprising on live apply), written into the terraform tree's README.
Author
Owner

Status 2026-11-17: the renderer emits ordered firewall chains and the gate enforces ordering on every render (24-rule chain loaded rb5009-a; sim proved .ids survive backup load, #74). BUT done-when wants the convention written into the tree README - docs/routeros/README.md currently only carries the issue-map table row, no convention section. Review: adopt what the renderer already does and document it, or leave for #71/#72 work. Marking ready-for-human.

Status 2026-11-17: the renderer emits ordered firewall chains and the gate enforces ordering on every render (24-rule chain loaded rb5009-a; sim proved .ids survive backup load, #74). BUT done-when wants the convention written into the tree README - docs/routeros/README.md currently only carries the issue-map table row, no convention section. Review: adopt what the renderer already does and document it, or leave for #71/#72 work. Marking ready-for-human.
Author
Owner

Read akpw/mktxp in full (HEAD 6c2311d). Premise correction: mktxp is not a Go apply CLI and has no terraform-like write path. It is a Python exporter/GitOps toolkit whose rsc module does live /export -> AST -> normalize -> format/split, device-to-git only (mktxp/rsc/dispatcher.py:36-40 registers exactly two actions; no apply, no state, no router writes in the tree). It did not solve ordering on apply; it dodged the problem by never applying anything and treats the device's order as the record. That is itself data here: nobody in this niche has made placement work, and the surviving tools all treat export order as load order.

What mktxp does encode, worth borrowing:

  1. Ordered-menu whitelist as a constant: /ip(v6) firewall {filter,nat,mangle,raw}, /ip(v6) route, /routing rule, /interface bridge port, /interface bridge vlan, /ip dhcp-server lease. Inside those, top-down order is copied, never sorted; every other menu is alphabetized by a fixed key ladder (target/find-expr, list+interface, name, interface, address) for zero-jitter diffs (rsc/middleware/sorter.py:19-78, pinned by tests/rsc/test_sorter.py).
  2. Canonical inter-menu order via numbered domains, 01-base ... 06-firewall ... 99-other, longest-prefix path match, user-extendable in config (rsc/handlers/chain.py:21-170); filename numbers ARE load order.
  3. Comments are opaque: comment= is never a sort key or placement input anywhere. No numbered-comment idiom exists there, so candidate 3 in this issue remains our own invention (mktxp-compatible, but not validated by it).
  4. Nothing is silently dropped: unknown paths go to 99-other.rsc, volatile fields (bridge mac-address under auto-mac=yes) are sanitized (rsc/middleware/sanitizer.py:26-37).

The convention, now written into docs/routeros/README.md by PR #134, absorbing the above:

  • Declared order = device order = render order. No place_before anywhere. Ordered menus are mktxp's list plus /interface bridge filter (their list misses it; the provider places there).
  • Rule comments stay "<chain>: <purpose> [tf:<key>]", comma-free (REST import-key constraint). Step-10 numbers enter with #69: the compiler is the first tool needing a total order from planned_values alone; until then numbers would be documentation only, and adding them later is a cheap comment update, never destroy/create.
  • routeros_move_items stays the janitor: one per chain, declared ID order, gate-allowlisted create that provably moves nothing; the provider's read-back TODO is covered by gate.sh byte-comparing the export, not by trusting it.
  • Renderer: ordered menus copied verbatim (already true, swap-detection in selftest); render.py normalize gained mktxp's unordered-menu sort (ORDERED_MENUS + SORT_LADDER) so gate comparisons and #72 diffs are jitter-free. #72 can additionally vendor mktxp rsc split itself (GPL-2, cron-ready) instead of the port.
  • #71: nothing to borrow, mktxp never writes to a device.

Full citations with path:line: docs/routeros/notes/2026-11-17-mktxp-patterns.md (in PR #134).

Read akpw/mktxp in full (HEAD 6c2311d). Premise correction: mktxp is not a Go apply CLI and has no terraform-like write path. It is a Python exporter/GitOps toolkit whose `rsc` module does live `/export` -> AST -> normalize -> `format`/`split`, device-to-git only (`mktxp/rsc/dispatcher.py:36-40` registers exactly two actions; no apply, no state, no router writes in the tree). It did not solve ordering on apply; it dodged the problem by never applying anything and treats the device's order as the record. That is itself data here: nobody in this niche has made placement work, and the surviving tools all treat export order as load order. What mktxp does encode, worth borrowing: 1. Ordered-menu whitelist as a constant: `/ip(v6) firewall {filter,nat,mangle,raw}`, `/ip(v6) route`, `/routing rule`, `/interface bridge port`, `/interface bridge vlan`, `/ip dhcp-server lease`. Inside those, top-down order is copied, never sorted; every other menu is alphabetized by a fixed key ladder (target/find-expr, list+interface, name, interface, address) for zero-jitter diffs (`rsc/middleware/sorter.py:19-78`, pinned by `tests/rsc/test_sorter.py`). 2. Canonical inter-menu order via numbered domains, `01-base ... 06-firewall ... 99-other`, longest-prefix path match, user-extendable in config (`rsc/handlers/chain.py:21-170`); filename numbers ARE load order. 3. Comments are opaque: `comment=` is never a sort key or placement input anywhere. No numbered-comment idiom exists there, so candidate 3 in this issue remains our own invention (mktxp-compatible, but not validated by it). 4. Nothing is silently dropped: unknown paths go to `99-other.rsc`, volatile fields (bridge `mac-address` under `auto-mac=yes`) are sanitized (`rsc/middleware/sanitizer.py:26-37`). The convention, now written into docs/routeros/README.md by PR #134, absorbing the above: - Declared order = device order = render order. No `place_before` anywhere. Ordered menus are mktxp's list plus `/interface bridge filter` (their list misses it; the provider places there). - Rule comments stay `"<chain>: <purpose> [tf:<key>]"`, comma-free (REST import-key constraint). Step-10 numbers enter with #69: the compiler is the first tool needing a total order from `planned_values` alone; until then numbers would be documentation only, and adding them later is a cheap comment `update`, never destroy/create. - `routeros_move_items` stays the janitor: one per chain, declared ID order, gate-allowlisted create that provably moves nothing; the provider's read-back TODO is covered by gate.sh byte-comparing the export, not by trusting it. - Renderer: ordered menus copied verbatim (already true, swap-detection in selftest); `render.py normalize` gained mktxp's unordered-menu sort (`ORDERED_MENUS` + `SORT_LADDER`) so gate comparisons and #72 diffs are jitter-free. #72 can additionally vendor `mktxp rsc split` itself (GPL-2, cron-ready) instead of the port. - #71: nothing to borrow, mktxp never writes to a device. Full citations with path:line: docs/routeros/notes/2026-11-17-mktxp-patterns.md (in PR #134).
Author
Owner

Done in #134: convention in docs/routeros/README.md (declared order = device order = render order; one move_items per chain; comments as import keys + metric identity), sim-proven reorder (routeros/sim/ordering/run.sh, re-run PASS on provider 1.99.1004 after the rebase), normalize sorts only SORTABLE_MENUS. mktxp notes: docs/routeros/notes/2026-11-17-mktxp-patterns.md.

Done in #134: convention in docs/routeros/README.md (declared order = device order = render order; one move_items per chain; comments as import keys + metric identity), sim-proven reorder (routeros/sim/ordering/run.sh, re-run PASS on provider 1.99.1004 after the rebase), normalize sorts only SORTABLE_MENUS. mktxp notes: docs/routeros/notes/2026-11-17-mktxp-patterns.md.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
lab/iac#67
No description provided.