fix(api): ensure proxy paths are encoded

Fixes #3651
This commit is contained in:
Łukasz Mierzwa
2021-10-27 22:23:05 +01:00
committed by Łukasz Mierzwa
parent fc16d67c02
commit e9c692a391
4 changed files with 40 additions and 12 deletions

View File

@@ -5,6 +5,8 @@
### Fixed
- Toast popup messages didn't respond to clicks.
- Alertmanager with brackets in the name wasn't able to create, edit
or delete silences #3651.
### Added

View File

@@ -50,14 +50,14 @@ var proxyTests = []proxyTest{
// valid alertmanager and methods
{
method: "POST",
localPath: "/proxy/alertmanager/dummy/api/v2/silences",
localPath: "/proxy/alertmanager/dummy%20with%20%28space%29/api/v2/silences",
upstreamURI: "http://localhost:9093/api/v2/silences",
code: 200,
response: "{\"status\":\"success\",\"data\":{\"silenceId\":\"d8a61ca8-ee2e-4076-999f-276f1e986bf3\"}}",
},
{
method: "DELETE",
localPath: "/proxy/alertmanager/dummy/api/v2/silence/d8a61ca8-ee2e-4076-999f-276f1e986bf3",
localPath: "/proxy/alertmanager/dummy%20with%20%28space%29/api/v2/silence/d8a61ca8-ee2e-4076-999f-276f1e986bf3",
upstreamURI: "http://localhost:9093/api/v2/silence/d8a61ca8-ee2e-4076-999f-276f1e986bf3",
code: 200,
response: "{\"status\":\"success\"}",
@@ -80,13 +80,13 @@ var proxyTests = []proxyTest{
// valid alertmanager name, but invalid method
{
method: "GET",
localPath: "/proxy/alertmanager/dummy/api/v2/silences",
localPath: "/proxy/alertmanager/dummy%20with%20%28space%29/api/v2/silences",
upstreamURI: "",
code: 405,
},
{
method: "GET",
localPath: "/proxy/alertmanager/dummy/api/v2/silence/d8a61ca8-ee2e-4076-999f-276f1e986bf3",
localPath: "/proxy/alertmanager/dummy%20with%20%28space%29/api/v2/silence/d8a61ca8-ee2e-4076-999f-276f1e986bf3",
upstreamURI: "http://localhost:9093/api/v2/silence/d8a61ca8-ee2e-4076-999f-276f1e986bf3",
code: 405,
},
@@ -106,7 +106,7 @@ func TestProxy(t *testing.T) {
r := testRouter()
am, err := alertmanager.NewAlertmanager(
"cluster",
"dummy",
"dummy with (space)",
"http://localhost:9093",
alertmanager.WithRequestTimeout(time.Second*5),
alertmanager.WithProxy(true),
@@ -155,7 +155,7 @@ type proxyHeaderTest struct {
var proxyHeaderTests = []proxyHeaderTest{
{
method: "POST",
localPath: "/proxy/alertmanager/dummy/api/v2/silences",
localPath: "/proxy/alertmanager/dummy%20with%20%28space%29/api/v2/silences",
upstreamURI: "http://localhost:9093/api/v2/silences",
code: 200,
alertmanagerURI: "http://localhost:9093",
@@ -166,7 +166,7 @@ var proxyHeaderTests = []proxyHeaderTest{
},
{
method: "POST",
localPath: "/proxy/alertmanager/dummy/api/v2/silences",
localPath: "/proxy/alertmanager/dummy%20with%20%28space%29/api/v2/silences",
upstreamURI: "http://alertmanager.example.com/api/v2/silences",
code: 200,
alertmanagerURI: "http://alertmanager.example.com",
@@ -177,7 +177,7 @@ var proxyHeaderTests = []proxyHeaderTest{
},
{
method: "POST",
localPath: "/proxy/alertmanager/dummy/api/v2/silences",
localPath: "/proxy/alertmanager/dummy%20with%20%28space%29/api/v2/silences",
upstreamURI: "http://alertmanager.example.com/api/v2/silences",
code: 200,
alertmanagerURI: "http://foo:bar@alertmanager.example.com",
@@ -188,7 +188,7 @@ var proxyHeaderTests = []proxyHeaderTest{
},
{
method: "POST",
localPath: "/proxy/alertmanager/dummy/api/v2/silences",
localPath: "/proxy/alertmanager/dummy%20with%20%28space%29/api/v2/silences",
upstreamURI: "http://alertmanager.example.com/api/v2/silences",
code: 200,
alertmanagerURI: "http://foo@alertmanager.example.com",
@@ -216,7 +216,7 @@ func TestProxyHeaders(t *testing.T) {
r := testRouter()
am, err := alertmanager.NewAlertmanager(
"cluster",
"dummy",
"dummy with (space)",
testCase.alertmanagerURI,
alertmanager.WithRequestTimeout(time.Second*5),
alertmanager.WithProxy(true),

View File

@@ -181,7 +181,7 @@ func (am *Alertmanager) pullSilences(version string) error {
// InternalURI is the URI of this Alertmanager that will be used for all request made by the UI
func (am *Alertmanager) InternalURI() string {
if am.ProxyRequests {
return fmt.Sprintf("./proxy/alertmanager/%s", am.Name)
return fmt.Sprintf("./proxy/alertmanager/%s", url.PathEscape(am.Name))
}
// strip all user/pass information, fetch() doesn't support it anyway

View File

@@ -10,6 +10,7 @@ import (
)
type uriTest struct {
name string
rawURI string
extURI string
proxy bool
@@ -19,48 +20,56 @@ type uriTest struct {
var uriTests = []uriTest{
{
name: "test",
rawURI: "http://alertmanager.example.com",
proxy: false,
internalURI: "http://alertmanager.example.com",
publicURI: "http://alertmanager.example.com",
},
{
name: "test",
rawURI: "http://alertmanager.example.com/foo",
proxy: false,
internalURI: "http://alertmanager.example.com/foo",
publicURI: "http://alertmanager.example.com/foo",
},
{
name: "test",
rawURI: "http://alertmanager.example.com",
proxy: true,
internalURI: "./proxy/alertmanager/test",
publicURI: "http://alertmanager.example.com",
},
{
name: "test",
rawURI: "http://alertmanager.example.com/foo",
proxy: true,
internalURI: "./proxy/alertmanager/test",
publicURI: "http://alertmanager.example.com/foo",
},
{
name: "test",
rawURI: "http://user:pass@alertmanager.example.com",
proxy: false,
internalURI: "http://alertmanager.example.com",
publicURI: "http://user:pass@alertmanager.example.com",
},
{
name: "test",
rawURI: "https://user:pass@alertmanager.example.com/foo",
proxy: false,
internalURI: "https://alertmanager.example.com/foo",
publicURI: "https://user:pass@alertmanager.example.com/foo",
},
{
name: "test",
rawURI: "http://user:pass@alertmanager.example.com",
proxy: true,
internalURI: "./proxy/alertmanager/test",
publicURI: "http://user:pass@alertmanager.example.com",
},
{
name: "test",
rawURI: "http://user:pass@alertmanager.example.com",
extURI: "http://am.example.com",
proxy: true,
@@ -68,6 +77,7 @@ var uriTests = []uriTest{
publicURI: "http://am.example.com",
},
{
name: "test",
rawURI: "http://alertmanager.example.com",
extURI: "http://am.example.com",
proxy: true,
@@ -75,6 +85,7 @@ var uriTests = []uriTest{
publicURI: "http://am.example.com",
},
{
name: "test",
rawURI: "http://user:pass@alertmanager.example.com",
extURI: "http://am.example.com",
proxy: false,
@@ -82,17 +93,32 @@ var uriTests = []uriTest{
publicURI: "http://am.example.com",
},
{
name: "test",
rawURI: "http://alertmanager.example.com",
extURI: "http://am.example.com",
proxy: false,
internalURI: "http://am.example.com",
publicURI: "http://am.example.com",
},
{
name: "test with (spaces)",
rawURI: "http://alertmanager.example.com",
proxy: true,
internalURI: `./proxy/alertmanager/test%20with%20%28spaces%29`,
publicURI: "http://alertmanager.example.com",
},
{
name: "test with (spaces)",
rawURI: "http://alertmanager.example.com",
proxy: true,
internalURI: `./proxy/alertmanager/test%20with%20%20%28spaces%29`,
publicURI: "http://alertmanager.example.com",
},
}
func TestAlertmanagerURI(t *testing.T) {
for i, test := range uriTests {
am, err := NewAlertmanager("cluster", "test", test.rawURI, WithExternalURI(test.extURI), WithProxy(test.proxy))
am, err := NewAlertmanager("cluster", test.name, test.rawURI, WithExternalURI(test.extURI), WithProxy(test.proxy))
if err != nil {
t.Error(err)
}