Debian Codegraph @

pkg/authentication/request/anonymous/anonymous_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 anonymous
18
19 import (
20 "net/http"
21 "net/url"
22 "testing"
23
24 "k8s.io/apimachinery/pkg/util/sets"
25 "k8s.io/apiserver/pkg/apis/apiserver"
26 "k8s.io/apiserver/pkg/authentication/user"
27 )
28
29 func TestAnonymous(t *testing.T) {
30 a := NewAuthenticator(nil)
31 r, ok, err := a.AuthenticateRequest(&http.Request{})
32 if err != nil {
33 t.Fatalf("Unexpected error %v", err)
34 }
35 if !ok {
36 t.Fatalf("Unexpectedly unauthenticated")
37 }
38 if r.User.GetName() != user.Anonymous {
39 t.Fatalf("Expected username %s, got %s", user.Anonymous, r.User.GetName())
40 }
41 if !sets.NewString(r.User.GetGroups()...).Equal(sets.NewString(user.AllUnauthenticated)) {
42 t.Fatalf("Expected group %s, got %v", user.AllUnauthenticated, r.User.GetGroups())
43 }
44 }
45
46 func TestAnonymousRestricted(t *testing.T) {
47 a := NewAuthenticator([]apiserver.AnonymousAuthCondition{
48 {
49 Path: "/healthz",
50 },
51 {
52 Path: "/readyz",
53 },
54 {
55 Path: "/livez",
56 },
57 })
58
59 testCases := []struct {
60 desc string
61 path string
62 want user.DefaultInfo
63 wantAllowed bool
64 }{
65 {
66 desc: "/healthz",
67 path: "https://123.123.123.123/healthz",
68 want: user.DefaultInfo{
69 Name: anonymousUser,
70 Groups: []string{unauthenticatedGroup},
71 },
72 wantAllowed: true,
73 },
74 {
75 desc: "/readyz",
76 path: "https://123.123.123.123/readyz",
77 want: user.DefaultInfo{
78 Name: anonymousUser,
79 Groups: []string{unauthenticatedGroup},
80 },
81 wantAllowed: true,
82 },
83 {
84 desc: "/livez",
85 path: "https://123.123.123.123/livez",
86 want: user.DefaultInfo{
87 Name: anonymousUser,
88 Groups: []string{unauthenticatedGroup},
89 },
90 wantAllowed: true,
91 },
92 {
93 desc: "/api",
94 path: "https://123.123.123.123/api",
95 wantAllowed: false,
96 },
97 }
98
99 for _, tc := range testCases {
100 t.Run(tc.desc, func(t *testing.T) {
101 u, err := url.Parse(tc.path)
102 if err != nil {
103 t.Fatal(err)
104 }
105 r, allowed, err := a.AuthenticateRequest(&http.Request{URL: u})
106 if err != nil {
107 t.Fatal(err)
108 }
109
110 if tc.wantAllowed != allowed {
111 t.Fatalf("want allowed: %v, got allowed: %v", tc.wantAllowed, allowed)
112 }
113
114 if !tc.wantAllowed {
115 return
116 }
117
118 if r.User.GetName() != tc.want.Name {
119 t.Fatalf("Expected username %s, got %s", user.Anonymous, r.User.GetName())
120 }
121 if !sets.NewString(r.User.GetGroups()...).Equal(sets.NewString(tc.want.Groups...)) {
122 t.Fatalf("Expected group %s, got %v", tc.want.Groups, r.User.GetGroups())
123 }
124 })
125 }
126 }