AuthZService: improve authz caching#9
Conversation
* remove the use of client side cache for in-proc authz client Co-authored-by: Gabriel MABILLE <gabriel.mabille@grafana.com> * add a permission denial cache, fetch perms if not in either of the caches Co-authored-by: Gabriel MABILLE <gabriel.mabille@grafana.com> * Clean up tests Co-authored-by: Ieva <ieva.vasiljeva@grafana.com> * Cache tests Co-authored-by: Ieva <ieva.vasiljeva@grafana.com> * Add test to list + cache Co-authored-by: Ieva <ieva.vasiljeva@grafana.com> * Add outdated cache test Co-authored-by: Ieva <ieva.vasiljeva@grafana.com> * Re-organize metrics Co-authored-by: Ieva <ieva.vasiljeva@grafana.com> --------- Co-authored-by: Gabriel MABILLE <gabriel.mabille@grafana.com>
|
@CodeAnt-AI: review |
|
CodeAnt AI is running the review. |
Nitpicks 🔍
|
| s.permDenialCache.Set(ctx, userPermDenialCacheKey("org-12", "test-uid", "dashboards:read", "dash1", "fold1"), true) | ||
|
|
||
| // Allow access to the dashboard to prove this is not checked | ||
| s.permCache.Set(ctx, userPermCacheKey("org-12", "test-uid", "dashboards:read"), map[string]bool{"dashboards:uid:dash1": false}) |
There was a problem hiding this comment.
Suggestion: In the "Should deny on explicit cache deny entry" test, the cached permission map is set to false for the dashboard scope even though the comment and intent are to simulate an allowed permission that is overridden by a deny-cache entry; with the current false value the permission cache alone would also deny access, so the test would still pass even if the denial cache were not consulted, failing to verify the intended behavior. [logic error]
Severity Level: Major ⚠️
- ⚠️ Denial-cache override behavior not validated by this unit test.
- ⚠️ Future regressions in `permDenialCache` may go undetected.| s.permCache.Set(ctx, userPermCacheKey("org-12", "test-uid", "dashboards:read"), map[string]bool{"dashboards:uid:dash1": false}) | |
| s.permCache.Set(ctx, userPermCacheKey("org-12", "test-uid", "dashboards:read"), map[string]bool{"dashboards:uid:dash1": true}) |
Steps of Reproduction ✅
1. Open `pkg/services/authz/rbac/service_test.go` and locate `TestService_CacheCheck`,
subtest `"Should deny on explicit cache deny entry"` at lines 973–995 (from the current
file contents).
2. Observe that this test sets the permission-denial cache to true at line 979:
`s.permDenialCache.Set(ctx, userPermDenialCacheKey(...\"dash1\", \"fold1\"), true)`, and
then sets the permission cache at line 982 with `map[string]bool{"dashboards:uid:dash1":
false}` while the preceding comment says `// Allow access to the dashboard to prove this
is not checked`.
3. Follow the production call path in `pkg/services/authz/rbac/service.go`:
`Service.Check` (lines 93–159) calls `getCachedIdentityPermissions` (lines 342–368), which
returns the cached map from `permCache`, and then `checkPermission` (lines 536–567) checks
`scopeMap[t.scope(req.Name)]` and only grants access if the value is `true`.
4. Because the test's cached permission map contains `"dashboards:uid:dash1": false`,
`checkPermission` will deny access even if the denial-cache lookup at lines 116–121
(`permDenialCache.Get(...)`) were removed or broken; thus the test would still pass
without the denial-cache behavior, proving that the current test does not actually verify
that the deny cache overrides an otherwise allowed permission.Prompt for AI Agent 🤖
This is a comment left during a code review.
**Path:** pkg/services/authz/rbac/service_test.go
**Line:** 982:982
**Comment:**
*Logic Error: In the "Should deny on explicit cache deny entry" test, the cached permission map is set to `false` for the dashboard scope even though the comment and intent are to simulate an allowed permission that is overridden by a deny-cache entry; with the current `false` value the permission cache alone would also deny access, so the test would still pass even if the denial cache were not consulted, failing to verify the intended behavior.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.|
CodeAnt AI finished running the review. |
|
This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 2 weeks if no further activity occurs. Please feel free to give a status update or ping for review. Thank you for your contributions! |
|
This pull request has been automatically closed because it has not had any further activity in the last 2 weeks. Thank you for your contributions! |
User description
Test 2nn
Summary by CodeRabbit
Release Notes
New Features
Performance
✏️ Tip: You can customize this high-level summary in your review settings.
nn---n*Replicated from [ai-code-review-evaluation/grafana-coderabbit#2](https://github.com/ai-code-review-evaluation/grafana-coderabbit/pull/2)*CodeAnt-AI Description
Improve authorization permission caching with explicit denial entries and cache-first checks
What Changed
Impact
✅ Fewer DB permission queries✅ Faster authz checks for repeated denies and cached permissions✅ Consistent deny behavior for repeated permission checks💡 Usage Guide
Checking Your Pull Request
Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.
Talking to CodeAnt AI
Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
Preserve Org Learnings with CodeAnt
You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
Check Your Repository Health
To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.