Skip to content

Commit

Permalink
Add check for templated Opsgenie receiver config (prometheus#3060)
Browse files Browse the repository at this point in the history
* Add check for templated Opsgenie receiver config

Solves prometheus#2887, where Opsgenie responder type can be templated according
to docs, but configuration logic checked for specific list of values only.

Signed-off-by: Erki Esken <erki@esken.net>
Co-authored-by: Simon Pasquier <spasquie@redhat.com>
  • Loading branch information
2 people authored and hoperays committed Apr 23, 2023
1 parent c40c29b commit 012160f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
14 changes: 11 additions & 3 deletions config/notifiers.go
Expand Up @@ -18,6 +18,7 @@ import (
"net/textproto"
"regexp"
"strings"
"text/template"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -541,9 +542,16 @@ func (c *OpsGenieConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
return errors.Errorf("opsGenieConfig responder %v has to have at least one of id, username or name specified", r)
}

r.Type = strings.ToLower(r.Type)
if !opsgenieTypeMatcher.MatchString(r.Type) {
return errors.Errorf("opsGenieConfig responder %v type does not match valid options %s", r, opsgenieValidTypesRe)
if strings.Contains(r.Type, "{{") {
_, err := template.New("").Parse(r.Type)
if err != nil {
return errors.Errorf("opsGenieConfig responder %v type is not a valid template: %v", r, err)
}
} else {
r.Type = strings.ToLower(r.Type)
if !opsgenieTypeMatcher.MatchString(r.Type) {
return errors.Errorf("opsGenieConfig responder %v type does not match valid options %s", r, opsgenieValidTypesRe)
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions config/notifiers_test.go
Expand Up @@ -743,6 +743,25 @@ api_url: http://example.com
responders:
- type: schedule
api_url: http://example.com
`,
err: true,
},
{
name: "valid responder type template",
in: `api_key: xyz
responders:
- id: foo
type: "{{/* valid comment */}}team"
api_url: http://example.com
`,
},
{
name: "invalid responder type template",
in: `api_key: xyz
responders:
- id: foo
type: "{{/* invalid comment }}team"
api_url: http://example.com
`,
err: true,
},
Expand Down

0 comments on commit 012160f

Please sign in to comment.