Skip to content
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

Add check for templated Opsgenie receiver config #3060

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions config/notifiers.go
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"regexp"
"strings"
"text/template"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -504,9 +505,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 @@ -634,6 +634,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