Skip to content

Commit

Permalink
feat: skip json parsing if allowedUser or ignoredUser are empty staka…
Browse files Browse the repository at this point in the history
  • Loading branch information
maxisam committed Jan 5, 2023
1 parent 9d85c26 commit 4bdad72
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version: v0.2.82
version: v0.2.83
18 changes: 10 additions & 8 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,16 @@ func (p *Proxy) proxyRequest(w http.ResponseWriter, r *http.Request, params http
return
}

if event := provider.GetEventType(*hook); provider.IsCommitterCheckEvent(event) {
committer := provider.GetCommitter(*hook, event)
log.Printf("Incoming request from user: %s", committer)
if p.isIgnoredUser(committer) || (!p.isAllowedUser(committer)) {
log.Printf("Ignoring request for user: %s", committer)
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("Ignoring request for user: %s", committer)))
return
if len(p.ignoredUsers) > 0 || len(p.allowedUsers) > 0 {
if event := provider.GetEventType(*hook); provider.IsCommitterCheckEvent(event) {
committer := provider.GetCommitter(*hook, event)
log.Printf("Incoming request from user: %s", committer)
if p.isIgnoredUser(committer) || (!p.isAllowedUser(committer)) {
log.Printf("Ignoring request for user: %s", committer)
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("Ignoring request for user: %s", committer)))
return
}
}
}

Expand Down
33 changes: 32 additions & 1 deletion pkg/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

const (
proxyGitlabTestSecret = "testSecret"
proxyGitlabTestEvent = "testEvent"
proxyGitlabTestEvent = "Push Hook"
proxyGitlabTestBody = "testBody"
httpBinURL = "httpbin.org"
httpBinURLInsecure = "http://" + httpBinURL
Expand Down Expand Up @@ -468,6 +468,7 @@ func TestProxy_proxyRequest(t *testing.T) {
upstreamURL string
allowedPaths []string
secret string
allowedUsers []string
}
type args struct {
request *http.Request
Expand Down Expand Up @@ -603,6 +604,35 @@ func TestProxy_proxyRequest(t *testing.T) {
},
wantStatusCode: http.StatusMethodNotAllowed,
},
{
name: "TestProxyRequestShouldNotParseJsonWithoutAllowedOrIgnoredUsersConfigured",
fields: fields{
provider: providers.GitlabProviderKind,
upstreamURL: httpBinURLSecure,
allowedPaths: []string{},
secret: "",
},
args: args{
request: createGitlabRequestWithPayload(http.MethodPost, "/post",
proxyGitlabTestSecret, proxyGitlabTestEvent, []byte("{}")),
},
wantStatusCode: http.StatusOK,
},
{
name: "TestProxyRequestShouldParseJsonWithAllowedOrIgnoredUsersConfigured",
fields: fields{
provider: providers.GitlabProviderKind,
upstreamURL: httpBinURLSecure,
allowedPaths: []string{},
secret: "",
allowedUsers: []string{"jsmith"},
},
args: args{
request: createGitlabRequestWithPayload(http.MethodPost, "/post",
proxyGitlabTestSecret, proxyGitlabTestEvent, proxyGitlabTestPayload),
},
wantStatusCode: http.StatusOK,
},
{
name: "TestProxyRequestWithInvalidHttpMethod",
fields: fields{
Expand Down Expand Up @@ -737,6 +767,7 @@ func TestProxy_proxyRequest(t *testing.T) {
upstreamURL: tt.fields.upstreamURL,
allowedPaths: tt.fields.allowedPaths,
secret: tt.fields.secret,
allowedUsers: tt.fields.allowedUsers,
}
router := httprouter.New()
router.POST("/*path", p.proxyRequest)
Expand Down

0 comments on commit 4bdad72

Please sign in to comment.