Skip to content

Commit 8e36c84

Browse files
authored
Validating new fields on the PagerDuty AM config (#5290)
* Not Allowing to set the new service_key_file and routing_key_file on AM config Signed-off-by: Alan Protasio <alanprot@gmail.com> * changelog Signed-off-by: Alan Protasio <alanprot@gmail.com> * fix multiples types Signed-off-by: Alan Protasio <alanprot@gmail.com> --------- Signed-off-by: Alan Protasio <alanprot@gmail.com>
1 parent 7e37e92 commit 8e36c84

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

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

pkg/alertmanager/api.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ const (
4040
)
4141

4242
var (
43-
errPasswordFileNotAllowed = errors.New("setting password_file, bearer_token_file and credentials_file is not allowed")
44-
errOAuth2SecretFileNotAllowed = errors.New("setting OAuth2 client_secret_file is not allowed")
45-
errTLSFileNotAllowed = errors.New("setting TLS ca_file, cert_file and key_file is not allowed")
46-
errSlackAPIURLFileNotAllowed = errors.New("setting Slack api_url_file and global slack_api_url_file is not allowed")
47-
errVictorOpsAPIKeyFileNotAllowed = errors.New("setting VictorOps api_key_file is not allowed")
48-
errOpsGenieAPIKeyFileNotAllowed = errors.New("setting OpsGenie api_key_file is not allowed")
43+
errPasswordFileNotAllowed = errors.New("setting password_file, bearer_token_file and credentials_file is not allowed")
44+
errOAuth2SecretFileNotAllowed = errors.New("setting OAuth2 client_secret_file is not allowed")
45+
errTLSFileNotAllowed = errors.New("setting TLS ca_file, cert_file and key_file is not allowed")
46+
errSlackAPIURLFileNotAllowed = errors.New("setting Slack api_url_file and global slack_api_url_file is not allowed")
47+
errVictorOpsAPIKeyFileNotAllowed = errors.New("setting VictorOps api_key_file is not allowed")
48+
errOpsGenieAPIKeyFileNotAllowed = errors.New("setting OpsGenie api_key_file is not allowed")
49+
errPagerDutyRoutingKeyFileNotAllowed = errors.New("setting PagerDuty routing_key_file is not allowed")
50+
errPagerDutyServiceKeyFileNotAllowed = errors.New("setting PagerDuty service_key_file is not allowed")
4951
)
5052

5153
// UserConfig is used to communicate a users alertmanager configs
@@ -356,6 +358,11 @@ func validateAlertmanagerConfig(cfg interface{}) error {
356358
if err := validateVictorOpsConfig(v.Interface().(config.VictorOpsConfig)); err != nil {
357359
return err
358360
}
361+
362+
case reflect.TypeOf(config.PagerdutyConfig{}):
363+
if err := validatePagerdutyConfig(v.Interface().(config.PagerdutyConfig)); err != nil {
364+
return err
365+
}
359366
}
360367

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

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

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

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

462469
// validateVictorOpsConfig validates the VictorOps config and returns an error if it contains
463-
// settings now allowed by Cortex.
470+
// settings not allowed by Cortex.
464471
func validateVictorOpsConfig(cfg config.VictorOpsConfig) error {
465472
if cfg.APIKeyFile != "" {
466473
return errVictorOpsAPIKeyFileNotAllowed
467474
}
468475
return nil
469476
}
477+
478+
// validatePagerdutyConfig validates the pager duty config and returns an error if it contains
479+
// settings not allowed by Cortex.
480+
func validatePagerdutyConfig(cfg config.PagerdutyConfig) error {
481+
if cfg.RoutingKeyFile != "" {
482+
return errPagerDutyRoutingKeyFileNotAllowed
483+
}
484+
485+
if cfg.ServiceKeyFile != "" {
486+
return errPagerDutyServiceKeyFileNotAllowed
487+
}
488+
489+
return nil
490+
}

pkg/alertmanager/api_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,34 @@ template_files:
563563
maxTemplateSize: 20,
564564
err: nil,
565565
},
566+
{
567+
name: "Should return error if PagerDuty routing_key_file is set",
568+
cfg: `
569+
alertmanager_config: |
570+
receivers:
571+
- name: default-receiver
572+
pagerduty_configs:
573+
- routing_key_file: /secrets
574+
575+
route:
576+
receiver: 'default-receiver'
577+
`,
578+
err: errors.Wrap(errPagerDutyRoutingKeyFileNotAllowed, "error validating Alertmanager config"),
579+
},
580+
{
581+
name: "Should return error if PagerDuty service_key_file is set",
582+
cfg: `
583+
alertmanager_config: |
584+
receivers:
585+
- name: default-receiver
586+
pagerduty_configs:
587+
- service_key_file: /secrets
588+
589+
route:
590+
receiver: 'default-receiver'
591+
`,
592+
err: errors.Wrap(errPagerDutyServiceKeyFileNotAllowed, "error validating Alertmanager config"),
593+
},
566594
}
567595

568596
limits := &mockAlertManagerLimits{}

0 commit comments

Comments
 (0)