From d0c533d92397416c4e4e8e98b918ccfb06bdb69b Mon Sep 17 00:00:00 2001 From: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com> Date: Thu, 8 Dec 2022 12:57:54 +0200 Subject: [PATCH 1/2] Login: Fix failure to login a new user via an external provider if quota are enabled --- pkg/services/quota/model.go | 2 ++ pkg/services/quota/quotaimpl/quota.go | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/services/quota/model.go b/pkg/services/quota/model.go index 091c60c4f366..c9915bebb0cd 100644 --- a/pkg/services/quota/model.go +++ b/pkg/services/quota/model.go @@ -11,7 +11,9 @@ import ( var ErrBadRequest = errutil.NewBase(errutil.StatusBadRequest, "quota.bad-request") var ErrInvalidTargetSrv = errutil.NewBase(errutil.StatusBadRequest, "quota.invalid-target") var ErrInvalidScope = errutil.NewBase(errutil.StatusBadRequest, "quota.invalid-scope") +var ErrFailedToGetScope = errutil.NewBase(errutil.StatusInternal, "quota.failed-get-scope") var ErrInvalidTarget = errutil.NewBase(errutil.StatusInternal, "quota.invalid-target-table") +var ErrUsageFoundForTarget = errutil.NewBase(errutil.StatusNotFound, "quota.missing-target-usage") var ErrTargetSrvConflict = errutil.NewBase(errutil.StatusBadRequest, "quota.target-srv-conflict") var ErrDisabled = errutil.NewBase(errutil.StatusForbidden, "quota.disabled", errutil.WithPublicMessage("Quotas not enabled")) var ErrInvalidTagFormat = errutil.NewBase(errutil.StatusInternal, "quota.invalid-invalid-tag-format") diff --git a/pkg/services/quota/quotaimpl/quota.go b/pkg/services/quota/quotaimpl/quota.go index 9cc8be38af53..d8dd66e7230a 100644 --- a/pkg/services/quota/quotaimpl/quota.go +++ b/pkg/services/quota/quotaimpl/quota.go @@ -2,7 +2,6 @@ package quotaimpl import ( "context" - "fmt" "sync" "github.com/grafana/grafana/pkg/infra/db" @@ -205,9 +204,25 @@ func (s *service) CheckQuotaReached(ctx context.Context, targetSrv quota.TargetS case limit == 0: return true, nil default: + scope, err := t.GetScope() + if err != nil { + return false, quota.ErrFailedToGetScope.Errorf("failed to get the scope for target: %s", t) + } + + // do not check user quota if the user information is not available (eg no user is signed in) + if scope == quota.UserScope && (scopeParams == nil || scopeParams.UserID == 0) { + continue + + } + + // do not check user quota if the org information is not available (eg no user is signed in) + if scope == quota.OrgScope && (scopeParams == nil || scopeParams.OrgID == 0) { + continue + } + u, ok := targetUsage.Get(t) if !ok { - return false, fmt.Errorf("no usage for target:%s", t) + return false, quota.ErrUsageFoundForTarget.Errorf("no usage for target:%s", t) } if u >= limit { return true, nil From 9463682e9654072d9902551c457a5f0beef25da5 Mon Sep 17 00:00:00 2001 From: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com> Date: Thu, 8 Dec 2022 16:28:24 +0200 Subject: [PATCH 2/2] lint --- pkg/services/quota/quotaimpl/quota.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/services/quota/quotaimpl/quota.go b/pkg/services/quota/quotaimpl/quota.go index d8dd66e7230a..65763f204633 100644 --- a/pkg/services/quota/quotaimpl/quota.go +++ b/pkg/services/quota/quotaimpl/quota.go @@ -212,7 +212,6 @@ func (s *service) CheckQuotaReached(ctx context.Context, targetSrv quota.TargetS // do not check user quota if the user information is not available (eg no user is signed in) if scope == quota.UserScope && (scopeParams == nil || scopeParams.UserID == 0) { continue - } // do not check user quota if the org information is not available (eg no user is signed in)