From 7969d6645a33e964f079f74b917924e50f3e9d0a Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Fri, 17 Jul 2020 13:54:01 +0200 Subject: [PATCH] Provisioning: Support alert notification channel secure settings (#26168) Closes #25968 --- docs/sources/administration/provisioning.md | 6 ++--- .../notifiers/alert_notifications.go | 2 ++ .../provisioning/notifiers/config_reader.go | 8 ++++--- .../notifiers/config_reader_test.go | 3 +++ .../correct-properties.yaml | 3 +++ pkg/services/provisioning/notifiers/types.go | 23 +++++++++++-------- 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/docs/sources/administration/provisioning.md b/docs/sources/administration/provisioning.md index 4c5010dcd03..867105890e1 100644 --- a/docs/sources/administration/provisioning.md +++ b/docs/sources/administration/provisioning.md @@ -307,7 +307,7 @@ By default Grafana will delete dashboards in the database if the file is removed ### Provision folders structure from filesystem to Grafana If you already store your dashboards using folders in a git repo or on a filesystem, and also you want to have the same folder names in the Grafana menu, you can use `foldersFromFilesStructure` option. -For example, to replicate these dashboards structure from the filesystem to Grafana, +For example, to replicate these dashboards structure from the filesystem to Grafana, ``` /etc/dashboards ├── /server @@ -320,7 +320,7 @@ For example, to replicate these dashboards structure from the filesystem to Graf you need to specify just this short provision configuration file. ```yaml apiVersion: 1 - + providers: - name: dashboards type: file @@ -535,4 +535,4 @@ The following sections detail the supported settings for each alert notification | Name | | ---- | -| url | +| url | \ No newline at end of file diff --git a/pkg/services/provisioning/notifiers/alert_notifications.go b/pkg/services/provisioning/notifiers/alert_notifications.go index 5831077f131..d3a3b34fcd5 100644 --- a/pkg/services/provisioning/notifiers/alert_notifications.go +++ b/pkg/services/provisioning/notifiers/alert_notifications.go @@ -94,6 +94,7 @@ func (dc *NotificationProvisioner) mergeNotifications(notificationToMerge []*not Type: notification.Type, IsDefault: notification.IsDefault, Settings: notification.SettingsToJSON(), + SecureSettings: notification.SecureSettings, OrgId: notification.OrgID, DisableResolveMessage: notification.DisableResolveMessage, Frequency: notification.Frequency, @@ -111,6 +112,7 @@ func (dc *NotificationProvisioner) mergeNotifications(notificationToMerge []*not Type: notification.Type, IsDefault: notification.IsDefault, Settings: notification.SettingsToJSON(), + SecureSettings: notification.SecureSettings, OrgId: notification.OrgID, DisableResolveMessage: notification.DisableResolveMessage, Frequency: notification.Frequency, diff --git a/pkg/services/provisioning/notifiers/config_reader.go b/pkg/services/provisioning/notifiers/config_reader.go index fe6aad52014..b1a3b58ecdd 100644 --- a/pkg/services/provisioning/notifiers/config_reader.go +++ b/pkg/services/provisioning/notifiers/config_reader.go @@ -7,6 +7,7 @@ import ( "path/filepath" "strings" + "github.com/grafana/grafana/pkg/components/securejsondata" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" @@ -147,9 +148,10 @@ func validateNotifications(notifications []*notificationsAsConfig) error { for _, notification := range notifications[i].Notifications { _, err := alerting.InitNotifier(&models.AlertNotification{ - Name: notification.Name, - Settings: notification.SettingsToJSON(), - Type: notification.Type, + Name: notification.Name, + Settings: notification.SettingsToJSON(), + SecureSettings: securejsondata.GetEncryptedJsonData(notification.SecureSettings), + Type: notification.Type, }) if err != nil { diff --git a/pkg/services/provisioning/notifiers/config_reader_test.go b/pkg/services/provisioning/notifiers/config_reader_test.go index 73ff2bf3f9c..486278f108e 100644 --- a/pkg/services/provisioning/notifiers/config_reader_test.go +++ b/pkg/services/provisioning/notifiers/config_reader_test.go @@ -66,6 +66,9 @@ func TestNotificationAsConfig(t *testing.T) { So(nt.Settings, ShouldResemble, map[string]interface{}{ "recipient": "XXX", "token": "xoxb", "uploadImage": true, "url": "https://slack.com", }) + So(nt.SecureSettings, ShouldResemble, map[string]string{ + "token": "xoxbsecure", "url": "https://slack.com/secure", + }) So(nt.SendReminder, ShouldBeTrue) So(nt.Frequency, ShouldEqual, "1h") diff --git a/pkg/services/provisioning/notifiers/testdata/test-configs/correct-properties/correct-properties.yaml b/pkg/services/provisioning/notifiers/testdata/test-configs/correct-properties/correct-properties.yaml index c4ad80b8275..417455ff857 100644 --- a/pkg/services/provisioning/notifiers/testdata/test-configs/correct-properties/correct-properties.yaml +++ b/pkg/services/provisioning/notifiers/testdata/test-configs/correct-properties/correct-properties.yaml @@ -11,6 +11,9 @@ notifiers: token: "xoxb" uploadImage: true url: https://slack.com + secure_settings: + url: https://slack.com/secure + token: "xoxbsecure" - name: another-not-default-notification type: email settings: diff --git a/pkg/services/provisioning/notifiers/types.go b/pkg/services/provisioning/notifiers/types.go index 640106a8750..3c320bf2b63 100644 --- a/pkg/services/provisioning/notifiers/types.go +++ b/pkg/services/provisioning/notifiers/types.go @@ -30,6 +30,7 @@ type notificationFromConfig struct { Frequency string IsDefault bool Settings map[string]interface{} + SecureSettings map[string]string } // notificationsAsConfigV0 is mapping for zero version configs. This is mapped to its normalised version. @@ -46,16 +47,17 @@ type deleteNotificationConfigV0 struct { } type notificationFromConfigV0 struct { - UID values.StringValue `json:"uid" yaml:"uid"` - OrgID values.Int64Value `json:"org_id" yaml:"org_id"` - OrgName values.StringValue `json:"org_name" yaml:"org_name"` - Name values.StringValue `json:"name" yaml:"name"` - Type values.StringValue `json:"type" yaml:"type"` - SendReminder values.BoolValue `json:"send_reminder" yaml:"send_reminder"` - DisableResolveMessage values.BoolValue `json:"disable_resolve_message" yaml:"disable_resolve_message"` - Frequency values.StringValue `json:"frequency" yaml:"frequency"` - IsDefault values.BoolValue `json:"is_default" yaml:"is_default"` - Settings values.JSONValue `json:"settings" yaml:"settings"` + UID values.StringValue `json:"uid" yaml:"uid"` + OrgID values.Int64Value `json:"org_id" yaml:"org_id"` + OrgName values.StringValue `json:"org_name" yaml:"org_name"` + Name values.StringValue `json:"name" yaml:"name"` + Type values.StringValue `json:"type" yaml:"type"` + SendReminder values.BoolValue `json:"send_reminder" yaml:"send_reminder"` + DisableResolveMessage values.BoolValue `json:"disable_resolve_message" yaml:"disable_resolve_message"` + Frequency values.StringValue `json:"frequency" yaml:"frequency"` + IsDefault values.BoolValue `json:"is_default" yaml:"is_default"` + Settings values.JSONValue `json:"settings" yaml:"settings"` + SecureSettings values.StringMapValue `json:"secure_settings" yaml:"secure_settings"` } func (notification notificationFromConfig) SettingsToJSON() *simplejson.Json { @@ -88,6 +90,7 @@ func (cfg *notificationsAsConfigV0) mapToNotificationFromConfig() *notifications DisableResolveMessage: notification.DisableResolveMessage.Value(), Frequency: notification.Frequency.Value(), SendReminder: notification.SendReminder.Value(), + SecureSettings: notification.SecureSettings.Value(), }) }