From 266baa089c15ed63fed1009bc5ff80655e183016 Mon Sep 17 00:00:00 2001 From: xginn8 Date: Thu, 7 Dec 2017 09:01:16 -0500 Subject: [PATCH] Adding check for webhook's URL formatting (#1129) * Adding check for webhook's URL formatting Since alertmanager will fail silently when trying to send to a schemaless URL, provide a way to check that a URL is properly formatted in alertmanager * updating error message as requested --- config/notifiers.go | 9 +++++++++ config/notifiers_test.go | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/config/notifiers.go b/config/notifiers.go index 5193100d..16bf320c 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -15,6 +15,7 @@ package config import ( "fmt" + "net/url" "strings" "time" ) @@ -292,6 +293,14 @@ func (c *WebhookConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { if c.URL == "" { return fmt.Errorf("missing URL in webhook config") } + url, err := url.Parse(c.URL) + if err != nil { + return err + } + if url.Scheme != "https" && url.Scheme != "http" { + return fmt.Errorf("scheme required for webhook url") + } + c.URL = url.String() return checkOverflow(c.XXX, "webhook config") } diff --git a/config/notifiers_test.go b/config/notifiers_test.go index b436f77e..e0b79dc0 100644 --- a/config/notifiers_test.go +++ b/config/notifiers_test.go @@ -110,6 +110,23 @@ url: '' } } +func TestWebhookURLIsAbsolute(t *testing.T) { + in := ` +url: 'localhost:9093' +` + var cfg WebhookConfig + err := yaml.Unmarshal([]byte(in), &cfg) + + expected := "scheme required for webhook url" + + if err == nil { + t.Fatalf("no error returned, expected:\n%v", expected) + } + if err.Error() != expected { + t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error()) + } +} + func TestOpsGenieAPIKeyIsPresent(t *testing.T) { in := ` api_key: ''