NGAlert: Remove unwanted fields from notification channel config (#34036)

Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>
This commit is contained in:
Ganesh Vernekar
2021-05-18 10:04:47 +02:00
committed by GitHub
parent bfcf82f861
commit 89c2b5e863
22 changed files with 138 additions and 132 deletions
+1 -4
View File
@@ -89,13 +89,10 @@ func (srv AlertmanagerSrv) RouteGetAlertingConfig(c *models.ReqContext) response
secureFields[k] = true
}
gr := apimodels.GettableGrafanaReceiver{
Uid: pr.Uid,
UID: pr.UID,
Name: pr.Name,
Type: pr.Type,
IsDefault: pr.IsDefault,
SendReminder: pr.SendReminder,
DisableResolveMessage: pr.DisableResolveMessage,
Frequency: pr.Frequency,
Settings: pr.Settings,
SecureFields: secureFields,
}
@@ -6,14 +6,13 @@ import (
"fmt"
"reflect"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
amv2 "github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/alertmanager/config"
"gopkg.in/yaml.v3"
amv2 "github.com/prometheus/alertmanager/api/v2/models"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
)
// swagger:route POST /api/alertmanager/{Recipient}/config/api/v1/alerts alertmanager RoutePostAlertingConfig
@@ -504,8 +503,23 @@ func AllReceivers(route *config.Route) (res []string) {
return res
}
type GettableGrafanaReceiver dtos.AlertNotification
type PostableGrafanaReceiver models.CreateAlertNotificationCommand
type GettableGrafanaReceiver struct {
UID string `json:"uid"`
Name string `json:"name"`
Type string `json:"type"`
DisableResolveMessage bool `json:"disableResolveMessage"`
Settings *simplejson.Json `json:"settings"`
SecureFields map[string]bool `json:"secureFields"`
}
type PostableGrafanaReceiver struct {
UID string `json:"uid"`
Name string `json:"name"`
Type string `json:"type"`
DisableResolveMessage bool `json:"disableResolveMessage"`
Settings *simplejson.Json `json:"settings"`
SecureSettings map[string]string `json:"secureSettings"`
}
type ReceiverType int
@@ -27,7 +27,6 @@ import (
"github.com/grafana/grafana/pkg/components/securejsondata"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/ngalert/logging"
"github.com/grafana/grafana/pkg/services/ngalert/metrics"
@@ -397,12 +396,10 @@ func (am *Alertmanager) buildReceiverIntegrations(receiver *apimodels.PostableAp
secureSettings[k] = d
}
var (
cfg = &models.AlertNotification{
Uid: r.Uid,
cfg = &channels.NotificationChannelConfig{
UID: r.UID,
Name: r.Name,
Type: r.Type,
IsDefault: r.IsDefault,
SendReminder: r.SendReminder,
DisableResolveMessage: r.DisableResolveMessage,
Settings: r.Settings,
SecureSettings: secureSettings,
@@ -23,7 +23,7 @@ import (
const defaultDingdingMsgType = "link"
// NewDingDingNotifier is the constructor for the Dingding notifier
func NewDingDingNotifier(model *models.AlertNotification, t *template.Template) (*DingDingNotifier, error) {
func NewDingDingNotifier(model *NotificationChannelConfig, t *template.Template) (*DingDingNotifier, error) {
if model.Settings == nil {
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
}
@@ -36,12 +36,18 @@ func NewDingDingNotifier(model *models.AlertNotification, t *template.Template)
msgType := model.Settings.Get("msgType").MustString(defaultDingdingMsgType)
return &DingDingNotifier{
NotifierBase: old_notifiers.NewNotifierBase(model),
MsgType: msgType,
URL: url,
Message: model.Settings.Get("message").MustString(`{{ template "default.message" .}}`),
log: log.New("alerting.notifier.dingding"),
tmpl: t,
NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{
Uid: model.UID,
Name: model.Name,
Type: model.Type,
DisableResolveMessage: model.DisableResolveMessage,
Settings: model.Settings,
}),
MsgType: msgType,
URL: url,
Message: model.Settings.Get("message").MustString(`{{ template "default.message" .}}`),
log: log.New("alerting.notifier.dingding"),
tmpl: t,
}, nil
}
@@ -104,7 +104,7 @@ func TestDingdingNotifier(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
m := &models.AlertNotification{
m := &NotificationChannelConfig{
Name: "dingding_testing",
Type: "dingding",
Settings: settingsJSON,
@@ -32,7 +32,7 @@ type EmailNotifier struct {
// NewEmailNotifier is the constructor function
// for the EmailNotifier.
func NewEmailNotifier(model *models.AlertNotification, t *template.Template) (*EmailNotifier, error) {
func NewEmailNotifier(model *NotificationChannelConfig, t *template.Template) (*EmailNotifier, error) {
if model.Settings == nil {
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
}
@@ -48,12 +48,18 @@ func NewEmailNotifier(model *models.AlertNotification, t *template.Template) (*E
addresses := util.SplitEmails(addressesString)
return &EmailNotifier{
NotifierBase: old_notifiers.NewNotifierBase(model),
Addresses: addresses,
SingleEmail: singleEmail,
Message: model.Settings.Get("message").MustString(),
log: log.New("alerting.notifier.email"),
tmpl: t,
NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{
Uid: model.UID,
Name: model.Name,
Type: model.Type,
DisableResolveMessage: model.DisableResolveMessage,
Settings: model.Settings,
}),
Addresses: addresses,
SingleEmail: singleEmail,
Message: model.Settings.Get("message").MustString(),
log: log.New("alerting.notifier.email"),
tmpl: t,
}, nil
}
@@ -26,7 +26,7 @@ func TestEmailNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &models.AlertNotification{
model := &NotificationChannelConfig{
Name: "ops",
Type: "email",
Settings: settingsJSON,
@@ -44,7 +44,7 @@ func TestEmailNotifier(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(json))
require.NoError(t, err)
emailNotifier, err := NewEmailNotifier(&models.AlertNotification{
emailNotifier, err := NewEmailNotifier(&NotificationChannelConfig{
Name: "ops",
Type: "email",
Settings: settingsJSON,
@@ -45,7 +45,7 @@ type PagerdutyNotifier struct {
}
// NewPagerdutyNotifier is the constructor for the PagerDuty notifier
func NewPagerdutyNotifier(model *models.AlertNotification, t *template.Template) (*PagerdutyNotifier, error) {
func NewPagerdutyNotifier(model *NotificationChannelConfig, t *template.Template) (*PagerdutyNotifier, error) {
if model.Settings == nil {
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
}
@@ -56,8 +56,14 @@ func NewPagerdutyNotifier(model *models.AlertNotification, t *template.Template)
}
return &PagerdutyNotifier{
NotifierBase: old_notifiers.NewNotifierBase(model),
Key: key,
NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{
Uid: model.UID,
Name: model.Name,
Type: model.Type,
DisableResolveMessage: model.DisableResolveMessage,
Settings: model.Settings,
}),
Key: key,
CustomDetails: map[string]string{
"firing": `{{ template "__text_alert_list" .Alerts.Firing }}`,
"resolved": `{{ template "__text_alert_list" .Alerts.Resolved }}`,
@@ -139,7 +139,7 @@ func TestPagerdutyNotifier(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
m := &models.AlertNotification{
m := &NotificationChannelConfig{
Name: "pageduty_testing",
Type: "pagerduty",
Settings: settingsJSON,
@@ -54,7 +54,7 @@ var reRecipient *regexp.Regexp = regexp.MustCompile("^((@[a-z0-9][a-zA-Z0-9._-]*
var SlackAPIEndpoint = "https://slack.com/api/chat.postMessage"
// NewSlackNotifier is the constructor for the Slack notifier
func NewSlackNotifier(model *models.AlertNotification, t *template.Template) (*SlackNotifier, error) {
func NewSlackNotifier(model *NotificationChannelConfig, t *template.Template) (*SlackNotifier, error) {
if model.Settings == nil {
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
}
@@ -112,7 +112,13 @@ func NewSlackNotifier(model *models.AlertNotification, t *template.Template) (*S
}
return &SlackNotifier{
NotifierBase: old_notifiers.NewNotifierBase(model),
NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{
Uid: model.UID,
Name: model.Name,
Type: model.Type,
DisableResolveMessage: model.DisableResolveMessage,
Settings: model.Settings,
}),
URL: apiURL,
Recipient: recipient,
MentionUsers: mentionUsers,
@@ -16,7 +16,6 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -176,7 +175,7 @@ func TestSlackNotifier(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
m := &models.AlertNotification{
m := &NotificationChannelConfig{
Name: "slack_testing",
Type: "slack",
Settings: settingsJSON,
@@ -30,7 +30,7 @@ type TeamsNotifier struct {
}
// NewTeamsNotifier is the constructor for Teams notifier.
func NewTeamsNotifier(model *models.AlertNotification, t *template.Template) (*TeamsNotifier, error) {
func NewTeamsNotifier(model *NotificationChannelConfig, t *template.Template) (*TeamsNotifier, error) {
if model.Settings == nil {
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
}
@@ -41,11 +41,17 @@ func NewTeamsNotifier(model *models.AlertNotification, t *template.Template) (*T
}
return &TeamsNotifier{
NotifierBase: old_notifiers.NewNotifierBase(model),
URL: u,
Message: model.Settings.Get("message").MustString(`{{ template "default.message" .}}`),
log: log.New("alerting.notifier.teams"),
tmpl: t,
NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{
Uid: model.UID,
Name: model.Name,
Type: model.Type,
DisableResolveMessage: model.DisableResolveMessage,
Settings: model.Settings,
}),
URL: u,
Message: model.Settings.Get("message").MustString(`{{ template "default.message" .}}`),
log: log.New("alerting.notifier.teams"),
tmpl: t,
}, nil
}
@@ -128,7 +128,7 @@ func TestTeamsNotifier(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
m := &models.AlertNotification{
m := &NotificationChannelConfig{
Name: "teams_testing",
Type: "teams",
Settings: settingsJSON,
@@ -35,7 +35,7 @@ type TelegramNotifier struct {
}
// NewTelegramNotifier is the constructor for the Telegram notifier
func NewTelegramNotifier(model *models.AlertNotification, t *template.Template) (*TelegramNotifier, error) {
func NewTelegramNotifier(model *NotificationChannelConfig, t *template.Template) (*TelegramNotifier, error) {
if model.Settings == nil {
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
}
@@ -53,12 +53,18 @@ func NewTelegramNotifier(model *models.AlertNotification, t *template.Template)
}
return &TelegramNotifier{
NotifierBase: old_notifiers.NewNotifierBase(model),
BotToken: botToken,
ChatID: chatID,
Message: message,
tmpl: t,
log: log.New("alerting.notifier.telegram"),
NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{
Uid: model.UID,
Name: model.Name,
Type: model.Type,
DisableResolveMessage: model.DisableResolveMessage,
Settings: model.Settings,
}),
BotToken: botToken,
ChatID: chatID,
Message: message,
tmpl: t,
log: log.New("alerting.notifier.telegram"),
}, nil
}
@@ -12,7 +12,6 @@ import (
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -101,7 +100,7 @@ func TestTelegramNotifier(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
m := &models.AlertNotification{
m := &NotificationChannelConfig{
Name: "telegram_testing",
Type: "telegram",
Settings: settingsJSON,
@@ -2,6 +2,9 @@ package channels
import (
"github.com/prometheus/common/model"
"github.com/grafana/grafana/pkg/components/securejsondata"
"github.com/grafana/grafana/pkg/components/simplejson"
)
const (
@@ -16,3 +19,20 @@ func getAlertStatusColor(status model.AlertStatus) string {
}
return ColorAlertResolved
}
type NotificationChannelConfig struct {
UID string `json:"uid"`
Name string `json:"name"`
Type string `json:"type"`
DisableResolveMessage bool `json:"disableResolveMessage"`
Settings *simplejson.Json `json:"settings"`
SecureSettings securejsondata.SecureJsonData `json:"secureSettings"`
}
// DecryptedValue returns decrypted value from secureSettings
func (an *NotificationChannelConfig) DecryptedValue(field string, fallback string) string {
if value, ok := an.SecureSettings.DecryptedValue(field); ok {
return value
}
return fallback
}
@@ -34,20 +34,26 @@ type WebhookNotifier struct {
// NewWebHookNotifier is the constructor for
// the WebHook notifier.
func NewWebHookNotifier(model *models.AlertNotification, t *template.Template) (*WebhookNotifier, error) {
func NewWebHookNotifier(model *NotificationChannelConfig, t *template.Template) (*WebhookNotifier, error) {
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
}
return &WebhookNotifier{
NotifierBase: old_notifiers.NewNotifierBase(model),
URL: url,
User: model.Settings.Get("username").MustString(),
Password: model.DecryptedValue("password", model.Settings.Get("password").MustString()),
HTTPMethod: model.Settings.Get("httpMethod").MustString("POST"),
MaxAlerts: model.Settings.Get("maxAlerts").MustInt(0),
log: log.New("alerting.notifier.webhook"),
tmpl: t,
NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{
Uid: model.UID,
Name: model.Name,
Type: model.Type,
DisableResolveMessage: model.DisableResolveMessage,
Settings: model.Settings,
}),
URL: url,
User: model.Settings.Get("username").MustString(),
Password: model.DecryptedValue("password", model.Settings.Get("password").MustString()),
HTTPMethod: model.Settings.Get("httpMethod").MustString("POST"),
MaxAlerts: model.Settings.Get("maxAlerts").MustInt(0),
log: log.New("alerting.notifier.webhook"),
tmpl: t,
}, nil
}
@@ -175,7 +175,7 @@ func TestWebhookNotifier(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
m := &models.AlertNotification{
m := &NotificationChannelConfig{
Name: "webhook_testing",
Type: "webhook",
Settings: settingsJSON,