-
Notifications
You must be signed in to change notification settings - Fork 0
AuthZService: improve authz caching #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: cache-optimization-baseline
Are you sure you want to change the base?
AuthZService: improve authz caching #9
Conversation
* remove the use of client side cache for in-proc authz client Co-authored-by: Gabriel MABILLE <[email protected]> * add a permission denial cache, fetch perms if not in either of the caches Co-authored-by: Gabriel MABILLE <[email protected]> * Clean up tests Co-authored-by: Ieva <[email protected]> * Cache tests Co-authored-by: Ieva <[email protected]> * Add test to list + cache Co-authored-by: Ieva <[email protected]> * Add outdated cache test Co-authored-by: Ieva <[email protected]> * Re-organize metrics Co-authored-by: Ieva <[email protected]> --------- Co-authored-by: Gabriel MABILLE <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.
Greptile OverviewConfidence Score: 2/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant Client
participant Service
participant DenialCache
participant PermCache
participant Database
Client->>Service: Check(request)
Service->>DenialCache: Get(permDenialKey)
alt denial cached
DenialCache-->>Service: found=true
Service-->>Client: Allowed=false (cached)
else denial not cached
DenialCache-->>Service: found=false
Service->>PermCache: Get(userPermKey)
alt permissions cached
PermCache-->>Service: cached permissions
Service->>Service: checkPermission(cachedPerms)
alt allowed
Service-->>Client: Allowed=true (cached)
else denied
Note over Service,DenialCache: BUG: denial not cached here
Service-->>Client: Allowed=false (not cached)
end
else permissions not cached
PermCache-->>Service: not found
Service->>Database: getIdentityPermissions()
Database-->>Service: fresh permissions
Service->>PermCache: Set(permissions)
Service->>Service: checkPermission(permissions)
alt denied
Service->>DenialCache: Set(permDenialKey, true)
end
Service-->>Client: Allowed=result
end
end
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4 files reviewed, 1 comment
| cachedPerms, err := s.getCachedIdentityPermissions(ctx, checkReq.Namespace, checkReq.IdentityType, checkReq.UserUID, checkReq.Action) | ||
| if err == nil { | ||
| allowed, err := s.checkPermission(ctx, cachedPerms, checkReq) | ||
| if err != nil { | ||
| ctxLogger.Error("could not check permission", "error", err) | ||
| s.metrics.requestCount.WithLabelValues("true", "true", req.GetVerb(), req.GetGroup(), req.GetResource()).Inc() | ||
| return deny, err | ||
| } | ||
| if allowed { | ||
| s.metrics.permissionCacheUsage.WithLabelValues("true", checkReq.Action).Inc() | ||
| s.metrics.requestCount.WithLabelValues("false", "true", req.GetVerb(), req.GetGroup(), req.GetResource()).Inc() | ||
| return &authzv1.CheckResponse{Allowed: allowed}, nil | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: denial cache won't be populated when cached permissions indicate denial
When getCachedIdentityPermissions returns cached permissions and checkPermission determines allowed=false, the code returns early without caching the denial (line 135 exits before reaching line 154 where denials are cached). This means subsequent identical requests will repeatedly call getCachedIdentityPermissions and checkPermission instead of hitting the denial cache.
The denial cache is only populated when permissions are fetched from the database (line 154), not when using cached permissions.
Prompt To Fix With AI
This is a comment left during a code review.
Path: pkg/services/authz/rbac/service.go
Line: 123:136
Comment:
**logic:** denial cache won't be populated when cached permissions indicate denial
When `getCachedIdentityPermissions` returns cached permissions and `checkPermission` determines `allowed=false`, the code returns early without caching the denial (line 135 exits before reaching line 154 where denials are cached). This means subsequent identical requests will repeatedly call `getCachedIdentityPermissions` and `checkPermission` instead of hitting the denial cache.
The denial cache is only populated when permissions are fetched from the database (line 154), not when using cached permissions.
How can I resolve this? If you propose a fix, please make it concise.
Test 2
Replicated from ai-code-review-evaluation/grafana-greptile#2