From 0b02315517d929b3da00606c005e6337b7e375eb Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Thu, 4 Feb 2016 11:56:14 +0100 Subject: [PATCH] Sanitize POST URL for AM integration --- notification/notification.go | 3 ++- notification/notification_test.go | 37 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/notification/notification.go b/notification/notification.go index 3279bd2c52..7bf63019ad 100644 --- a/notification/notification.go +++ b/notification/notification.go @@ -18,6 +18,7 @@ import ( "encoding/json" "fmt" "net/http" + "strings" "sync" "time" @@ -239,7 +240,7 @@ func (n *Handler) setMore() { } func (n *Handler) postURL() string { - return n.opts.AlertmanagerURL + alertPushEndpoint + return strings.TrimRight(n.opts.AlertmanagerURL, "/") + alertPushEndpoint } func (n *Handler) send(alerts ...*model.Alert) error { diff --git a/notification/notification_test.go b/notification/notification_test.go index 29d057b65d..7c6de0a7ad 100644 --- a/notification/notification_test.go +++ b/notification/notification_test.go @@ -25,6 +25,43 @@ import ( "github.com/prometheus/common/model" ) +func TestHandlerPostURL(t *testing.T) { + var cases = []struct { + in, out string + }{ + { + in: "http://localhost:9093", + out: "http://localhost:9093/api/v1/alerts", + }, + { + in: "http://localhost:9093/", + out: "http://localhost:9093/api/v1/alerts", + }, + { + in: "http://localhost:9093/prefix", + out: "http://localhost:9093/prefix/api/v1/alerts", + }, + { + in: "http://localhost:9093/prefix//", + out: "http://localhost:9093/prefix/api/v1/alerts", + }, + { + in: "http://localhost:9093/prefix//", + out: "http://localhost:9093/prefix/api/v1/alerts", + }, + } + h := &Handler{ + opts: &HandlerOptions{}, + } + + for _, c := range cases { + h.opts.AlertmanagerURL = c.in + if res := h.postURL(); res != c.out { + t.Errorf("Expected post URL %q for %q but got %q", c.out, c.in, res) + } + } +} + func TestHandlerNextBatch(t *testing.T) { h := New(&HandlerOptions{})