Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/providers/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (p *GitlabProvider) Validate(hook Hook) bool {
func (p *GitlabProvider) GetCommitter(hook Hook) string {
var payloadData GitlabPushPayload
if err := json.Unmarshal(hook.Payload, &payloadData); err != nil {
log.Printf("Gitlab hook payload unmarshalling failed")
log.Printf("Gitlab hook payload unmarshalling failed: %v", err)
return ""
}

Expand Down
16 changes: 9 additions & 7 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,15 @@ func (p *Proxy) proxyRequest(w http.ResponseWriter, r *http.Request, params http
return
}

committer := provider.GetCommitter(*hook)
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 {
committer := provider.GetCommitter(*hook)
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(strings.TrimSpace(p.secret)) > 0 && !provider.Validate(*hook) {
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