diff --git a/pkg/services/authn/grpcutils/namespace_checker.go b/pkg/services/authn/grpcutils/namespace_checker.go new file mode 100644 index 00000000000..6ba656e7bdf --- /dev/null +++ b/pkg/services/authn/grpcutils/namespace_checker.go @@ -0,0 +1,23 @@ +package grpcutils + +import ( + authzlib "github.com/grafana/authlib/authz" + "github.com/grafana/authlib/claims" + + "github.com/grafana/grafana/pkg/setting" +) + +func NewNamespaceAccessChecker(cfg *setting.Cfg) authzlib.NamespaceAccessChecker { + if cfg.StackID != "" { + return authzlib.NewNamespaceAccessChecker( + claims.CloudNamespaceFormatter, + authzlib.WithIDTokenNamespaceAccessCheckerOption(true), + ) + } + + return authzlib.NewNamespaceAccessChecker( + claims.OrgNamespaceFormatter, + authzlib.WithDisableAccessTokenNamespaceAccessCheckerOption(), + authzlib.WithIDTokenNamespaceAccessCheckerOption(true), + ) +} diff --git a/pkg/services/grpcserver/service.go b/pkg/services/grpcserver/service.go index f63b670780a..db61e749474 100644 --- a/pkg/services/grpcserver/service.go +++ b/pkg/services/grpcserver/service.go @@ -7,7 +7,6 @@ import ( "time" authzlib "github.com/grafana/authlib/authz" - "github.com/grafana/authlib/claims" "github.com/grafana/dskit/instrument" "github.com/grafana/dskit/middleware" "github.com/grafana/grafana-plugin-sdk-go/backend" @@ -20,6 +19,7 @@ import ( "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/tracing" "github.com/grafana/grafana/pkg/registry" + "github.com/grafana/grafana/pkg/services/authn/grpcutils" "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/grpcserver/interceptors" "github.com/grafana/grafana/pkg/setting" @@ -73,13 +73,7 @@ func ProvideService(cfg *setting.Cfg, features featuremgmt.FeatureToggles, authe var opts []grpc.ServerOption - // We don't need to support the CloudNamespaceFormatter here, because the grpcserver is only used on-prem. - // TODO(drclau): validate this assumption. - namespaceChecker := authzlib.NewNamespaceAccessChecker( - claims.OrgNamespaceFormatter, - authzlib.WithDisableAccessTokenNamespaceAccessCheckerOption(), - authzlib.WithIDTokenNamespaceAccessCheckerOption(true), - ) + namespaceChecker := grpcutils.NewNamespaceAccessChecker(cfg) stackIdExtractor := authzlib.MetadataStackIDExtractor(authzlib.DefaultStackIDMetadataKey) // Default auth is admin token check, but this can be overridden by