From f54c8157efd7f21693880fc1d3af428073c753eb Mon Sep 17 00:00:00 2001 From: Matthew McGinn Date: Tue, 5 Dec 2017 15:12:26 -0500 Subject: [PATCH 1/2] 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 --- 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 5193100d11..cdfe4776c7 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("non-absolute URL in webhook config") + } + c.URL = url.String() return checkOverflow(c.XXX, "webhook config") } diff --git a/config/notifiers_test.go b/config/notifiers_test.go index b436f77e2a..767b875e18 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 := "non-absolute URL in webhook config" + + 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: '' From ea444f053c7e869644f099b451b69e7e23c9a128 Mon Sep 17 00:00:00 2001 From: Matthew McGinn Date: Thu, 7 Dec 2017 08:01:21 -0500 Subject: [PATCH 2/2] updating error message as requested --- config/notifiers.go | 2 +- config/notifiers_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/notifiers.go b/config/notifiers.go index cdfe4776c7..16bf320c8f 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -298,7 +298,7 @@ func (c *WebhookConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { return err } if url.Scheme != "https" && url.Scheme != "http" { - return fmt.Errorf("non-absolute URL in webhook config") + 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 767b875e18..e0b79dc00b 100644 --- a/config/notifiers_test.go +++ b/config/notifiers_test.go @@ -117,7 +117,7 @@ url: 'localhost:9093' var cfg WebhookConfig err := yaml.Unmarshal([]byte(in), &cfg) - expected := "non-absolute URL in webhook config" + expected := "scheme required for webhook url" if err == nil { t.Fatalf("no error returned, expected:\n%v", expected)