CloudMigrations: Check contact point permissions before fetching it (#113159)
This commit is contained in:
@@ -40,6 +40,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
ngalertstore "github.com/grafana/grafana/pkg/services/ngalert/store"
|
||||
ngalertfakes "github.com/grafana/grafana/pkg/services/ngalert/tests/fakes"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginaccesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
|
||||
"github.com/grafana/grafana/pkg/services/quota/quotatest"
|
||||
@@ -790,7 +791,15 @@ func TestGetPlugins(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
|
||||
user := &user.SignedInUser{OrgID: 1}
|
||||
user := &user.SignedInUser{
|
||||
OrgID: 1,
|
||||
Permissions: map[int64]map[string][]string{
|
||||
1: {
|
||||
pluginaccesscontrol.ActionInstall: {pluginaccesscontrol.ScopeProvider.GetResourceAllScope()},
|
||||
pluginaccesscontrol.ActionWrite: {pluginaccesscontrol.ScopeProvider.GetResourceAllScope()},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
s.pluginStore = pluginstore.NewFakePluginStore([]pluginstore.Plugin{
|
||||
{
|
||||
@@ -1001,7 +1010,7 @@ func setUpServiceTest(t *testing.T, cfgOverrides ...configOverrides) cloudmigrat
|
||||
mockFolder,
|
||||
&pluginstore.FakePluginStore{},
|
||||
&pluginsettings.FakePluginSettings{},
|
||||
actest.FakeAccessControl{ExpectedEvaluate: true},
|
||||
accessControl,
|
||||
fakeAccessControlService,
|
||||
kvstore.ProvideService(sqlStore),
|
||||
&libraryelementsfake.LibraryElementService{},
|
||||
|
||||
@@ -9,9 +9,12 @@ import (
|
||||
"github.com/prometheus/common/model"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
ngalertapi "github.com/grafana/grafana/pkg/services/ngalert/api/compat"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/provisioning"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
@@ -79,6 +82,13 @@ type contactPoint struct {
|
||||
}
|
||||
|
||||
func (s *Service) getContactPoints(ctx context.Context, signedInUser *user.SignedInUser) ([]contactPoint, error) {
|
||||
userIsOrgAdmin := signedInUser.HasRole(org.RoleAdmin)
|
||||
hasAccess, _ := s.accessControl.Evaluate(ctx, signedInUser, ac.EvalPermission(ac.ActionAlertingReceiversReadSecrets, models.ScopeReceiversAll))
|
||||
if !userIsOrgAdmin && !hasAccess {
|
||||
msg := "user '%s' is not allowed to read contact point secrets, missing 'alert.notifications.receivers.secrets:read' permission, which can be granted through the 'Admin' or 'Alerting > Full admin access' roles"
|
||||
return nil, fmt.Errorf(msg, signedInUser.UserUID)
|
||||
}
|
||||
|
||||
query := provisioning.ContactPointQuery{
|
||||
OrgID: signedInUser.GetOrgID(),
|
||||
Decrypt: true, // needed to recreate the settings in the target instance.
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -13,7 +12,6 @@ import (
|
||||
"github.com/prometheus/alertmanager/pkg/labels"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/errutil"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
@@ -127,10 +125,7 @@ func TestGetContactPoints(t *testing.T) {
|
||||
|
||||
contactPoints, err := s.getContactPoints(ctx, user)
|
||||
require.Nil(t, contactPoints)
|
||||
|
||||
gfErr := errutil.Error{}
|
||||
require.ErrorAs(t, err, &gfErr)
|
||||
require.Equal(t, http.StatusForbidden, gfErr.Reason.Status().HTTPStatus())
|
||||
require.Contains(t, err.Error(), "alert.notifications.receivers.secrets:read")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -822,7 +822,7 @@ func TestEncodeDecode(t *testing.T) {
|
||||
Private: grafanaPrivateKey[:],
|
||||
},
|
||||
crypto.NewNacl(),
|
||||
"",
|
||||
t.TempDir(),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user