Skip to content

Commit

Permalink
Add receiver validation for msteamsv2 and rocketchat (#6606)
Browse files Browse the repository at this point in the history
Signed-off-by: SungJin1212 <[email protected]>
  • Loading branch information
SungJin1212 authored Feb 26, 2025
1 parent 72f03ab commit 89f36f6
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [ENHANCEMENT] StoreGateway: Emit more histogram buckets on the `cortex_querier_storegateway_refetches_per_query` metric. #6570
* [ENHANCEMENT] Querier: Apply bytes limiter to LabelNames and LabelValuesForLabelNames. #6568
* [ENHANCEMENT] Query Frontend: Add a `too_many_tenants` reason label value to `cortex_rejected_queries_total` metric to track the rejected query count due to the # of tenant limits. #6569
* [ENHANCEMENT] Alertmanager: Add receiver validations for msteamsv2 and rocketchat. #6606
* [BUGFIX] Ingester: Avoid error or early throttling when READONLY ingesters are present in the ring #6517
* [BUGFIX] Ingester: Fix labelset data race condition. #6573
* [BUGFIX] Compactor: Cleaner should not put deletion marker for blocks with no-compact marker. #6576
Expand Down
48 changes: 48 additions & 0 deletions pkg/alertmanager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ var (
errPushOverUserKeyFileNotAllowed = errors.New("setting PushOver user_key_file is not allowed")
errPushOverTokenFileNotAllowed = errors.New("setting PushOver token_file is not allowed")
errTelegramBotTokenFileNotAllowed = errors.New("setting Telegram bot_token_file is not allowed")
errMSTeamsWebhookUrlFileNotAllowed = errors.New("setting MSTeams webhook_url_file is not allowed")
errMSTeamsV2WebhookUrlFileNotAllowed = errors.New("setting MSTeamsV2 webhook_url_file is not allowed")
errRocketChatTokenIdFileNotAllowed = errors.New("setting RocketChat token_id_file is not allowed")
errRocketChatTokenFileNotAllowed = errors.New("setting RocketChat token_file is not allowed")
)

// UserConfig is used to communicate a users alertmanager configs
Expand Down Expand Up @@ -383,6 +387,18 @@ func validateAlertmanagerConfig(cfg interface{}) error {
if err := validateTelegramConfig(v.Interface().(config.TelegramConfig)); err != nil {
return err
}
case reflect.TypeOf(config.MSTeamsConfig{}):
if err := validateMSTeamsConfig(v.Interface().(config.MSTeamsConfig)); err != nil {
return err
}
case reflect.TypeOf(config.MSTeamsV2Config{}):
if err := validateMSTeamsV2Config(v.Interface().(config.MSTeamsV2Config)); err != nil {
return err
}
case reflect.TypeOf(config.RocketchatConfig{}):
if err := validateRocketChatConfig(v.Interface().(config.RocketchatConfig)); err != nil {
return err
}
}

// If the input config is a struct, recursively iterate on all fields.
Expand Down Expand Up @@ -540,3 +556,35 @@ func validateTelegramConfig(cfg config.TelegramConfig) error {
}
return nil
}

// validateMSTeamsConfig validates the MSTeams Config and returns an error if it contains
// settings not allowed by Cortex.
func validateMSTeamsConfig(cfg config.MSTeamsConfig) error {
if cfg.WebhookURLFile != "" {
return errMSTeamsWebhookUrlFileNotAllowed
}
return nil
}

// validateMSTeamsV2Config validates the MSTeamsV2 Config and returns an error if it contains
// settings not allowed by Cortex.
func validateMSTeamsV2Config(cfg config.MSTeamsV2Config) error {
if cfg.WebhookURLFile != "" {
return errMSTeamsV2WebhookUrlFileNotAllowed
}
return nil
}

// validateRocketChatConfig validates the RocketChat Config and returns an error if it contains
// settings not allowed by Cortex.
func validateRocketChatConfig(cfg config.RocketchatConfig) error {
if cfg.TokenIDFile != "" {
return errRocketChatTokenIdFileNotAllowed
}

if cfg.TokenFile != "" {
return errRocketChatTokenFileNotAllowed
}

return nil
}
54 changes: 54 additions & 0 deletions pkg/alertmanager/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,60 @@ alertmanager_config: |
`,
err: errors.Wrap(errTelegramBotTokenFileNotAllowed, "error validating Alertmanager config"),
},
{
name: "Should return error if MSTeams webhook_url_file is set",
cfg: `
alertmanager_config: |
receivers:
- name: default-receiver
msteams_configs:
- webhook_url_file: /urlFile
route:
receiver: 'default-receiver'
`,
err: errors.Wrap(errMSTeamsWebhookUrlFileNotAllowed, "error validating Alertmanager config"),
},
{
name: "Should return error if MSTeamsV2 webhook_url_file is set",
cfg: `
alertmanager_config: |
receivers:
- name: default-receiver
msteamsv2_configs:
- webhook_url_file: /urlFile
route:
receiver: 'default-receiver'
`,
err: errors.Wrap(errMSTeamsV2WebhookUrlFileNotAllowed, "error validating Alertmanager config"),
},
{
name: "Should return error if RocketChat token_id_file is set",
cfg: `
alertmanager_config: |
receivers:
- name: default-receiver
rocketchat_configs:
- token_id_file: /tokenIdFile
token: 'token'
route:
receiver: 'default-receiver'
`,
err: errors.Wrap(errRocketChatTokenIdFileNotAllowed, "error validating Alertmanager config"),
},
{
name: "Should return error if RocketChat token_file is set",
cfg: `
alertmanager_config: |
receivers:
- name: default-receiver
rocketchat_configs:
- token_file: /tokenFile
token_id: 'tokenId'
route:
receiver: 'default-receiver'
`,
err: errors.Wrap(errRocketChatTokenFileNotAllowed, "error validating Alertmanager config"),
},
}

limits := &mockAlertManagerLimits{}
Expand Down

0 comments on commit 89f36f6

Please sign in to comment.