RBAC: Optimize permissions caching (#92412)

* Access control: Use composite cache key for team permissions

* use composite key for teams

* use cache for hotpath (getCachedUserPermissions)

* fix linter

* fix sorting

---------

Co-authored-by: Jeff Levin <jeff@levinology.com>
This commit is contained in:
Alexander Zobnin
2024-08-27 10:31:52 +02:00
committed by GitHub
parent a54ec2341c
commit 488e994d37
3 changed files with 29 additions and 3 deletions

View File

@ -2,12 +2,14 @@ package accesscontrol
import (
"fmt"
"slices"
"strconv"
"strings"
"github.com/grafana/grafana/pkg/apimachinery/identity"
)
func GetPermissionCacheKey(user identity.Requester) string {
func GetUserPermissionCacheKey(user identity.Requester) string {
return fmt.Sprintf("rbac-permissions-%s", user.GetCacheKey())
}
@ -41,3 +43,12 @@ func GetBasicRolePermissionCacheKey(role string, orgID int64) string {
func GetTeamPermissionCacheKey(teamID int64, orgID int64) string {
return fmt.Sprintf("rbac-permissions-team-%d-%d", orgID, teamID)
}
func GetTeamPermissionCompositeCacheKey(teamIds []int64, orgID int64) string {
teams := make([]string, 0)
for _, id := range teamIds {
teams = append(teams, strconv.FormatInt(id, 10))
}
slices.Sort(teams)
return fmt.Sprintf("rbac-permissions-team-%d-%s", orgID, strings.Join(teams, "-"))
}