Files
Aaron Godin d89235aa8c IAM: fix GetSearchPermissionCacheKey uniqueness (#95192)
* fix: Change users permissions search to use a consistent key without collisions

* Move HashString to cacheutils

* Change error handling logic for what to do with a cache key

* Add a test that confirms search cache key consistency
2024-10-23 15:37:30 -05:00

59 lines
1.5 KiB
Go

package accesscontrol
import (
"bytes"
"encoding/base64"
"encoding/gob"
"fmt"
"hash/fnv"
"strings"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/apimachinery/identity"
)
func (s *SearchOptions) HashString() (string, error) {
if s == nil {
return "", nil
}
var buf bytes.Buffer
encoder := gob.NewEncoder(&buf)
if err := encoder.Encode(s); err != nil {
return "", err
}
h := fnv.New64a()
_, err := h.Write(buf.Bytes())
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
}
func GetUserPermissionCacheKey(user identity.Requester) string {
return fmt.Sprintf("rbac-permissions-%s", user.GetCacheKey())
}
func GetSearchPermissionCacheKey(log log.Logger, user identity.Requester, searchOptions SearchOptions) (string, error) {
searchHash, err := searchOptions.HashString()
if err != nil {
return "", err
}
key := fmt.Sprintf("rbac-permissions-%s-%s", user.GetCacheKey(), searchHash)
return key, nil
}
func GetUserDirectPermissionCacheKey(user identity.Requester) string {
return fmt.Sprintf("rbac-permissions-direct-%s", user.GetCacheKey())
}
func GetBasicRolePermissionCacheKey(role string, orgID int64) string {
roleKey := strings.Replace(role, " ", "_", -1)
roleKey = strings.ToLower(roleKey)
return fmt.Sprintf("rbac-permissions-basic-role-%d-%s", orgID, roleKey)
}
func GetTeamPermissionCacheKey(teamID int64, orgID int64) string {
return fmt.Sprintf("rbac-permissions-team-%d-%d", orgID, teamID)
}