Replace signed in user for identity.requester (#73750)
* Make identity.Requester available at Context * Clean pkg/services/guardian/guardian.go * Clean guardian provider and guardian AC * Clean pkg/api/team.go * Clean ctxhandler, datasources, plugin and live * Question: what to do with the UserDisplayDTO? * Clean dashboards and guardian * Remove identity.Requester from ReqContext * Implement NewUserDisplayDTOFromRequester * Fix tests * Change status code numbers for http constants * Upgrade signature of ngalert services * log parsing errors instead of throwing error * Fix tests and add logs * linting
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
||||
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/httpclient"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/services/auth/identity"
|
||||
)
|
||||
|
||||
// DataSourceService interface for interacting with datasources.
|
||||
@@ -64,8 +64,8 @@ type DataSourceService interface {
|
||||
// CacheService interface for retrieving a cached datasource.
|
||||
type CacheService interface {
|
||||
// GetDatasource gets a datasource identified by datasource numeric identifier.
|
||||
GetDatasource(ctx context.Context, datasourceID int64, user *user.SignedInUser, skipCache bool) (*DataSource, error)
|
||||
GetDatasource(ctx context.Context, datasourceID int64, user identity.Requester, skipCache bool) (*DataSource, error)
|
||||
|
||||
// GetDatasourceByUID gets a datasource identified by datasource unique identifier (UID).
|
||||
GetDatasourceByUID(ctx context.Context, datasourceUID string, user *user.SignedInUser, skipCache bool) (*DataSource, error)
|
||||
GetDatasourceByUID(ctx context.Context, datasourceUID string, user identity.Requester, skipCache bool) (*DataSource, error)
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ package datasources
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/auth/identity"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
type FakeCacheService struct {
|
||||
@@ -13,7 +13,7 @@ type FakeCacheService struct {
|
||||
|
||||
var _ datasources.CacheService = &FakeCacheService{}
|
||||
|
||||
func (c *FakeCacheService) GetDatasource(ctx context.Context, datasourceID int64, user *user.SignedInUser, skipCache bool) (*datasources.DataSource, error) {
|
||||
func (c *FakeCacheService) GetDatasource(ctx context.Context, datasourceID int64, user identity.Requester, skipCache bool) (*datasources.DataSource, error) {
|
||||
for _, datasource := range c.DataSources {
|
||||
if datasource.ID == datasourceID {
|
||||
return datasource, nil
|
||||
@@ -22,7 +22,7 @@ func (c *FakeCacheService) GetDatasource(ctx context.Context, datasourceID int64
|
||||
return nil, datasources.ErrDataSourceNotFound
|
||||
}
|
||||
|
||||
func (c *FakeCacheService) GetDatasourceByUID(ctx context.Context, datasourceUID string, user *user.SignedInUser, skipCache bool) (*datasources.DataSource, error) {
|
||||
func (c *FakeCacheService) GetDatasourceByUID(ctx context.Context, datasourceUID string, user identity.Requester, skipCache bool) (*datasources.DataSource, error) {
|
||||
for _, datasource := range c.DataSources {
|
||||
if datasource.UID == datasourceUID {
|
||||
return datasource, nil
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package guardian
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/services/auth/identity"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
type DatasourceGuardianProvider interface {
|
||||
New(orgID int64, user *user.SignedInUser, dataSources ...datasources.DataSource) DatasourceGuardian
|
||||
New(orgID int64, user identity.Requester, dataSources ...datasources.DataSource) DatasourceGuardian
|
||||
}
|
||||
|
||||
type DatasourceGuardian interface {
|
||||
@@ -20,6 +20,6 @@ func ProvideGuardian() *OSSProvider {
|
||||
|
||||
type OSSProvider struct{}
|
||||
|
||||
func (p *OSSProvider) New(orgID int64, user *user.SignedInUser, dataSources ...datasources.DataSource) DatasourceGuardian {
|
||||
func (p *OSSProvider) New(orgID int64, user identity.Requester, dataSources ...datasources.DataSource) DatasourceGuardian {
|
||||
return &AllowGuardian{}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/infra/localcache"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/services/auth/identity"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/datasources/guardian"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -38,7 +38,7 @@ type CacheServiceImpl struct {
|
||||
func (dc *CacheServiceImpl) GetDatasource(
|
||||
ctx context.Context,
|
||||
datasourceID int64,
|
||||
user *user.SignedInUser,
|
||||
user identity.Requester,
|
||||
skipCache bool,
|
||||
) (*datasources.DataSource, error) {
|
||||
cacheKey := idKey(datasourceID)
|
||||
@@ -46,7 +46,7 @@ func (dc *CacheServiceImpl) GetDatasource(
|
||||
if !skipCache {
|
||||
if cached, found := dc.CacheService.Get(cacheKey); found {
|
||||
ds := cached.(*datasources.DataSource)
|
||||
if ds.OrgID == user.OrgID {
|
||||
if ds.OrgID == user.GetOrgID() {
|
||||
if err := dc.canQuery(user, ds); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -55,9 +55,9 @@ func (dc *CacheServiceImpl) GetDatasource(
|
||||
}
|
||||
}
|
||||
|
||||
dc.logger.FromContext(ctx).Debug("Querying for data source via SQL store", "id", datasourceID, "orgId", user.OrgID)
|
||||
dc.logger.FromContext(ctx).Debug("Querying for data source via SQL store", "id", datasourceID, "orgId", user.GetOrgID())
|
||||
|
||||
query := &datasources.GetDataSourceQuery{ID: datasourceID, OrgID: user.OrgID}
|
||||
query := &datasources.GetDataSourceQuery{ID: datasourceID, OrgID: user.GetOrgID()}
|
||||
ss := SqlStore{db: dc.SQLStore, logger: dc.logger}
|
||||
ds, err := ss.GetDataSource(ctx, query)
|
||||
if err != nil {
|
||||
@@ -79,21 +79,21 @@ func (dc *CacheServiceImpl) GetDatasource(
|
||||
func (dc *CacheServiceImpl) GetDatasourceByUID(
|
||||
ctx context.Context,
|
||||
datasourceUID string,
|
||||
user *user.SignedInUser,
|
||||
user identity.Requester,
|
||||
skipCache bool,
|
||||
) (*datasources.DataSource, error) {
|
||||
if datasourceUID == "" {
|
||||
return nil, fmt.Errorf("can not get data source by uid, uid is empty")
|
||||
}
|
||||
if user.OrgID == 0 {
|
||||
if user.GetOrgID() == 0 {
|
||||
return nil, fmt.Errorf("can not get data source by uid, orgId is missing")
|
||||
}
|
||||
uidCacheKey := uidKey(user.OrgID, datasourceUID)
|
||||
uidCacheKey := uidKey(user.GetOrgID(), datasourceUID)
|
||||
|
||||
if !skipCache {
|
||||
if cached, found := dc.CacheService.Get(uidCacheKey); found {
|
||||
ds := cached.(*datasources.DataSource)
|
||||
if ds.OrgID == user.OrgID {
|
||||
if ds.OrgID == user.GetOrgID() {
|
||||
if err := dc.canQuery(user, ds); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -102,8 +102,8 @@ func (dc *CacheServiceImpl) GetDatasourceByUID(
|
||||
}
|
||||
}
|
||||
|
||||
dc.logger.FromContext(ctx).Debug("Querying for data source via SQL store", "uid", datasourceUID, "orgId", user.OrgID)
|
||||
query := &datasources.GetDataSourceQuery{UID: datasourceUID, OrgID: user.OrgID}
|
||||
dc.logger.FromContext(ctx).Debug("Querying for data source via SQL store", "uid", datasourceUID, "orgId", user.GetOrgID())
|
||||
query := &datasources.GetDataSourceQuery{UID: datasourceUID, OrgID: user.GetOrgID()}
|
||||
ss := SqlStore{db: dc.SQLStore, logger: dc.logger}
|
||||
ds, err := ss.GetDataSource(ctx, query)
|
||||
if err != nil {
|
||||
@@ -128,8 +128,8 @@ func uidKey(orgID int64, uid string) string {
|
||||
return fmt.Sprintf("ds-orgid-uid-%d-%s", orgID, uid)
|
||||
}
|
||||
|
||||
func (dc *CacheServiceImpl) canQuery(user *user.SignedInUser, ds *datasources.DataSource) error {
|
||||
guardian := dc.dsGuardian.New(user.OrgID, user, *ds)
|
||||
func (dc *CacheServiceImpl) canQuery(user identity.Requester, ds *datasources.DataSource) error {
|
||||
guardian := dc.dsGuardian.New(user.GetOrgID(), user, *ds)
|
||||
if canQuery, err := guardian.CanQuery(ds.ID); err != nil || !canQuery {
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user