Debian Codegraph @

pkg/authentication/request/headerrequest/requestheader_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 headerrequest
18
19 import (
20 "net/http"
21 "reflect"
22 "testing"
23
24 "github.com/google/go-cmp/cmp"
25
26 "k8s.io/apiserver/pkg/authentication/user"
27 )
28
29 func TestRequestHeader(t *testing.T) {
30 testcases := map[string]struct {
31 nameHeaders []string
32 uidHeaders []string
33 groupHeaders []string
34 extraPrefixHeaders []string
35 requestHeaders http.Header
36 finalHeaders http.Header
37
38 expectedUser user.Info
39 expectedOk bool
40 }{
41 "empty": {},
42 "user no match": {
43 nameHeaders: []string{"X-Remote-User"},
44 },
45 "user match": {
46 nameHeaders: []string{"X-Remote-User"},
47 requestHeaders: http.Header{"X-Remote-User": {"Bob"}},
48 expectedUser: &user.DefaultInfo{
49 Name: "Bob",
50 Groups: []string{},
51 Extra: map[string][]string{},
52 },
53 expectedOk: true,
54 },
55 "user exact match": {
56 nameHeaders: []string{"X-Remote-User"},
57 requestHeaders: http.Header{
58 "Prefixed-X-Remote-User-With-Suffix": {"Bob"},
59 "X-Remote-User-With-Suffix": {"Bob"},
60 },
61 },
62 "user first match": {
63 nameHeaders: []string{
64 "X-Remote-User",
65 "A-Second-X-Remote-User",
66 "Another-X-Remote-User",
67 },
68 requestHeaders: http.Header{
69 "X-Remote-User": {"", "First header, second value"},
70 "A-Second-X-Remote-User": {"Second header, first value", "Second header, second value"},
71 "Another-X-Remote-User": {"Third header, first value"}},
72 expectedUser: &user.DefaultInfo{
73 Name: "Second header, first value",
74 Groups: []string{},
75 Extra: map[string][]string{},
76 },
77 expectedOk: true,
78 },
79 "user case-insensitive": {
80 nameHeaders: []string{"x-REMOTE-user"}, // configured headers can be case-insensitive
81 requestHeaders: http.Header{"X-Remote-User": {"Bob"}}, // the parsed headers are normalized by the http package
82 expectedUser: &user.DefaultInfo{
83 Name: "Bob",
84 Groups: []string{},
85 Extra: map[string][]string{},
86 },
87 expectedOk: true,
88 },
89
90 "groups none": {
91 nameHeaders: []string{"X-Remote-User"},
92 groupHeaders: []string{"X-Remote-Group"},
93 requestHeaders: http.Header{
94 "X-Remote-User": {"Bob"},
95 },
96 expectedUser: &user.DefaultInfo{
97 Name: "Bob",
98 Groups: []string{},
99 Extra: map[string][]string{},
100 },
101 expectedOk: true,
102 },
103 "groups all matches": {
104 nameHeaders: []string{"X-Remote-User"},
105 groupHeaders: []string{"X-Remote-Group-1", "X-Remote-Group-2"},
106 requestHeaders: http.Header{
107 "X-Remote-User": {"Bob"},
108 "X-Remote-Group-1": {"one-a", "one-b"},
109 "X-Remote-Group-2": {"two-a", "two-b"},
110 },
111 expectedUser: &user.DefaultInfo{
112 Name: "Bob",
113 Groups: []string{"one-a", "one-b", "two-a", "two-b"},
114 Extra: map[string][]string{},
115 },
116 expectedOk: true,
117 },
118 "groups case-insensitive": {
119 nameHeaders: []string{"X-REMOTE-User"},
120 groupHeaders: []string{"X-REMOTE-Group"},
121 requestHeaders: http.Header{
122 "X-Remote-User": {"Bob"},
123 "X-Remote-Group": {"Users"},
124 },
125 expectedUser: &user.DefaultInfo{
126 Name: "Bob",
127 Groups: []string{"Users"},
128 Extra: map[string][]string{},
129 },
130 expectedOk: true,
131 },
132 "uid none": {
133 nameHeaders: []string{"X-Remote-User"},
134 uidHeaders: []string{"X-Remote-Uid"},
135 requestHeaders: http.Header{
136 "X-Remote-User": {"Bob"},
137 },
138 expectedUser: &user.DefaultInfo{
139 Name: "Bob",
140 UID: "",
141 Groups: []string{},
142 Extra: map[string][]string{},
143 },
144 expectedOk: true,
145 },
146 "uid exact match": {
147 nameHeaders: []string{"X-Remote-User"},
148 uidHeaders: []string{"X-Remote-Uid"},
149 requestHeaders: http.Header{
150 "X-Remote-User": {"Bob"},
151 // The keys in http.Header MUST be http.CanonicalHeaderKey.
152 // Hence X-Remote-Uid-1 instead of X-Remote-UID-1.
153 "X-Remote-Uid-1": {"8f5ea9d1-a5ed-4d02-80a2-26709216350b"},
154 "X-Remote-Uid-2": {"c7644180-c774-4a9b-81e5-3eef76f087ab"},
155 },
156 finalHeaders: http.Header{
157 "X-Remote-Uid-1": {"8f5ea9d1-a5ed-4d02-80a2-26709216350b"},
158 "X-Remote-Uid-2": {"c7644180-c774-4a9b-81e5-3eef76f087ab"},
159 },
160 expectedUser: &user.DefaultInfo{
161 Name: "Bob",
162 UID: "",
163 Groups: []string{},
164 Extra: map[string][]string{},
165 },
166 expectedOk: true,
167 },
168 "uid first match": {
169 nameHeaders: []string{"X-Remote-User"},
170 uidHeaders: []string{"X-Remote-Uid-1", "X-Remote-Uid-2"},
171 requestHeaders: http.Header{
172 "X-Remote-User": {"Bob"},
173 "X-Remote-Uid-1": {"8f5ea9d1-a5ed-4d02-80a2-26709216350b"},
174 "X-Remote-Uid-2": {"c7644180-c774-4a9b-81e5-3eef76f087ab"},
175 },
176 expectedUser: &user.DefaultInfo{
177 Name: "Bob",
178 UID: "8f5ea9d1-a5ed-4d02-80a2-26709216350b",
179 Groups: []string{},
180 Extra: map[string][]string{},
181 },
182 expectedOk: true,
183 },
184 "extra prefix matches case-insensitive": {
185 nameHeaders: []string{"X-Remote-User"},
186 uidHeaders: []string{"X-Remote-UID"},
187 groupHeaders: []string{"X-Remote-Group-1", "X-Remote-Group-2"},
188 extraPrefixHeaders: []string{"X-Remote-Extra-1-", "X-Remote-Extra-2-"},
189 requestHeaders: http.Header{
190 "X-Remote-User": {"Bob"},
191 "X-Remote-Uid": {"2ca80fb0-60ea-4ecf-951c-89af843b0402"},
192 "X-Remote-Group-1": {"one-a", "one-b"},
193 "X-Remote-Group-2": {"two-a", "two-b"},
194 "X-Remote-extra-1-key1": {"alfa", "bravo"},
195 "X-Remote-Extra-1-Key2": {"charlie", "delta"},
196 "X-Remote-Extra-1-": {"india", "juliet"},
197 "X-Remote-extra-2-": {"kilo", "lima"},
198 "X-Remote-extra-2-Key1": {"echo", "foxtrot"},
199 "X-Remote-Extra-2-key2": {"golf", "hotel"},
200 },
201 expectedUser: &user.DefaultInfo{
202 Name: "Bob",
203 UID: "2ca80fb0-60ea-4ecf-951c-89af843b0402",
204 Groups: []string{"one-a", "one-b", "two-a", "two-b"},
205 Extra: map[string][]string{
206 "key1": {"alfa", "bravo", "echo", "foxtrot"},
207 "key2": {"charlie", "delta", "golf", "hotel"},
208 "": {"india", "juliet", "kilo", "lima"},
209 },
210 },
211 expectedOk: true,
212 },
213
214 "extra prefix matches case-insensitive with unrelated headers": {
215 nameHeaders: []string{"X-Remote-User"},
216 groupHeaders: []string{"X-Remote-Group-1", "X-Remote-Group-2"},
217 extraPrefixHeaders: []string{"X-Remote-Extra-1-", "X-Remote-Extra-2-"},
218 requestHeaders: http.Header{
219 "X-Group-Remote": {"snorlax"}, // unrelated header
220 "X-Group-Bear": {"panda"}, // another unrelated header
221 "X-Remote-User": {"Bob"},
222 "X-Remote-Group-1": {"one-a", "one-b"},
223 "X-Remote-Group-2": {"two-a", "two-b"},
224 "X-Remote-extra-1-key1": {"alfa", "bravo"},
225 "X-Remote-Extra-1-Key2": {"charlie", "delta"},
226 "X-Remote-Extra-1-": {"india", "juliet"},
227 "X-Remote-extra-2-": {"kilo", "lima"},
228 "X-Remote-extra-2-Key1": {"echo", "foxtrot"},
229 "X-Remote-Extra-2-key2": {"golf", "hotel"},
230 },
231 finalHeaders: http.Header{
232 "X-Group-Remote": {"snorlax"},
233 "X-Group-Bear": {"panda"},
234 },
235 expectedUser: &user.DefaultInfo{
236 Name: "Bob",
237 Groups: []string{"one-a", "one-b", "two-a", "two-b"},
238 Extra: map[string][]string{
239 "key1": {"alfa", "bravo", "echo", "foxtrot"},
240 "key2": {"charlie", "delta", "golf", "hotel"},
241 "": {"india", "juliet", "kilo", "lima"},
242 },
243 },
244 expectedOk: true,
245 },
246
247 "escaped extra keys": {
248 nameHeaders: []string{"X-Remote-User"},
249 uidHeaders: []string{"X-Remote-Uid"},
250 groupHeaders: []string{"X-Remote-Group"},
251 extraPrefixHeaders: []string{"X-Remote-Extra-"},
252 requestHeaders: http.Header{
253 "X-Remote-User": {"Bob"},
254 "X-Remote-Uid": {"2ca80fb0-60ea-4ecf-951c-89af843b0402"},
255 "X-Remote-Group": {"one-a", "one-b"},
256 "X-Remote-Extra-Alpha": {"alphabetical"},
257 "X-Remote-Extra-Alph4num3r1c": {"alphanumeric"},
258 "X-Remote-Extra-Percent%20encoded": {"percent encoded"},
259 "X-Remote-Extra-Almost%zzpercent%xxencoded": {"not quite percent encoded"},
260 "X-Remote-Extra-Example.com%2fpercent%2520encoded": {"url with double percent encoding"},
261 "X-Remote-Extra-Example.com%2F%E4%BB%8A%E6%97%A5%E3%81%AF": {"url with unicode"},
262 "X-Remote-Extra-Abc123!#$+.-_*\\^`~|'": {"header key legal characters"},
263 },
264 expectedUser: &user.DefaultInfo{
265 Name: "Bob",
266 UID: "2ca80fb0-60ea-4ecf-951c-89af843b0402",
267 Groups: []string{"one-a", "one-b"},
268 Extra: map[string][]string{
269 "alpha": {"alphabetical"},
270 "alph4num3r1c": {"alphanumeric"},
271 "percent encoded": {"percent encoded"},
272 "almost%zzpercent%xxencoded": {"not quite percent encoded"},
273 "example.com/percent%20encoded": {"url with double percent encoding"},
274 "example.com/今日は": {"url with unicode"},
275 "abc123!#$+.-_*\\^`~|'": {"header key legal characters"},
276 },
277 },
278 expectedOk: true,
279 },
280 }
281
282 for k, testcase := range testcases {
283 t.Run(k, func(t *testing.T) {
284 auth, err := New(testcase.nameHeaders, testcase.uidHeaders, testcase.groupHeaders, testcase.extraPrefixHeaders)
285 if err != nil {
286 t.Fatal(err)
287 }
288 req := &http.Request{Header: testcase.requestHeaders}
289
290 resp, ok, _ := auth.AuthenticateRequest(req)
291 if testcase.expectedOk != ok {
292 t.Errorf("%v: expected %v, got %v", k, testcase.expectedOk, ok)
293 }
294 if !ok {
295 return
296 }
297 if e, a := testcase.expectedUser, resp.User; !reflect.DeepEqual(e, a) {
298 t.Errorf("%v: expected %#v, got %#v", k, e, a)
299 }
300
301 want := testcase.finalHeaders
302 if want == nil && testcase.requestHeaders != nil {
303 want = http.Header{}
304 }
305 if diff := cmp.Diff(want, testcase.requestHeaders); len(diff) > 0 {
306 t.Errorf("unexpected final headers (-want +got):\n%s", diff)
307 }
308 })
309 }
310 }