Skip to content

Commit 8d96d99

Browse files
committed
add missing crashhandler
Signed-off-by: Tim Ramlot <[email protected]>
1 parent 13c946d commit 8d96d99

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

pkg/webhook/admission/http.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ func init() {
4242
var _ http.Handler = &Webhook{}
4343

4444
func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
45+
defer utilruntime.HandleCrash(func(_ interface{}) {
46+
// Assume the crash happened before the response was written.
47+
http.Error(w, "internal server error", http.StatusInternalServerError)
48+
})
49+
4550
ctx := r.Context()
4651
if wh.WithContextFunc != nil {
4752
ctx = wh.WithContextFunc(ctx, r)

pkg/webhook/admission/http_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,33 @@ var _ = Describe("Admission Webhooks", func() {
198198
return respRecorder.Body.Len()
199199
}, time.Second*3).Should(Equal(0))
200200
})
201+
202+
It("should handle crashes", func() {
203+
req := &http.Request{
204+
Header: http.Header{"Content-Type": []string{"application/json"}},
205+
Method: http.MethodPost,
206+
Body: nopCloser{Reader: bytes.NewBufferString(`{"spec":{"token":"foobar"}}`)},
207+
}
208+
webhook := &Webhook{
209+
Handler: &fakeHandler{
210+
fn: func(ctx context.Context, req Request) Response {
211+
panic("boom")
212+
},
213+
},
214+
}
215+
216+
expected := `internal server error
217+
`
218+
(func() {
219+
defer func() {
220+
if r := recover(); r != nil {
221+
Expect(r).To(Equal("boom"))
222+
}
223+
}()
224+
webhook.ServeHTTP(respRecorder, req)
225+
})()
226+
Expect(respRecorder.Body.String()).To(Equal(expected))
227+
})
201228
})
202229
})
203230

pkg/webhook/authentication/http.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ func init() {
4242
var _ http.Handler = &Webhook{}
4343

4444
func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
45+
defer utilruntime.HandleCrash(func(_ interface{}) {
46+
// Assume the crash happened before the response was written.
47+
http.Error(w, "internal server error", http.StatusInternalServerError)
48+
})
49+
4550
ctx := r.Context()
4651
if wh.WithContextFunc != nil {
4752
ctx = wh.WithContextFunc(ctx, r)

pkg/webhook/authentication/http_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,33 @@ var _ = Describe("Authentication Webhooks", func() {
181181
webhook.ServeHTTP(respRecorder, req.WithContext(ctx))
182182
Expect(respRecorder.Body.String()).To(Equal(expected))
183183
})
184+
185+
It("should handle crashes", func() {
186+
req := &http.Request{
187+
Header: http.Header{"Content-Type": []string{"application/json"}},
188+
Method: http.MethodPost,
189+
Body: nopCloser{Reader: bytes.NewBufferString(`{"spec":{"token":"foobar"}}`)},
190+
}
191+
webhook := &Webhook{
192+
Handler: &fakeHandler{
193+
fn: func(ctx context.Context, req Request) Response {
194+
panic("boom")
195+
},
196+
},
197+
}
198+
199+
expected := `internal server error
200+
`
201+
(func() {
202+
defer func() {
203+
if r := recover(); r != nil {
204+
Expect(r).To(Equal("boom"))
205+
}
206+
}()
207+
webhook.ServeHTTP(respRecorder, req)
208+
})()
209+
Expect(respRecorder.Body.String()).To(Equal(expected))
210+
})
184211
})
185212
})
186213

0 commit comments

Comments
 (0)