mirror of
https://github.com/prymitive/karma
synced 2026-05-07 03:26:52 +00:00
fix(tests): add more proxy tests
This commit is contained in:
committed by
Łukasz Mierzwa
parent
db3d37185e
commit
6d2092f2b7
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user