Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove writer object from validateRequest params #7

Merged
merged 1 commit into from
Jun 27, 2020
Merged
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
17 changes: 9 additions & 8 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,38 @@ func main() {
func validateAndParseRequest(w http.ResponseWriter, r *http.Request, handler *WebhookHandler) {
log.Print("Handling webhook request ...")

body, err := handler.validateRequest(w, r);
body, code, err := handler.validateRequest(r);
if err != nil {
failRequest(w, err)
failRequest(w, code, err)
return
}

webhook, err := handler.parse(body)
if err != nil {
failRequest(w, err)
failRequest(w, code, err)
return
}

if !handler.isEligible(webhook) {
succeedRequest(w)
succeedRequest(w, http.StatusOK)
return
}

//deployment := &Deployment{Request: webhook}
//defer deployment.release()

succeedRequest(w)
succeedRequest(w, code)
}

func succeedRequest(w http.ResponseWriter) {
func succeedRequest(w http.ResponseWriter, code int) {
log.Print("Webhook request handled successfully")
w.WriteHeader(http.StatusAccepted)
w.WriteHeader(code)
}

func failRequest(w http.ResponseWriter, err error) {
func failRequest(w http.ResponseWriter, code int, err error) {
log.Printf("Error handling webhook request: %v", err)

w.WriteHeader(code)
_, writeErr := w.Write([]byte(err.Error()))

if writeErr != nil {
Expand Down
16 changes: 6 additions & 10 deletions cmd/server/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,29 @@ func (handler *WebhookHandler) isEligible(webhook *WebhookContext) bool {
return true
}

func (handler *WebhookHandler) validateRequest(w http.ResponseWriter, r *http.Request) ([]byte, error) {
func (handler *WebhookHandler) validateRequest(r *http.Request) ([]byte, int, error) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return nil, fmt.Errorf("invalid method %s, only POST requests are allowed", r.Method)
return nil, http.StatusMethodNotAllowed, fmt.Errorf("invalid method %s, only POST requests are allowed", r.Method)
}

if contentType := r.Header.Get("Content-Type"); contentType != jsonContentType {
w.WriteHeader(http.StatusBadRequest)
return nil, fmt.Errorf("unsupported content type %s, only %s is supported", contentType, jsonContentType)
return nil, http.StatusBadRequest, fmt.Errorf("unsupported content type %s, only %s is supported", contentType, jsonContentType)
}

body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return nil, fmt.Errorf("could not read request body: %v", err)
return nil, http.StatusBadRequest, fmt.Errorf("could not read request body: %v", err)
}

delivery := r.Header.Get("x-github-delivery")
signature := r.Header.Get("x-hub-signature")
event := r.Header.Get("x-github-event")

if len(signature) == 0 || len(event) == 0 || len(delivery) == 0 || !verifySignature(handler.Secret, signature, body) {
w.WriteHeader(http.StatusBadRequest)
return nil, errors.New("missing github event signature, webhook event, id or signature is invalid")
return nil, http.StatusBadRequest, errors.New("missing github event signature, webhook event, id or signature is invalid")
}

return body, nil
return body, http.StatusAccepted, nil
}

func (handler *WebhookHandler) parse(body []byte) (*WebhookContext, error) {
Expand Down
25 changes: 18 additions & 7 deletions cmd/server/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import (
func TestWebhookValidateRequestRejectsNonHttpPost(t *testing.T) {
reader := strings.NewReader("request payload")
req := httptest.NewRequest(http.MethodGet, "/", reader)
writer := httptest.NewRecorder()

handler := &WebhookHandler{}
_, err := handler.validateRequest(writer, req)
_, code, err := handler.validateRequest(req)

if code != http.StatusMethodNotAllowed {
t.Errorf("expected %d, got %d", http.StatusMethodNotAllowed, code)
}

if err == nil || err.Error() != "invalid method GET, only POST requests are allowed" {
t.Error("expected invalid http method error")
Expand All @@ -27,7 +30,7 @@ func TestWebhookValidateRequestRejectsNonJsonContentType(t *testing.T) {
req.Header.Add("Content-Type", "application/yaml")

handler := &WebhookHandler{}
_, err := handler.validateRequest(httptest.NewRecorder(), req)
_, _, err := handler.validateRequest(req)

if err == nil || err.Error() != "unsupported content type application/yaml, only application/json is supported" {
t.Error("expected invalid content-type error")
Expand All @@ -40,24 +43,28 @@ func TestWebhookValidateRequestRejectsMissingSignature(t *testing.T) {
req.Header.Add("Content-Type", "application/json")

handler := &WebhookHandler{}
_, err := handler.validateRequest(httptest.NewRecorder(), req)
_, code, err := handler.validateRequest(req)

if err == nil || err.Error() != "missing github event signature, webhook event, id or signature is invalid" {
t.Error("expected invalid request content error")
}

req.Header.Add("x-github-delivery", "x")
req.Header.Add("x-github-event", "y")
_, err = handler.validateRequest(httptest.NewRecorder(), req)
_, _, err = handler.validateRequest(req)
if err == nil || err.Error() != "missing github event signature, webhook event, id or signature is invalid" {
t.Error("expected invalid request content error")
}

req.Header.Add("x-hub-signature", "sig")
_, err = handler.validateRequest(httptest.NewRecorder(), req)
_, code, err = handler.validateRequest(req)
if err == nil || err.Error() != "missing github event signature, webhook event, id or signature is invalid" {
t.Error("expected invalid request content error")
}

if code != http.StatusBadRequest {
t.Errorf("expected %d, got %d", http.StatusBadRequest, code)
}
}

func TestWebhookValidateRequestSucceeds(t *testing.T) {
Expand All @@ -78,7 +85,7 @@ func TestWebhookValidateRequestSucceeds(t *testing.T) {
Secret: []byte("secret"),
}

body, err := handler.validateRequest(httptest.NewRecorder(), req)
body, code, err := handler.validateRequest(req)
if err != nil {
t.Error("expected valid request to succeed: " + err.Error())
}
Expand All @@ -87,6 +94,10 @@ func TestWebhookValidateRequestSucceeds(t *testing.T) {
if parsedBody != payload {
t.Error("wrong content received")
}

if code != http.StatusAccepted {
t.Errorf("expected %d, got %d", http.StatusAccepted, code)
}
}

func TestWebhookIsEligibleForNonWhitelistedOrgFails(t *testing.T) {
Expand Down