RBAC: Make fixed role UIDs deterministic (#76239)

* Add fixed role UID

Co-authored-by: Karl Persson <kalle.persson@grafana.com>

* Use base64 url encoding

---------

Co-authored-by: Karl Persson <kalle.persson@grafana.com>
This commit is contained in:
Gabriel MABILLE
2023-10-10 16:29:31 +02:00
committed by GitHub
co-authored by Karl Persson
parent 2a2401e673
commit 9dd38de5c1
2 changed files with 13 additions and 0 deletions
+1
View File
@@ -321,6 +321,7 @@ func (cmd *SaveExternalServiceRoleCommand) Validate() error {
const (
GlobalOrgID = 0
FixedRolePrefix = "fixed:"
FixedRoleUIDPrefix = "fixed_"
ManagedRolePrefix = "managed:"
BasicRolePrefix = "basic:"
PluginRolePrefix = "plugins:"
+12
View File
@@ -1,6 +1,9 @@
package accesscontrol
import (
// #nosec G505 Used only for generating a 160 bit hash, it's not used for security purposes
"crypto/sha1"
"encoding/base64"
"fmt"
"strings"
"sync"
@@ -253,6 +256,15 @@ func ConcatPermissions(permissions ...[]Permission) []Permission {
return perms
}
// FixedRoleUID generates a UID of 34 bytes: "fixed_" + base64(sha1(roleName))
func FixedRoleUID(roleName string) string {
// #nosec G505 Used only for generating a 160 bit hash, it's not used for security purposes
hasher := sha1.New()
hasher.Write([]byte(roleName))
return fmt.Sprintf("%s%s", FixedRoleUIDPrefix, base64.RawURLEncoding.EncodeToString(hasher.Sum(nil)))
}
// ValidateFixedRole errors when a fixed role does not match expected pattern
func ValidateFixedRole(role RoleDTO) error {
if !strings.HasPrefix(role.Name, FixedRolePrefix) {