Skip to content

🐛 fix(collect): bound SAR access cache with LRU eviction + hit/miss metrics (REL-05) - #175

Merged
konih merged 1 commit into
mainfrom
fix/rel-05-access-cache-lru
Aug 3, 2026
Merged

konih merged 1 commit into
mainfrom
fix/rel-05-access-cache-lru

Conversation

@konih

@konih konih commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Audit finding REL-05 (P3) — the SAR access-check cache in internal/collect/access.go was 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). New kollect_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 MustRegister body). Non-blocking note: the catalog test mirrors rather than reads the live registry (pre-existing pattern).

Disjoint (collect + metrics).

Copilot AI review requested due to automatic review settings August 3, 2026 12:43
@konih
konih merged commit 8743cd9 into main Aug 3, 2026
22 checks passed
@konih
konih deleted the fix/rel-05-access-cache-lru branch August 3, 2026 12:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).
@sonarqubecloud

sonarqubecloud Bot commented Aug 3, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.74468% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/collect/access.go 95.65% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

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.

2 participants