Debian Codegraph @

plugin/pkg/authorizer/webhook/webhook_v1beta1_test.go

raw
1 /*
2 Copyright 2016 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 webhook
18
19 import (
20 "context"
21 "crypto/tls"
22 "crypto/x509"
23 "encoding/json"
24 "fmt"
25 "io/ioutil"
26 "net/http"
27 "net/http/httptest"
28 "net/url"
29 "os"
30 "path/filepath"
31 "reflect"
32 "strings"
33 "testing"
34 "text/template"
35 "time"
36
37 "github.com/google/go-cmp/cmp"
38
39 authorizationv1beta1 "k8s.io/api/authorization/v1beta1"
40 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
41 authzconfig "k8s.io/apiserver/pkg/apis/apiserver"
42 "k8s.io/apiserver/pkg/authentication/user"
43 "k8s.io/apiserver/pkg/authorization/authorizer"
44 authorizationcel "k8s.io/apiserver/pkg/authorization/cel"
45 webhookutil "k8s.io/apiserver/pkg/util/webhook"
46 v1 "k8s.io/client-go/tools/clientcmd/api/v1"
47 )
48
49 func TestV1beta1NewFromConfig(t *testing.T) {
50 dir, err := ioutil.TempDir("", "")
51 if err != nil {
52 t.Fatal(err)
53 }
54 defer os.RemoveAll(dir)
55
56 data := struct {
57 CA string
58 Cert string
59 Key string
60 }{
61 CA: filepath.Join(dir, "ca.pem"),
62 Cert: filepath.Join(dir, "clientcert.pem"),
63 Key: filepath.Join(dir, "clientkey.pem"),
64 }
65
66 files := []struct {
67 name string
68 data []byte
69 }{
70 {data.CA, caCert},
71 {data.Cert, clientCert},
72 {data.Key, clientKey},
73 }
74 for _, file := range files {
75 if err := ioutil.WriteFile(file.name, file.data, 0400); err != nil {
76 t.Fatal(err)
77 }
78 }
79
80 tests := []struct {
81 msg string
82 configTmpl string
83 wantErr bool
84 }{
85 {
86 msg: "a single cluster and single user",
87 configTmpl: `
88 clusters:
89 - cluster:
90 certificate-authority: {{ .CA }}
91 server: https://authz.example.com
92 name: foobar
93 users:
94 - name: a cluster
95 user:
96 client-certificate: {{ .Cert }}
97 client-key: {{ .Key }}
98 `,
99 wantErr: true,
100 },
101 {
102 msg: "multiple clusters with no context",
103 configTmpl: `
104 clusters:
105 - cluster:
106 certificate-authority: {{ .CA }}
107 server: https://authz.example.com
108 name: foobar
109 - cluster:
110 certificate-authority: a bad certificate path
111 server: https://authz.example.com
112 name: barfoo
113 users:
114 - name: a name
115 user:
116 client-certificate: {{ .Cert }}
117 client-key: {{ .Key }}
118 `,
119 wantErr: true,
120 },
121 {
122 msg: "multiple clusters with a context",
123 configTmpl: `
124 clusters:
125 - cluster:
126 certificate-authority: a bad certificate path
127 server: https://authz.example.com
128 name: foobar
129 - cluster:
130 certificate-authority: {{ .CA }}
131 server: https://authz.example.com
132 name: barfoo
133 users:
134 - name: a name
135 user:
136 client-certificate: {{ .Cert }}
137 client-key: {{ .Key }}
138 contexts:
139 - name: default
140 context:
141 cluster: barfoo
142 user: a name
143 current-context: default
144 `,
145 wantErr: false,
146 },
147 {
148 msg: "cluster with bad certificate path specified",
149 configTmpl: `
150 clusters:
151 - cluster:
152 certificate-authority: a bad certificate path
153 server: https://authz.example.com
154 name: foobar
155 - cluster:
156 certificate-authority: {{ .CA }}
157 server: https://authz.example.com
158 name: barfoo
159 users:
160 - name: a name
161 user:
162 client-certificate: {{ .Cert }}
163 client-key: {{ .Key }}
164 contexts:
165 - name: default
166 context:
167 cluster: foobar
168 user: a name
169 current-context: default
170 `,
171 wantErr: true,
172 },
173 }
174
175 for _, tt := range tests {
176 // Use a closure so defer statements trigger between loop iterations.
177 err := func() error {
178 tempfile, err := ioutil.TempFile("", "")
179 if err != nil {
180 return err
181 }
182 p := tempfile.Name()
183 defer os.Remove(p)
184
185 tmpl, err := template.New("test").Parse(tt.configTmpl)
186 if err != nil {
187 return fmt.Errorf("failed to parse test template: %v", err)
188 }
189 if err := tmpl.Execute(tempfile, data); err != nil {
190 return fmt.Errorf("failed to execute test template: %v", err)
191 }
192 // Create a new authorizer
193 clientConfig, err := webhookutil.LoadKubeconfig(p, nil)
194 if err != nil {
195 return err
196 }
197 sarClient, err := subjectAccessReviewInterfaceFromConfig(clientConfig, "v1beta1", testRetryBackoff)
198 if err != nil {
199 return fmt.Errorf("error building sar client: %v", err)
200 }
201 _, err = newWithBackoff(sarClient, 0, 0, testRetryBackoff, authorizer.DecisionNoOpinion, []authzconfig.WebhookMatchCondition{}, noopAuthorizerMetrics(), authorizationcel.NewDefaultCompiler(), "")
202 return err
203 }()
204 if err != nil && !tt.wantErr {
205 t.Errorf("failed to load plugin from config %q: %v", tt.msg, err)
206 }
207 if err == nil && tt.wantErr {
208 t.Errorf("wanted an error when loading config, did not get one: %q", tt.msg)
209 }
210 }
211 }
212
213 // V1beta1Service mocks a remote service.
214 type V1beta1Service interface {
215 Review(*authorizationv1beta1.SubjectAccessReview)
216 HTTPStatusCode() int
217 }
218
219 // NewV1beta1TestServer wraps a V1beta1Service as an httptest.Server.
220 func NewV1beta1TestServer(s V1beta1Service, cert, key, caCert []byte) (*httptest.Server, error) {
221 const webhookPath = "/testserver"
222 var tlsConfig *tls.Config
223 if cert != nil {
224 cert, err := tls.X509KeyPair(cert, key)
225 if err != nil {
226 return nil, err
227 }
228 tlsConfig = &tls.Config{Certificates: []tls.Certificate{cert}}
229 }
230
231 if caCert != nil {
232 rootCAs := x509.NewCertPool()
233 rootCAs.AppendCertsFromPEM(caCert)
234 if tlsConfig == nil {
235 tlsConfig = &tls.Config{}
236 }
237 tlsConfig.ClientCAs = rootCAs
238 tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
239 }
240
241 serveHTTP := func(w http.ResponseWriter, r *http.Request) {
242 if r.Method != "POST" {
243 http.Error(w, fmt.Sprintf("unexpected method: %v", r.Method), http.StatusMethodNotAllowed)
244 return
245 }
246 if r.URL.Path != webhookPath {
247 http.Error(w, fmt.Sprintf("unexpected path: %v", r.URL.Path), http.StatusNotFound)
248 return
249 }
250
251 var review authorizationv1beta1.SubjectAccessReview
252 bodyData, _ := ioutil.ReadAll(r.Body)
253 if err := json.Unmarshal(bodyData, &review); err != nil {
254 http.Error(w, fmt.Sprintf("failed to decode body: %v", err), http.StatusBadRequest)
255 return
256 }
257
258 // ensure we received the serialized review as expected
259 if review.APIVersion != "authorization.k8s.io/v1beta1" {
260 http.Error(w, fmt.Sprintf("wrong api version: %s", string(bodyData)), http.StatusBadRequest)
261 return
262 }
263 // once we have a successful request, always call the review to record that we were called
264 s.Review(&review)
265 if s.HTTPStatusCode() < 200 || s.HTTPStatusCode() >= 300 {
266 http.Error(w, "HTTP Error", s.HTTPStatusCode())
267 return
268 }
269 type status struct {
270 Allowed bool `json:"allowed"`
271 Reason string `json:"reason"`
272 EvaluationError string `json:"evaluationError"`
273 }
274 resp := struct {
275 APIVersion string `json:"apiVersion"`
276 Status status `json:"status"`
277 }{
278 APIVersion: authorizationv1beta1.SchemeGroupVersion.String(),
279 Status: status{review.Status.Allowed, review.Status.Reason, review.Status.EvaluationError},
280 }
281 w.Header().Set("Content-Type", "application/json")
282 json.NewEncoder(w).Encode(resp)
283 }
284
285 server := httptest.NewUnstartedServer(http.HandlerFunc(serveHTTP))
286 server.TLS = tlsConfig
287 server.StartTLS()
288
289 // Adjust the path to point to our custom path
290 serverURL, _ := url.Parse(server.URL)
291 serverURL.Path = webhookPath
292 server.URL = serverURL.String()
293
294 return server, nil
295 }
296
297 // A service that can be set to allow all or deny all authorization requests.
298 type mockV1beta1Service struct {
299 allow bool
300 statusCode int
301 called int
302 }
303
304 func (m *mockV1beta1Service) Review(r *authorizationv1beta1.SubjectAccessReview) {
305 m.called++
306 r.Status.Allowed = m.allow
307 }
308 func (m *mockV1beta1Service) Allow() { m.allow = true }
309 func (m *mockV1beta1Service) Deny() { m.allow = false }
310 func (m *mockV1beta1Service) HTTPStatusCode() int { return m.statusCode }
311
312 // newV1beta1Authorizer creates a temporary kubeconfig file from the provided arguments and attempts to load
313 // a new WebhookAuthorizer from it.
314 func newV1beta1Authorizer(callbackURL string, clientCert, clientKey, ca []byte, cacheTime time.Duration) (*WebhookAuthorizer, error) {
315 tempfile, err := ioutil.TempFile("", "")
316 if err != nil {
317 return nil, err
318 }
319 p := tempfile.Name()
320 defer os.Remove(p)
321 config := v1.Config{
322 Clusters: []v1.NamedCluster{
323 {
324 Cluster: v1.Cluster{Server: callbackURL, CertificateAuthorityData: ca},
325 },
326 },
327 AuthInfos: []v1.NamedAuthInfo{
328 {
329 AuthInfo: v1.AuthInfo{ClientCertificateData: clientCert, ClientKeyData: clientKey},
330 },
331 },
332 }
333 if err := json.NewEncoder(tempfile).Encode(config); err != nil {
334 return nil, err
335 }
336 clientConfig, err := webhookutil.LoadKubeconfig(p, nil)
337 if err != nil {
338 return nil, err
339 }
340 sarClient, err := subjectAccessReviewInterfaceFromConfig(clientConfig, "v1beta1", testRetryBackoff)
341 if err != nil {
342 return nil, fmt.Errorf("error building sar client: %v", err)
343 }
344 return newWithBackoff(sarClient, cacheTime, cacheTime, testRetryBackoff, authorizer.DecisionNoOpinion, []authzconfig.WebhookMatchCondition{}, noopAuthorizerMetrics(), authorizationcel.NewDefaultCompiler(), "")
345 }
346
347 func TestV1beta1TLSConfig(t *testing.T) {
348 tests := []struct {
349 test string
350 clientCert, clientKey, clientCA []byte
351 serverCert, serverKey, serverCA []byte
352 wantAuth, wantErr bool
353 }{
354 {
355 test: "TLS setup between client and server",
356 clientCert: clientCert, clientKey: clientKey, clientCA: caCert,
357 serverCert: serverCert, serverKey: serverKey, serverCA: caCert,
358 wantAuth: true,
359 },
360 {
361 test: "Server does not require client auth",
362 clientCA: caCert,
363 serverCert: serverCert, serverKey: serverKey,
364 wantAuth: true,
365 },
366 {
367 test: "Server does not require client auth, client provides it",
368 clientCert: clientCert, clientKey: clientKey, clientCA: caCert,
369 serverCert: serverCert, serverKey: serverKey,
370 wantAuth: true,
371 },
372 {
373 test: "Client does not trust server",
374 clientCert: clientCert, clientKey: clientKey,
375 serverCert: serverCert, serverKey: serverKey,
376 wantErr: true,
377 },
378 {
379 test: "Server does not trust client",
380 clientCert: clientCert, clientKey: clientKey, clientCA: caCert,
381 serverCert: serverCert, serverKey: serverKey, serverCA: badCACert,
382 wantErr: true,
383 },
384 {
385 // Plugin does not support insecure configurations.
386 test: "Server is using insecure connection",
387 wantErr: true,
388 },
389 }
390 for _, tt := range tests {
391 // Use a closure so defer statements trigger between loop iterations.
392 func() {
393 service := new(mockV1beta1Service)
394 service.statusCode = 200
395
396 server, err := NewV1beta1TestServer(service, tt.serverCert, tt.serverKey, tt.serverCA)
397 if err != nil {
398 t.Errorf("%s: failed to create server: %v", tt.test, err)
399 return
400 }
401 defer server.Close()
402
403 wh, err := newV1beta1Authorizer(server.URL, tt.clientCert, tt.clientKey, tt.clientCA, 0)
404 if err != nil {
405 t.Errorf("%s: failed to create client: %v", tt.test, err)
406 return
407 }
408
409 attr := authorizer.AttributesRecord{User: &user.DefaultInfo{}}
410
411 // Allow all and see if we get an error.
412 service.Allow()
413 decision, _, err := wh.Authorize(context.Background(), attr)
414 if tt.wantAuth {
415 if decision != authorizer.DecisionAllow {
416 t.Errorf("expected successful authorization")
417 }
418 } else {
419 if decision == authorizer.DecisionAllow {
420 t.Errorf("expected failed authorization")
421 }
422 }
423 if tt.wantErr {
424 if err == nil {
425 t.Errorf("expected error making authorization request: %v", err)
426 }
427 return
428 }
429 if err != nil {
430 t.Errorf("%s: failed to authorize with AllowAll policy: %v", tt.test, err)
431 return
432 }
433
434 service.Deny()
435 if decision, _, _ := wh.Authorize(context.Background(), attr); decision == authorizer.DecisionAllow {
436 t.Errorf("%s: incorrectly authorized with DenyAll policy", tt.test)
437 }
438 }()
439 }
440 }
441
442 // recorderV1beta1Service records all access review requests.
443 type recorderV1beta1Service struct {
444 last authorizationv1beta1.SubjectAccessReview
445 err error
446 }
447
448 func (rec *recorderV1beta1Service) Review(r *authorizationv1beta1.SubjectAccessReview) {
449 rec.last = authorizationv1beta1.SubjectAccessReview{}
450 rec.last = *r
451 r.Status.Allowed = true
452 }
453
454 func (rec *recorderV1beta1Service) Last() (authorizationv1beta1.SubjectAccessReview, error) {
455 return rec.last, rec.err
456 }
457
458 func (rec *recorderV1beta1Service) HTTPStatusCode() int { return 200 }
459
460 func TestV1beta1Webhook(t *testing.T) {
461 serv := new(recorderV1beta1Service)
462 s, err := NewV1beta1TestServer(serv, serverCert, serverKey, caCert)
463 if err != nil {
464 t.Fatal(err)
465 }
466 defer s.Close()
467
468 wh, err := newV1beta1Authorizer(s.URL, clientCert, clientKey, caCert, 0)
469 if err != nil {
470 t.Fatal(err)
471 }
472
473 expTypeMeta := metav1.TypeMeta{
474 APIVersion: "authorization.k8s.io/v1beta1",
475 Kind: "SubjectAccessReview",
476 }
477
478 tests := []struct {
479 attr authorizer.Attributes
480 want authorizationv1beta1.SubjectAccessReview
481 }{
482 {
483 attr: authorizer.AttributesRecord{User: &user.DefaultInfo{}},
484 want: authorizationv1beta1.SubjectAccessReview{
485 TypeMeta: expTypeMeta,
486 Spec: authorizationv1beta1.SubjectAccessReviewSpec{
487 NonResourceAttributes: &authorizationv1beta1.NonResourceAttributes{},
488 },
489 },
490 },
491 {
492 attr: authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "jane"}},
493 want: authorizationv1beta1.SubjectAccessReview{
494 TypeMeta: expTypeMeta,
495 Spec: authorizationv1beta1.SubjectAccessReviewSpec{
496 User: "jane",
497 NonResourceAttributes: &authorizationv1beta1.NonResourceAttributes{},
498 },
499 },
500 },
501 {
502 attr: authorizer.AttributesRecord{
503 User: &user.DefaultInfo{
504 Name: "jane",
505 UID: "1",
506 Groups: []string{"group1", "group2"},
507 },
508 Verb: "GET",
509 Namespace: "kittensandponies",
510 APIGroup: "group3",
511 APIVersion: "v7beta3",
512 Resource: "pods",
513 Subresource: "proxy",
514 Name: "my-pod",
515 ResourceRequest: true,
516 Path: "/foo",
517 },
518 want: authorizationv1beta1.SubjectAccessReview{
519 TypeMeta: expTypeMeta,
520 Spec: authorizationv1beta1.SubjectAccessReviewSpec{
521 User: "jane",
522 UID: "1",
523 Groups: []string{"group1", "group2"},
524 ResourceAttributes: &authorizationv1beta1.ResourceAttributes{
525 Verb: "GET",
526 Namespace: "kittensandponies",
527 Group: "group3",
528 Version: "v7beta3",
529 Resource: "pods",
530 Subresource: "proxy",
531 Name: "my-pod",
532 },
533 },
534 },
535 },
536 }
537
538 for i, tt := range tests {
539 decision, _, err := wh.Authorize(context.Background(), tt.attr)
540 if err != nil {
541 t.Fatal(err)
542 }
543 if decision != authorizer.DecisionAllow {
544 t.Errorf("case %d: authorization failed", i)
545 continue
546 }
547
548 gotAttr, err := serv.Last()
549 if err != nil {
550 t.Errorf("case %d: failed to deserialize webhook request: %v", i, err)
551 continue
552 }
553 if !reflect.DeepEqual(gotAttr, tt.want) {
554 t.Errorf("case %d: got != want:\n%s", i, cmp.Diff(gotAttr, tt.want))
555 }
556 }
557 }
558
559 // TestWebhookCache verifies that error responses from the server are not
560 // cached, but successful responses are.
561 func TestV1beta1WebhookCache(t *testing.T) {
562 serv := new(mockV1beta1Service)
563 s, err := NewV1beta1TestServer(serv, serverCert, serverKey, caCert)
564 if err != nil {
565 t.Fatal(err)
566 }
567 defer s.Close()
568
569 // Create an authorizer that caches successful responses "forever" (100 days).
570 wh, err := newV1beta1Authorizer(s.URL, clientCert, clientKey, caCert, 2400*time.Hour)
571 if err != nil {
572 t.Fatal(err)
573 }
574
575 aliceAttr := authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "alice"}}
576 bobAttr := authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "bob"}}
577 aliceRidiculousAttr := authorizer.AttributesRecord{
578 User: &user.DefaultInfo{Name: "alice"},
579 ResourceRequest: true,
580 Verb: strings.Repeat("v", 2000),
581 APIGroup: strings.Repeat("g", 2000),
582 APIVersion: strings.Repeat("a", 2000),
583 Resource: strings.Repeat("r", 2000),
584 Name: strings.Repeat("n", 2000),
585 }
586 bobRidiculousAttr := authorizer.AttributesRecord{
587 User: &user.DefaultInfo{Name: "bob"},
588 ResourceRequest: true,
589 Verb: strings.Repeat("v", 2000),
590 APIGroup: strings.Repeat("g", 2000),
591 APIVersion: strings.Repeat("a", 2000),
592 Resource: strings.Repeat("r", 2000),
593 Name: strings.Repeat("n", 2000),
594 }
595
596 type webhookCacheTestCase struct {
597 name string
598
599 attr authorizer.AttributesRecord
600
601 allow bool
602 statusCode int
603
604 expectedErr bool
605 expectedAuthorized bool
606 expectedCalls int
607 }
608
609 tests := []webhookCacheTestCase{
610 // server error and 429's retry
611 {name: "server errors retry", attr: aliceAttr, allow: false, statusCode: 500, expectedErr: true, expectedAuthorized: false, expectedCalls: 5},
612 {name: "429s retry", attr: aliceAttr, allow: false, statusCode: 429, expectedErr: true, expectedAuthorized: false, expectedCalls: 5},
613 // regular errors return errors but do not retry
614 {name: "404 doesnt retry", attr: aliceAttr, allow: false, statusCode: 404, expectedErr: true, expectedAuthorized: false, expectedCalls: 1},
615 {name: "403 doesnt retry", attr: aliceAttr, allow: false, statusCode: 403, expectedErr: true, expectedAuthorized: false, expectedCalls: 1},
616 {name: "401 doesnt retry", attr: aliceAttr, allow: false, statusCode: 401, expectedErr: true, expectedAuthorized: false, expectedCalls: 1},
617 // successful responses are cached
618 {name: "alice successful request", attr: aliceAttr, allow: true, statusCode: 200, expectedErr: false, expectedAuthorized: true, expectedCalls: 1},
619 // later requests within the cache window don't hit the backend
620 {name: "alice cached request", attr: aliceAttr, allow: false, statusCode: 500, expectedErr: false, expectedAuthorized: true, expectedCalls: 0},
621
622 // a request with different attributes doesn't hit the cache
623 {name: "bob failed request", attr: bobAttr, allow: false, statusCode: 500, expectedErr: true, expectedAuthorized: false, expectedCalls: 5},
624 // successful response for other attributes is cached
625 {name: "bob unauthorized request", attr: bobAttr, allow: false, statusCode: 200, expectedErr: false, expectedAuthorized: false, expectedCalls: 1},
626 // later requests within the cache window don't hit the backend
627 {name: "bob unauthorized cached request", attr: bobAttr, allow: false, statusCode: 500, expectedErr: false, expectedAuthorized: false, expectedCalls: 0},
628 // ridiculous unauthorized requests are not cached.
629 {name: "ridiculous unauthorized request", attr: bobRidiculousAttr, allow: false, statusCode: 200, expectedErr: false, expectedAuthorized: false, expectedCalls: 1},
630 // later ridiculous requests within the cache window still hit the backend
631 {name: "ridiculous unauthorized request again", attr: bobRidiculousAttr, allow: false, statusCode: 200, expectedErr: false, expectedAuthorized: false, expectedCalls: 1},
632 // ridiculous authorized requests are not cached.
633 {name: "ridiculous authorized request", attr: aliceRidiculousAttr, allow: true, statusCode: 200, expectedErr: false, expectedAuthorized: true, expectedCalls: 1},
634 // later ridiculous requests within the cache window still hit the backend
635 {name: "ridiculous authorized request again", attr: aliceRidiculousAttr, allow: true, statusCode: 200, expectedErr: false, expectedAuthorized: true, expectedCalls: 1},
636 }
637
638 for i, test := range tests {
639 t.Run(test.name, func(t *testing.T) {
640 serv.called = 0
641 serv.allow = test.allow
642 serv.statusCode = test.statusCode
643 authorized, _, err := wh.Authorize(context.Background(), test.attr)
644 if test.expectedErr && err == nil {
645 t.Fatalf("%d: Expected error", i)
646 } else if !test.expectedErr && err != nil {
647 t.Fatalf("%d: unexpected error: %v", i, err)
648 }
649
650 if test.expectedAuthorized != (authorized == authorizer.DecisionAllow) {
651 t.Errorf("%d: expected authorized=%v, got %v", i, test.expectedAuthorized, authorized)
652 }
653
654 if test.expectedCalls != serv.called {
655 t.Errorf("%d: expected %d calls, got %d", i, test.expectedCalls, serv.called)
656 }
657 })
658 }
659 }