Alerting: Remove the POST endpoint for the internal Grafana Alertmanager config (#103819)
* Remove POST config for Grafana Alertmanager * Delete auth + test for removed path * Alerting: Remove check for `alertingApiServer` toggle in UI (#103805) * Remove check for alertingApiServer in UI * Update tests to no longer care about alertingApiServer * Add contact points handlers now that we use alertingApiServer all the time * Fix test broken from removing camelCase for UIDs --------- Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
This commit is contained in:
co-authored by
Tom Ratcliffe
parent
c47ab101d1
commit
a5288db624
@@ -2,7 +2,6 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
@@ -101,90 +100,6 @@ func TestContextWithTimeoutFromRequest(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAlertmanagerConfig(t *testing.T) {
|
||||
sut := createSut(t)
|
||||
|
||||
t.Run("assert 404 Not Found when applying config to nonexistent org", func(t *testing.T) {
|
||||
rc := contextmodel.ReqContext{
|
||||
Context: &web.Context{
|
||||
Req: &http.Request{},
|
||||
},
|
||||
SignedInUser: &user.SignedInUser{
|
||||
OrgID: 12,
|
||||
},
|
||||
}
|
||||
request := createAmConfigRequest(t, validConfig)
|
||||
|
||||
response := sut.RoutePostAlertingConfig(&rc, request)
|
||||
|
||||
require.Equal(t, 404, response.Status())
|
||||
require.Contains(t, string(response.Body()), "Alertmanager does not exist for this organization")
|
||||
})
|
||||
|
||||
t.Run("assert 202 when config successfully applied", func(t *testing.T) {
|
||||
rc := contextmodel.ReqContext{
|
||||
Context: &web.Context{
|
||||
Req: &http.Request{},
|
||||
},
|
||||
SignedInUser: &user.SignedInUser{
|
||||
OrgID: 1,
|
||||
},
|
||||
}
|
||||
request := createAmConfigRequest(t, validConfig)
|
||||
|
||||
response := sut.RoutePostAlertingConfig(&rc, request)
|
||||
|
||||
require.Equal(t, 202, response.Status())
|
||||
})
|
||||
|
||||
t.Run("assert 202 when alertmanager to configure is not ready", func(t *testing.T) {
|
||||
sut := createSut(t)
|
||||
rc := contextmodel.ReqContext{
|
||||
Context: &web.Context{
|
||||
Req: &http.Request{},
|
||||
},
|
||||
SignedInUser: &user.SignedInUser{
|
||||
OrgID: 3, // Org 3 was initialized with broken config.
|
||||
},
|
||||
}
|
||||
request := createAmConfigRequest(t, validConfig)
|
||||
|
||||
response := sut.RoutePostAlertingConfig(&rc, request)
|
||||
|
||||
require.Equal(t, 202, response.Status())
|
||||
})
|
||||
|
||||
t.Run("assert config hash doesn't change when sending RouteGetAlertingConfig back to RoutePostAlertingConfig", func(t *testing.T) {
|
||||
rc := contextmodel.ReqContext{
|
||||
Context: &web.Context{
|
||||
Req: &http.Request{},
|
||||
},
|
||||
SignedInUser: &user.SignedInUser{
|
||||
OrgID: 1,
|
||||
},
|
||||
}
|
||||
request := createAmConfigRequest(t, validConfigWithSecureSetting)
|
||||
|
||||
r := sut.RoutePostAlertingConfig(&rc, request)
|
||||
require.Equal(t, 202, r.Status())
|
||||
|
||||
getResponse := sut.RouteGetAlertingConfig(&rc)
|
||||
require.Equal(t, 200, getResponse.Status())
|
||||
|
||||
body := getResponse.Body()
|
||||
hash := md5.Sum(body)
|
||||
postable, err := notifier.Load(body)
|
||||
require.NoError(t, err)
|
||||
|
||||
r = sut.RoutePostAlertingConfig(&rc, *postable)
|
||||
require.Equal(t, 202, r.Status())
|
||||
|
||||
getResponse = sut.RouteGetAlertingConfig(&rc)
|
||||
require.Equal(t, 200, getResponse.Status())
|
||||
|
||||
newHash := md5.Sum(getResponse.Body())
|
||||
require.Equal(t, hash, newHash)
|
||||
})
|
||||
|
||||
t.Run("when objects are not provisioned", func(t *testing.T) {
|
||||
t.Run("route from GET config has no provenance", func(t *testing.T) {
|
||||
sut := createSut(t)
|
||||
@@ -229,9 +144,8 @@ func TestAlertmanagerConfig(t *testing.T) {
|
||||
t.Run("contact point from GET config has expected provenance", func(t *testing.T) {
|
||||
sut := createSut(t)
|
||||
rc := createRequestCtxInOrg(1)
|
||||
request := createAmConfigRequest(t, validConfig)
|
||||
|
||||
_ = sut.RoutePostAlertingConfig(rc, request)
|
||||
RoutePostAlertingConfig(t, sut.mam, rc, validConfig)
|
||||
|
||||
response := sut.RouteGetAlertingConfig(rc)
|
||||
body := asGettableUserConfig(t, response)
|
||||
@@ -347,11 +261,8 @@ func TestGetAlertmanagerConfiguration_NewSecretField(t *testing.T) {
|
||||
}
|
||||
}
|
||||
`
|
||||
postable := createAmConfigRequest(t, postWithoutChanges)
|
||||
|
||||
res = sut.RoutePostAlertingConfig(rc, postable)
|
||||
require.Equal(t, 202, res.Status())
|
||||
|
||||
RoutePostAlertingConfig(t, sut.mam, rc, postWithoutChanges)
|
||||
// Check that the secret field "integrationKey" is now encrypted in SecureSettings.
|
||||
savedConfig := &apimodels.PostableUserConfig{}
|
||||
err = json.Unmarshal([]byte(configs[orgId].AlertmanagerConfiguration), savedConfig)
|
||||
@@ -400,31 +311,6 @@ func TestAlertmanagerAutogenConfig(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("route POST config", func(t *testing.T) {
|
||||
t.Run("does not save autogen routes", func(t *testing.T) {
|
||||
sut, configs := createSutForAutogen(t)
|
||||
rc := createRequestCtxInOrg(1)
|
||||
request := createAmConfigRequest(t, validConfigWithAutogen)
|
||||
response := sut.RoutePostAlertingConfig(rc, request)
|
||||
require.Equal(t, 202, response.Status())
|
||||
|
||||
compare(t, validConfigWithoutAutogen, configs[1].AlertmanagerConfiguration)
|
||||
})
|
||||
|
||||
t.Run("provenance guard ignores autogen routes", func(t *testing.T) {
|
||||
sut := createSut(t)
|
||||
rc := createRequestCtxInOrg(1)
|
||||
request := createAmConfigRequest(t, validConfigWithoutAutogen)
|
||||
_ = sut.RoutePostAlertingConfig(rc, request)
|
||||
|
||||
setRouteProvenance(t, 1, sut.mam.ProvStore)
|
||||
request = createAmConfigRequest(t, validConfigWithAutogen)
|
||||
request.AlertmanagerConfig.Route.Provenance = apimodels.Provenance(ngmodels.ProvenanceAPI)
|
||||
response := sut.RoutePostAlertingConfig(rc, request)
|
||||
require.Equal(t, 202, response.Status())
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("route GET config", func(t *testing.T) {
|
||||
t.Run("when admin return autogen routes", func(t *testing.T) {
|
||||
sut, _ := createSutForAutogen(t)
|
||||
@@ -691,16 +577,6 @@ func createSut(t *testing.T) AlertmanagerSrv {
|
||||
}
|
||||
}
|
||||
|
||||
func createAmConfigRequest(t *testing.T, config string) apimodels.PostableUserConfig {
|
||||
t.Helper()
|
||||
|
||||
request := apimodels.PostableUserConfig{}
|
||||
err := request.UnmarshalJSON([]byte(config))
|
||||
require.NoError(t, err)
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
func createMultiOrgAlertmanager(t *testing.T, configs map[int64]*ngmodels.AlertConfiguration) *notifier.MultiOrgAlertmanager {
|
||||
t.Helper()
|
||||
|
||||
@@ -849,40 +725,6 @@ var validConfigWithAutogen = `{
|
||||
}
|
||||
`
|
||||
|
||||
var validConfigWithSecureSetting = `{
|
||||
"template_files": {
|
||||
"a": "template"
|
||||
},
|
||||
"alertmanager_config": {
|
||||
"route": {
|
||||
"receiver": "grafana-default-email"
|
||||
},
|
||||
"receivers": [{
|
||||
"name": "grafana-default-email",
|
||||
"grafana_managed_receiver_configs": [{
|
||||
"uid": "",
|
||||
"name": "email receiver",
|
||||
"type": "email",
|
||||
"settings": {
|
||||
"addresses": "<example@email.com>"
|
||||
}
|
||||
}]},
|
||||
{
|
||||
"name": "slack",
|
||||
"grafana_managed_receiver_configs": [{
|
||||
"uid": "",
|
||||
"name": "slack1",
|
||||
"type": "slack",
|
||||
"settings": {"text": "slack text"},
|
||||
"secureSettings": {
|
||||
"url": "secure url"
|
||||
}
|
||||
}]
|
||||
}]
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
var brokenConfig = `
|
||||
"alertmanager_config": {
|
||||
"route": {
|
||||
@@ -947,3 +789,13 @@ func asGettableHistoricUserConfigs(t *testing.T, r response.Response) []apimodel
|
||||
require.NoError(t, err)
|
||||
return body
|
||||
}
|
||||
|
||||
// RoutePostAlertingConfig drop-in replacement for removed POST endpoint to make test transition easier.
|
||||
func RoutePostAlertingConfig(t *testing.T, mam *notifier.MultiOrgAlertmanager, rc *contextmodel.ReqContext, amConfig string) {
|
||||
t.Helper()
|
||||
cfg := apimodels.PostableUserConfig{}
|
||||
err := json.Unmarshal([]byte(amConfig), &cfg)
|
||||
require.NoError(t, err)
|
||||
err = mam.SaveAndApplyAlertmanagerConfiguration(rc.Req.Context(), 1, cfg)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user