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

This commit is contained in:
George Robinson 2021-11-22 11:56:18 +00:00 committed by GitHub
parent 4b2c90b834
commit 9122e7f647
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 109 additions and 41 deletions

View File

@ -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}

View File

@ -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())

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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"}

View File

@ -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"}

View File

@ -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"}

View File

@ -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())

View File

@ -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())

View File

@ -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())

View File

@ -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 == "" {

View File

@ -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())

View File

@ -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())

View File

@ -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())

View File

@ -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"}

View File

@ -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())

View File

@ -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 == "" {

View File

@ -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())

View File

@ -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()

View File

@ -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())

View File

@ -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())

View File

@ -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())

View File

@ -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"}

View File

@ -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 == "" {

View File

@ -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())