From f585312ba42c8e98fb95a9dd37a705d85b6bb63b Mon Sep 17 00:00:00 2001 From: Sylvain Rabot Date: Mon, 22 Oct 2018 22:34:28 +0200 Subject: [PATCH 1/3] fix(api): proxy when upstream url has a path Signed-off-by: Sylvain Rabot --- proxy.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/proxy.go b/proxy.go index c43ff662f..e50abc675 100644 --- a/proxy.go +++ b/proxy.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" "net/http" "net/http/httputil" "net/url" @@ -45,6 +46,11 @@ func NewAlertmanagerProxy(alertmanager *alertmanager.Alertmanager) (*httputil.Re // set hostname of proxied target req.Host = upstreamURL.Host + + // Prepend with upstream URL path if exists + if len(upstreamURL.Path) > 0 { + req.URL.Path = strings.TrimSuffix(upstreamURL.Path, "/") + req.URL.Path + } log.Debugf("[%s] Proxy request for %s", alertmanager.Name, req.URL.Path) }, From 380022f01fa3bd6c2878d9045fab0fbbe74667b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Wed, 24 Oct 2018 21:25:47 +0100 Subject: [PATCH 2/3] chore(style): reformat proxy.go --- proxy.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/proxy.go b/proxy.go index e50abc675..cac88197e 100644 --- a/proxy.go +++ b/proxy.go @@ -2,14 +2,14 @@ package main import ( "fmt" - "strings" "net/http" "net/http/httputil" "net/url" + "strings" + "github.com/gin-gonic/gin" "github.com/prymitive/karma/internal/alertmanager" "github.com/prymitive/karma/internal/config" - "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" ) @@ -46,7 +46,7 @@ func NewAlertmanagerProxy(alertmanager *alertmanager.Alertmanager) (*httputil.Re // set hostname of proxied target req.Host = upstreamURL.Host - + // Prepend with upstream URL path if exists if len(upstreamURL.Path) > 0 { req.URL.Path = strings.TrimSuffix(upstreamURL.Path, "/") + req.URL.Path From 3a66080e9d339fa90d8f1a9bf6b22aebf1de18a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Wed, 24 Oct 2018 21:35:03 +0100 Subject: [PATCH 3/3] feat(tests): add a test case for proxying to sub-URI --- proxy_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/proxy_test.go b/proxy_test.go index b9465a083..f998ebe99 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -224,3 +224,34 @@ func TestProxyHeaders(t *testing.T) { } } } + +func TestProxyToSubURIAlertmanager(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + r := ginTestEngine() + am, err := alertmanager.NewAlertmanager( + "suburi", + "http://alertmanager.example.com/suburi", + alertmanager.WithRequestTimeout(time.Second*5), + alertmanager.WithProxy(true), + ) + if err != nil { + t.Error(err) + } + err = setupRouterProxyHandlers(r, am) + if err != nil { + t.Errorf("Failed to setup proxy for Alertmanager %s: %s", am.Name, err) + } + + httpmock.RegisterResponder("POST", "http://alertmanager.example.com/suburi/api/v1/silences", func(req *http.Request) (*http.Response, error) { + return httpmock.NewStringResponse(200, "ok"), nil + }) + + req := httptest.NewRequest("POST", "/proxy/alertmanager/suburi/api/v1/silences", nil) + resp := newCloseNotifyingRecorder() + r.ServeHTTP(resp, req) + if resp.Code != 200 { + t.Errorf("Got response code %d instead of 200", resp.Code) + } +}