Skip to content

core: match segment wildcard policy paths without splitting them - #32211

Open
b84330808 wants to merge 2 commits into
hashicorp:mainfrom
b84330808:acl-segment-wildcard-no-alloc
Open

b84330808 wants to merge 2 commits into
hashicorp:mainfrom
b84330808:acl-segment-wildcard-no-alloc

Conversation

@b84330808

@b84330808 b84330808 commented Nov 20, 2026 •

Copy link
Copy Markdown

Description

When a request path misses the exact rules, AllowOperation matches it against every segment wildcard (+) rule of the token's policies, and it does that for both slash forms of the path. For each rule, tryMatchWildcardPath split the rule with strings.Split and filled a segments slice that nothing reads, so every check allocated two slices per + rule. With many + rules, as in the policies from #24289 (38 policies with paths such as TEST/+/SUB/*), that is most of the time of the check (see below), and the check runs on every request, once per path in sys/capabilities*, and once per mount in sys/internal/ui/mounts.

This walks the rule's segments with strings.Cut and drops the unused slice. The bare mount loop in CheckAllowedFromNonExactPaths (used by sys/internal/ui/mounts) needs its segments, so it keeps them, but it gets the same walk and only allocates them once a rule's first segment matches. Matching results don't change.

Benchmarks: I ran 6 rounds of each build, alternating base and patched, on an i7-9700 with Windows 11 and Go 1.27.4. Times are medians, with the range in brackets.

BenchmarkACL_SegmentWildcardRules (added in vault/acl_test.go): one ACL with N rules like engine3/+/team7/*, checking a path that matches one of them (-benchtime 1s):

rules check before after allocs/op before allocs/op after
10 AllowOperation 3.0 µs [3.0–4.0] 1.4 µs [1.4–3.3] 49 7
100 AllowOperation 23.3 µs [22.9–23.8] 8.3 µs [8.1–8.4] 409 7
1000 AllowOperation 231.9 µs [223.3–248.7] 79.6 µs [78.8–84.3] 4,009 7
1000 Capabilities 232.2 µs [229.4–256.6] 79.2 µs [78.6–80.8] 4,010 8
1000 hasMountAccess (no access) 256.4 µs [254.7–299.0] 76.9 µs [76.4–78.8] 4,005 5

Through Core.HandleRequest (a local benchmark I didn't include): 10 KV v1 mounts and a token with 38 policies of 25 + rules each (950 rules), -benchtime 2s:

request before after allocs/op before allocs/op after
read of a KV secret 1.05 ms [1.03–1.07] 0.86 ms [0.85–0.89] 11,519 7,714
sys/capabilities-self, 100 paths 102.9 ms [101.0–106.2] 84.4 ms [82.9–86.3] 1,150,496 770,136
sys/internal/ui/mounts 4.80 ms [4.70–4.85] 2.59 ms [2.58–2.67] 51,816 16,434

With 10 policies of 10 rules (100 rules), the same requests got 6.6%, 6.7% and 25% faster.

Tests: the existing TestACL_* (including TestACL_SegmentWildcardPriority, TestACL_SegmentWildcardPriority_BareMount and TestACL_ListTrailingSlashDeny), TestCapabilities*, TestSystemBackend_Capabilities*, TestSystemBackend_PathCapabilities, TestSystemBackend_InternalUIMount(s) and TestPolicyStore_* pass before and after. I also compared the old and new loops on 5 million random rule and path pairs each, and they gave the same results.

Changelog: changelog/32211.txt:

core: Reduce the time and memory used to check requests against policies with many segment wildcard (`+`) paths.

Every ACL check that misses the exact rules matches the path against each
segment wildcard ("+") rule. tryMatchWildcardPath split every rule with
strings.Split and filled a segments slice that nothing read, so each check
allocated two slices per "+" rule, and AllowOperation runs it for both
slash forms of the path. Walk the rule's segments with strings.Cut instead
and drop the unused slice.

The bare mount loop in CheckAllowedFromNonExactPaths, which
sys/internal/ui/mounts runs for each mount, gets the same walk, and only
allocates its segments once a rule's first segment matches.

BenchmarkACL_SegmentWildcardRules measures both.
@b84330808
b84330808 requested a review from a team as a code owner November 20, 2026 22:42
@b84330808
b84330808 requested a review from akshya96 November 20, 2026 22:42

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant