Alerting: Feature toggle to disallow sending alerts externally (#87982)
* Define feature toggle * Implement feature toggle
This commit is contained in:
@@ -144,6 +144,7 @@ func (api *API) RegisterAPIEndpoints(m *metrics.API) {
|
||||
store: api.AdminConfigStore,
|
||||
log: logger,
|
||||
alertmanagerProvider: api.AlertsRouter,
|
||||
featureManager: api.FeatureManager,
|
||||
},
|
||||
), m)
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
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"
|
||||
@@ -24,6 +25,7 @@ type ConfigSrv struct {
|
||||
alertmanagerProvider ExternalAlertmanagerProvider
|
||||
store store.AdminConfigurationStore
|
||||
log log.Logger
|
||||
featureManager featuremgmt.FeatureToggles
|
||||
}
|
||||
|
||||
func (srv ConfigSrv) RouteGetAlertmanagers(c *contextmodel.ReqContext) response.Response {
|
||||
@@ -75,6 +77,11 @@ func (srv ConfigSrv) RoutePostNGalertConfig(c *contextmodel.ReqContext, body api
|
||||
return response.Error(http.StatusBadRequest, "Invalid alertmanager choice specified", err)
|
||||
}
|
||||
|
||||
disableExternal := srv.featureManager.IsEnabled(c.Req.Context(), featuremgmt.FlagAlertingDisableSendAlertsExternal)
|
||||
if disableExternal && sendAlertsTo != ngmodels.InternalAlertmanager {
|
||||
return response.Error(http.StatusBadRequest, "Sending alerts to external alertmanagers is disallowed on this instance", err)
|
||||
}
|
||||
|
||||
externalAlertmanagers, err := srv.externalAlertmanagers(c.Req.Context(), c.SignedInUser.GetOrgID())
|
||||
if err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "Couldn't fetch the external Alertmanagers from datasources", err)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
fakeDatasources "github.com/grafana/grafana/pkg/services/datasources/fakes"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"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"
|
||||
@@ -22,6 +23,7 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
|
||||
datasources []*datasources.DataSource
|
||||
statusCode int
|
||||
message string
|
||||
features featuremgmt.FeatureToggles
|
||||
}{
|
||||
{
|
||||
name: "setting the choice to external by having a enabled external am datasource should succeed",
|
||||
@@ -38,6 +40,7 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
|
||||
},
|
||||
statusCode: http.StatusCreated,
|
||||
message: "admin configuration updated",
|
||||
features: featuremgmt.WithFeatures(),
|
||||
},
|
||||
{
|
||||
name: "setting the choice to external by having a disabled external am datasource should fail",
|
||||
@@ -52,6 +55,7 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
|
||||
},
|
||||
statusCode: http.StatusBadRequest,
|
||||
message: "At least one Alertmanager must be provided or configured as a datasource that handles alerts to choose this option",
|
||||
features: featuremgmt.WithFeatures(),
|
||||
},
|
||||
{
|
||||
name: "setting the choice to external and having no am configured should fail",
|
||||
@@ -59,6 +63,7 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
|
||||
datasources: []*datasources.DataSource{},
|
||||
statusCode: http.StatusBadRequest,
|
||||
message: "At least one Alertmanager must be provided or configured as a datasource that handles alerts to choose this option",
|
||||
features: featuremgmt.WithFeatures(),
|
||||
},
|
||||
{
|
||||
name: "setting the choice to all and having no external am configured should succeed",
|
||||
@@ -66,6 +71,7 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
|
||||
datasources: []*datasources.DataSource{},
|
||||
statusCode: http.StatusCreated,
|
||||
message: "admin configuration updated",
|
||||
features: featuremgmt.WithFeatures(),
|
||||
},
|
||||
{
|
||||
name: "setting the choice to internal should always succeed",
|
||||
@@ -73,13 +79,38 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
|
||||
datasources: []*datasources.DataSource{},
|
||||
statusCode: http.StatusCreated,
|
||||
message: "admin configuration updated",
|
||||
features: featuremgmt.WithFeatures(),
|
||||
},
|
||||
{
|
||||
name: "setting the choice to internal should succeed when external disallowed",
|
||||
alertmanagerChoice: definitions.InternalAlertmanager,
|
||||
datasources: []*datasources.DataSource{},
|
||||
statusCode: http.StatusCreated,
|
||||
message: "admin configuration updated",
|
||||
features: featuremgmt.WithFeatures(featuremgmt.FlagAlertingDisableSendAlertsExternal),
|
||||
},
|
||||
{
|
||||
name: "setting the choice to all should fail when external disallowed",
|
||||
alertmanagerChoice: definitions.AllAlertmanagers,
|
||||
datasources: []*datasources.DataSource{},
|
||||
statusCode: http.StatusBadRequest,
|
||||
message: "Sending alerts to external alertmanagers is disallowed on this instance",
|
||||
features: featuremgmt.WithFeatures(featuremgmt.FlagAlertingDisableSendAlertsExternal),
|
||||
},
|
||||
{
|
||||
name: "setting the choice to external should fail when external disallowed",
|
||||
alertmanagerChoice: definitions.ExternalAlertmanagers,
|
||||
datasources: []*datasources.DataSource{},
|
||||
statusCode: http.StatusBadRequest,
|
||||
message: "Sending alerts to external alertmanagers is disallowed on this instance",
|
||||
features: featuremgmt.WithFeatures(featuremgmt.FlagAlertingDisableSendAlertsExternal),
|
||||
},
|
||||
}
|
||||
ctx := createRequestCtxInOrg(1)
|
||||
ctx.OrgRole = org.RoleAdmin
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
sut := createAPIAdminSut(t, test.datasources)
|
||||
sut := createAPIAdminSut(t, test.datasources, test.features)
|
||||
resp := sut.RoutePostNGalertConfig(ctx, definitions.PostableNGalertConfig{
|
||||
AlertmanagersChoice: test.alertmanagerChoice,
|
||||
})
|
||||
@@ -93,11 +124,12 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
|
||||
}
|
||||
|
||||
func createAPIAdminSut(t *testing.T,
|
||||
datasources []*datasources.DataSource) ConfigSrv {
|
||||
datasources []*datasources.DataSource, features featuremgmt.FeatureToggles) ConfigSrv {
|
||||
return ConfigSrv{
|
||||
datasourceService: &fakeDatasources.FakeDataSourceService{
|
||||
DataSources: datasources,
|
||||
},
|
||||
store: store.NewFakeAdminConfigStore(t),
|
||||
store: store.NewFakeAdminConfigStore(t),
|
||||
featureManager: features,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user