diff --git a/config/config.go b/config/config.go index 3f14c4450f..dd0cbd2d2a 100644 --- a/config/config.go +++ b/config/config.go @@ -341,10 +341,19 @@ func (c *Config) UnmarshalYAML(unmarshal func(any) error) error { return errors.New("at most one of slack_api_url & slack_api_url_file must be configured") } - if (c.Global.SlackAppToken != "" || len(c.Global.SlackAppTokenFile) > 0) && (c.Global.SlackAPIURL != nil || len(c.Global.SlackAPIURLFile) > 0) { - // Support transition from workaround suggested in https://github.com/prometheus/alertmanager/issues/2513, - // where users might set `slack_api_url` at the top level and then have `http_config` with individual - // bearer tokens in the receivers. + // slack_api_url_file can't be compared with slack_app_url at load time, + // so the transition check below can't apply. Reject the combination. + if len(c.Global.SlackAPIURLFile) > 0 && (c.Global.SlackAppToken != "" || len(c.Global.SlackAppTokenFile) > 0) { + return errors.New("at most one of slack_app_token/slack_app_token_file & slack_api_url/slack_api_url_file must be configured") + } + + // Support transition from workaround suggested in https://github.com/prometheus/alertmanager/issues/2513, + // where users might set `slack_api_url` at the top level and then have `http_config` with individual + // bearer tokens in the receivers. + // + // We test SlackAPIURL.String() value because a redacted URL (e.g. from /api/v2/status) + // deserialises as &url.URL{}, whose String() is "". The real value is unknown, so we can't test its contents. + if (c.Global.SlackAppToken != "" || len(c.Global.SlackAppTokenFile) > 0) && c.Global.SlackAPIURL != nil && c.Global.SlackAPIURL.String() != "" { if c.Global.SlackAPIURL.String() != c.Global.SlackAppURL.String() { return errors.New("at most one of slack_app_token/slack_app_token_file & slack_api_url/slack_api_url_file must be configured") } diff --git a/config/config_test.go b/config/config_test.go index 28f08982a5..32ba8a69a9 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1398,6 +1398,99 @@ func TestSlackPostUpdatesToThreadWebhookURL(t *testing.T) { require.EqualError(t, err, "post_updates_to_thread can only be used with bot tokens. api_url must be set to https://slack.com/api/chat.postMessage") } +func TestSlackRedactedConfigUpdateMessage(t *testing.T) { + // A redacted config (e.g. from /api/v2/status) has api_url: . + // SecretURL.UnmarshalYAML deserialises this into an empty *url.URL. + // The update_message validation must not reject it. + in := ` +route: + receiver: foo +receivers: + - name: foo + slack_configs: + - channel: alerts + api_url: + update_message: true +` + conf, err := Load(in) + require.NoError(t, err) + require.True(t, conf.Receivers[0].SlackConfigs[0].UpdateMessage) +} + +func TestSlackRedactedConfigPostUpdatesToThread(t *testing.T) { + in := ` +route: + receiver: foo +receivers: + - name: foo + slack_configs: + - channel: alerts + api_url: + post_updates_to_thread: true +` + conf, err := Load(in) + require.NoError(t, err) + require.True(t, conf.Receivers[0].SlackConfigs[0].PostUpdatesToThread) +} + +func TestSlackRedactedGlobalAPIURL(t *testing.T) { + // A redacted global slack_api_url (e.g. from /api/v2/status) + // must not cause a validation error. + in := ` +global: + slack_api_url: + slack_app_token: +route: + receiver: foo +receivers: + - name: foo + slack_configs: + - channel: alerts +` + _, err := Load(in) + require.NoError(t, err) +} + +func TestSlackRedactedConfigReload(t *testing.T) { + // Config.String() redacts api_url, as served by /api/v2/status. + // Loading that output must not fail the bot-token validation. + in := ` +global: + slack_api_url: https://slack.com/api/chat.postMessage +route: + receiver: foo +receivers: + - name: foo + slack_configs: + - channel: alerts + update_message: true + post_updates_to_thread: true +` + conf, err := Load(in) + require.NoError(t, err) + + _, err = Load(conf.String()) + require.NoError(t, err) +} + +func TestSlackGlobalAPIURLFileWithAppToken(t *testing.T) { + // slack_api_url_file can't be compared with slack_app_url at load time, + // so it can't be combined with slack_app_token. + in := ` +global: + slack_app_token: xoxb-token + slack_api_url_file: /etc/alertmanager/slack-url +route: + receiver: foo +receivers: + - name: foo + slack_configs: + - channel: alerts +` + _, err := Load(in) + require.EqualError(t, err, "at most one of slack_app_token/slack_app_token_file & slack_api_url/slack_api_url_file must be configured") +} + func TestSlackGlobalAppToken(t *testing.T) { conf, err := LoadFile("testdata/conf.slack-default-app-token.yml") if err != nil { diff --git a/notify/slack/config.go b/notify/slack/config.go index 000911efc8..cdbadd4275 100644 --- a/notify/slack/config.go +++ b/notify/slack/config.go @@ -230,6 +230,11 @@ func validateBotTokenAPIURL(option string, enabled bool, apiURL *amcommoncfg.Sec if !enabled { return nil } + // A redacted api_url (e.g. from /api/v2/status) deserialises as + // &url.URL{}, whose String() is "". The real value is unknown. + if apiURL != nil && apiURL.String() == "" { + return nil + } if apiURL == nil && apiURLFile != "" { return errors.New(option + " can't be used with api_url_file") }