Move SignedInUser to user service and RoleType and Roles to org (#53445)

* Move SignedInUser to user service and RoleType and Roles to org

* Use go naming convention for roles

* Fix some imports and leftovers

* Fix ldap debug test

* Fix lint

* Fix lint 2

* Fix lint 3

* Fix type and not needed conversion

* Clean up messages in api tests

* Clean up api tests 2
This commit is contained in:
idafurjes
2022-08-10 11:56:48 +02:00
committed by GitHub
parent 46004037e2
commit 6afad51761
278 changed files with 1758 additions and 1543 deletions

View File

@@ -23,8 +23,10 @@ import (
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/ngalert/notifier"
"github.com/grafana/grafana/pkg/services/ngalert/provisioning"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/secrets/fakes"
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/web"
@@ -167,7 +169,7 @@ func TestAlertmanagerConfig(t *testing.T) {
Context: &web.Context{
Req: &http.Request{},
},
SignedInUser: &models.SignedInUser{
SignedInUser: &user.SignedInUser{
OrgId: 12,
},
}
@@ -184,7 +186,7 @@ func TestAlertmanagerConfig(t *testing.T) {
Context: &web.Context{
Req: &http.Request{},
},
SignedInUser: &models.SignedInUser{
SignedInUser: &user.SignedInUser{
OrgId: 1,
},
}
@@ -201,7 +203,7 @@ func TestAlertmanagerConfig(t *testing.T) {
Context: &web.Context{
Req: &http.Request{},
},
SignedInUser: &models.SignedInUser{
SignedInUser: &user.SignedInUser{
OrgId: 3, // Org 3 was initialized with broken config.
},
}
@@ -331,8 +333,8 @@ func TestSilenceCreate(t *testing.T) {
Context: &web.Context{
Req: &http.Request{},
},
SignedInUser: &models.SignedInUser{
OrgRole: models.ROLE_EDITOR,
SignedInUser: &user.SignedInUser{
OrgRole: org.RoleEditor,
OrgId: 1,
},
}
@@ -353,7 +355,7 @@ func TestRouteCreateSilence(t *testing.T) {
name string
silence func() apimodels.PostableSilence
accessControl func() accesscontrol.AccessControl
role models.RoleType
role org.RoleType
expectedStatus int
}{
{
@@ -380,7 +382,7 @@ func TestRouteCreateSilence(t *testing.T) {
accessControl: func() accesscontrol.AccessControl {
return acMock.New().WithDisabled()
},
role: models.ROLE_VIEWER,
role: org.RoleViewer,
expectedStatus: http.StatusUnauthorized,
},
{
@@ -389,7 +391,7 @@ func TestRouteCreateSilence(t *testing.T) {
accessControl: func() accesscontrol.AccessControl {
return acMock.New().WithDisabled()
},
role: models.ROLE_EDITOR,
role: org.RoleEditor,
expectedStatus: http.StatusAccepted,
},
{
@@ -398,7 +400,7 @@ func TestRouteCreateSilence(t *testing.T) {
accessControl: func() accesscontrol.AccessControl {
return acMock.New().WithDisabled()
},
role: models.ROLE_ADMIN,
role: org.RoleAdmin,
expectedStatus: http.StatusAccepted,
},
{
@@ -425,7 +427,7 @@ func TestRouteCreateSilence(t *testing.T) {
accessControl: func() accesscontrol.AccessControl {
return acMock.New().WithDisabled()
},
role: models.ROLE_VIEWER,
role: org.RoleViewer,
expectedStatus: http.StatusUnauthorized,
},
{
@@ -434,7 +436,7 @@ func TestRouteCreateSilence(t *testing.T) {
accessControl: func() accesscontrol.AccessControl {
return acMock.New().WithDisabled()
},
role: models.ROLE_EDITOR,
role: org.RoleEditor,
expectedStatus: http.StatusAccepted,
},
{
@@ -443,7 +445,7 @@ func TestRouteCreateSilence(t *testing.T) {
accessControl: func() accesscontrol.AccessControl {
return acMock.New().WithDisabled()
},
role: models.ROLE_ADMIN,
role: org.RoleAdmin,
expectedStatus: http.StatusAccepted,
},
}
@@ -457,7 +459,7 @@ func TestRouteCreateSilence(t *testing.T) {
Context: &web.Context{
Req: &http.Request{},
},
SignedInUser: &models.SignedInUser{
SignedInUser: &user.SignedInUser{
OrgRole: tesCase.role,
OrgId: 1,
},
@@ -624,7 +626,7 @@ func createRequestCtxInOrg(org int64) *models.ReqContext {
Context: &web.Context{
Req: &http.Request{},
},
SignedInUser: &models.SignedInUser{
SignedInUser: &user.SignedInUser{
OrgId: org,
},
}

View File

@@ -13,6 +13,7 @@ import (
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/ngalert/store"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/util"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
@@ -43,7 +44,7 @@ func (srv ConfigSrv) RouteGetAlertmanagers(c *models.ReqContext) response.Respon
}
func (srv ConfigSrv) RouteGetNGalertConfig(c *models.ReqContext) response.Response {
if c.OrgRole != models.ROLE_ADMIN {
if c.OrgRole != org.RoleAdmin {
return accessForbiddenResp()
}
@@ -66,7 +67,7 @@ func (srv ConfigSrv) RouteGetNGalertConfig(c *models.ReqContext) response.Respon
}
func (srv ConfigSrv) RoutePostNGalertConfig(c *models.ReqContext, body apimodels.PostableNGalertConfig) response.Response {
if c.OrgRole != models.ROLE_ADMIN {
if c.OrgRole != org.RoleAdmin {
return accessForbiddenResp()
}
@@ -108,7 +109,7 @@ func (srv ConfigSrv) RoutePostNGalertConfig(c *models.ReqContext, body apimodels
}
func (srv ConfigSrv) RouteDeleteNGalertConfig(c *models.ReqContext) response.Response {
if c.OrgRole != models.ROLE_ADMIN {
if c.OrgRole != org.RoleAdmin {
return accessForbiddenResp()
}

View File

@@ -6,11 +6,11 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/datasources"
fakeDatasources "github.com/grafana/grafana/pkg/services/datasources/fakes"
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/ngalert/store"
"github.com/grafana/grafana/pkg/services/org"
"github.com/stretchr/testify/require"
)
@@ -89,7 +89,7 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
},
}
ctx := createRequestCtxInOrg(1)
ctx.OrgRole = models.ROLE_ADMIN
ctx.OrgRole = org.RoleAdmin
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
sut := createAPIAdminSut(t, test.datasources)

View File

@@ -21,6 +21,8 @@ import (
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/ngalert/state"
"github.com/grafana/grafana/pkg/services/ngalert/store"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/web"
)
@@ -90,7 +92,7 @@ func TestRouteGetAlertStatuses(t *testing.T) {
_, _, _, api := setupAPI(t)
req, err := http.NewRequest("GET", "/api/v1/alerts", nil)
require.NoError(t, err)
c := &models.ReqContext{Context: &web.Context{Req: req}, SignedInUser: &models.SignedInUser{OrgId: orgID}}
c := &models.ReqContext{Context: &web.Context{Req: req}, SignedInUser: &user.SignedInUser{OrgId: orgID}}
r := api.RouteGetAlertStatuses(c)
require.Equal(t, http.StatusOK, r.Status())
@@ -109,7 +111,7 @@ func TestRouteGetAlertStatuses(t *testing.T) {
fakeAIM.GenerateAlertInstances(1, util.GenerateShortUID(), 2)
req, err := http.NewRequest("GET", "/api/v1/alerts", nil)
require.NoError(t, err)
c := &models.ReqContext{Context: &web.Context{Req: req}, SignedInUser: &models.SignedInUser{OrgId: orgID}}
c := &models.ReqContext{Context: &web.Context{Req: req}, SignedInUser: &user.SignedInUser{OrgId: orgID}}
r := api.RouteGetAlertStatuses(c)
require.Equal(t, http.StatusOK, r.Status())
@@ -151,7 +153,7 @@ func TestRouteGetAlertStatuses(t *testing.T) {
fakeAIM.GenerateAlertInstances(1, util.GenerateShortUID(), 2, withAlertingState())
req, err := http.NewRequest("GET", "/api/v1/alerts", nil)
require.NoError(t, err)
c := &models.ReqContext{Context: &web.Context{Req: req}, SignedInUser: &models.SignedInUser{OrgId: orgID}}
c := &models.ReqContext{Context: &web.Context{Req: req}, SignedInUser: &user.SignedInUser{OrgId: orgID}}
r := api.RouteGetAlertStatuses(c)
require.Equal(t, http.StatusOK, r.Status())
@@ -193,7 +195,7 @@ func TestRouteGetAlertStatuses(t *testing.T) {
fakeAIM.GenerateAlertInstances(orgID, util.GenerateShortUID(), 2)
req, err := http.NewRequest("GET", "/api/v1/alerts?includeInternalLabels=true", nil)
require.NoError(t, err)
c := &models.ReqContext{Context: &web.Context{Req: req}, SignedInUser: &models.SignedInUser{OrgId: orgID}}
c := &models.ReqContext{Context: &web.Context{Req: req}, SignedInUser: &user.SignedInUser{OrgId: orgID}}
r := api.RouteGetAlertStatuses(c)
require.Equal(t, http.StatusOK, r.Status())
@@ -255,7 +257,7 @@ func TestRouteGetRuleStatuses(t *testing.T) {
req, err := http.NewRequest("GET", "/api/v1/rules", nil)
require.NoError(t, err)
c := &models.ReqContext{Context: &web.Context{Req: req}, SignedInUser: &models.SignedInUser{OrgId: orgID, OrgRole: models.ROLE_VIEWER}}
c := &models.ReqContext{Context: &web.Context{Req: req}, SignedInUser: &user.SignedInUser{OrgId: orgID, OrgRole: org.RoleViewer}}
t.Run("with no rules", func(t *testing.T) {
_, _, _, api := setupAPI(t)
@@ -325,7 +327,7 @@ func TestRouteGetRuleStatuses(t *testing.T) {
req, err := http.NewRequest("GET", "/api/v1/rules?includeInternalLabels=true", nil)
require.NoError(t, err)
c := &models.ReqContext{Context: &web.Context{Req: req}, SignedInUser: &models.SignedInUser{OrgId: orgID, OrgRole: models.ROLE_VIEWER}}
c := &models.ReqContext{Context: &web.Context{Req: req}, SignedInUser: &user.SignedInUser{OrgId: orgID, OrgRole: org.RoleViewer}}
r := api.RouteGetRuleStatuses(c)
require.Equal(t, http.StatusOK, r.Status())

View File

@@ -22,6 +22,7 @@ import (
"github.com/grafana/grafana/pkg/services/secrets"
secrets_fakes "github.com/grafana/grafana/pkg/services/secrets/fakes"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/web"
)
@@ -372,7 +373,7 @@ func createTestRequestCtx() gfcore.ReqContext {
Context: &web.Context{
Req: &http.Request{},
},
SignedInUser: &gfcore.SignedInUser{
SignedInUser: &user.SignedInUser{
OrgId: 1,
},
}

View File

@@ -23,6 +23,8 @@ import (
"github.com/grafana/grafana/pkg/services/ngalert/provisioning"
"github.com/grafana/grafana/pkg/services/ngalert/schedule"
"github.com/grafana/grafana/pkg/services/ngalert/store"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/web"
)
@@ -82,7 +84,7 @@ func TestRouteDeleteAlertRules(t *testing.T) {
scheduler.On("DeleteAlertRule", mock.Anything).Panic("should not be called")
ac := acMock.New().WithDisabled()
request := createRequestContext(orgID, models2.ROLE_VIEWER, nil)
request := createRequestContext(orgID, org.RoleViewer, nil)
response := createService(ac, ruleStore, scheduler).RouteDeleteAlertRules(request, folder.Title, "")
require.Equalf(t, 401, response.Status(), "Expected 403 but got %d: %v", response.Status(), string(response.Body()))
@@ -102,7 +104,7 @@ func TestRouteDeleteAlertRules(t *testing.T) {
scheduler.On("DeleteAlertRule", mock.Anything)
ac := acMock.New().WithDisabled()
request := createRequestContext(orgID, models2.ROLE_EDITOR, nil)
request := createRequestContext(orgID, org.RoleEditor, nil)
response := createService(ac, ruleStore, scheduler).RouteDeleteAlertRules(request, folder.Title, "")
require.Equalf(t, 202, response.Status(), "Expected 202 but got %d: %v", response.Status(), string(response.Body()))
assertRulesDeleted(t, rulesInFolder, ruleStore, scheduler)
@@ -124,7 +126,7 @@ func TestRouteDeleteAlertRules(t *testing.T) {
scheduler.On("DeleteAlertRule", mock.Anything)
ac := acMock.New().WithDisabled()
request := createRequestContext(orgID, models2.ROLE_EDITOR, nil)
request := createRequestContext(orgID, org.RoleEditor, nil)
response := createService(ac, ruleStore, scheduler).RouteDeleteAlertRules(request, folder.Title, groupName)
require.Equalf(t, 202, response.Status(), "Expected 202 but got %d: %v", response.Status(), string(response.Body()))
assertRulesDeleted(t, rulesInFolderInGroup, ruleStore, scheduler)
@@ -148,7 +150,7 @@ func TestRouteDeleteAlertRules(t *testing.T) {
err := svc.provenanceStore.SetProvenance(context.Background(), rulesInFolder[0], orgID, models.ProvenanceAPI)
require.NoError(t, err)
request := createRequestContext(orgID, models2.ROLE_EDITOR, nil)
request := createRequestContext(orgID, org.RoleEditor, nil)
response := svc.RouteDeleteAlertRules(request, folder.Title, "")
require.Equalf(t, 202, response.Status(), "Expected 202 but got %d: %v", response.Status(), string(response.Body()))
assertRulesDeleted(t, rulesInFolder[1:], ruleStore, scheduler)
@@ -316,7 +318,7 @@ func TestRouteGetNamespaceRulesConfig(t *testing.T) {
ruleStore.PutRule(context.Background(), expectedRules...)
ac := acMock.New().WithDisabled()
req := createRequestContext(orgID, models2.ROLE_VIEWER, nil)
req := createRequestContext(orgID, org.RoleViewer, nil)
response := createService(ac, ruleStore, nil).RouteGetNamespaceRulesConfig(req, folder.Title)
require.Equal(t, http.StatusAccepted, response.Status())
@@ -359,7 +361,7 @@ func TestRouteGetNamespaceRulesConfig(t *testing.T) {
err := svc.provenanceStore.SetProvenance(context.Background(), rule, orgID, models.ProvenanceAPI)
require.NoError(t, err)
req := createRequestContext(orgID, models2.ROLE_VIEWER, nil)
req := createRequestContext(orgID, org.RoleViewer, nil)
response := svc.RouteGetNamespaceRulesConfig(req, folder.Title)
require.Equal(t, http.StatusAccepted, response.Status())
@@ -394,7 +396,7 @@ func TestRouteGetNamespaceRulesConfig(t *testing.T) {
ruleStore.PutRule(context.Background(), expectedRules...)
ac := acMock.New().WithDisabled()
response := createService(ac, ruleStore, nil).RouteGetNamespaceRulesConfig(createRequestContext(orgID, models2.ROLE_VIEWER, nil), folder.Title)
response := createService(ac, ruleStore, nil).RouteGetNamespaceRulesConfig(createRequestContext(orgID, org.RoleViewer, nil), folder.Title)
require.Equal(t, http.StatusAccepted, response.Status())
result := &apimodels.NamespaceConfigResponse{}
@@ -476,7 +478,7 @@ func TestRouteGetRulesConfig(t *testing.T) {
ruleStore.PutRule(context.Background(), expectedRules...)
ac := acMock.New().WithDisabled()
response := createService(ac, ruleStore, nil).RouteGetRulesConfig(createRequestContext(orgID, models2.ROLE_VIEWER, nil))
response := createService(ac, ruleStore, nil).RouteGetRulesConfig(createRequestContext(orgID, org.RoleViewer, nil))
require.Equal(t, http.StatusOK, response.Status())
result := &apimodels.NamespaceConfigResponse{}
@@ -556,7 +558,7 @@ func TestRouteGetRulesGroupConfig(t *testing.T) {
ruleStore.PutRule(context.Background(), expectedRules...)
ac := acMock.New().WithDisabled()
response := createService(ac, ruleStore, nil).RouteGetRulesGroupConfig(createRequestContext(orgID, models2.ROLE_VIEWER, nil), folder.Title, groupKey.RuleGroup)
response := createService(ac, ruleStore, nil).RouteGetRulesGroupConfig(createRequestContext(orgID, org.RoleViewer, nil), folder.Title, groupKey.RuleGroup)
require.Equal(t, http.StatusAccepted, response.Status())
result := &apimodels.RuleGroupConfigResponse{}
@@ -657,7 +659,7 @@ func createService(ac *acMock.Mock, store *store.FakeRuleStore, scheduler schedu
}
}
func createRequestContext(orgID int64, role models2.RoleType, params map[string]string) *models2.ReqContext {
func createRequestContext(orgID int64, role org.RoleType, params map[string]string) *models2.ReqContext {
uri, _ := url.Parse("http://localhost")
ctx := web.Context{Req: &http.Request{
URL: uri,
@@ -668,7 +670,7 @@ func createRequestContext(orgID int64, role models2.RoleType, params map[string]
return &models2.ReqContext{
IsSignedIn: true,
SignedInUser: &models2.SignedInUser{
SignedInUser: &user.SignedInUser{
OrgRole: role,
OrgId: orgID,
},

View File

@@ -17,6 +17,7 @@ import (
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
"github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/web"
)
@@ -26,7 +27,7 @@ func TestRouteTestGrafanaRuleConfig(t *testing.T) {
Context: &web.Context{
Req: &http.Request{},
},
SignedInUser: &models2.SignedInUser{
SignedInUser: &user.SignedInUser{
OrgId: 1,
},
}
@@ -94,7 +95,7 @@ func TestRouteTestGrafanaRuleConfig(t *testing.T) {
Req: &http.Request{},
},
IsSignedIn: false,
SignedInUser: &models2.SignedInUser{
SignedInUser: &user.SignedInUser{
OrgId: 1,
},
}
@@ -149,7 +150,7 @@ func TestRouteEvalQueries(t *testing.T) {
Context: &web.Context{
Req: &http.Request{},
},
SignedInUser: &models2.SignedInUser{
SignedInUser: &user.SignedInUser{
OrgId: 1,
},
}
@@ -218,7 +219,7 @@ func TestRouteEvalQueries(t *testing.T) {
Req: &http.Request{},
},
IsSignedIn: false,
SignedInUser: &models2.SignedInUser{
SignedInUser: &user.SignedInUser{
OrgId: 1,
},
}

View File

@@ -12,6 +12,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/datasourceproxy"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/web"
)
@@ -113,7 +114,7 @@ type fakeCacheService struct {
err error
}
func (f fakeCacheService) GetDatasource(_ context.Context, datasourceID int64, _ *models.SignedInUser, _ bool) (*datasources.DataSource, error) {
func (f fakeCacheService) GetDatasource(_ context.Context, datasourceID int64, _ *user.SignedInUser, _ bool) (*datasources.DataSource, error) {
if f.err != nil {
return nil, f.err
}
@@ -121,7 +122,7 @@ func (f fakeCacheService) GetDatasource(_ context.Context, datasourceID int64, _
return f.datasource, nil
}
func (f fakeCacheService) GetDatasourceByUID(ctx context.Context, datasourceUID string, user *models.SignedInUser, skipCache bool) (*datasources.DataSource, error) {
func (f fakeCacheService) GetDatasourceByUID(ctx context.Context, datasourceUID string, user *user.SignedInUser, skipCache bool) (*datasources.DataSource, error) {
if f.err != nil {
return nil, f.err
}

View File

@@ -21,6 +21,7 @@ import (
"github.com/grafana/grafana/pkg/services/datasources"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/web"
)
@@ -188,7 +189,7 @@ func messageExtractor(resp *response.NormalResponse) (interface{}, error) {
return map[string]string{"message": string(resp.Body())}, nil
}
func validateCondition(ctx context.Context, c ngmodels.Condition, user *models.SignedInUser, skipCache bool, datasourceCache datasources.CacheService) error {
func validateCondition(ctx context.Context, c ngmodels.Condition, user *user.SignedInUser, skipCache bool, datasourceCache datasources.CacheService) error {
if len(c.Data) == 0 {
return nil
}
@@ -215,7 +216,7 @@ func conditionValidator(c *models.ReqContext, cache datasources.CacheService) fu
}
}
func validateQueriesAndExpressions(ctx context.Context, data []ngmodels.AlertQuery, user *models.SignedInUser, skipCache bool, datasourceCache datasources.CacheService) (map[string]struct{}, error) {
func validateQueriesAndExpressions(ctx context.Context, data []ngmodels.AlertQuery, user *user.SignedInUser, skipCache bool, datasourceCache datasources.CacheService) (map[string]struct{}, error) {
refIDs := make(map[string]struct{})
if len(data) == 0 {
return nil, nil