Chore: Fix error handling in postDashboard, remove UserDisplayDTO, fix live redis client initialization (#87206)
* clean up error handling in postDashboard and remove UserDisplayDTO * replace GetUserUID with GetUID and GetNamespacedUID, enforce namespace constant type * lint fix * lint fix * more lint fixes
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/registry"
|
||||
@@ -100,22 +99,18 @@ func (s *SearchOptions) ComputeUserID() (int64, error) {
|
||||
if s.NamespacedID == "" {
|
||||
return 0, errors.New("namespacedID must be set")
|
||||
}
|
||||
// Split namespaceID into namespace and ID
|
||||
parts := strings.Split(s.NamespacedID, ":")
|
||||
// Validate namespace ID format
|
||||
if len(parts) != 2 {
|
||||
return 0, fmt.Errorf("invalid namespaced ID: %s", s.NamespacedID)
|
||||
}
|
||||
// Validate namespace type is user or service account
|
||||
if parts[0] != identity.NamespaceUser && parts[0] != identity.NamespaceServiceAccount {
|
||||
return 0, fmt.Errorf("invalid namespace: %s", parts[0])
|
||||
}
|
||||
// Validate namespace ID is a number
|
||||
id, err := strconv.ParseInt(parts[1], 10, 64)
|
||||
|
||||
id, err := identity.ParseNamespaceID(s.NamespacedID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("invalid namespaced ID: %s", s.NamespacedID)
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
|
||||
// Validate namespace type is user or service account
|
||||
if id.Namespace() != identity.NamespaceUser && id.Namespace() != identity.NamespaceServiceAccount {
|
||||
return 0, fmt.Errorf("invalid namespace: %s", id.Namespace())
|
||||
}
|
||||
|
||||
return id.ParseInt()
|
||||
}
|
||||
|
||||
type SyncUserRolesCommand struct {
|
||||
|
||||
@@ -2,6 +2,7 @@ package acimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -537,7 +538,7 @@ func TestService_SearchUsersPermissions(t *testing.T) {
|
||||
// only the user's basic roles and the user's stored permissions
|
||||
name: "check namespacedId filter works correctly",
|
||||
siuPermissions: listAllPerms,
|
||||
searchOption: accesscontrol.SearchOptions{NamespacedID: identity.NamespaceServiceAccount + ":1"},
|
||||
searchOption: accesscontrol.SearchOptions{NamespacedID: fmt.Sprintf("%s:1", identity.NamespaceServiceAccount)},
|
||||
ramRoles: map[string]*accesscontrol.RoleDTO{
|
||||
string(roletype.RoleEditor): {Permissions: []accesscontrol.Permission{
|
||||
{Action: accesscontrol.ActionTeamsRead, Scope: "teams:*"},
|
||||
@@ -607,7 +608,7 @@ func TestService_SearchUserPermissions(t *testing.T) {
|
||||
name: "ram only",
|
||||
searchOption: accesscontrol.SearchOptions{
|
||||
ActionPrefix: "teams",
|
||||
NamespacedID: identity.NamespaceUser + ":2",
|
||||
NamespacedID: fmt.Sprintf("%s:2", identity.NamespaceUser),
|
||||
},
|
||||
ramRoles: map[string]*accesscontrol.RoleDTO{
|
||||
string(roletype.RoleEditor): {Permissions: []accesscontrol.Permission{
|
||||
@@ -632,7 +633,7 @@ func TestService_SearchUserPermissions(t *testing.T) {
|
||||
name: "stored only",
|
||||
searchOption: accesscontrol.SearchOptions{
|
||||
ActionPrefix: "teams",
|
||||
NamespacedID: identity.NamespaceUser + ":2",
|
||||
NamespacedID: fmt.Sprintf("%s:2", identity.NamespaceUser),
|
||||
},
|
||||
storedPerms: map[int64][]accesscontrol.Permission{
|
||||
1: {{Action: accesscontrol.ActionTeamsRead, Scope: "teams:id:1"}},
|
||||
@@ -652,7 +653,7 @@ func TestService_SearchUserPermissions(t *testing.T) {
|
||||
name: "ram and stored",
|
||||
searchOption: accesscontrol.SearchOptions{
|
||||
ActionPrefix: "teams",
|
||||
NamespacedID: identity.NamespaceUser + ":2",
|
||||
NamespacedID: fmt.Sprintf("%s:2", identity.NamespaceUser),
|
||||
},
|
||||
ramRoles: map[string]*accesscontrol.RoleDTO{
|
||||
string(roletype.RoleAdmin): {Permissions: []accesscontrol.Permission{
|
||||
@@ -682,7 +683,7 @@ func TestService_SearchUserPermissions(t *testing.T) {
|
||||
name: "check action prefix filter works correctly",
|
||||
searchOption: accesscontrol.SearchOptions{
|
||||
ActionPrefix: "teams",
|
||||
NamespacedID: identity.NamespaceUser + ":1",
|
||||
NamespacedID: fmt.Sprintf("%s:1", identity.NamespaceUser),
|
||||
},
|
||||
ramRoles: map[string]*accesscontrol.RoleDTO{
|
||||
string(roletype.RoleEditor): {Permissions: []accesscontrol.Permission{
|
||||
@@ -704,7 +705,7 @@ func TestService_SearchUserPermissions(t *testing.T) {
|
||||
name: "check action filter works correctly",
|
||||
searchOption: accesscontrol.SearchOptions{
|
||||
Action: accesscontrol.ActionTeamsRead,
|
||||
NamespacedID: identity.NamespaceUser + ":1",
|
||||
NamespacedID: fmt.Sprintf("%s:1", identity.NamespaceUser),
|
||||
},
|
||||
ramRoles: map[string]*accesscontrol.RoleDTO{
|
||||
string(roletype.RoleEditor): {Permissions: []accesscontrol.Permission{
|
||||
|
||||
@@ -549,7 +549,7 @@ func TestIntegrationAccessControlStore_SearchUsersPermissions(t *testing.T) {
|
||||
},
|
||||
options: accesscontrol.SearchOptions{
|
||||
ActionPrefix: "teams:",
|
||||
NamespacedID: identity.NamespaceUser + ":1",
|
||||
NamespacedID: fmt.Sprintf("%s:1", identity.NamespaceUser),
|
||||
},
|
||||
wantPerm: map[int64][]accesscontrol.Permission{
|
||||
1: {{Action: "teams:read", Scope: "teams:id:1"}, {Action: "teams:read", Scope: "teams:id:10"},
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func setupTestEnv(b *testing.B, resourceCount, permissionPerResource int) (map[string][]string, map[string]bool) {
|
||||
func setupTestEnv(resourceCount, permissionPerResource int) (map[string][]string, map[string]bool) {
|
||||
res := map[string][]string{}
|
||||
ids := make(map[string]bool, resourceCount)
|
||||
|
||||
@@ -25,7 +25,7 @@ func setupTestEnv(b *testing.B, resourceCount, permissionPerResource int) (map[s
|
||||
}
|
||||
|
||||
func benchGetMetadata(b *testing.B, resourceCount, permissionPerResource int) {
|
||||
permissions, ids := setupTestEnv(b, resourceCount, permissionPerResource)
|
||||
permissions, ids := setupTestEnv(resourceCount, permissionPerResource)
|
||||
b.ResetTimer()
|
||||
|
||||
var metadata map[string]Metadata
|
||||
|
||||
Reference in New Issue
Block a user