Skip to content

Commit

Permalink
add missing crashhandler
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
  • Loading branch information
inteon committed Dec 3, 2023
1 parent 8d817e7 commit b2c9381
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/webhook/admission/http.go
Expand Up @@ -42,6 +42,11 @@ func init() {
var _ http.Handler = &Webhook{}

func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer utilruntime.HandleCrash(func(_ interface{}) {
// Assume the crash happened before the response was written.
http.Error(w, "internal server error", http.StatusInternalServerError)
})

ctx := r.Context()
if wh.WithContextFunc != nil {
ctx = wh.WithContextFunc(ctx, r)
Expand Down
27 changes: 27 additions & 0 deletions pkg/webhook/admission/http_test.go
Expand Up @@ -198,6 +198,33 @@ var _ = Describe("Admission Webhooks", func() {
return respRecorder.Body.Len()
}, time.Second*3).Should(Equal(0))
})

It("should handle crashes", func() {
req := &http.Request{
Header: http.Header{"Content-Type": []string{"application/json"}},
Method: http.MethodPost,
Body: nopCloser{Reader: bytes.NewBufferString(`{"spec":{"token":"foobar"}}`)},
}
webhook := &Webhook{
Handler: &fakeHandler{
fn: func(ctx context.Context, req Request) Response {
panic("boom")
},
},
}

expected := `internal server error
`
(func() {
defer func() {
if r := recover(); r != nil {
Expect(r).To(Equal("boom"))
}
}()
webhook.ServeHTTP(respRecorder, req)
})()
Expect(respRecorder.Body.String()).To(Equal(expected))
})
})
})

Expand Down
5 changes: 5 additions & 0 deletions pkg/webhook/authentication/http.go
Expand Up @@ -42,6 +42,11 @@ func init() {
var _ http.Handler = &Webhook{}

func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer utilruntime.HandleCrash(func(_ interface{}) {
// Assume the crash happened before the response was written.
http.Error(w, "internal server error", http.StatusInternalServerError)
})

ctx := r.Context()
if wh.WithContextFunc != nil {
ctx = wh.WithContextFunc(ctx, r)
Expand Down
27 changes: 27 additions & 0 deletions pkg/webhook/authentication/http_test.go
Expand Up @@ -181,6 +181,33 @@ var _ = Describe("Authentication Webhooks", func() {
webhook.ServeHTTP(respRecorder, req.WithContext(ctx))
Expect(respRecorder.Body.String()).To(Equal(expected))
})

It("should handle crashes", func() {
req := &http.Request{
Header: http.Header{"Content-Type": []string{"application/json"}},
Method: http.MethodPost,
Body: nopCloser{Reader: bytes.NewBufferString(`{"spec":{"token":"foobar"}}`)},
}
webhook := &Webhook{
Handler: &fakeHandler{
fn: func(ctx context.Context, req Request) Response {
panic("boom")
},
},
}

expected := `internal server error
`
(func() {
defer func() {
if r := recover(); r != nil {
Expect(r).To(Equal("boom"))
}
}()
webhook.ServeHTTP(respRecorder, req)
})()
Expect(respRecorder.Body.String()).To(Equal(expected))
})
})
})

Expand Down

0 comments on commit b2c9381

Please sign in to comment.