fix(tests): add more proxy tests

This commit is contained in:
Łukasz Mierzwa
2020-10-30 16:35:32 +00:00
committed by Łukasz Mierzwa
parent db3d37185e
commit 6d2092f2b7
2 changed files with 45 additions and 24 deletions

View File

@@ -84,14 +84,14 @@ func handlePostRequest(alertmanager *alertmanager.Alertmanager, h http.Handler)
Str("uri", r.RequestURI).
Msg("Proxy request")
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
log.Error().Err(err).
Str("alertmanager", alertmanager.Name).
Str("method", r.Method).
Str("uri", r.RequestURI).
Msg("Failed to close proxied request")
Msg("Failed to read proxied request")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -112,33 +112,35 @@ func handlePostRequest(alertmanager *alertmanager.Alertmanager, h http.Handler)
return
}
silence, err := m.Unmarshal(body)
if err != nil {
log.Error().
Err(err).
Str("alertmanager", alertmanager.Name).
Str("method", r.Method).
Str("uri", r.RequestURI).
Msg("Failed to unmarshal silence body")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for i, acl := range silenceACLs {
username := getUserFromContext(r)
isAllowed, err := acl.isAllowed(alertmanager.Name, silence, username)
log.Debug().Int("index", i).Bool("allowed", isAllowed).Err(err).Msg("ACL rule check")
if len(silenceACLs) > 0 {
silence, err := m.Unmarshal(body)
if err != nil {
log.Warn().Err(err).
log.Error().
Err(err).
Str("alertmanager", alertmanager.Name).
Str("method", r.Method).
Str("uri", r.RequestURI).
Msg("Proxy request was blocked by ACL rule")
http.Error(w, err.Error(), http.StatusBadRequest)
Msg("Failed to unmarshal silence body")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if isAllowed {
break
for i, acl := range silenceACLs {
username := getUserFromContext(r)
isAllowed, err := acl.isAllowed(alertmanager.Name, silence, username)
log.Debug().Int("index", i).Bool("allowed", isAllowed).Err(err).Msg("ACL rule check")
if err != nil {
log.Warn().Err(err).
Str("alertmanager", alertmanager.Name).
Str("method", r.Method).
Str("uri", r.RequestURI).
Msg("Proxy request was blocked by ACL rule")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if isAllowed {
break
}
}
}

View File

@@ -445,7 +445,6 @@ func TestProxyUserRewrite(t *testing.T) {
]}`,
proxyRequestBody: `{"id":"1234567890","comment":"comment","createdBy":"john","endsAt":"2000-02-01T00:02:03.000Z","matchers":[{"isRegex":false,"name":"alertname","value":"Fake Alert"},{"isRegex":true,"name":"foo","value":"(bar|baz)"}],"startsAt":"2000-02-01T00:00:00.000Z"}`,
},
{
name: "header auth, missing header",
responseCode: 401,
@@ -1133,6 +1132,26 @@ func TestProxySilenceACL(t *testing.T) {
frontednRequestBody: defaultBody,
responseCode: 400,
},
{
name: "invalid silence JSON",
silenceACLs: []*silenceACL{
{
Action: "block",
Reason: "block all regex silences",
Scope: silenceACLScope{
Filters: []silenceFilter{
{NameRegex: regexp.MustCompile(".*"), ValueRegex: regexp.MustCompile(".*"), IsRegex: true},
},
Groups: []string{},
Alertmanagers: []string{},
},
Matchers: aclMatchers{},
},
},
requestUsername: "bob",
frontednRequestBody: `{XXXX: 1bC]}`,
responseCode: 500,
},
}
for _, testCase := range proxyTests {