feat(cache): unified cache abstraction with in-memory and NATS KV backends#2952
Conversation
There was a problem hiding this comment.
7 issues found across 17 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="pkg/cache/natskv_test.go">
<violation number="1" location="pkg/cache/natskv_test.go:106">
P2: Do not ignore `Get` errors in the purge test; assert them so backend failures can't be hidden as cache misses.</violation>
</file>
<file name="pkg/cache/natskv.go">
<violation number="1" location="pkg/cache/natskv.go:90">
P1: `sanitizeKey` introduces key-collision risk by mapping `:` to `.`, which can make distinct input keys resolve to the same NATS KV key.</violation>
<violation number="2" location="pkg/cache/natskv.go:99">
P2: These cache methods ignore the caller context and always use `context.Background()`, so cancellation/deadline propagation is lost.</violation>
</file>
<file name="pkg/cache/cache.go">
<violation number="1" location="pkg/cache/cache.go:86">
P1: Reject non-positive TTL values; currently negative TTLs are accepted and create invalid cache expiration behavior.</violation>
<violation number="2" location="pkg/cache/cache.go:94">
P1: Fail fast when NATS is configured without a bucket name instead of silently falling back to in-memory cache.</violation>
</file>
<file name="pkg/cache/memory.go">
<violation number="1" location="pkg/cache/memory.go:32">
P2: Using `NewLRU` with size `0` disables LRU eviction and creates an unlimited-size cache.</violation>
</file>
<file name="pkg/policies/policy_groups.go">
<violation number="1" location="pkg/policies/policy_groups.go:239">
P1: `opts.GroupCache` is passed without a nil fallback, which can panic when loading chainloop policy groups from call sites that don't set `GroupCache`.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
There was a problem hiding this comment.
1 issue found across 4 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="pkg/cache/natskv.go">
<violation number="1" location="pkg/cache/natskv.go:187">
P2: Do not silently discard per-key purge errors; at least log failures to avoid reporting a successful purge when keys were not removed.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Code reviewFound 4 issues:
chainloop/pkg/policies/group_loader.go Lines 116 to 118 in 7a2e219 chainloop/app/cli/pkg/action/attestation_init.go Lines 353 to 358 in 7a2e219
chainloop/pkg/policies/loader.go Lines 161 to 215 in 7a2e219
chainloop/pkg/policies/policies.go Lines 200 to 215 in 7a2e219 chainloop/pkg/policies/policies.go Lines 455 to 475 in 7a2e219
Note: Issues 2-4 share a root cause -- the branch diverged from |
…ckends New pkg/cache/ shared library providing a generic Cache[T] interface with two backends: in-memory LRU (default) and NATS JetStream KV. Backend is auto-selected based on whether a NATS connection is provided. Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
Replace package-level membershipsCache variable with an injected Cache[*entities.Membership] instance, wired through server.Opts. Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
Replace local expirable.LRU in attjwtmiddleware with an injected Cache[*jwt.MapClaims] instance, wired through server.Opts. Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
Replace package-level map caches with injected Cache[T] instances in ChainloopLoader and ChainloopGroupLoader. Default in-memory caches with 5-minute TTL are created when no external cache is provided, fixing the previous unbounded growth behavior. Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
- Use caller context instead of context.Background() in NATS KV ops - Use NATS KV Purge (removes all revisions) instead of list+delete - Replace panic with silent fallback in default claims cache creation - Move GroupCache default creation to NewPolicyGroupVerifier only - Add fail-open comment documenting graceful degradation strategy Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
- Make middleware cache errors fail-open (log + fall through) - Consolidate duplicate timeout constants into remoteLoaderFetchTimeout - Use bounded-timeout context for all cache ops inside singleflight - Remove duplicate groupCache nil-guard in NewPolicyGroupVerifier - Remove unnecessary comment in grpc.go middleware setup Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
7a2e219 to
f6379cf
Compare
- Remove claimsCache fallback in attmiddleware (always wired via DI) - Default groupCache in NewPolicyVerifier, remove redundant fallback from getGroupLoader - Validate TTL > 0 and require bucket name when NATS is configured - Use base64url encoding for NATS KV keys to avoid collisions - Add default maxSize (1000) to in-memory LRU to bound growth - Log purge errors instead of silently discarding them - Assert Get errors in purge test Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
There was a problem hiding this comment.
1 issue found across 7 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="pkg/policies/policy_groups.go">
<violation number="1" location="pkg/policies/policy_groups.go:269">
P1: Ensure a non-nil GroupCache is always provided. Passing nil into NewChainloopGroupLoader will panic when Load calls c.cache.Get/Set.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
LoadPolicyGroup is a public function called directly from the CLI without going through PolicyVerifier, so opts.GroupCache can be nil. Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
Wire a logger into cache constructors so cache hits, misses, and initialization are visible. Rename NATS KV buckets to use chainloop- prefix for consistency with existing streams. Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
Summary
pkg/cache/shared library with a genericCache[T]interface, backed by in-memory LRU (default) or NATS JetStream KV (when configured)Closes #2951