Skip to content

Commit

Permalink
Added Alertmanager Telegram support and blocked new OpsGenie api_key_…
Browse files Browse the repository at this point in the history
…file config option

Signed-off-by: Marco Pracucci <marco@pracucci.com>
  • Loading branch information
pracucci committed Apr 22, 2022
1 parent ba05b85 commit cf49e37
Show file tree
Hide file tree
Showing 38 changed files with 7,509 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -217,6 +217,7 @@ require (
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.57.0 // indirect
gopkg.in/telebot.v3 v3.0.0 // indirect
)

// Override since git.apache.org is down. The docs say to fetch from github.
Expand Down
1 change: 1 addition & 0 deletions go.sum
Expand Up @@ -2757,6 +2757,7 @@ gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/square/go-jose.v2 v2.2.2/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/square/go-jose.v2 v2.3.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/square/go-jose.v2 v2.5.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/telebot.v3 v3.0.0 h1:UgHIiE/RdjoDi6nf4xACM7PU3TqiPVV9vvTydCEnrTo=
gopkg.in/telebot.v3 v3.0.0/go.mod h1:7rExV8/0mDDNu9epSrDm/8j22KLaActH1Tbee6YjzWg=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
Expand Down
4 changes: 4 additions & 0 deletions pkg/alertmanager/alertmanager.go
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/prometheus/alertmanager/notify/pushover"
"github.com/prometheus/alertmanager/notify/slack"
"github.com/prometheus/alertmanager/notify/sns"
"github.com/prometheus/alertmanager/notify/telegram"
"github.com/prometheus/alertmanager/notify/victorops"
"github.com/prometheus/alertmanager/notify/webhook"
"github.com/prometheus/alertmanager/notify/wechat"
Expand Down Expand Up @@ -494,6 +495,9 @@ func buildReceiverIntegrations(nc *config.Receiver, tmpl *template.Template, fir
for i, c := range nc.SNSConfigs {
add("sns", i, c, func(l log.Logger) (notify.Notifier, error) { return sns.New(c, tmpl, l, httpOps...) })
}
for i, c := range nc.TelegramConfigs {
add("telegram", i, c, func(l log.Logger) (notify.Notifier, error) { return telegram.New(c, tmpl, l, httpOps...) })
}
// If we add support for more integrations, we need to add them to validation as well. See validation.allowedIntegrationNames field.
if errs.Len() > 0 {
return nil, &errs
Expand Down
30 changes: 24 additions & 6 deletions pkg/alertmanager/api.go
Expand Up @@ -47,12 +47,13 @@ const (
)

var (
errPasswordFileNotAllowed = errors.New("setting password_file, bearer_token_file and credentials_file is not allowed")
errOAuth2SecretFileNotAllowed = errors.New("setting OAuth2 client_secret_file is not allowed")
errProxyURLNotAllowed = errors.New("setting proxy_url is not allowed")
errTLSFileNotAllowed = errors.New("setting TLS ca_file, cert_file and key_file is not allowed")
errSlackAPIURLFileNotAllowed = errors.New("setting Slack api_url_file and global slack_api_url_file is not allowed")
errVictorOpsAPIKeyFileNotAllowed = errors.New("setting VictorOps api_key_file is not allowed")
errPasswordFileNotAllowed = errors.New("setting password_file, bearer_token_file and credentials_file is not allowed")
errOAuth2SecretFileNotAllowed = errors.New("setting OAuth2 client_secret_file is not allowed")
errProxyURLNotAllowed = errors.New("setting proxy_url is not allowed")
errTLSFileNotAllowed = errors.New("setting TLS ca_file, cert_file and key_file is not allowed")
errSlackAPIURLFileNotAllowed = errors.New("setting Slack api_url_file and global slack_api_url_file is not allowed")
errVictorOpsAPIKeyFileNotAllowed = errors.New("setting VictorOps api_key_file is not allowed")
errOpsGenieAPIKeyFileFileNotAllowed = errors.New("setting OpsGenie api_key_file and global opsgenie_api_key_file is not allowed")
)

// UserConfig is used to communicate a users alertmanager configs
Expand Down Expand Up @@ -354,6 +355,11 @@ func validateAlertmanagerConfig(cfg interface{}) error {
return err
}

case reflect.TypeOf(config.OpsGenieConfig{}):
if err := validateOpsGenieConfig(v.Interface().(config.OpsGenieConfig)); err != nil {
return err
}

case reflect.TypeOf(config.VictorOpsConfig{}):
if err := validateVictorOpsConfig(v.Interface().(config.VictorOpsConfig)); err != nil {
return err
Expand Down Expand Up @@ -440,6 +446,9 @@ func validateGlobalConfig(cfg config.GlobalConfig) error {
if cfg.SlackAPIURLFile != "" {
return errSlackAPIURLFileNotAllowed
}
if cfg.OpsGenieAPIKeyFile != "" {
return errOpsGenieAPIKeyFileFileNotAllowed
}
return nil
}

Expand All @@ -460,3 +469,12 @@ func validateVictorOpsConfig(cfg config.VictorOpsConfig) error {
}
return nil
}

// validateOpsGenieConfig validates the OpsGenie config and returns an error if it contains
// settings now allowed by Mimir.
func validateOpsGenieConfig(cfg config.OpsGenieConfig) error {
if cfg.APIKeyFile != "" {
return errOpsGenieAPIKeyFileFileNotAllowed
}
return nil
}
31 changes: 31 additions & 0 deletions pkg/alertmanager/api_test.go
Expand Up @@ -442,6 +442,37 @@ alertmanager_config: |
`,
err: errors.Wrap(errSlackAPIURLFileNotAllowed, "error validating Alertmanager config"),
},
{
name: "Should return error if global opsgenie_api_key_file is set",
cfg: `
alertmanager_config: |
global:
opsgenie_api_key_file: /secrets
receivers:
- name: default-receiver
webhook_configs:
- url: http://localhost
route:
receiver: 'default-receiver'
`,
err: errors.Wrap(errOpsGenieAPIKeyFileFileNotAllowed, "error validating Alertmanager config"),
},
{
name: "Should return error if OpsGenie api_key_file is set",
cfg: `
alertmanager_config: |
receivers:
- name: default-receiver
opsgenie_configs:
- api_key_file: /secrets
route:
receiver: 'default-receiver'
`,
err: errors.Wrap(errOpsGenieAPIKeyFileFileNotAllowed, "error validating Alertmanager config"),
},
{
name: "Should return error if VictorOps api_key_file is set",
cfg: `
Expand Down
17 changes: 17 additions & 0 deletions pkg/alertmanager/multitenant_test.go
Expand Up @@ -455,6 +455,23 @@ receivers:
region: us-east-1
access_key: xxx
secret_key: xxx
`, backendURL)
},
},
"telegram": {
getAlertmanagerConfig: func(backendURL string) string {
return fmt.Sprintf(`
route:
receiver: telegram
group_wait: 0s
group_interval: 1s
receivers:
- name: telegram
telegram_configs:
- api_url: %s
bot_token: xxx
chat_id: 111
`, backendURL)
},
},
Expand Down
102 changes: 102 additions & 0 deletions vendor/github.com/prometheus/alertmanager/notify/telegram/telegram.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions vendor/gopkg.in/telebot.v3/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/gopkg.in/telebot.v3/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cf49e37

Please sign in to comment.