Skip to content

Validating new fields on the PagerDuty AM config #5290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## master / unreleased
* [CHANGE] Alertmanager: Validating new fields on the PagerDuty AM config. #5290
* [BUGFIX] Ruler: Validate if rule group can be safely converted back to rule group yaml from protobuf message #5265
* [BUGFIX] Querier: Convert gRPC `ResourceExhausted` status code from store gateway to 422 limit error. #5286

Expand Down
41 changes: 31 additions & 10 deletions pkg/alertmanager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ 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")
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")
errOpsGenieAPIKeyFileNotAllowed = errors.New("setting OpsGenie 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")
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")
errOpsGenieAPIKeyFileNotAllowed = errors.New("setting OpsGenie api_key_file is not allowed")
errPagerDutyRoutingKeyFileNotAllowed = errors.New("setting PagerDuty routing_key_file is not allowed")
errPagerDutyServiceKeyFileNotAllowed = errors.New("setting PagerDuty service_key_file is not allowed")
)

// UserConfig is used to communicate a users alertmanager configs
Expand Down Expand Up @@ -356,6 +358,11 @@ func validateAlertmanagerConfig(cfg interface{}) error {
if err := validateVictorOpsConfig(v.Interface().(config.VictorOpsConfig)); err != nil {
return err
}

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

// If the input config is a struct, recursively iterate on all fields.
Expand Down Expand Up @@ -430,7 +437,7 @@ func validateReceiverTLSConfig(cfg commoncfg.TLSConfig) error {
}

// validateGlobalConfig validates the Global config and returns an error if it contains
// settings now allowed by Cortex.
// settings not allowed by Cortex.
func validateGlobalConfig(cfg config.GlobalConfig) error {
if cfg.OpsGenieAPIKeyFile != "" {
return errOpsGenieAPIKeyFileNotAllowed
Expand All @@ -442,7 +449,7 @@ func validateGlobalConfig(cfg config.GlobalConfig) error {
}

// validateOpsGenieConfig validates the OpsGenie config and returns an error if it contains
// settings now allowed by Cortex.
// settings not allowed by Cortex.
func validateOpsGenieConfig(cfg config.OpsGenieConfig) error {
if cfg.APIKeyFile != "" {
return errOpsGenieAPIKeyFileNotAllowed
Expand All @@ -451,7 +458,7 @@ func validateOpsGenieConfig(cfg config.OpsGenieConfig) error {
}

// validateSlackConfig validates the Slack config and returns an error if it contains
// settings now allowed by Cortex.
// settings not allowed by Cortex.
func validateSlackConfig(cfg config.SlackConfig) error {
if cfg.APIURLFile != "" {
return errSlackAPIURLFileNotAllowed
Expand All @@ -460,10 +467,24 @@ func validateSlackConfig(cfg config.SlackConfig) error {
}

// validateVictorOpsConfig validates the VictorOps config and returns an error if it contains
// settings now allowed by Cortex.
// settings not allowed by Cortex.
func validateVictorOpsConfig(cfg config.VictorOpsConfig) error {
if cfg.APIKeyFile != "" {
return errVictorOpsAPIKeyFileNotAllowed
}
return nil
}

// validatePagerdutyConfig validates the pager duty config and returns an error if it contains
// settings not allowed by Cortex.
func validatePagerdutyConfig(cfg config.PagerdutyConfig) error {
if cfg.RoutingKeyFile != "" {
return errPagerDutyRoutingKeyFileNotAllowed
}

if cfg.ServiceKeyFile != "" {
return errPagerDutyServiceKeyFileNotAllowed
}

return nil
}
28 changes: 28 additions & 0 deletions pkg/alertmanager/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,34 @@ template_files:
maxTemplateSize: 20,
err: nil,
},
{
name: "Should return error if PagerDuty routing_key_file is set",
cfg: `
alertmanager_config: |
receivers:
- name: default-receiver
pagerduty_configs:
- routing_key_file: /secrets

route:
receiver: 'default-receiver'
`,
err: errors.Wrap(errPagerDutyRoutingKeyFileNotAllowed, "error validating Alertmanager config"),
},
{
name: "Should return error if PagerDuty service_key_file is set",
cfg: `
alertmanager_config: |
receivers:
- name: default-receiver
pagerduty_configs:
- service_key_file: /secrets

route:
receiver: 'default-receiver'
`,
err: errors.Wrap(errPagerDutyServiceKeyFileNotAllowed, "error validating Alertmanager config"),
},
}

limits := &mockAlertManagerLimits{}
Expand Down