Skip to content

Commit

Permalink
Adding check for webhook's URL formatting (#1129)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
xginn8 authored and stuartnelson3 committed Dec 7, 2017
1 parent 9b12714 commit 266baa0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package config

import (
"fmt"
"net/url"
"strings"
"time"
)
Expand Down Expand Up @@ -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")
}

Expand Down
17 changes: 17 additions & 0 deletions config/notifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
Expand Down

0 comments on commit 266baa0

Please sign in to comment.