Alerting: Disable /api/admin/pause-all-alerts with Unified Alerting (#51895)

/api/admin/pause-all-alerts only takes effect for legacy alerts. This
change returns a 403 if it's called when legacy alerting is disabled.

Fixes #51729
This commit is contained in:
Joe Blubaugh 2022-07-12 15:01:31 +08:00 committed by GitHub
parent ef77c93934
commit 0cbc14961a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 74 deletions

View File

@ -507,7 +507,14 @@ func (hs *HTTPServer) NotificationTest(c *models.ReqContext) response.Response {
}
// POST /api/alerts/:alertId/pause
func (hs *HTTPServer) PauseAlert(c *models.ReqContext) response.Response {
func (hs *HTTPServer) PauseAlert(legacyAlertingEnabled *bool) func(c *models.ReqContext) response.Response {
if legacyAlertingEnabled == nil || !*legacyAlertingEnabled {
return func(_ *models.ReqContext) response.Response {
return response.Error(http.StatusBadRequest, "legacy alerting is disabled, so this call has no effect.", nil)
}
}
return func(c *models.ReqContext) response.Response {
dto := dtos.PauseAlertCommand{}
if err := web.Bind(c.Req, &dto); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
@ -565,9 +572,17 @@ func (hs *HTTPServer) PauseAlert(c *models.ReqContext) response.Response {
result["message"] = "Alert " + pausedState
return response.JSON(http.StatusOK, result)
}
}
// POST /api/admin/pause-all-alerts
func (hs *HTTPServer) PauseAllAlerts(c *models.ReqContext) response.Response {
func (hs *HTTPServer) PauseAllAlerts(legacyAlertingEnabled *bool) func(c *models.ReqContext) response.Response {
if legacyAlertingEnabled == nil || !*legacyAlertingEnabled {
return func(_ *models.ReqContext) response.Response {
return response.Error(http.StatusBadRequest, "legacy alerting is disabled, so this call has no effect.", nil)
}
}
return func(c *models.ReqContext) response.Response {
dto := dtos.PauseAllAlertsCommand{}
if err := web.Bind(c.Req, &dto); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
@ -595,3 +610,4 @@ func (hs *HTTPServer) PauseAllAlerts(c *models.ReqContext) response.Response {
return response.JSON(http.StatusOK, result)
}
}

View File

@ -145,7 +145,9 @@ func postAlertScenario(t *testing.T, hs *HTTPServer, desc string, url string, ro
sc.context.OrgId = testOrgID
sc.context.OrgRole = role
return hs.PauseAlert(c)
legacyAlertingEnabled := new(bool)
*legacyAlertingEnabled = true
return hs.PauseAlert(legacyAlertingEnabled)(c)
})
sc.m.Post(routePattern, sc.defaultHandler)

View File

@ -16,6 +16,7 @@ import (
"github.com/grafana/grafana/pkg/services/featuremgmt"
publicdashboardsapi "github.com/grafana/grafana/pkg/services/publicdashboards/api"
"github.com/grafana/grafana/pkg/services/serviceaccounts"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/web"
)
@ -460,7 +461,7 @@ func (hs *HTTPServer) registerRoutes() {
apiRoute.Group("/alerts", func(alertsRoute routing.RouteRegister) {
alertsRoute.Post("/test", routing.Wrap(hs.AlertTest))
alertsRoute.Post("/:alertId/pause", reqEditorRole, routing.Wrap(hs.PauseAlert))
alertsRoute.Post("/:alertId/pause", reqEditorRole, routing.Wrap(hs.PauseAlert(setting.AlertingEnabled)))
alertsRoute.Get("/:alertId", hs.ValidateOrgAlert, routing.Wrap(hs.GetAlert))
alertsRoute.Get("/", routing.Wrap(hs.GetAlerts))
alertsRoute.Get("/states-for-dashboard", routing.Wrap(hs.GetAlertStatesForDashboard))
@ -554,7 +555,7 @@ func (hs *HTTPServer) registerRoutes() {
adminRoute.Get("/settings/features", authorize(reqGrafanaAdmin, ac.EvalPermission(ac.ActionSettingsRead)), hs.Features.HandleGetSettings)
}
adminRoute.Get("/stats", authorize(reqGrafanaAdmin, ac.EvalPermission(ac.ActionServerStatsRead)), routing.Wrap(hs.AdminGetStats))
adminRoute.Post("/pause-all-alerts", reqGrafanaAdmin, routing.Wrap(hs.PauseAllAlerts))
adminRoute.Post("/pause-all-alerts", reqGrafanaAdmin, routing.Wrap(hs.PauseAllAlerts(setting.AlertingEnabled)))
if hs.ThumbService != nil && hs.Features.IsEnabled(featuremgmt.FlagDashboardPreviewsAdmin) {
adminRoute.Post("/crawler/start", reqGrafanaAdmin, routing.Wrap(hs.ThumbService.StartCrawler))