|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package authentication |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + "net/http" |
| 25 | + "net/http/httptest" |
| 26 | + |
| 27 | + . "github.com/onsi/ginkgo" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | + authenticationv1 "k8s.io/api/authentication/v1" |
| 30 | + |
| 31 | + logf "sigs.k8s.io/controller-runtime/pkg/internal/log" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/runtime/inject" |
| 33 | +) |
| 34 | + |
| 35 | +var _ = Describe("Authentication Webhooks", func() { |
| 36 | + |
| 37 | + const ( |
| 38 | + gvkJSONv1 = `"kind":"TokenReview","apiVersion":"authentication.k8s.io/v1"` |
| 39 | + gvkJSONv1beta1 = `"kind":"TokenReview","apiVersion":"authentication.k8s.io/v1beta1"` |
| 40 | + ) |
| 41 | + |
| 42 | + Describe("HTTP Handler", func() { |
| 43 | + var respRecorder *httptest.ResponseRecorder |
| 44 | + webhook := &Webhook{ |
| 45 | + Handler: nil, |
| 46 | + } |
| 47 | + BeforeEach(func() { |
| 48 | + respRecorder = &httptest.ResponseRecorder{ |
| 49 | + Body: bytes.NewBuffer(nil), |
| 50 | + } |
| 51 | + _, err := inject.LoggerInto(log.WithName("test-webhook"), webhook) |
| 52 | + Expect(err).NotTo(HaveOccurred()) |
| 53 | + }) |
| 54 | + |
| 55 | + It("should return bad-request when given an empty body", func() { |
| 56 | + req := &http.Request{Body: nil} |
| 57 | + |
| 58 | + expected := `{"metadata":{"creationTimestamp":null},"spec":{},"status":{"user":{},"error":"request body is empty"}} |
| 59 | +` |
| 60 | + webhook.ServeHTTP(respRecorder, req) |
| 61 | + Expect(respRecorder.Body.String()).To(Equal(expected)) |
| 62 | + }) |
| 63 | + |
| 64 | + It("should return bad-request when given the wrong content-type", func() { |
| 65 | + req := &http.Request{ |
| 66 | + Header: http.Header{"Content-Type": []string{"application/foo"}}, |
| 67 | + Method: http.MethodPost, |
| 68 | + Body: nopCloser{Reader: bytes.NewBuffer(nil)}, |
| 69 | + } |
| 70 | + |
| 71 | + expected := `{"metadata":{"creationTimestamp":null},"spec":{},"status":{"user":{},"error":"contentType=application/foo, expected application/json"}} |
| 72 | +` |
| 73 | + webhook.ServeHTTP(respRecorder, req) |
| 74 | + Expect(respRecorder.Body.String()).To(Equal(expected)) |
| 75 | + }) |
| 76 | + |
| 77 | + It("should return bad-request when given an undecodable body", func() { |
| 78 | + req := &http.Request{ |
| 79 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 80 | + Method: http.MethodPost, |
| 81 | + Body: nopCloser{Reader: bytes.NewBufferString("{")}, |
| 82 | + } |
| 83 | + |
| 84 | + expected := `{"metadata":{"creationTimestamp":null},"spec":{},"status":{"user":{},"error":"couldn't get version/kind; json parse error: unexpected end of JSON input"}} |
| 85 | +` |
| 86 | + webhook.ServeHTTP(respRecorder, req) |
| 87 | + Expect(respRecorder.Body.String()).To(Equal(expected)) |
| 88 | + }) |
| 89 | + |
| 90 | + It("should return bad-request when given an undecodable body", func() { |
| 91 | + req := &http.Request{ |
| 92 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 93 | + Method: http.MethodPost, |
| 94 | + Body: nopCloser{Reader: bytes.NewBufferString(`{"spec":{"token":""}}`)}, |
| 95 | + } |
| 96 | + |
| 97 | + expected := `{"metadata":{"creationTimestamp":null},"spec":{},"status":{"user":{},"error":"token is empty"}} |
| 98 | +` |
| 99 | + webhook.ServeHTTP(respRecorder, req) |
| 100 | + Expect(respRecorder.Body.String()).To(Equal(expected)) |
| 101 | + }) |
| 102 | + |
| 103 | + It("should return the response given by the handler with version defaulted to v1", func() { |
| 104 | + req := &http.Request{ |
| 105 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 106 | + Method: http.MethodPost, |
| 107 | + Body: nopCloser{Reader: bytes.NewBufferString(`{"spec":{"token":"foobar"}}`)}, |
| 108 | + } |
| 109 | + webhook := &Webhook{ |
| 110 | + Handler: &fakeHandler{}, |
| 111 | + log: logf.RuntimeLog.WithName("webhook"), |
| 112 | + } |
| 113 | + |
| 114 | + expected := fmt.Sprintf(`{%s,"metadata":{"creationTimestamp":null},"spec":{},"status":{"authenticated":true,"user":{}}} |
| 115 | +`, gvkJSONv1) |
| 116 | + |
| 117 | + webhook.ServeHTTP(respRecorder, req) |
| 118 | + Expect(respRecorder.Body.String()).To(Equal(expected)) |
| 119 | + }) |
| 120 | + |
| 121 | + It("should return the v1 response given by the handler", func() { |
| 122 | + req := &http.Request{ |
| 123 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 124 | + Method: http.MethodPost, |
| 125 | + Body: nopCloser{Reader: bytes.NewBufferString(fmt.Sprintf(`{%s,"spec":{"token":"foobar"}}`, gvkJSONv1))}, |
| 126 | + } |
| 127 | + webhook := &Webhook{ |
| 128 | + Handler: &fakeHandler{}, |
| 129 | + log: logf.RuntimeLog.WithName("webhook"), |
| 130 | + } |
| 131 | + |
| 132 | + expected := fmt.Sprintf(`{%s,"metadata":{"creationTimestamp":null},"spec":{},"status":{"authenticated":true,"user":{}}} |
| 133 | +`, gvkJSONv1) |
| 134 | + webhook.ServeHTTP(respRecorder, req) |
| 135 | + Expect(respRecorder.Body.String()).To(Equal(expected)) |
| 136 | + }) |
| 137 | + |
| 138 | + It("should return the v1beta1 response given by the handler", func() { |
| 139 | + req := &http.Request{ |
| 140 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 141 | + Method: http.MethodPost, |
| 142 | + Body: nopCloser{Reader: bytes.NewBufferString(fmt.Sprintf(`{%s,"spec":{"token":"foobar"}}`, gvkJSONv1beta1))}, |
| 143 | + } |
| 144 | + webhook := &Webhook{ |
| 145 | + Handler: &fakeHandler{}, |
| 146 | + log: logf.RuntimeLog.WithName("webhook"), |
| 147 | + } |
| 148 | + |
| 149 | + expected := fmt.Sprintf(`{%s,"metadata":{"creationTimestamp":null},"spec":{},"status":{"authenticated":true,"user":{}}} |
| 150 | +`, gvkJSONv1beta1) |
| 151 | + webhook.ServeHTTP(respRecorder, req) |
| 152 | + Expect(respRecorder.Body.String()).To(Equal(expected)) |
| 153 | + }) |
| 154 | + |
| 155 | + It("should present the Context from the HTTP request, if any", func() { |
| 156 | + req := &http.Request{ |
| 157 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 158 | + Method: http.MethodPost, |
| 159 | + Body: nopCloser{Reader: bytes.NewBufferString(`{"spec":{"token":"foobar"}}`)}, |
| 160 | + } |
| 161 | + type ctxkey int |
| 162 | + const key ctxkey = 1 |
| 163 | + const value = "from-ctx" |
| 164 | + webhook := &Webhook{ |
| 165 | + Handler: &fakeHandler{ |
| 166 | + fn: func(ctx context.Context, req Request) Response { |
| 167 | + <-ctx.Done() |
| 168 | + return Authenticated(ctx.Value(key).(string), authenticationv1.UserInfo{}) |
| 169 | + }, |
| 170 | + }, |
| 171 | + log: logf.RuntimeLog.WithName("webhook"), |
| 172 | + } |
| 173 | + |
| 174 | + expected := fmt.Sprintf(`{%s,"metadata":{"creationTimestamp":null},"spec":{},"status":{"authenticated":true,"user":{},"error":%q}} |
| 175 | +`, gvkJSONv1, value) |
| 176 | + |
| 177 | + ctx, cancel := context.WithCancel(context.WithValue(context.Background(), key, value)) |
| 178 | + cancel() |
| 179 | + webhook.ServeHTTP(respRecorder, req.WithContext(ctx)) |
| 180 | + Expect(respRecorder.Body.String()).To(Equal(expected)) |
| 181 | + }) |
| 182 | + |
| 183 | + It("should mutate the Context from the HTTP request, if func supplied", func() { |
| 184 | + req := &http.Request{ |
| 185 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 186 | + Method: http.MethodPost, |
| 187 | + Body: nopCloser{Reader: bytes.NewBufferString(`{"spec":{"token":"foobar"}}`)}, |
| 188 | + } |
| 189 | + type ctxkey int |
| 190 | + const key ctxkey = 1 |
| 191 | + webhook := &Webhook{ |
| 192 | + Handler: &fakeHandler{ |
| 193 | + fn: func(ctx context.Context, req Request) Response { |
| 194 | + return Authenticated(ctx.Value(key).(string), authenticationv1.UserInfo{}) |
| 195 | + }, |
| 196 | + }, |
| 197 | + WithContextFunc: func(ctx context.Context, r *http.Request) context.Context { |
| 198 | + return context.WithValue(ctx, key, r.Header["Content-Type"][0]) |
| 199 | + }, |
| 200 | + log: logf.RuntimeLog.WithName("webhook"), |
| 201 | + } |
| 202 | + |
| 203 | + expected := fmt.Sprintf(`{%s,"metadata":{"creationTimestamp":null},"spec":{},"status":{"authenticated":true,"user":{},"error":%q}} |
| 204 | +`, gvkJSONv1, "application/json") |
| 205 | + |
| 206 | + ctx, cancel := context.WithCancel(context.Background()) |
| 207 | + cancel() |
| 208 | + webhook.ServeHTTP(respRecorder, req.WithContext(ctx)) |
| 209 | + Expect(respRecorder.Body.String()).To(Equal(expected)) |
| 210 | + }) |
| 211 | + }) |
| 212 | +}) |
| 213 | + |
| 214 | +type nopCloser struct { |
| 215 | + io.Reader |
| 216 | +} |
| 217 | + |
| 218 | +func (nopCloser) Close() error { return nil } |
| 219 | + |
| 220 | +type fakeHandler struct { |
| 221 | + invoked bool |
| 222 | + fn func(context.Context, Request) Response |
| 223 | + injectedString string |
| 224 | +} |
| 225 | + |
| 226 | +func (h *fakeHandler) InjectString(s string) error { |
| 227 | + h.injectedString = s |
| 228 | + return nil |
| 229 | +} |
| 230 | + |
| 231 | +func (h *fakeHandler) Handle(ctx context.Context, req Request) Response { |
| 232 | + h.invoked = true |
| 233 | + if h.fn != nil { |
| 234 | + return h.fn(ctx, req) |
| 235 | + } |
| 236 | + return Response{TokenReview: authenticationv1.TokenReview{ |
| 237 | + Status: authenticationv1.TokenReviewStatus{ |
| 238 | + Authenticated: true, |
| 239 | + }, |
| 240 | + }} |
| 241 | +} |
0 commit comments