From f58aee3255c75252188493b3ff7c3b501a4698cb Mon Sep 17 00:00:00 2001 From: Erki Esken Date: Tue, 6 Sep 2022 14:34:31 +0300 Subject: [PATCH 1/2] Add check for templated Opsgenie receiver config Solves #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 --- config/notifiers.go | 14 +++++++++++--- config/notifiers_test.go | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/config/notifiers.go b/config/notifiers.go index 95df28db43..aedb17d367 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -17,6 +17,7 @@ import ( "fmt" "regexp" "strings" + "text/template" "time" "github.com/pkg/errors" @@ -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) + } } } diff --git a/config/notifiers_test.go b/config/notifiers_test.go index 889abab627..6b0611f3ec 100644 --- a/config/notifiers_test.go +++ b/config/notifiers_test.go @@ -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, }, From 2c96a0279bd29e4c8c2384aef6a6b51c9c8ef90e Mon Sep 17 00:00:00 2001 From: Erki Esken Date: Mon, 28 Nov 2022 09:37:44 +0200 Subject: [PATCH 2/2] Update config/notifiers.go Co-authored-by: Simon Pasquier Signed-off-by: Erki Esken --- config/notifiers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/notifiers.go b/config/notifiers.go index aedb17d367..d0446a6834 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -506,7 +506,7 @@ func (c *OpsGenieConfig) UnmarshalYAML(unmarshal func(interface{}) error) error } if strings.Contains(r.Type, "{{") { - _, err := template.New("").Parse(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) }