Skip to content

Commit 4655ddd

Browse files
committed
feat: skip json parsing if allowedUser or ignoredUser are empty stakater#84
1 parent 9d85c26 commit 4655ddd

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

pkg/proxy/proxy.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,16 @@ func (p *Proxy) proxyRequest(w http.ResponseWriter, r *http.Request, params http
143143
return
144144
}
145145

146-
if event := provider.GetEventType(*hook); provider.IsCommitterCheckEvent(event) {
147-
committer := provider.GetCommitter(*hook, event)
148-
log.Printf("Incoming request from user: %s", committer)
149-
if p.isIgnoredUser(committer) || (!p.isAllowedUser(committer)) {
150-
log.Printf("Ignoring request for user: %s", committer)
151-
w.WriteHeader(http.StatusOK)
152-
w.Write([]byte(fmt.Sprintf("Ignoring request for user: %s", committer)))
153-
return
146+
if len(p.ignoredUsers) > 0 || len(p.allowedUsers) > 0 {
147+
if event := provider.GetEventType(*hook); provider.IsCommitterCheckEvent(event) {
148+
committer := provider.GetCommitter(*hook, event)
149+
log.Printf("Incoming request from user: %s", committer)
150+
if p.isIgnoredUser(committer) || (!p.isAllowedUser(committer)) {
151+
log.Printf("Ignoring request for user: %s", committer)
152+
w.WriteHeader(http.StatusOK)
153+
w.Write([]byte(fmt.Sprintf("Ignoring request for user: %s", committer)))
154+
return
155+
}
154156
}
155157
}
156158

pkg/proxy/proxy_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
const (
1717
proxyGitlabTestSecret = "testSecret"
18-
proxyGitlabTestEvent = "testEvent"
18+
proxyGitlabTestEvent = "Push Hook"
1919
proxyGitlabTestBody = "testBody"
2020
httpBinURL = "httpbin.org"
2121
httpBinURLInsecure = "http://" + httpBinURL
@@ -468,6 +468,7 @@ func TestProxy_proxyRequest(t *testing.T) {
468468
upstreamURL string
469469
allowedPaths []string
470470
secret string
471+
allowedUsers []string
471472
}
472473
type args struct {
473474
request *http.Request
@@ -603,6 +604,35 @@ func TestProxy_proxyRequest(t *testing.T) {
603604
},
604605
wantStatusCode: http.StatusMethodNotAllowed,
605606
},
607+
{
608+
name: "TestProxyRequestShouldNotParseJsonWithoutAllowedOrIgnoredUsersConfigured",
609+
fields: fields{
610+
provider: providers.GitlabProviderKind,
611+
upstreamURL: httpBinURLSecure,
612+
allowedPaths: []string{},
613+
secret: "",
614+
},
615+
args: args{
616+
request: createGitlabRequestWithPayload(http.MethodPost, "/post",
617+
proxyGitlabTestSecret, proxyGitlabTestEvent, []byte("{}")),
618+
},
619+
wantStatusCode: http.StatusOK,
620+
},
621+
{
622+
name: "TestProxyRequestShouldParseJsonWithAllowedOrIgnoredUsersConfigured",
623+
fields: fields{
624+
provider: providers.GitlabProviderKind,
625+
upstreamURL: httpBinURLSecure,
626+
allowedPaths: []string{},
627+
secret: "",
628+
allowedUsers: []string{"jsmith"},
629+
},
630+
args: args{
631+
request: createGitlabRequestWithPayload(http.MethodPost, "/post",
632+
proxyGitlabTestSecret, proxyGitlabTestEvent, proxyGitlabTestPayload),
633+
},
634+
wantStatusCode: http.StatusOK,
635+
},
606636
{
607637
name: "TestProxyRequestWithInvalidHttpMethod",
608638
fields: fields{
@@ -737,6 +767,7 @@ func TestProxy_proxyRequest(t *testing.T) {
737767
upstreamURL: tt.fields.upstreamURL,
738768
allowedPaths: tt.fields.allowedPaths,
739769
secret: tt.fields.secret,
770+
allowedUsers: tt.fields.allowedUsers,
740771
}
741772
router := httprouter.New()
742773
router.POST("/*path", p.proxyRequest)

0 commit comments

Comments
 (0)