Lift the routable-token decoders out of Rule and add a non-throwing decoder

What does this MR do and why?

decodeRoutableTokenPayload and decodeRoutableJson are private methods on Rule, reachable only by declaring a transform in a JSON ruleset. !1381 (closed) routes GET /jwt/auth, where a Docker client authenticates with Authorization: Basic and no declarative rule can express the algorithm that endpoint needs, so it gets a hand-written handler that has to read the cell id out of the very same token from outside the rule engine. Two code paths deriving a cell from one token must not disagree, so this MR gives them one decoder to share.

Commit 1 moves the two decoders, parseKeyValuePair and the base36 helpers off Rule into a new src/routable_token.ts, taking ROUTING_KEYS with them. decodeBase64Match stays behind: it re-matches against a pattern supplied by the rule, which makes it engine machinery rather than token grammar. Import rewiring only, and green with no test edits, which is the gate on it.

Commit 2 adds decodeRoutableToken: one call, both encodings, { cellId, encoding } or null.

  • It never throws, unlike the two decoders it wraps. A caller reaching for it is routing on a best-effort hint, so an undecodable token is not an error, it is a request this code did not decide, and undecided requests already have a home. The two wrapped decoders keep throwing, which is correct inside the rule engine where a malformed token is the 400 the client has earned.
  • Recognition is by shape, never by prefix, because prefixes are instance-configurable (PrefixHelper.prepend_instance_prefix turns glpat- into acme-glpat-). Both encodings split into three dot-separated segments; the middle one discriminates them.
  • An unrecognised version returns null with no branch for it, which is the same answer an explicit branch would give.
  • c, not o, matching every routable-token rule in the ruleset. Reading a different key on one path is the same two-paths-disagreeing problem one level down.

No shipping path changes. config/ruleset/ and src/rules/ behaviour are untouched and byte-identical including what they throw on, pinned by the existing transform.json fixtures. The grammar is still written down twice, once here and once per rule; unifying it and adding the test that keeps it unified is !1383 (merged), stacked directly on top.

npm test 35 files / 419 tests → 36 / 445. tsc --noEmit, eslint . and prettier --check clean at both commits.

References

How to set up and validate locally

  1. npm test covers all of it; nothing here needs a running stack.
  2. npx vitest run test/routable_token.spec.ts for the decodeRoutableToken unit tests: both encodings, an instance-prefixed token, the three-segment discrimination, the payload bounds at 27 and 321, and fourteen credentials that must come back null.
  3. git show <commit 1> --stat, which has no test file in it, the gate on commit 1 being a pure move.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Sangwoo Han

Merge request reports

Loading
Loading