Authz: add support to use folder api to fetch folder tree (#100038)
* Add FolderStore interface * Authz: add implementation to use folders api and use it inproc with loopback config * Add tracing and add rest.Config for talking with folder api using access tokens * Restructure test to get rid of circular dependencies in tests * use correct group version kind --------- Co-authored-by: gamab <gabriel.mabille@grafana.com>
This commit is contained in:
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
authzv1 "github.com/grafana/authlib/authz/proto/v1"
|
||||
"github.com/grafana/authlib/cache"
|
||||
claims "github.com/grafana/authlib/types"
|
||||
"github.com/grafana/authlib/types"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
@@ -41,6 +41,7 @@ type Service struct {
|
||||
authzextv1.UnimplementedAuthzExtentionServiceServer
|
||||
|
||||
store store.Store
|
||||
folderStore store.FolderStore
|
||||
permissionStore store.PermissionStore
|
||||
identityStore legacy.LegacyIdentityStore
|
||||
|
||||
@@ -63,6 +64,7 @@ type Service struct {
|
||||
|
||||
func NewService(
|
||||
sql legacysql.LegacyDatabaseProvider,
|
||||
folderStore store.FolderStore,
|
||||
identityStore legacy.LegacyIdentityStore,
|
||||
permissionStore store.PermissionStore,
|
||||
logger log.Logger,
|
||||
@@ -72,6 +74,7 @@ func NewService(
|
||||
) *Service {
|
||||
return &Service{
|
||||
store: store.NewStore(sql, tracer),
|
||||
folderStore: folderStore,
|
||||
permissionStore: permissionStore,
|
||||
identityStore: identityStore,
|
||||
logger: logger,
|
||||
@@ -209,40 +212,42 @@ func (s *Service) validateListRequest(ctx context.Context, req *authzv1.ListRequ
|
||||
return listReq, nil
|
||||
}
|
||||
|
||||
func validateNamespace(ctx context.Context, nameSpace string) (claims.NamespaceInfo, error) {
|
||||
func validateNamespace(ctx context.Context, nameSpace string) (types.NamespaceInfo, error) {
|
||||
if nameSpace == "" {
|
||||
return claims.NamespaceInfo{}, status.Error(codes.InvalidArgument, "namespace is required")
|
||||
return types.NamespaceInfo{}, status.Error(codes.InvalidArgument, "namespace is required")
|
||||
}
|
||||
authInfo, has := claims.AuthInfoFrom(ctx)
|
||||
authInfo, has := types.AuthInfoFrom(ctx)
|
||||
if !has {
|
||||
return claims.NamespaceInfo{}, status.Error(codes.Internal, "could not get auth info from context")
|
||||
return types.NamespaceInfo{}, status.Error(codes.Internal, "could not get auth info from context")
|
||||
}
|
||||
if !claims.NamespaceMatches(authInfo.GetNamespace(), nameSpace) {
|
||||
return claims.NamespaceInfo{}, status.Error(codes.PermissionDenied, "namespace does not match")
|
||||
if !types.NamespaceMatches(authInfo.GetNamespace(), nameSpace) {
|
||||
return types.NamespaceInfo{}, status.Error(codes.PermissionDenied, "namespace does not match")
|
||||
}
|
||||
|
||||
ns, err := claims.ParseNamespace(nameSpace)
|
||||
ns, err := types.ParseNamespace(nameSpace)
|
||||
if err != nil {
|
||||
return claims.NamespaceInfo{}, err
|
||||
return types.NamespaceInfo{}, err
|
||||
}
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (s *Service) validateSubject(ctx context.Context, subject string) (string, claims.IdentityType, error) {
|
||||
func (s *Service) validateSubject(ctx context.Context, subject string) (string, types.IdentityType, error) {
|
||||
if subject == "" {
|
||||
return "", "", status.Error(codes.InvalidArgument, "subject is required")
|
||||
}
|
||||
|
||||
ctxLogger := s.logger.FromContext(ctx)
|
||||
identityType, userUID, err := claims.ParseTypeID(subject)
|
||||
identityType, userUID, err := types.ParseTypeID(subject)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// Permission check currently only checks user, anonymous user, service account and renderer permissions
|
||||
if !(identityType == claims.TypeUser || identityType == claims.TypeServiceAccount || identityType == claims.TypeAnonymous || identityType == claims.TypeRenderService) {
|
||||
if !types.IsIdentityType(identityType, types.TypeUser, types.TypeServiceAccount, types.TypeAnonymous, types.TypeRenderService) {
|
||||
ctxLogger.Error("unsupported identity type", "type", identityType)
|
||||
return "", "", status.Error(codes.PermissionDenied, "unsupported identity type")
|
||||
}
|
||||
|
||||
return userUID, identityType, nil
|
||||
}
|
||||
|
||||
@@ -264,30 +269,29 @@ func (s *Service) validateAction(ctx context.Context, group, resource, verb stri
|
||||
return action, nil
|
||||
}
|
||||
|
||||
func (s *Service) getIdentityPermissions(ctx context.Context, ns claims.NamespaceInfo, idType claims.IdentityType, userID, action string) (map[string]bool, error) {
|
||||
func (s *Service) getIdentityPermissions(ctx context.Context, ns types.NamespaceInfo, idType types.IdentityType, userID, action string) (map[string]bool, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "authz_direct_db.service.getIdentityPermissions")
|
||||
defer span.End()
|
||||
|
||||
// When checking folder creation permissions, also check edit and admin action sets for folder, as the scoped folder create actions aren't stored in the DB separately
|
||||
var actionSets []string
|
||||
if action == "folders:create" {
|
||||
actionSets = append(actionSets, "folders:edit")
|
||||
actionSets = append(actionSets, "folders:admin")
|
||||
actionSets = append(actionSets, "folders:edit", "folders:admin")
|
||||
}
|
||||
|
||||
switch idType {
|
||||
case claims.TypeAnonymous:
|
||||
case types.TypeAnonymous:
|
||||
return s.getAnonymousPermissions(ctx, ns, action, actionSets)
|
||||
case claims.TypeRenderService:
|
||||
case types.TypeRenderService:
|
||||
return s.getRendererPermissions(ctx, action)
|
||||
case claims.TypeUser, claims.TypeServiceAccount:
|
||||
case types.TypeUser, types.TypeServiceAccount:
|
||||
return s.getUserPermissions(ctx, ns, userID, action, actionSets)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported identity type: %s", idType)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) getUserPermissions(ctx context.Context, ns claims.NamespaceInfo, userID, action string, actionSets []string) (map[string]bool, error) {
|
||||
func (s *Service) getUserPermissions(ctx context.Context, ns types.NamespaceInfo, userID, action string, actionSets []string) (map[string]bool, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "authz_direct_db.service.getUserPermissions")
|
||||
defer span.End()
|
||||
|
||||
@@ -342,7 +346,7 @@ func (s *Service) getUserPermissions(ctx context.Context, ns claims.NamespaceInf
|
||||
return res.(map[string]bool), nil
|
||||
}
|
||||
|
||||
func (s *Service) getAnonymousPermissions(ctx context.Context, ns claims.NamespaceInfo, action string, actionSets []string) (map[string]bool, error) {
|
||||
func (s *Service) getAnonymousPermissions(ctx context.Context, ns types.NamespaceInfo, action string, actionSets []string) (map[string]bool, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "authz_direct_db.service.getAnonymousPermissions")
|
||||
defer span.End()
|
||||
|
||||
@@ -378,7 +382,7 @@ func (s *Service) getRendererPermissions(ctx context.Context, action string) (ma
|
||||
return map[string]bool{}, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetUserIdentifiers(ctx context.Context, ns claims.NamespaceInfo, userUID string) (*store.UserIdentifiers, error) {
|
||||
func (s *Service) GetUserIdentifiers(ctx context.Context, ns types.NamespaceInfo, userUID string) (*store.UserIdentifiers, error) {
|
||||
uidCacheKey := userIdentifierCacheKey(ns.Value, userUID)
|
||||
if cached, ok := s.idCache.Get(ctx, uidCacheKey); ok {
|
||||
return &cached, nil
|
||||
@@ -397,7 +401,7 @@ func (s *Service) GetUserIdentifiers(ctx context.Context, ns claims.NamespaceInf
|
||||
userIDQuery = store.UserIdentifierQuery{UserUID: userUID}
|
||||
}
|
||||
userIdentifiers, err := s.store.GetUserIdentifiers(ctx, userIDQuery)
|
||||
if err != nil || userIdentifiers == nil {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get user internal id: %w", err)
|
||||
}
|
||||
|
||||
@@ -407,7 +411,7 @@ func (s *Service) GetUserIdentifiers(ctx context.Context, ns claims.NamespaceInf
|
||||
return userIdentifiers, nil
|
||||
}
|
||||
|
||||
func (s *Service) getUserTeams(ctx context.Context, ns claims.NamespaceInfo, userIdentifiers *store.UserIdentifiers) ([]int64, error) {
|
||||
func (s *Service) getUserTeams(ctx context.Context, ns types.NamespaceInfo, userIdentifiers *store.UserIdentifiers) ([]int64, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "authz_direct_db.service.getUserTeams")
|
||||
defer span.End()
|
||||
|
||||
@@ -441,7 +445,7 @@ func (s *Service) getUserTeams(ctx context.Context, ns claims.NamespaceInfo, use
|
||||
return teamIDs, nil
|
||||
}
|
||||
|
||||
func (s *Service) getUserBasicRole(ctx context.Context, ns claims.NamespaceInfo, userIdentifiers *store.UserIdentifiers) (store.BasicRole, error) {
|
||||
func (s *Service) getUserBasicRole(ctx context.Context, ns types.NamespaceInfo, userIdentifiers *store.UserIdentifiers) (store.BasicRole, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "authz_direct_db.service.getUserBasicRole")
|
||||
defer span.End()
|
||||
|
||||
@@ -535,7 +539,7 @@ func (s *Service) checkInheritedPermissions(ctx context.Context, scopeMap map[st
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (s *Service) buildFolderTree(ctx context.Context, ns claims.NamespaceInfo) (folderTree, error) {
|
||||
func (s *Service) buildFolderTree(ctx context.Context, ns types.NamespaceInfo) (folderTree, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "authz_direct_db.service.buildFolderTree")
|
||||
defer span.End()
|
||||
|
||||
@@ -545,7 +549,7 @@ func (s *Service) buildFolderTree(ctx context.Context, ns claims.NamespaceInfo)
|
||||
}
|
||||
|
||||
res, err, _ := s.sf.Do(ns.Value+"_buildFolderTree", func() (interface{}, error) {
|
||||
folders, err := s.store.GetFolders(ctx, ns)
|
||||
folders, err := s.folderStore.ListFolders(ctx, ns)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get folders: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user