Repository navigation
🐛 fix(collect): bound SAR access cache with LRU eviction + hit/miss metrics (REL-05) - #175
Merged
Merged
Conversation
…ss metrics (REL-05)
There was a problem hiding this comment.
Pull request overview
This PR addresses audit finding REL-05 by bounding the SelfSubjectAccessReview access-check cache with an LRU eviction policy and adding Prometheus hit/miss observability for cache outcomes.
Changes:
- Replaces the unbounded SAR decision cache with a capacity-bounded LRU cache (TTL preserved) in
internal/collect/access.go. - Adds
kollect_access_cache_total{result=hit|miss}and registers/catalogs the metric in the metrics subsystem. - Adds/extends unit tests to cover LRU eviction, concurrency cap enforcement, and hit/miss metric increments.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/metrics/metrics.go | Adds hit/miss label constants and introduces/registers kollect_access_cache_total. |
| internal/metrics/metrics_catalog.go | Catalogs the new kollect_access_cache_total metric for documentation/ops visibility. |
| internal/metrics/metrics_catalog_test.go | Updates the registered metric name list to include the new metric. |
| internal/collect/access.go | Implements a bounded LRU cache for SAR decisions and increments hit/miss metrics. |
| internal/collect/access_test.go | Adds LRU/cap/metrics tests and a reusable fake SAR client helper. |
Suppressed comments (1)
internal/collect/access_test.go:36
- allowAllSARClient will panic if calls is non-nil but mu is nil (mu.Lock()). Since this is a reusable test helper, make it safe to call with a nil mutex, and use the clearer "(*calls)++" form.
if calls != nil {
mu.Lock()
*calls++
mu.Unlock()
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Name: "kollect_access_cache_total", | ||
| Type: "counter", | ||
| Labels: []string{"result"}, | ||
| Help: "SelfSubjectAccessReview access-cache outcomes (REL-05): hit serves a cached decision, miss issues a fresh SAR.", |
Comment on lines
+229
to
+230
| Help: "SelfSubjectAccessReview access-cache outcomes (REL-05): hit serves a cached " + | ||
| "decision, miss issues a fresh SubjectAccessReview.", |
Comment on lines
+23
to
+26
| // allowAllSARClient returns a fake clientset whose SAR reactor allows everything | ||
| // and increments *calls once per SubjectAccessReview. The counter pointer lets a | ||
| // test assert how many times the underlying access check was actually invoked | ||
| // (i.e. how many cache misses reached the API server). |
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Audit finding REL-05 (P3) — the SAR access-check cache in
internal/collect/access.gowas an unbounded map (30s TTL, evict-on-read only) → unbounded memory under pathological key cardinality, and no observability.Fix: keyed LRU (container/list, default cap 4096) trims to capacity after each insert; recency updated on read + write; 30s TTL preserved; single
sync.Mutex(read path now mutates recency). Newkollect_access_cache_total{result=hit|miss}counter (registered + cataloged).TDD:
TestAccessCheckerEvictsLRU,TestAccessCheckerConcurrentRespectsCap(-race),TestAccessCheckerRecordsHitMissMetrics. Correctness preserved — evicted/expired key recomputes a fresh SAR with the same result.Independent review: APPROVE, no P0/P1 — verified bound holds (no off-by-one), race-clean, metric actually registered (read the
MustRegisterbody). Non-blocking note: the catalog test mirrors rather than reads the live registry (pre-existing pattern).Disjoint (collect + metrics).