diff --git a/pkg/services/export/commit_helper.go b/pkg/services/export/commit_helper.go index 221912c7738..0cff51fcf76 100644 --- a/pkg/services/export/commit_helper.go +++ b/pkg/services/export/commit_helper.go @@ -13,8 +13,8 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/data" jsoniter "github.com/json-iterator/go" + "github.com/grafana/grafana/pkg/infra/appcontext" "github.com/grafana/grafana/pkg/infra/db" - "github.com/grafana/grafana/pkg/services/store" "github.com/grafana/grafana/pkg/services/user" ) @@ -77,7 +77,7 @@ func (ch *commitHelper) initOrg(ctx context.Context, sql db.DB, orgID int64) err OrgID: orgID, // gets filled in from each row UserID: 0, } - ch.ctx = store.ContextWithUser(context.Background(), rowUser) + ch.ctx = appcontext.WithUser(context.Background(), rowUser) return err }) } diff --git a/pkg/services/export/entity_store.go b/pkg/services/export/entity_store.go index 630e683cec9..53ed63f9e5d 100644 --- a/pkg/services/export/entity_store.go +++ b/pkg/services/export/entity_store.go @@ -6,13 +6,13 @@ import ( "sync" "time" + "github.com/grafana/grafana/pkg/infra/appcontext" "github.com/grafana/grafana/pkg/infra/db" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/dashboardsnapshots" "github.com/grafana/grafana/pkg/services/playlist" "github.com/grafana/grafana/pkg/services/sqlstore/session" - "github.com/grafana/grafana/pkg/services/store" "github.com/grafana/grafana/pkg/services/store/entity" "github.com/grafana/grafana/pkg/services/store/kind/snapshot" "github.com/grafana/grafana/pkg/services/user" @@ -101,7 +101,7 @@ func (e *entityStoreJob) start(ctx context.Context) { OrgID: 0, // gets filled in from each row UserID: 0, } - ctx = store.ContextWithUser(ctx, rowUser) + ctx = appcontext.WithUser(ctx, rowUser) what := models.StandardKindDashboard e.status.Count[what] = 0 diff --git a/pkg/services/export/service.go b/pkg/services/export/service.go index a1e7950e784..3e7f03e14d1 100644 --- a/pkg/services/export/service.go +++ b/pkg/services/export/service.go @@ -11,6 +11,7 @@ import ( "time" "github.com/grafana/grafana/pkg/api/response" + "github.com/grafana/grafana/pkg/infra/appcontext" "github.com/grafana/grafana/pkg/infra/db" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/models" @@ -20,7 +21,6 @@ import ( "github.com/grafana/grafana/pkg/services/live" "github.com/grafana/grafana/pkg/services/org" "github.com/grafana/grafana/pkg/services/playlist" - "github.com/grafana/grafana/pkg/services/store" "github.com/grafana/grafana/pkg/services/store/entity" "github.com/grafana/grafana/pkg/setting" ) @@ -225,7 +225,7 @@ func (ex *StandardExport) HandleRequestExport(c *models.ReqContext) response.Res return response.Error(http.StatusLocked, "export already running", nil) } - ctx := store.ContextWithUser(context.Background(), c.SignedInUser) + ctx := appcontext.WithUser(context.Background(), c.SignedInUser) var job Job broadcast := func(s ExportStatus) { ex.broadcastStatus(c.OrgID, s) diff --git a/pkg/services/playlist/playlistimpl/entity_store.go b/pkg/services/playlist/playlistimpl/entity_store.go index 4d44ad85e2a..068302975dc 100644 --- a/pkg/services/playlist/playlistimpl/entity_store.go +++ b/pkg/services/playlist/playlistimpl/entity_store.go @@ -5,10 +5,10 @@ import ( "encoding/json" "fmt" + "github.com/grafana/grafana/pkg/infra/appcontext" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/playlist" "github.com/grafana/grafana/pkg/services/sqlstore/session" - objectstore "github.com/grafana/grafana/pkg/services/store" "github.com/grafana/grafana/pkg/services/store/entity" "github.com/grafana/grafana/pkg/services/user" ) @@ -44,7 +44,7 @@ func (s *entityStoreImpl) sync() { UserID: 0, // Admin user IsGrafanaAdmin: true, } - ctx := objectstore.ContextWithUser(context.Background(), rowUser) + ctx := appcontext.WithUser(context.Background(), rowUser) for _, info := range results { dto, err := s.sqlimpl.Get(ctx, &playlist.GetPlaylistByUidQuery{ OrgId: info.OrgID, diff --git a/pkg/services/store/auth.go b/pkg/services/store/auth.go index b9d7cb8bf9b..6ef47fe94f9 100644 --- a/pkg/services/store/auth.go +++ b/pkg/services/store/auth.go @@ -1,44 +1,11 @@ package store import ( - "context" "fmt" - "github.com/grafana/grafana/pkg/models" - "github.com/grafana/grafana/pkg/services/contexthandler/ctxkey" - grpccontext "github.com/grafana/grafana/pkg/services/grpcserver/context" "github.com/grafana/grafana/pkg/services/user" ) -type testUserKey struct{} - -func ContextWithUser(ctx context.Context, data *user.SignedInUser) context.Context { - return context.WithValue(ctx, testUserKey{}, data) -} - -// UserFromContext ** Experimental ** -// TODO: move to global infra package / new auth service -func UserFromContext(ctx context.Context) *user.SignedInUser { - grpcCtx := grpccontext.FromContext(ctx) - if grpcCtx != nil { - return grpcCtx.SignedInUser - } - - // Explicitly set in context - u, ok := ctx.Value(testUserKey{}).(*user.SignedInUser) - if ok && u != nil { - return u - } - - // From the HTTP request - c, ok := ctxkey.Get(ctx).(*models.ReqContext) - if !ok || c == nil || c.SignedInUser == nil { - return nil - } - - return c.SignedInUser -} - // Really just spitballing here :) this should hook into a system that can give better display info func GetUserIDString(user *user.SignedInUser) string { if user == nil { diff --git a/pkg/services/store/entity/sqlstash/sql_storage_server.go b/pkg/services/store/entity/sqlstash/sql_storage_server.go index 3dc4d90e7a5..132f47c3428 100644 --- a/pkg/services/store/entity/sqlstash/sql_storage_server.go +++ b/pkg/services/store/entity/sqlstash/sql_storage_server.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "github.com/grafana/grafana/pkg/infra/appcontext" "github.com/grafana/grafana/pkg/infra/db" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/slugify" @@ -109,7 +110,10 @@ func (s *sqlEntityServer) validateGRN(ctx context.Context, grn *entity.GRN) (*en if grn == nil { return nil, fmt.Errorf("missing GRN") } - user := store.UserFromContext(ctx) + user, err := appcontext.User(ctx) + if err != nil { + return nil, err + } if grn.TenantId == 0 { grn.TenantId = user.OrgID } else if grn.TenantId != user.OrgID { @@ -281,7 +285,10 @@ func (s *sqlEntityServer) AdminWrite(ctx context.Context, r *entity.AdminWriteEn updatedAt := r.UpdatedAt updatedBy := r.UpdatedBy if updatedBy == "" { - modifier := store.UserFromContext(ctx) + modifier, err := appcontext.User(ctx) + if err != nil { + return nil, err + } if modifier == nil { return nil, fmt.Errorf("can not find user in context") } @@ -618,7 +625,10 @@ func (s *sqlEntityServer) History(ctx context.Context, r *entity.EntityHistoryRe } func (s *sqlEntityServer) Search(ctx context.Context, r *entity.EntitySearchRequest) (*entity.EntitySearchResponse, error) { - user := store.UserFromContext(ctx) + user, err := appcontext.User(ctx) + if err != nil { + return nil, err + } if user == nil { return nil, fmt.Errorf("missing user in context") } diff --git a/pkg/services/store/entity/tests/common.go b/pkg/services/store/entity/tests/common.go index 78628eccbaf..b40122f9cef 100644 --- a/pkg/services/store/entity/tests/common.go +++ b/pkg/services/store/entity/tests/common.go @@ -5,12 +5,12 @@ import ( "testing" apikeygenprefix "github.com/grafana/grafana/pkg/components/apikeygenprefixed" + "github.com/grafana/grafana/pkg/infra/appcontext" "github.com/grafana/grafana/pkg/server" "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/org" saAPI "github.com/grafana/grafana/pkg/services/serviceaccounts/api" saTests "github.com/grafana/grafana/pkg/services/serviceaccounts/tests" - "github.com/grafana/grafana/pkg/services/store" "github.com/grafana/grafana/pkg/services/store/entity" "github.com/grafana/grafana/pkg/services/user" "github.com/grafana/grafana/pkg/tests/testinfra" @@ -86,6 +86,6 @@ func createTestContext(t *testing.T) testContext { authToken: authToken, client: client, user: serviceAccountUser, - ctx: store.ContextWithUser(context.Background(), serviceAccountUser), + ctx: appcontext.WithUser(context.Background(), serviceAccountUser), } } diff --git a/pkg/services/store/resolver/ds_cache.go b/pkg/services/store/resolver/ds_cache.go index 64081d057a7..93c648ebc8f 100644 --- a/pkg/services/store/resolver/ds_cache.go +++ b/pkg/services/store/resolver/ds_cache.go @@ -6,9 +6,9 @@ import ( "sync" "time" + "github.com/grafana/grafana/pkg/infra/appcontext" "github.com/grafana/grafana/pkg/plugins" "github.com/grafana/grafana/pkg/services/datasources" - "github.com/grafana/grafana/pkg/services/store" "github.com/grafana/grafana/pkg/tsdb/grafanads" ) @@ -122,9 +122,12 @@ func (c *dsCache) getDS(ctx context.Context, uid string) (*dsVal, error) { } } - orgID := store.UserFromContext(ctx).OrgID + usr, err := appcontext.User(ctx) + if err != nil { + return nil, nil // no user + } - v, ok := c.cache[orgID] + v, ok := c.cache[usr.OrgID] if !ok { return nil, nil // org not found } diff --git a/pkg/services/store/resolver/service_test.go b/pkg/services/store/resolver/service_test.go index 92d305761cc..b420b27a508 100644 --- a/pkg/services/store/resolver/service_test.go +++ b/pkg/services/store/resolver/service_test.go @@ -4,17 +4,17 @@ import ( "context" "testing" + "github.com/grafana/grafana/pkg/infra/appcontext" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/plugins" "github.com/grafana/grafana/pkg/services/datasources" fakeDatasources "github.com/grafana/grafana/pkg/services/datasources/fakes" - "github.com/grafana/grafana/pkg/services/store" "github.com/grafana/grafana/pkg/services/user" "github.com/stretchr/testify/require" ) func TestResolver(t *testing.T) { - ctxOrg1 := store.ContextWithUser(context.Background(), &user.SignedInUser{OrgID: 1}) + ctxOrg1 := appcontext.WithUser(context.Background(), &user.SignedInUser{OrgID: 1}) ds := &fakeDatasources.FakeDataSourceService{ DataSources: []*datasources.DataSource{