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
This commit is contained in:
xginn8 2017-12-07 09:01:16 -05:00 committed by stuart nelson
parent 9b127147bd
commit 266baa089c
2 changed files with 26 additions and 0 deletions

View File

@ -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")
}

View File

@ -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: ''