2022-02-05 01:42:04 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2022-03-16 21:36:58 +08:00
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2022-02-05 01:42:04 +08:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2022-03-16 21:36:58 +08:00
|
|
|
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2022-02-05 01:42:04 +08:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
|
|
|
|
)
|
|
|
|
|
|
2022-03-16 21:36:58 +08:00
|
|
|
//nolint:gocyclo
|
2022-02-05 01:42:04 +08:00
|
|
|
func (api *API) authorize(method, path string) web.Handler {
|
2022-04-28 16:46:18 +08:00
|
|
|
authorize := ac.Middleware(api.AccessControl)
|
2022-03-16 21:36:58 +08:00
|
|
|
var eval ac.Evaluator = nil
|
|
|
|
|
|
|
|
|
|
switch method + path {
|
|
|
|
|
// Alert Rules
|
|
|
|
|
|
|
|
|
|
// Grafana Paths
|
2024-03-20 10:20:30 +08:00
|
|
|
case http.MethodDelete + "/api/ruler/grafana/api/v1/rules/{Namespace}/{Groupname}",
|
|
|
|
|
http.MethodDelete + "/api/ruler/grafana/api/v1/rules/{Namespace}":
|
|
|
|
|
eval = ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleDelete, dashboards.ScopeFoldersProvider.GetResourceScopeUID(ac.Parameter(":Namespace"))),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead, dashboards.ScopeFoldersProvider.GetResourceScopeUID(ac.Parameter(":Namespace"))),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead, dashboards.ScopeFoldersProvider.GetResourceScopeUID(ac.Parameter(":Namespace"))),
|
|
|
|
|
)
|
2022-03-16 21:36:58 +08:00
|
|
|
case http.MethodGet + "/api/ruler/grafana/api/v1/rules/{Namespace}/{Groupname}":
|
2024-03-20 10:20:30 +08:00
|
|
|
eval = ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead, dashboards.ScopeFoldersProvider.GetResourceScopeUID(ac.Parameter(":Namespace"))),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead, dashboards.ScopeFoldersProvider.GetResourceScopeUID(ac.Parameter(":Namespace"))),
|
|
|
|
|
)
|
2022-03-16 21:36:58 +08:00
|
|
|
case http.MethodGet + "/api/ruler/grafana/api/v1/rules/{Namespace}":
|
2024-03-20 10:20:30 +08:00
|
|
|
eval = ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead, dashboards.ScopeFoldersProvider.GetResourceScopeUID(ac.Parameter(":Namespace"))),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead, dashboards.ScopeFoldersProvider.GetResourceScopeUID(ac.Parameter(":Namespace"))),
|
|
|
|
|
)
|
2023-10-02 23:47:59 +08:00
|
|
|
case http.MethodGet + "/api/ruler/grafana/api/v1/rules",
|
|
|
|
|
http.MethodGet + "/api/ruler/grafana/api/v1/export/rules":
|
2022-03-16 21:36:58 +08:00
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleRead)
|
2025-02-04 02:26:18 +08:00
|
|
|
case http.MethodGet + "/api/ruler/grafana/api/v1/rule/{RuleUID}",
|
|
|
|
|
http.MethodGet + "/api/ruler/grafana/api/v1/rule/{RuleUID}/versions":
|
2024-05-02 22:24:59 +08:00
|
|
|
eval = ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead),
|
|
|
|
|
)
|
2023-10-02 23:47:59 +08:00
|
|
|
case http.MethodPost + "/api/ruler/grafana/api/v1/rules/{Namespace}/export":
|
2024-01-26 04:19:51 +08:00
|
|
|
scope := dashboards.ScopeFoldersProvider.GetResourceScopeUID(ac.Parameter(":Namespace"))
|
2023-10-02 23:47:59 +08:00
|
|
|
// more granular permissions are enforced by the handler via "authorizeRuleChanges"
|
2024-03-20 10:20:30 +08:00
|
|
|
eval = ac.EvalAll(ac.EvalPermission(ac.ActionAlertingRuleRead, scope),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead, scope),
|
|
|
|
|
)
|
2025-05-13 23:04:01 +08:00
|
|
|
case http.MethodPost + "/api/ruler/grafana/api/v1/rules/{Namespace}",
|
|
|
|
|
http.MethodPatch + "/api/ruler/grafana/api/v1/rules/{Namespace}":
|
2024-01-26 04:19:51 +08:00
|
|
|
scope := dashboards.ScopeFoldersProvider.GetResourceScopeUID(ac.Parameter(":Namespace"))
|
2022-03-16 21:36:58 +08:00
|
|
|
// more granular permissions are enforced by the handler via "authorizeRuleChanges"
|
2024-03-20 10:20:30 +08:00
|
|
|
eval = ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead, scope),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead, scope),
|
|
|
|
|
ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleUpdate, scope),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleCreate, scope),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleDelete, scope),
|
|
|
|
|
),
|
2022-03-16 21:36:58 +08:00
|
|
|
)
|
2025-03-15 04:14:06 +08:00
|
|
|
case http.MethodDelete + "/api/ruler/grafana/api/v1/trash/rule/guid/{RuleGUID}":
|
|
|
|
|
return middleware.ReqOrgAdmin
|
2024-02-06 02:12:15 +08:00
|
|
|
|
|
|
|
|
// Grafana rule state history paths
|
2023-02-03 01:34:00 +08:00
|
|
|
case http.MethodGet + "/api/v1/rules/history":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleRead)
|
2022-03-16 21:36:58 +08:00
|
|
|
|
|
|
|
|
// Grafana, Prometheus-compatible Paths
|
|
|
|
|
case http.MethodGet + "/api/prometheus/grafana/api/v1/rules":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleRead)
|
|
|
|
|
|
|
|
|
|
// Grafana Rules Testing Paths
|
|
|
|
|
case http.MethodPost + "/api/v1/rule/test/grafana":
|
|
|
|
|
// additional authorization is done in the request handler
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleRead)
|
2022-12-14 22:44:14 +08:00
|
|
|
// Grafana Rules Testing Paths
|
|
|
|
|
case http.MethodPost + "/api/v1/rule/backtest":
|
|
|
|
|
// additional authorization is done in the request handler
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleRead)
|
2022-03-16 21:36:58 +08:00
|
|
|
case http.MethodPost + "/api/v1/eval":
|
|
|
|
|
// additional authorization is done in the request handler
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleRead)
|
|
|
|
|
|
|
|
|
|
// Lotex Paths
|
2022-05-05 19:58:32 +08:00
|
|
|
case http.MethodDelete + "/api/ruler/{DatasourceUID}/api/v1/rules/{Namespace}":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleExternalWrite, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
|
|
|
|
case http.MethodDelete + "/api/ruler/{DatasourceUID}/api/v1/rules/{Namespace}/{Groupname}":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleExternalWrite, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
|
|
|
|
case http.MethodGet + "/api/ruler/{DatasourceUID}/api/v1/rules/{Namespace}":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
|
|
|
|
case http.MethodGet + "/api/ruler/{DatasourceUID}/api/v1/rules/{Namespace}/{Groupname}":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
|
|
|
|
case http.MethodGet + "/api/ruler/{DatasourceUID}/api/v1/rules":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
|
|
|
|
case http.MethodPost + "/api/ruler/{DatasourceUID}/api/v1/rules/{Namespace}":
|
2024-09-28 03:23:21 +08:00
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleExternalWrite, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
2022-03-16 21:36:58 +08:00
|
|
|
|
|
|
|
|
// Lotex Prometheus-compatible Paths
|
2022-05-07 03:05:02 +08:00
|
|
|
case http.MethodGet + "/api/prometheus/{DatasourceUID}/api/v1/rules":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
2022-03-16 21:36:58 +08:00
|
|
|
|
|
|
|
|
// Lotex Rules testing
|
2022-05-17 19:10:20 +08:00
|
|
|
case http.MethodPost + "/api/v1/rule/test/{DatasourceUID}":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingRuleExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
2022-03-16 21:36:58 +08:00
|
|
|
|
2025-02-12 15:13:21 +08:00
|
|
|
// convert/prometheus API paths
|
|
|
|
|
case http.MethodGet + "/api/convert/prometheus/config/v1/rules/{NamespaceTitle}/{Group}",
|
2025-02-28 00:20:49 +08:00
|
|
|
http.MethodGet + "/api/convert/api/prom/rules/{NamespaceTitle}/{Group}",
|
|
|
|
|
http.MethodGet + "/api/convert/prometheus/config/v1/rules/{NamespaceTitle}",
|
|
|
|
|
http.MethodGet + "/api/convert/api/prom/rules/{NamespaceTitle}":
|
2025-02-12 15:13:21 +08:00
|
|
|
eval = ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead),
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-28 00:20:49 +08:00
|
|
|
case http.MethodGet + "/api/convert/prometheus/config/v1/rules",
|
|
|
|
|
http.MethodGet + "/api/convert/api/prom/rules":
|
2025-02-25 18:26:36 +08:00
|
|
|
eval = ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead),
|
|
|
|
|
)
|
2025-02-12 15:13:21 +08:00
|
|
|
|
2025-02-28 00:20:49 +08:00
|
|
|
case http.MethodPost + "/api/convert/prometheus/config/v1/rules/{NamespaceTitle}",
|
2025-03-27 23:30:11 +08:00
|
|
|
http.MethodPost + "/api/convert/api/prom/rules/{NamespaceTitle}",
|
|
|
|
|
http.MethodPost + "/api/convert/prometheus/config/v1/rules",
|
2025-04-09 16:40:22 +08:00
|
|
|
http.MethodPost + "/api/convert/api/prom/rules":
|
2025-02-12 15:13:21 +08:00
|
|
|
eval = ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleCreate),
|
2025-02-25 18:26:36 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningSetStatus),
|
2025-02-12 15:13:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
case http.MethodDelete + "/api/convert/prometheus/config/v1/rules/{NamespaceTitle}/{Group}",
|
2025-02-28 00:20:49 +08:00
|
|
|
http.MethodDelete + "/api/convert/api/prom/rules/{NamespaceTitle}/{Group}",
|
|
|
|
|
http.MethodDelete + "/api/convert/prometheus/config/v1/rules/{NamespaceTitle}",
|
|
|
|
|
http.MethodDelete + "/api/convert/api/prom/rules/{NamespaceTitle}":
|
2025-03-04 00:59:01 +08:00
|
|
|
eval = ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleDelete),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningSetStatus),
|
2025-02-12 15:13:21 +08:00
|
|
|
)
|
|
|
|
|
|
2025-06-11 19:45:02 +08:00
|
|
|
case http.MethodPost + "/api/convert/api/v1/alerts",
|
|
|
|
|
http.MethodDelete + "/api/convert/api/v1/alerts":
|
2025-06-10 17:35:57 +08:00
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingNotificationsWrite)
|
2025-06-11 19:45:02 +08:00
|
|
|
case http.MethodGet + "/api/convert/api/v1/alerts":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingNotificationsRead)
|
2025-06-10 17:35:57 +08:00
|
|
|
|
2022-03-16 21:36:58 +08:00
|
|
|
// Alert Instances and Silences
|
|
|
|
|
|
2024-05-04 03:32:30 +08:00
|
|
|
// Silences for Grafana paths.
|
|
|
|
|
// These permissions are required but not sufficient, further authorization is done in the request handler.
|
|
|
|
|
case http.MethodDelete + "/api/alertmanager/grafana/api/v2/silence/{SilenceId}": // Delete endpoint is used for silence expiration.
|
|
|
|
|
eval = ac.EvalAll(
|
|
|
|
|
ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingInstanceRead),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingSilencesRead),
|
|
|
|
|
),
|
|
|
|
|
ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingInstanceUpdate),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingSilencesWrite),
|
|
|
|
|
),
|
|
|
|
|
)
|
2022-03-16 21:36:58 +08:00
|
|
|
case http.MethodGet + "/api/alertmanager/grafana/api/v2/silence/{SilenceId}":
|
2024-05-04 03:32:30 +08:00
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingInstanceRead),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingSilencesRead),
|
|
|
|
|
)
|
2022-03-16 21:36:58 +08:00
|
|
|
case http.MethodGet + "/api/alertmanager/grafana/api/v2/silences":
|
2024-05-04 03:32:30 +08:00
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingInstanceRead),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingSilencesRead),
|
|
|
|
|
)
|
2022-03-16 21:36:58 +08:00
|
|
|
case http.MethodPost + "/api/alertmanager/grafana/api/v2/silences":
|
2024-05-04 03:32:30 +08:00
|
|
|
eval = ac.EvalAll(
|
|
|
|
|
ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingInstanceRead),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingSilencesRead),
|
|
|
|
|
),
|
|
|
|
|
ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingInstanceCreate),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingInstanceUpdate),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingSilencesCreate),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingSilencesWrite),
|
|
|
|
|
),
|
|
|
|
|
)
|
2022-03-16 21:36:58 +08:00
|
|
|
|
|
|
|
|
// Alert Instances. Grafana Paths
|
|
|
|
|
case http.MethodGet + "/api/alertmanager/grafana/api/v2/alerts/groups":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingInstanceRead)
|
|
|
|
|
case http.MethodGet + "/api/alertmanager/grafana/api/v2/alerts":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingInstanceRead)
|
|
|
|
|
|
|
|
|
|
// Grafana Prometheus-compatible Paths
|
|
|
|
|
case http.MethodGet + "/api/prometheus/grafana/api/v1/alerts":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingInstanceRead)
|
|
|
|
|
|
|
|
|
|
// Silences. External AM.
|
2022-04-29 15:25:22 +08:00
|
|
|
case http.MethodDelete + "/api/alertmanager/{DatasourceUID}/api/v2/silence/{SilenceId}":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingInstancesExternalWrite, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
|
|
|
|
case http.MethodPost + "/api/alertmanager/{DatasourceUID}/api/v2/silences":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingInstancesExternalWrite, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
|
|
|
|
case http.MethodGet + "/api/alertmanager/{DatasourceUID}/api/v2/silence/{SilenceId}":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingInstancesExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
|
|
|
|
case http.MethodGet + "/api/alertmanager/{DatasourceUID}/api/v2/silences":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingInstancesExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
2022-03-16 21:36:58 +08:00
|
|
|
|
|
|
|
|
// Alert instances. External AM.
|
2022-04-29 15:25:22 +08:00
|
|
|
case http.MethodGet + "/api/alertmanager/{DatasourceUID}/api/v2/alerts/groups":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingInstancesExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
|
|
|
|
case http.MethodGet + "/api/alertmanager/{DatasourceUID}/api/v2/alerts":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingInstancesExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
|
|
|
|
case http.MethodPost + "/api/alertmanager/{DatasourceUID}/api/v2/alerts":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingInstancesExternalWrite, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
2022-03-16 21:36:58 +08:00
|
|
|
|
|
|
|
|
// Prometheus-compatible Paths
|
2022-05-07 03:05:02 +08:00
|
|
|
case http.MethodGet + "/api/prometheus/{DatasourceUID}/api/v1/alerts":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingInstancesExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
2022-03-16 21:36:58 +08:00
|
|
|
|
|
|
|
|
// Notification Policies, Contact Points and Templates
|
|
|
|
|
|
|
|
|
|
// Grafana Paths
|
|
|
|
|
case http.MethodDelete + "/api/alertmanager/grafana/config/api/v1/alerts": // reset alertmanager config to the default
|
2022-05-20 22:55:07 +08:00
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingNotificationsWrite)
|
2022-03-16 21:36:58 +08:00
|
|
|
case http.MethodGet + "/api/alertmanager/grafana/config/api/v1/alerts":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingNotificationsRead)
|
2023-04-01 04:43:04 +08:00
|
|
|
case http.MethodGet + "/api/alertmanager/grafana/config/history":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingNotificationsRead)
|
2022-03-16 21:36:58 +08:00
|
|
|
case http.MethodGet + "/api/alertmanager/grafana/api/v2/status":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingNotificationsRead)
|
2023-04-06 02:10:03 +08:00
|
|
|
case http.MethodPost + "/api/alertmanager/grafana/config/history/{id}/_activate":
|
|
|
|
|
eval = ac.EvalAny(ac.EvalPermission(ac.ActionAlertingNotificationsWrite))
|
2022-10-03 21:58:41 +08:00
|
|
|
case http.MethodGet + "/api/alertmanager/grafana/config/api/v1/receivers":
|
2024-10-08 03:21:29 +08:00
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsRead),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingReceiversRead),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingReceiversReadSecrets),
|
|
|
|
|
)
|
2022-03-16 21:36:58 +08:00
|
|
|
case http.MethodPost + "/api/alertmanager/grafana/config/api/v1/receivers/test":
|
2024-10-05 03:52:44 +08:00
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsWrite),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingReceiversTest),
|
|
|
|
|
)
|
2023-04-28 22:56:59 +08:00
|
|
|
case http.MethodPost + "/api/alertmanager/grafana/config/api/v1/templates/test":
|
2024-10-05 03:52:44 +08:00
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsWrite),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsTemplatesRead),
|
|
|
|
|
)
|
2022-03-16 21:36:58 +08:00
|
|
|
|
|
|
|
|
// External Alertmanager Paths
|
2022-04-29 15:25:22 +08:00
|
|
|
case http.MethodDelete + "/api/alertmanager/{DatasourceUID}/config/api/v1/alerts":
|
2022-05-20 22:55:07 +08:00
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingNotificationsExternalWrite, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
2022-04-29 15:25:22 +08:00
|
|
|
case http.MethodGet + "/api/alertmanager/{DatasourceUID}/api/v2/status":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingNotificationsExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
|
|
|
|
case http.MethodGet + "/api/alertmanager/{DatasourceUID}/config/api/v1/alerts":
|
|
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingNotificationsExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
|
|
|
|
case http.MethodPost + "/api/alertmanager/{DatasourceUID}/config/api/v1/alerts":
|
2022-06-03 21:12:34 +08:00
|
|
|
eval = ac.EvalPermission(ac.ActionAlertingNotificationsExternalWrite, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
|
2022-03-16 21:36:58 +08:00
|
|
|
|
2022-09-15 02:03:10 +08:00
|
|
|
case http.MethodGet + "/api/v1/ngalert":
|
|
|
|
|
// let user with any alerting permission access this API
|
|
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingInstanceRead),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingInstancesExternalRead),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleExternalRead),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsRead),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsExternalRead),
|
|
|
|
|
)
|
2022-03-16 21:36:58 +08:00
|
|
|
// Raw Alertmanager Config Paths
|
|
|
|
|
case http.MethodDelete + "/api/v1/ngalert/admin_config",
|
|
|
|
|
http.MethodGet + "/api/v1/ngalert/admin_config",
|
|
|
|
|
http.MethodPost + "/api/v1/ngalert/admin_config",
|
|
|
|
|
http.MethodGet + "/api/v1/ngalert/alertmanagers":
|
|
|
|
|
return middleware.ReqOrgAdmin
|
2022-04-06 05:48:51 +08:00
|
|
|
|
2024-09-28 02:56:32 +08:00
|
|
|
// Grafana-only Provisioning Export Paths for everything except contact points.
|
2023-10-07 02:48:20 +08:00
|
|
|
case http.MethodGet + "/api/v1/provisioning/policies/export",
|
2023-12-12 10:36:51 +08:00
|
|
|
http.MethodGet + "/api/v1/provisioning/mute-timings/export",
|
|
|
|
|
http.MethodGet + "/api/v1/provisioning/mute-timings/{name}/export":
|
2023-10-07 02:48:20 +08:00
|
|
|
eval = ac.EvalAny(
|
2024-05-10 01:19:07 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsRead), // organization scope
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningRead), // organization scope
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsProvisioningRead), // organization scope
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningReadSecrets), // organization scope
|
2023-10-07 02:48:20 +08:00
|
|
|
)
|
|
|
|
|
|
2024-09-28 02:56:32 +08:00
|
|
|
// Grafana-only Provisioning Export Paths for contact points.
|
|
|
|
|
case http.MethodGet + "/api/v1/provisioning/contact-points/export":
|
|
|
|
|
perms := []ac.Evaluator{
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsRead), // organization scope
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningRead), // organization scope
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsProvisioningRead), // organization scope
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningReadSecrets), // organization scope
|
2025-04-12 05:38:53 +08:00
|
|
|
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingReceiversRead), // organization scope
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingReceiversReadSecrets), // organization scope
|
2024-09-28 02:56:32 +08:00
|
|
|
}
|
|
|
|
|
eval = ac.EvalAny(perms...)
|
|
|
|
|
|
2024-03-23 03:37:10 +08:00
|
|
|
case http.MethodGet + "/api/v1/provisioning/alert-rules",
|
|
|
|
|
http.MethodGet + "/api/v1/provisioning/alert-rules/export":
|
|
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningRead),
|
2024-05-10 01:19:07 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingRulesProvisioningRead),
|
2024-03-23 03:37:10 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningReadSecrets),
|
|
|
|
|
ac.EvalAll( // scopes are enforced in the handler
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
case http.MethodGet + "/api/v1/provisioning/alert-rules/{UID}",
|
|
|
|
|
http.MethodGet + "/api/v1/provisioning/alert-rules/{UID}/export":
|
|
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningRead),
|
2024-05-10 01:19:07 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingRulesProvisioningRead),
|
2024-03-23 03:37:10 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningReadSecrets),
|
|
|
|
|
ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
case http.MethodGet + "/api/v1/provisioning/folder/{FolderUID}/rule-groups/{Group}",
|
|
|
|
|
http.MethodGet + "/api/v1/provisioning/folder/{FolderUID}/rule-groups/{Group}/export":
|
|
|
|
|
scope := dashboards.ScopeFoldersProvider.GetResourceScopeUID(ac.Parameter(":FolderUID"))
|
|
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningRead),
|
2024-05-10 01:19:07 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingRulesProvisioningRead),
|
2024-03-23 03:37:10 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningReadSecrets),
|
|
|
|
|
ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead, scope),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead, scope),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
2022-06-03 22:45:08 +08:00
|
|
|
case http.MethodGet + "/api/v1/provisioning/policies",
|
|
|
|
|
http.MethodGet + "/api/v1/provisioning/contact-points",
|
|
|
|
|
http.MethodGet + "/api/v1/provisioning/templates",
|
|
|
|
|
http.MethodGet + "/api/v1/provisioning/templates/{name}",
|
|
|
|
|
http.MethodGet + "/api/v1/provisioning/mute-timings",
|
2024-03-23 03:37:10 +08:00
|
|
|
http.MethodGet + "/api/v1/provisioning/mute-timings/{name}":
|
|
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningRead),
|
2024-05-10 01:19:07 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsProvisioningRead), // organization scope
|
2024-03-23 03:37:10 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningReadSecrets),
|
2024-03-23 06:14:15 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsRead),
|
2024-03-23 03:37:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Grafana-only Provisioning Write Paths
|
|
|
|
|
case http.MethodPost + "/api/v1/provisioning/alert-rules":
|
|
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningWrite),
|
2024-05-10 01:19:07 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingRulesProvisioningWrite),
|
2024-03-23 06:14:15 +08:00
|
|
|
ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleCreate), // more granular permissions are enforced by the handler via "authorizeRuleChanges"
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningSetStatus),
|
|
|
|
|
),
|
2024-03-23 03:37:10 +08:00
|
|
|
)
|
|
|
|
|
case http.MethodPut + "/api/v1/provisioning/alert-rules/{UID}":
|
|
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningWrite),
|
2024-05-10 01:19:07 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingRulesProvisioningWrite),
|
2024-03-23 06:14:15 +08:00
|
|
|
ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleUpdate), // more granular permissions are enforced by the handler via "authorizeRuleChanges"
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningSetStatus),
|
|
|
|
|
),
|
2024-03-23 03:37:10 +08:00
|
|
|
)
|
|
|
|
|
case http.MethodDelete + "/api/v1/provisioning/alert-rules/{UID}":
|
|
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningWrite),
|
2024-05-10 01:19:07 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingRulesProvisioningWrite),
|
2024-03-23 06:14:15 +08:00
|
|
|
ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleDelete), // more granular permissions are enforced by the handler via "authorizeRuleChanges"
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningSetStatus),
|
|
|
|
|
),
|
2024-03-23 03:37:10 +08:00
|
|
|
)
|
|
|
|
|
case http.MethodDelete + "/api/v1/provisioning/folder/{FolderUID}/rule-groups/{Group}":
|
|
|
|
|
scope := dashboards.ScopeFoldersProvider.GetResourceScopeUID(ac.Parameter(":FolderUID"))
|
|
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningWrite),
|
2024-05-10 01:19:07 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingRulesProvisioningWrite),
|
2024-03-23 03:37:10 +08:00
|
|
|
ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleDelete, scope),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead, scope),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead, scope),
|
2024-03-23 06:14:15 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningSetStatus),
|
2024-03-23 03:37:10 +08:00
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
case http.MethodPut + "/api/v1/provisioning/folder/{FolderUID}/rule-groups/{Group}":
|
|
|
|
|
scope := dashboards.ScopeFoldersProvider.GetResourceScopeUID(ac.Parameter(":FolderUID"))
|
|
|
|
|
eval = ac.EvalAny(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningWrite),
|
2024-05-10 01:19:07 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingRulesProvisioningWrite),
|
2024-03-23 03:37:10 +08:00
|
|
|
ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleRead, scope),
|
|
|
|
|
ac.EvalPermission(dashboards.ActionFoldersRead, scope),
|
2024-03-23 06:14:15 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningSetStatus),
|
2024-03-23 03:37:10 +08:00
|
|
|
ac.EvalAny( // the exact permissions will be checked after the operations are determined
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleUpdate, scope),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleCreate, scope),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingRuleDelete, scope),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
2022-04-06 05:48:51 +08:00
|
|
|
|
2022-06-03 22:45:08 +08:00
|
|
|
case http.MethodPut + "/api/v1/provisioning/policies",
|
2022-07-09 05:23:18 +08:00
|
|
|
http.MethodDelete + "/api/v1/provisioning/policies",
|
2022-06-03 22:45:08 +08:00
|
|
|
http.MethodPost + "/api/v1/provisioning/contact-points",
|
|
|
|
|
http.MethodPut + "/api/v1/provisioning/contact-points/{UID}",
|
|
|
|
|
http.MethodDelete + "/api/v1/provisioning/contact-points/{UID}",
|
|
|
|
|
http.MethodPut + "/api/v1/provisioning/templates/{name}",
|
|
|
|
|
http.MethodDelete + "/api/v1/provisioning/templates/{name}",
|
|
|
|
|
http.MethodPost + "/api/v1/provisioning/mute-timings",
|
|
|
|
|
http.MethodPut + "/api/v1/provisioning/mute-timings/{name}",
|
2024-03-23 03:37:10 +08:00
|
|
|
http.MethodDelete + "/api/v1/provisioning/mute-timings/{name}":
|
2024-03-23 06:14:15 +08:00
|
|
|
eval = ac.EvalAny(
|
2024-05-10 01:19:07 +08:00
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningWrite), // organization scope,
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsProvisioningWrite), // organization scope
|
2024-03-23 06:14:15 +08:00
|
|
|
ac.EvalAll(
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingNotificationsWrite),
|
|
|
|
|
ac.EvalPermission(ac.ActionAlertingProvisioningSetStatus),
|
|
|
|
|
),
|
|
|
|
|
)
|
2022-03-16 21:36:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if eval != nil {
|
2023-05-24 16:49:42 +08:00
|
|
|
return authorize(eval)
|
2022-03-16 21:36:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
panic(fmt.Sprintf("no authorization handler for method [%s] of endpoint [%s]", method, path))
|
2022-02-05 01:42:04 +08:00
|
|
|
}
|