Alerting: Check for nil model.Settings and models.SecureSettings (#37738)

visualization-suggestion-cards^2
George Robinson 4 years ago committed by GitHub
parent 4b2c90b834
commit 9122e7f647
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      pkg/services/ngalert/notifier/channels/alertmanager.go
  2. 16
      pkg/services/ngalert/notifier/channels/alertmanager_test.go
  3. 2
      pkg/services/ngalert/notifier/channels/dingding.go
  4. 2
      pkg/services/ngalert/notifier/channels/discord.go
  5. 2
      pkg/services/ngalert/notifier/channels/email.go
  6. 4
      pkg/services/ngalert/notifier/channels/googlechat.go
  7. 4
      pkg/services/ngalert/notifier/channels/kafka.go
  8. 7
      pkg/services/ngalert/notifier/channels/line.go
  9. 8
      pkg/services/ngalert/notifier/channels/line_test.go
  10. 6
      pkg/services/ngalert/notifier/channels/opsgenie.go
  11. 8
      pkg/services/ngalert/notifier/channels/opsgenie_test.go
  12. 3
      pkg/services/ngalert/notifier/channels/pagerduty.go
  13. 8
      pkg/services/ngalert/notifier/channels/pagerduty_test.go
  14. 3
      pkg/services/ngalert/notifier/channels/pushover.go
  15. 8
      pkg/services/ngalert/notifier/channels/pushover_test.go
  16. 4
      pkg/services/ngalert/notifier/channels/sensugo.go
  17. 8
      pkg/services/ngalert/notifier/channels/sensugo_test.go
  18. 3
      pkg/services/ngalert/notifier/channels/slack.go
  19. 8
      pkg/services/ngalert/notifier/channels/slack_test.go
  20. 3
      pkg/services/ngalert/notifier/channels/telegram.go
  21. 8
      pkg/services/ngalert/notifier/channels/telegram_test.go
  22. 4
      pkg/services/ngalert/notifier/channels/threema.go
  23. 8
      pkg/services/ngalert/notifier/channels/threema_test.go
  24. 4
      pkg/services/ngalert/notifier/channels/victorops.go
  25. 5
      pkg/services/ngalert/notifier/channels/webhook.go
  26. 10
      pkg/services/ngalert/notifier/channels/webhook_test.go

@ -22,7 +22,9 @@ func NewAlertmanagerNotifier(model *NotificationChannelConfig, _ *template.Templ
if model.Settings == nil {
return nil, receiverInitError{Reason: "no settings supplied"}
}
if model.SecureSettings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no secure settings supplied"}
}
urlStr := model.Settings.Get("url").MustString()
if urlStr == "" {
return nil, receiverInitError{Reason: "could not find url property in settings", Cfg: *model}

@ -49,11 +49,13 @@ func TestNewAlertmanagerNotifier(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
secureSettings := make(map[string][]byte)
m := &NotificationChannelConfig{
Name: c.receiverName,
Type: "alertmanager",
Settings: settingsJSON,
Name: c.receiverName,
Type: "alertmanager",
Settings: settingsJSON,
SecureSettings: secureSettings,
}
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())
@ -130,11 +132,13 @@ func TestAlertmanagerNotifier_Notify(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
secureSettings := make(map[string][]byte)
m := &NotificationChannelConfig{
Name: c.receiverName,
Type: "alertmanager",
Settings: settingsJSON,
Name: c.receiverName,
Type: "alertmanager",
Settings: settingsJSON,
SecureSettings: secureSettings,
}
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())

@ -19,7 +19,7 @@ const defaultDingdingMsgType = "link"
// NewDingDingNotifier is the constructor for the Dingding notifier
func NewDingDingNotifier(model *NotificationChannelConfig, t *template.Template) (*DingDingNotifier, error) {
if model.Settings == nil {
return nil, receiverInitError{Reason: "no settings supplied", Cfg: *model}
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
url := model.Settings.Get("url").MustString()

@ -28,7 +28,7 @@ type DiscordNotifier struct {
func NewDiscordNotifier(model *NotificationChannelConfig, t *template.Template) (*DiscordNotifier, error) {
if model.Settings == nil {
return nil, receiverInitError{Reason: "no settings supplied", Cfg: *model}
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
avatarURL := model.Settings.Get("avatar_url").MustString()

@ -29,7 +29,7 @@ type EmailNotifier struct {
// for the EmailNotifier.
func NewEmailNotifier(model *NotificationChannelConfig, t *template.Template) (*EmailNotifier, error) {
if model.Settings == nil {
return nil, receiverInitError{Reason: "no settings supplied", Cfg: *model}
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
addressesString := model.Settings.Get("addresses").MustString()

@ -25,6 +25,10 @@ type GoogleChatNotifier struct {
}
func NewGoogleChatNotifier(model *NotificationChannelConfig, t *template.Template) (*GoogleChatNotifier, error) {
if model.Settings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, receiverInitError{Cfg: *model, Reason: "could not find url property in settings"}

@ -27,6 +27,10 @@ type KafkaNotifier struct {
// NewKafkaNotifier is the constructor function for the Kafka notifier.
func NewKafkaNotifier(model *NotificationChannelConfig, t *template.Template) (*KafkaNotifier, error) {
if model.Settings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
endpoint := model.Settings.Get("kafkaRestProxy").MustString()
if endpoint == "" {
return nil, receiverInitError{Cfg: *model, Reason: "could not find kafka rest proxy endpoint property in settings"}

@ -19,6 +19,13 @@ var (
// NewLineNotifier is the constructor for the LINE notifier
func NewLineNotifier(model *NotificationChannelConfig, t *template.Template, fn GetDecryptedValueFn) (*LineNotifier, error) {
if model.Settings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
if model.SecureSettings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no secure settings supplied"}
}
token := fn(context.Background(), model.SecureSettings, "token", model.Settings.Get("token").MustString())
if token == "" {
return nil, receiverInitError{Cfg: *model, Reason: "could not find token in settings"}

@ -83,11 +83,13 @@ func TestLineNotifier(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
secureSettings := make(map[string][]byte)
m := &NotificationChannelConfig{
Name: "line_testing",
Type: "line",
Settings: settingsJSON,
Name: "line_testing",
Type: "line",
Settings: settingsJSON,
SecureSettings: secureSettings,
}
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())

@ -42,6 +42,12 @@ type OpsgenieNotifier struct {
// NewOpsgenieNotifier is the constructor for the Opsgenie notifier
func NewOpsgenieNotifier(model *NotificationChannelConfig, t *template.Template, fn GetDecryptedValueFn) (*OpsgenieNotifier, error) {
if model.Settings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
if model.SecureSettings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no secure settings supplied"}
}
autoClose := model.Settings.Get("autoClose").MustBool(true)
overridePriority := model.Settings.Get("overridePriority").MustBool(true)
apiKey := fn(context.Background(), model.SecureSettings, "apiKey", model.Settings.Get("apiKey").MustString())

@ -163,11 +163,13 @@ func TestOpsgenieNotifier(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
secureSettings := make(map[string][]byte)
m := &NotificationChannelConfig{
Name: "opsgenie_testing",
Type: "opsgenie",
Settings: settingsJSON,
Name: "opsgenie_testing",
Type: "opsgenie",
Settings: settingsJSON,
SecureSettings: secureSettings,
}
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())

@ -44,6 +44,9 @@ func NewPagerdutyNotifier(model *NotificationChannelConfig, t *template.Template
if model.Settings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
if model.SecureSettings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no secure settings supplied"}
}
key := fn(context.Background(), model.SecureSettings, "integrationKey", model.Settings.Get("integrationKey").MustString())
if key == "" {

@ -129,11 +129,13 @@ func TestPagerdutyNotifier(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
secureSettings := make(map[string][]byte)
m := &NotificationChannelConfig{
Name: "pageduty_testing",
Type: "pagerduty",
Settings: settingsJSON,
Name: "pageduty_testing",
Type: "pagerduty",
Settings: settingsJSON,
SecureSettings: secureSettings,
}
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())

@ -43,6 +43,9 @@ func NewPushoverNotifier(model *NotificationChannelConfig, t *template.Template,
if model.Settings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
if model.SecureSettings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no secure settings supplied"}
}
userKey := fn(context.Background(), model.SecureSettings, "userKey", model.Settings.Get("userKey").MustString())
APIToken := fn(context.Background(), model.SecureSettings, "apiToken", model.Settings.Get("apiToken").MustString())

@ -137,11 +137,13 @@ func TestPushoverNotifier(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
secureSettings := make(map[string][]byte)
m := &NotificationChannelConfig{
Name: "pushover_testing",
Type: "pushover",
Settings: settingsJSON,
Name: "pushover_testing",
Type: "pushover",
Settings: settingsJSON,
SecureSettings: secureSettings,
}
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())

@ -34,7 +34,9 @@ func NewSensuGoNotifier(model *NotificationChannelConfig, t *template.Template,
if model.Settings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
if model.SecureSettings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no secure settings supplied"}
}
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, receiverInitError{Cfg: *model, Reason: "could not find URL property in settings"}

@ -134,11 +134,13 @@ func TestSensuGoNotifier(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
secureSettings := make(map[string][]byte)
m := &NotificationChannelConfig{
Name: "Sensu Go",
Type: "sensugo",
Settings: settingsJSON,
Name: "Sensu Go",
Type: "sensugo",
Settings: settingsJSON,
SecureSettings: secureSettings,
}
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())

@ -48,6 +48,9 @@ func NewSlackNotifier(model *NotificationChannelConfig, t *template.Template, fn
if model.Settings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
if model.SecureSettings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no secure settings supplied"}
}
slackURL := fn(context.Background(), model.SecureSettings, "url", model.Settings.Get("url").MustString())
if slackURL == "" {

@ -165,11 +165,13 @@ func TestSlackNotifier(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
secureSettings := make(map[string][]byte)
m := &NotificationChannelConfig{
Name: "slack_testing",
Type: "slack",
Settings: settingsJSON,
Name: "slack_testing",
Type: "slack",
Settings: settingsJSON,
SecureSettings: secureSettings,
}
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())

@ -33,6 +33,9 @@ func NewTelegramNotifier(model *NotificationChannelConfig, t *template.Template,
if model.Settings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
if model.SecureSettings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no secure settings supplied"}
}
botToken := fn(context.Background(), model.SecureSettings, "bottoken", model.Settings.Get("bottoken").MustString())
chatID := model.Settings.Get("chatid").MustString()

@ -89,11 +89,13 @@ func TestTelegramNotifier(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
secureSettings := make(map[string][]byte)
m := &NotificationChannelConfig{
Name: "telegram_testing",
Type: "telegram",
Settings: settingsJSON,
Name: "telegram_testing",
Type: "telegram",
Settings: settingsJSON,
SecureSettings: secureSettings,
}
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())

@ -35,7 +35,9 @@ func NewThreemaNotifier(model *NotificationChannelConfig, t *template.Template,
if model.Settings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
if model.SecureSettings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no secure settings supplied"}
}
gatewayID := model.Settings.Get("gateway_id").MustString()
recipientID := model.Settings.Get("recipient_id").MustString()
apiSecret := fn(context.Background(), model.SecureSettings, "api_secret", model.Settings.Get("api_secret").MustString())

@ -101,11 +101,13 @@ func TestThreemaNotifier(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
secureSettings := make(map[string][]byte)
m := &NotificationChannelConfig{
Name: "threema_testing",
Type: "threema",
Settings: settingsJSON,
Name: "threema_testing",
Type: "threema",
Settings: settingsJSON,
SecureSettings: secureSettings,
}
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())

@ -28,6 +28,10 @@ const (
// NewVictoropsNotifier creates an instance of VictoropsNotifier that
// handles posting notifications to Victorops REST API
func NewVictoropsNotifier(model *NotificationChannelConfig, t *template.Template) (*VictoropsNotifier, error) {
if model.Settings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, receiverInitError{Cfg: *model, Reason: "could not find victorops url property in settings"}

@ -31,7 +31,10 @@ type WebhookNotifier struct {
// the WebHook notifier.
func NewWebHookNotifier(model *NotificationChannelConfig, t *template.Template, fn GetDecryptedValueFn) (*WebhookNotifier, error) {
if model.Settings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "could not find settings property"}
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
}
if model.SecureSettings == nil {
return nil, receiverInitError{Cfg: *model, Reason: "no secure settings supplied"}
}
url := model.Settings.Get("url").MustString()
if url == "" {

@ -182,12 +182,14 @@ func TestWebhookNotifier(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
require.NoError(t, err)
secureSettings := make(map[string][]byte)
m := &NotificationChannelConfig{
Name: "webhook_testing",
Type: "webhook",
Settings: settingsJSON,
OrgID: orgID,
OrgID: orgID,
Name: "webhook_testing",
Type: "webhook",
Settings: settingsJSON,
SecureSettings: secureSettings,
}
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())

Loading…
Cancel
Save