mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
RBAC: Add cache for oss rbac permissions (#55098)
* RBAC: Add cache for oss permissions * RBAC: include service account actions * RBAC: revert changes to fetch service account permissions * Update comment for setting * RBAC: Disable permission chache for tests
This commit is contained in:
parent
716bdde3f6
commit
870929b463
@ -666,7 +666,7 @@ managed_identity_client_id =
|
|||||||
|
|
||||||
#################################### Role-based Access Control ###########
|
#################################### Role-based Access Control ###########
|
||||||
[rbac]
|
[rbac]
|
||||||
# If enabled, cache permissions in a in memory cache (Enterprise only)
|
# If enabled, cache permissions in a in memory cache
|
||||||
permission_cache = true
|
permission_cache = true
|
||||||
|
|
||||||
#################################### SMTP / Emailing #####################
|
#################################### SMTP / Emailing #####################
|
||||||
|
@ -17,6 +17,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/api/response"
|
"github.com/grafana/grafana/pkg/api/response"
|
||||||
"github.com/grafana/grafana/pkg/api/routing"
|
"github.com/grafana/grafana/pkg/api/routing"
|
||||||
"github.com/grafana/grafana/pkg/infra/fs"
|
"github.com/grafana/grafana/pkg/infra/fs"
|
||||||
|
"github.com/grafana/grafana/pkg/infra/localcache"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/infra/remotecache"
|
"github.com/grafana/grafana/pkg/infra/remotecache"
|
||||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||||
@ -379,7 +380,7 @@ func setupHTTPServerWithCfgDb(
|
|||||||
acService = acmock
|
acService = acmock
|
||||||
} else {
|
} else {
|
||||||
var err error
|
var err error
|
||||||
acService, err = acimpl.ProvideService(cfg, database.ProvideService(db), routeRegister)
|
acService, err = acimpl.ProvideService(cfg, database.ProvideService(db), routeRegister, localcache.ProvideService())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
ac = acimpl.ProvideAccessControl(cfg)
|
ac = acimpl.ProvideAccessControl(cfg)
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,11 @@ package acimpl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/routing"
|
"github.com/grafana/grafana/pkg/api/routing"
|
||||||
|
"github.com/grafana/grafana/pkg/infra/localcache"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||||
@ -14,8 +17,12 @@ import (
|
|||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ProvideService(cfg *setting.Cfg, store accesscontrol.Store, routeRegister routing.RouteRegister) (*Service, error) {
|
const (
|
||||||
service := ProvideOSSService(cfg, store)
|
cacheTTL = 10 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
func ProvideService(cfg *setting.Cfg, store accesscontrol.Store, routeRegister routing.RouteRegister, cache *localcache.CacheService) (*Service, error) {
|
||||||
|
service := ProvideOSSService(cfg, store, cache)
|
||||||
|
|
||||||
if !accesscontrol.IsDisabled(cfg) {
|
if !accesscontrol.IsDisabled(cfg) {
|
||||||
api.NewAccessControlAPI(routeRegister, service).RegisterAPIEndpoints()
|
api.NewAccessControlAPI(routeRegister, service).RegisterAPIEndpoints()
|
||||||
@ -27,11 +34,12 @@ func ProvideService(cfg *setting.Cfg, store accesscontrol.Store, routeRegister r
|
|||||||
return service, nil
|
return service, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProvideOSSService(cfg *setting.Cfg, store accesscontrol.Store) *Service {
|
func ProvideOSSService(cfg *setting.Cfg, store accesscontrol.Store, cache *localcache.CacheService) *Service {
|
||||||
s := &Service{
|
s := &Service{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
store: store,
|
store: store,
|
||||||
log: log.New("accesscontrol.service"),
|
log: log.New("accesscontrol.service"),
|
||||||
|
cache: cache,
|
||||||
roles: accesscontrol.BuildBasicRoleDefinitions(),
|
roles: accesscontrol.BuildBasicRoleDefinitions(),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +51,7 @@ type Service struct {
|
|||||||
log log.Logger
|
log log.Logger
|
||||||
cfg *setting.Cfg
|
cfg *setting.Cfg
|
||||||
store accesscontrol.Store
|
store accesscontrol.Store
|
||||||
|
cache *localcache.CacheService
|
||||||
registrations accesscontrol.RegistrationList
|
registrations accesscontrol.RegistrationList
|
||||||
roles map[string]*accesscontrol.RoleDTO
|
roles map[string]*accesscontrol.RoleDTO
|
||||||
}
|
}
|
||||||
@ -63,12 +72,19 @@ var actionsToFetch = append(
|
|||||||
)
|
)
|
||||||
|
|
||||||
// GetUserPermissions returns user permissions based on built-in roles
|
// GetUserPermissions returns user permissions based on built-in roles
|
||||||
func (s *Service) GetUserPermissions(ctx context.Context, user *user.SignedInUser, _ accesscontrol.Options) ([]accesscontrol.Permission, error) {
|
func (s *Service) GetUserPermissions(ctx context.Context, user *user.SignedInUser, options accesscontrol.Options) ([]accesscontrol.Permission, error) {
|
||||||
timer := prometheus.NewTimer(metrics.MAccessPermissionsSummary)
|
timer := prometheus.NewTimer(metrics.MAccessPermissionsSummary)
|
||||||
defer timer.ObserveDuration()
|
defer timer.ObserveDuration()
|
||||||
|
|
||||||
permissions := make([]accesscontrol.Permission, 0)
|
if !s.cfg.RBACPermissionCache || !user.HasUniqueId() {
|
||||||
|
return s.getUserPermissions(ctx, user, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.getCachedUserPermissions(ctx, user, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) getUserPermissions(ctx context.Context, user *user.SignedInUser, options accesscontrol.Options) ([]accesscontrol.Permission, error) {
|
||||||
|
permissions := make([]accesscontrol.Permission, 0)
|
||||||
for _, builtin := range accesscontrol.GetOrgRoles(user) {
|
for _, builtin := range accesscontrol.GetOrgRoles(user) {
|
||||||
if basicRole, ok := s.roles[builtin]; ok {
|
if basicRole, ok := s.roles[builtin]; ok {
|
||||||
permissions = append(permissions, basicRole.Permissions...)
|
permissions = append(permissions, basicRole.Permissions...)
|
||||||
@ -89,6 +105,32 @@ func (s *Service) GetUserPermissions(ctx context.Context, user *user.SignedInUse
|
|||||||
return append(permissions, dbPermissions...), nil
|
return append(permissions, dbPermissions...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Service) getCachedUserPermissions(ctx context.Context, user *user.SignedInUser, options accesscontrol.Options) ([]accesscontrol.Permission, error) {
|
||||||
|
key, err := permissionCacheKey(user)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !options.ReloadCache {
|
||||||
|
permissions, ok := s.cache.Get(key)
|
||||||
|
if ok {
|
||||||
|
s.log.Debug("using cached permissions", "key", key)
|
||||||
|
return permissions.([]accesscontrol.Permission), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s.log.Debug("fetch permissions from store", "key", key)
|
||||||
|
permissions, err := s.getUserPermissions(ctx, user, options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.log.Debug("cache permissions", "key", key)
|
||||||
|
s.cache.Set(key, permissions, cacheTTL)
|
||||||
|
|
||||||
|
return permissions, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Service) DeleteUserPermissions(ctx context.Context, orgID int64, userID int64) error {
|
func (s *Service) DeleteUserPermissions(ctx context.Context, orgID int64, userID int64) error {
|
||||||
return s.store.DeleteUserPermissions(ctx, orgID, userID)
|
return s.store.DeleteUserPermissions(ctx, orgID, userID)
|
||||||
}
|
}
|
||||||
@ -140,3 +182,11 @@ func (s *Service) RegisterFixedRoles(ctx context.Context) error {
|
|||||||
func (s *Service) IsDisabled() bool {
|
func (s *Service) IsDisabled() bool {
|
||||||
return accesscontrol.IsDisabled(s.cfg)
|
return accesscontrol.IsDisabled(s.cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func permissionCacheKey(user *user.SignedInUser) (string, error) {
|
||||||
|
key, err := user.GetCacheKey()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("rbac-permissions-%s", key), nil
|
||||||
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/routing"
|
"github.com/grafana/grafana/pkg/api/routing"
|
||||||
|
"github.com/grafana/grafana/pkg/infra/localcache"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||||
@ -59,6 +60,7 @@ func TestUsageMetrics(t *testing.T) {
|
|||||||
cfg,
|
cfg,
|
||||||
database.ProvideService(sqlstore.InitTestDB(t)),
|
database.ProvideService(sqlstore.InitTestDB(t)),
|
||||||
routing.NewRouteRegister(),
|
routing.NewRouteRegister(),
|
||||||
|
localcache.ProvideService(),
|
||||||
)
|
)
|
||||||
require.NoError(t, errInitAc)
|
require.NoError(t, errInitAc)
|
||||||
assert.Equal(t, tt.expectedValue, s.GetUsageStats(context.Background())["stats.oss.accesscontrol.enabled.count"])
|
assert.Equal(t, tt.expectedValue, s.GetUsageStats(context.Background())["stats.oss.accesscontrol.enabled.count"])
|
||||||
|
@ -258,6 +258,20 @@ func (e DatasourcePermissionsService) MapActions(permission accesscontrol.Resour
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ServiceAccountEditActions = []string{
|
||||||
|
serviceaccounts.ActionRead,
|
||||||
|
serviceaccounts.ActionWrite,
|
||||||
|
}
|
||||||
|
ServiceAccountAdminActions = []string{
|
||||||
|
serviceaccounts.ActionRead,
|
||||||
|
serviceaccounts.ActionWrite,
|
||||||
|
serviceaccounts.ActionDelete,
|
||||||
|
serviceaccounts.ActionPermissionsRead,
|
||||||
|
serviceaccounts.ActionPermissionsWrite,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
type ServiceAccountPermissionsService struct {
|
type ServiceAccountPermissionsService struct {
|
||||||
*resourcepermissions.Service
|
*resourcepermissions.Service
|
||||||
}
|
}
|
||||||
@ -283,8 +297,8 @@ func ProvideServiceAccountPermissions(
|
|||||||
BuiltInRoles: false,
|
BuiltInRoles: false,
|
||||||
},
|
},
|
||||||
PermissionsToActions: map[string][]string{
|
PermissionsToActions: map[string][]string{
|
||||||
"Edit": {serviceaccounts.ActionRead, serviceaccounts.ActionWrite},
|
"Edit": ServiceAccountEditActions,
|
||||||
"Admin": {serviceaccounts.ActionRead, serviceaccounts.ActionWrite, serviceaccounts.ActionDelete, serviceaccounts.ActionPermissionsRead, serviceaccounts.ActionPermissionsWrite},
|
"Admin": ServiceAccountAdminActions,
|
||||||
},
|
},
|
||||||
ReaderRoleName: "Service account permission reader",
|
ReaderRoleName: "Service account permission reader",
|
||||||
WriterRoleName: "Service account permission writer",
|
WriterRoleName: "Service account permission writer",
|
||||||
|
@ -202,6 +202,11 @@ func CreateGrafDir(t *testing.T, opts ...GrafanaOpts) (string, string) {
|
|||||||
_, err = alertingSect.NewKey("max_attempts", "3")
|
_, err = alertingSect.NewKey("max_attempts", "3")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
rbacSect, err := cfg.NewSection("rbac")
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = rbacSect.NewKey("permission_cache", "false")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
getOrCreateSection := func(name string) (*ini.Section, error) {
|
getOrCreateSection := func(name string) (*ini.Section, error) {
|
||||||
section, err := cfg.GetSection(name)
|
section, err := cfg.GetSection(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user