Skip to content

Commit 5968f11

Browse files
author
Jim Minter
committed
resolve logic errors introduced in openshift#11077 and return 40x errors instead of 500 errors where possible
1 parent eab01e0 commit 5968f11

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

pkg/build/registry/buildconfig/webhook.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ func (w *WebHook) ServeHTTP(writer http.ResponseWriter, req *http.Request, ctx k
5555
switch err {
5656
case webhook.ErrSecretMismatch, webhook.ErrHookNotEnabled:
5757
return errors.NewUnauthorized(fmt.Sprintf("the webhook %q for %q did not accept your secret", hookType, name))
58-
case nil:
59-
return nil
60-
default:
58+
case webhook.MethodNotSupported:
59+
return errors.NewMethodNotSupported(buildapi.Resource("buildconfighook"), req.Method)
60+
}
61+
if _, ok := err.(*errors.StatusError); !ok {
6162
return errors.NewInternalError(fmt.Errorf("hook failed: %v", err))
6263
}
64+
return err
6365
}
6466
warning := err
6567

pkg/build/webhook/generic/generic.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/golang/glog"
1111

1212
kapi "k8s.io/kubernetes/pkg/api"
13+
"k8s.io/kubernetes/pkg/api/errors"
1314
"k8s.io/kubernetes/pkg/util/yaml"
1415

1516
"github.com/openshift/origin/pkg/build/api"
@@ -46,7 +47,7 @@ func (p *WebHookPlugin) Extract(buildCfg *api.BuildConfig, secret, path string,
4647
if len(contentType) != 0 {
4748
contentType, _, err = mime.ParseMediaType(contentType)
4849
if err != nil {
49-
return revision, envvars, false, fmt.Errorf("error parsing Content-Type: %s", err)
50+
return revision, envvars, false, errors.NewBadRequest(fmt.Sprintf("error parsing Content-Type: %s", err))
5051
}
5152
}
5253

@@ -61,7 +62,7 @@ func (p *WebHookPlugin) Extract(buildCfg *api.BuildConfig, secret, path string,
6162

6263
body, err := ioutil.ReadAll(req.Body)
6364
if err != nil {
64-
return revision, envvars, false, err
65+
return revision, envvars, false, errors.NewBadRequest(err.Error())
6566
}
6667

6768
if len(body) == 0 {
@@ -115,8 +116,8 @@ func (p *WebHookPlugin) Extract(buildCfg *api.BuildConfig, secret, path string,
115116
}
116117

117118
func verifyRequest(req *http.Request) error {
118-
if method := req.Method; method != "POST" {
119-
return fmt.Errorf("Unsupported HTTP method %s", method)
119+
if req.Method != "POST" {
120+
return webhook.MethodNotSupported
120121
}
121122
return nil
122123
}

pkg/build/webhook/generic/generic_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestVerifyRequestForMethod(t *testing.T) {
8989
plugin := New()
9090
revision, _, proceed, err := plugin.Extract(buildConfig, "secret100", "", req)
9191

92-
if err == nil || !strings.Contains(err.Error(), "Unsupported HTTP method") {
92+
if err == nil || !strings.Contains(err.Error(), "unsupported HTTP method") {
9393
t.Errorf("Expected unsupported HTTP method, got %v!", err)
9494
}
9595
if proceed {

pkg/build/webhook/github/github.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@ package github
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"io/ioutil"
87
"mime"
98
"net/http"
109

1110
kapi "k8s.io/kubernetes/pkg/api"
11+
"k8s.io/kubernetes/pkg/api/errors"
1212

1313
"github.com/golang/glog"
1414
"github.com/openshift/origin/pkg/build/api"
1515
"github.com/openshift/origin/pkg/build/webhook"
1616
)
1717

18-
var (
19-
ErrNoGitHubEvent = errors.New("missing X-GitHub-Event, X-Gogs-Event or X-Gitlab-Event")
20-
)
21-
2218
// WebHook used for processing github webhook requests.
2319
type WebHook struct{}
2420

@@ -58,18 +54,18 @@ func (p *WebHook) Extract(buildCfg *api.BuildConfig, secret, path string, req *h
5854
}
5955
method := getEvent(req.Header)
6056
if method != "ping" && method != "push" && method != "Push Hook" {
61-
return revision, envvars, proceed, fmt.Errorf("Unknown X-GitHub-Event, X-Gogs-Event or X-Gitlab-Event %s", method)
57+
return revision, envvars, proceed, errors.NewBadRequest(fmt.Sprintf("Unknown X-GitHub-Event, X-Gogs-Event or X-Gitlab-Event %s", method))
6258
}
6359
if method == "ping" {
6460
return revision, envvars, proceed, err
6561
}
6662
body, err := ioutil.ReadAll(req.Body)
6763
if err != nil {
68-
return revision, envvars, proceed, err
64+
return revision, envvars, proceed, errors.NewBadRequest(err.Error())
6965
}
7066
var event pushEvent
7167
if err = json.Unmarshal(body, &event); err != nil {
72-
return revision, envvars, proceed, err
68+
return revision, envvars, proceed, errors.NewBadRequest(err.Error())
7369
}
7470
if !webhook.GitRefMatches(event.Ref, webhook.DefaultConfigRef, &buildCfg.Spec.Source) {
7571
glog.V(2).Infof("Skipping build for BuildConfig %s/%s. Branch reference from '%s' does not match configuration", buildCfg.Namespace, buildCfg, event)
@@ -89,18 +85,18 @@ func (p *WebHook) Extract(buildCfg *api.BuildConfig, secret, path string, req *h
8985

9086
func verifyRequest(req *http.Request) error {
9187
if method := req.Method; method != "POST" {
92-
return fmt.Errorf("unsupported HTTP method %s", method)
88+
return webhook.MethodNotSupported
9389
}
9490
contentType := req.Header.Get("Content-Type")
9591
mediaType, _, err := mime.ParseMediaType(contentType)
9692
if err != nil {
97-
return fmt.Errorf("non-parseable Content-Type %s (%s)", contentType, err)
93+
return errors.NewBadRequest(fmt.Sprintf("non-parseable Content-Type %s (%s)", contentType, err))
9894
}
9995
if mediaType != "application/json" {
100-
return fmt.Errorf("unsupported Content-Type %s", contentType)
96+
return errors.NewBadRequest(fmt.Sprintf("unsupported Content-Type %s", contentType))
10197
}
10298
if len(getEvent(req.Header)) == 0 {
103-
return ErrNoGitHubEvent
99+
return errors.NewBadRequest("missing X-GitHub-Event, X-Gogs-Event or X-Gitlab-Event")
104100
}
105101
return nil
106102
}

pkg/build/webhook/webhook.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ const (
1919
)
2020

2121
var (
22-
ErrSecretMismatch = errors.New("the provided secret does not match")
23-
ErrHookNotEnabled = errors.New("the specified hook is not enabled")
22+
ErrSecretMismatch = errors.New("the provided secret does not match")
23+
ErrHookNotEnabled = errors.New("the specified hook is not enabled")
24+
MethodNotSupported = errors.New("unsupported HTTP method")
2425
)
2526

2627
// Plugin for Webhook verification is dependent on the sending side, it can be

0 commit comments

Comments
 (0)