From ba9c18d9c37a4f9836daeaf37f5e1d9533decbf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Philippe=20Qu=C3=A9m=C3=A9ner?= Date: Wed, 20 Jul 2022 19:58:36 +0200 Subject: [PATCH] Alerting: use static channel configuration to determinate secure fields (#52527) * Alerting: use static channel configuration to determinate secure fields * move to channels package * introduce channel_config package to fix cyclic import * add missing changes * compare type to type --- pkg/api/alerting.go | 4 +- .../definitions/provisioning_contactpoints.go | 49 +++++-------------- .../available_channels.go | 2 +- .../alerting/api_available_channel_test.go | 4 +- 4 files changed, 17 insertions(+), 42 deletions(-) rename pkg/services/ngalert/notifier/{ => channels_config}/available_channels.go (99%) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index d9d868f4e96..fa46accb629 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -13,7 +13,7 @@ import ( "github.com/grafana/grafana/pkg/services/alerting" "github.com/grafana/grafana/pkg/services/datasources" "github.com/grafana/grafana/pkg/services/guardian" - "github.com/grafana/grafana/pkg/services/ngalert/notifier" + "github.com/grafana/grafana/pkg/services/ngalert/notifier/channels_config" "github.com/grafana/grafana/pkg/services/search" "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" @@ -198,7 +198,7 @@ func (hs *HTTPServer) GetAlert(c *models.ReqContext) response.Response { func (hs *HTTPServer) GetAlertNotifiers(ngalertEnabled bool) func(*models.ReqContext) response.Response { return func(_ *models.ReqContext) response.Response { if ngalertEnabled { - return response.JSON(http.StatusOK, notifier.GetAvailableNotifiers()) + return response.JSON(http.StatusOK, channels_config.GetAvailableNotifiers()) } // TODO(codesome): This wont be required in 8.0 since ngalert // will be enabled by default with no disabling. This is to be removed later. diff --git a/pkg/services/ngalert/api/tooling/definitions/provisioning_contactpoints.go b/pkg/services/ngalert/api/tooling/definitions/provisioning_contactpoints.go index 894271203ee..1efc0cd2cee 100644 --- a/pkg/services/ngalert/api/tooling/definitions/provisioning_contactpoints.go +++ b/pkg/services/ngalert/api/tooling/definitions/provisioning_contactpoints.go @@ -5,6 +5,7 @@ import ( "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/services/ngalert/notifier/channels" + "github.com/grafana/grafana/pkg/services/ngalert/notifier/channels_config" ) // swagger:route GET /api/v1/provisioning/contact-points provisioning stable RouteGetContactpoints @@ -118,43 +119,17 @@ func (e *EmbeddedContactPoint) Valid(decryptFunc channels.GetDecryptedValueFn) e } func (e *EmbeddedContactPoint) SecretKeys() ([]string, error) { - switch e.Type { - case "alertmanager": - return []string{"basicAuthPassword"}, nil - case "dingding": - return []string{}, nil - case "discord": - return []string{}, nil - case "email": - return []string{}, nil - case "googlechat": - return []string{}, nil - case "kafka": - return []string{}, nil - case "line": - return []string{"token"}, nil - case "opsgenie": - return []string{"apiKey"}, nil - case "pagerduty": - return []string{"integrationKey"}, nil - case "pushover": - return []string{"userKey", "apiToken"}, nil - case "sensugo": - return []string{"apiKey"}, nil - case "slack": - return []string{"url", "token"}, nil - case "teams": - return []string{}, nil - case "telegram": - return []string{"bottoken"}, nil - case "threema": - return []string{"api_secret"}, nil - case "victorops": - return []string{}, nil - case "webhook": - return []string{}, nil - case "wecom": - return []string{"url"}, nil + notifiers := channels_config.GetAvailableNotifiers() + for _, n := range notifiers { + if n.Type == e.Type { + secureFields := []string{} + for _, field := range n.Options { + if field.Secure { + secureFields = append(secureFields, field.PropertyName) + } + } + return secureFields, nil + } } return nil, fmt.Errorf("no secrets configured for type '%s'", e.Type) } diff --git a/pkg/services/ngalert/notifier/available_channels.go b/pkg/services/ngalert/notifier/channels_config/available_channels.go similarity index 99% rename from pkg/services/ngalert/notifier/available_channels.go rename to pkg/services/ngalert/notifier/channels_config/available_channels.go index 48c82c7bc7d..f3b75e27757 100644 --- a/pkg/services/ngalert/notifier/available_channels.go +++ b/pkg/services/ngalert/notifier/channels_config/available_channels.go @@ -1,4 +1,4 @@ -package notifier +package channels_config import ( "github.com/grafana/grafana/pkg/services/alerting" diff --git a/pkg/tests/api/alerting/api_available_channel_test.go b/pkg/tests/api/alerting/api_available_channel_test.go index 1002f4fc390..4fd7cc3a077 100644 --- a/pkg/tests/api/alerting/api_available_channel_test.go +++ b/pkg/tests/api/alerting/api_available_channel_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/require" "github.com/grafana/grafana/pkg/models" - "github.com/grafana/grafana/pkg/services/ngalert/notifier" + "github.com/grafana/grafana/pkg/services/ngalert/notifier/channels_config" "github.com/grafana/grafana/pkg/services/user" "github.com/grafana/grafana/pkg/tests/testinfra" ) @@ -44,7 +44,7 @@ func TestAvailableChannels(t *testing.T) { require.NoError(t, err) require.Equal(t, 200, resp.StatusCode) - expNotifiers := notifier.GetAvailableNotifiers() + expNotifiers := channels_config.GetAvailableNotifiers() expJson, err := json.Marshal(expNotifiers) require.NoError(t, err) require.Equal(t, string(expJson), string(b))