Alerting: Add provenance guard to config api (#50147)

* Alerting: add provenance guard to config api

* add tests

* only guard if config valid

* adapt error message

* simplify logic

* rename arguments

* make logic more straight forward

* rename opt to options

* remove useless maps
This commit is contained in:
Jean-Philippe Quéméner
2022-06-04 14:55:46 +02:00
committed by GitHub
parent 0cec12b8a9
commit 4cc8c6f745
3 changed files with 744 additions and 1 deletions
+9 -1
View File
@@ -217,7 +217,15 @@ func (srv AlertmanagerSrv) RouteGetSilences(c *models.ReqContext) response.Respo
}
func (srv AlertmanagerSrv) RoutePostAlertingConfig(c *models.ReqContext, body apimodels.PostableUserConfig) response.Response {
err := srv.mam.ApplyAlertmanagerConfiguration(c.Req.Context(), c.OrgId, body)
currentConfig, err := srv.mam.GetAlertmanagerConfiguration(c.Req.Context(), c.OrgId)
// If a config is present and valid we proceed with the guard, otherwise we
// just bypass the guard which is okay as we are anyway in an invalid state.
if err == nil {
if err := srv.provenanceGuard(currentConfig, body); err != nil {
return ErrResp(http.StatusBadRequest, err, "")
}
}
err = srv.mam.ApplyAlertmanagerConfiguration(c.Req.Context(), c.OrgId, body)
if err == nil {
return response.JSON(http.StatusAccepted, util.DynMap{"message": "configuration created"})
}