Chore: Remove GetSignedInUserWithCacheCtx from store interface (#53734)

* Remove delete suer from store interface

* Remove get signed in user with cache ctx from store interface

* Support options when setting up access control tests

* Fix broken tests

* Fix lint

* Add user fake to middleware

* Fix middleware tests, remove usertest being initialised twice

Co-authored-by: Karl Persson <kalle.persson@grafana.com>
This commit is contained in:
idafurjes
2022-08-16 16:08:59 +02:00
committed by GitHub
co-authored by Karl Persson
parent f3085b1cac
commit fa2e74cd6e
16 changed files with 119 additions and 97 deletions
+6 -6
View File
@@ -92,7 +92,7 @@ func newID() string {
type OrgIDGetter func(c *models.ReqContext) (int64, error)
type userCache interface {
GetSignedInUserWithCacheCtx(ctx context.Context, query *models.GetSignedInUserQuery) error
GetSignedInUserWithCacheCtx(ctx context.Context, query *user.GetSignedInUserQuery) (*user.SignedInUser, error)
}
func AuthorizeInOrgMiddleware(ac AccessControl, cache userCache) func(web.Handler, OrgIDGetter, Evaluator) web.Handler {
@@ -114,15 +114,15 @@ func AuthorizeInOrgMiddleware(ac AccessControl, cache userCache) func(web.Handle
userCopy.OrgName = ""
userCopy.OrgRole = ""
} else {
query := models.GetSignedInUserQuery{UserId: c.UserID, OrgId: orgID}
err := cache.GetSignedInUserWithCacheCtx(c.Req.Context(), &query)
query := user.GetSignedInUserQuery{UserID: c.UserID, OrgID: orgID}
queryResult, err := cache.GetSignedInUserWithCacheCtx(c.Req.Context(), &query)
if err != nil {
deny(c, nil, fmt.Errorf("failed to authenticate user in target org: %w", err))
return
}
userCopy.OrgID = query.Result.OrgID
userCopy.OrgName = query.Result.OrgName
userCopy.OrgRole = query.Result.OrgRole
userCopy.OrgID = queryResult.OrgID
userCopy.OrgName = queryResult.OrgName
userCopy.OrgRole = queryResult.OrgRole
}
authorize(c, ac, &userCopy, evaluator)
+3 -3
View File
@@ -33,7 +33,7 @@ func (h *ContextHandler) initContextWithJWT(ctx *models.ReqContext, orgId int64)
return true
}
query := models.GetSignedInUserQuery{OrgId: orgId}
query := user.GetSignedInUserQuery{OrgID: orgId}
sub, _ := claims["sub"].(string)
@@ -83,7 +83,7 @@ func (h *ContextHandler) initContextWithJWT(ctx *models.ReqContext, orgId int64)
}
}
err = h.SQLStore.GetSignedInUserWithCacheCtx(ctx.Req.Context(), &query)
queryResult, err := h.userService.GetSignedInUserWithCacheCtx(ctx.Req.Context(), &query)
if err != nil {
if errors.Is(err, user.ErrUserNotFound) {
ctx.Logger.Debug(
@@ -100,7 +100,7 @@ func (h *ContextHandler) initContextWithJWT(ctx *models.ReqContext, orgId int64)
return true
}
ctx.SignedInUser = query.Result
ctx.SignedInUser = queryResult
ctx.IsSignedIn = true
return true
+15 -15
View File
@@ -300,13 +300,13 @@ func (h *ContextHandler) initContextWithAPIKey(reqContext *models.ReqContext) bo
//There is a service account attached to the API key
//Use service account linked to API key as the signed in user
querySignedInUser := models.GetSignedInUserQuery{UserId: *apikey.ServiceAccountId, OrgId: apikey.OrgId}
err := h.SQLStore.GetSignedInUserWithCacheCtx(reqContext.Req.Context(), &querySignedInUser)
querySignedInUser := user.GetSignedInUserQuery{UserID: *apikey.ServiceAccountId, OrgID: apikey.OrgId}
querySignedInUserResult, err := h.userService.GetSignedInUserWithCacheCtx(reqContext.Req.Context(), &querySignedInUser)
if err != nil {
reqContext.Logger.Error(
"Failed to link API key to service account in",
"id", querySignedInUser.UserId,
"org", querySignedInUser.OrgId,
"id", querySignedInUser.UserID,
"org", querySignedInUser.OrgID,
"err", err,
)
reqContext.JsonApiErr(http.StatusInternalServerError, "Unable to link API key to service account", err)
@@ -314,13 +314,13 @@ func (h *ContextHandler) initContextWithAPIKey(reqContext *models.ReqContext) bo
}
// disabled service accounts are not allowed to access the API
if querySignedInUser.Result.IsDisabled {
if querySignedInUserResult.IsDisabled {
reqContext.JsonApiErr(http.StatusUnauthorized, "Service account is disabled", nil)
return true
}
reqContext.IsSignedIn = true
reqContext.SignedInUser = querySignedInUser.Result
reqContext.SignedInUser = querySignedInUserResult
return true
}
@@ -365,8 +365,8 @@ func (h *ContextHandler) initContextWithBasicAuth(reqContext *models.ReqContext,
usr := authQuery.User
query := models.GetSignedInUserQuery{UserId: usr.ID, OrgId: orgID}
err = h.SQLStore.GetSignedInUserWithCacheCtx(ctx, &query)
query := user.GetSignedInUserQuery{UserID: usr.ID, OrgID: orgID}
queryResult, err := h.userService.GetSignedInUserWithCacheCtx(ctx, &query)
if err != nil {
reqContext.Logger.Error(
"Failed at user signed in",
@@ -377,7 +377,7 @@ func (h *ContextHandler) initContextWithBasicAuth(reqContext *models.ReqContext,
return true
}
reqContext.SignedInUser = query.Result
reqContext.SignedInUser = queryResult
reqContext.IsSignedIn = true
return true
}
@@ -402,14 +402,14 @@ func (h *ContextHandler) initContextWithToken(reqContext *models.ReqContext, org
return false
}
query := models.GetSignedInUserQuery{UserId: token.UserId, OrgId: orgID}
err = h.SQLStore.GetSignedInUserWithCacheCtx(ctx, &query)
query := user.GetSignedInUserQuery{UserID: token.UserId, OrgID: orgID}
queryResult, err := h.userService.GetSignedInUserWithCacheCtx(ctx, &query)
if err != nil {
reqContext.Logger.Error("Failed to get user with id", "userId", token.UserId, "error", err)
return false
}
reqContext.SignedInUser = query.Result
reqContext.SignedInUser = queryResult
reqContext.IsSignedIn = true
reqContext.UserToken = token
@@ -478,10 +478,10 @@ func (h *ContextHandler) initContextWithRenderAuth(reqContext *models.ReqContext
// UserID can be 0 for background tasks and, in this case, there is no user info to retrieve
if renderUser.UserID != 0 {
query := models.GetSignedInUserQuery{UserId: renderUser.UserID, OrgId: renderUser.OrgID}
err := h.SQLStore.GetSignedInUserWithCacheCtx(ctx, &query)
query := user.GetSignedInUserQuery{UserID: renderUser.UserID, OrgID: renderUser.OrgID}
queryResult, err := h.userService.GetSignedInUserWithCacheCtx(ctx, &query)
if err == nil {
reqContext.SignedInUser = query.Result
reqContext.SignedInUser = queryResult
}
}
@@ -146,11 +146,6 @@ func (m *SQLStoreMock) GetUserOrgList(ctx context.Context, query *models.GetUser
return m.ExpectedError
}
func (m *SQLStoreMock) GetSignedInUserWithCacheCtx(ctx context.Context, query *models.GetSignedInUserQuery) error {
query.Result = m.ExpectedSignedInUser
return m.ExpectedError
}
func (m *SQLStoreMock) GetSignedInUser(ctx context.Context, query *models.GetSignedInUserQuery) error {
query.Result = m.ExpectedSignedInUser
return m.ExpectedError
-1
View File
@@ -34,7 +34,6 @@ type Store interface {
GetUserProfile(ctx context.Context, query *models.GetUserProfileQuery) error
GetUserOrgList(ctx context.Context, query *models.GetUserOrgListQuery) error
GetSignedInUser(ctx context.Context, query *models.GetSignedInUserQuery) error
GetSignedInUserWithCacheCtx(ctx context.Context, query *models.GetSignedInUserQuery) error
UpdateUserPermissions(userID int64, isAdmin bool) error
SetUserHelpFlag(ctx context.Context, cmd *models.SetUserHelpFlagCommand) error
CreateTeam(name, email string, orgID int64) (models.Team, error)
+4 -1
View File
@@ -55,10 +55,13 @@ func (f *FakeUserService) SetUsingOrg(ctx context.Context, cmd *user.SetUsingOrg
}
func (f *FakeUserService) GetSignedInUserWithCacheCtx(ctx context.Context, query *user.GetSignedInUserQuery) (*user.SignedInUser, error) {
return f.ExpectedSignedInUser, f.ExpectedError
return f.GetSignedInUser(ctx, query)
}
func (f *FakeUserService) GetSignedInUser(ctx context.Context, query *user.GetSignedInUserQuery) (*user.SignedInUser, error) {
if f.ExpectedSignedInUser == nil {
return &user.SignedInUser{}, f.ExpectedError
}
return f.ExpectedSignedInUser, f.ExpectedError
}