-
Notifications
You must be signed in to change notification settings - Fork 66
/
authorizer_external_test.go
327 lines (318 loc) · 7.8 KB
/
authorizer_external_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"fmt"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/require"
"k8s.io/apiserver/pkg/authentication/user"
"net/http"
"net/url"
"testing"
)
const (
mockAuthUrl = "https://test/auth"
// Issuer: "Issuer", Username: "Test", "Role: Test"
JWT = "eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiVGVzdCIsIklzc3VlciI6Iklzc3VlciIsIlVzZXJuYW1lIjoiVGVzdCIsImV4cCI6MTY2NzM4MzY0MiwiaWF0IjoxNjY3MzgzNjQyfQ.D75w4zDiQfTwDtrFCz0m9qlBalLhoEhxWqw83unoFCk\n"
/* The decoded JWT token has the following content:
Header:
{
"alg": "HS256"
}
Payload:
{
"Role": "Test",
"Issuer": "Issuer",
"Username": "Test",
"exp": 1667383642,
"iat": 1667383642
}
*/
)
func createRequest(host string, addJWT bool) *http.Request {
var currentUrl url.URL
currentUrl.Path = "/test"
var headers = http.Header{}
if addJWT {
headers.Add("Authorization", fmt.Sprintf("Bearer: %s", JWT))
}
return &http.Request{
Method: "GET",
URL: ¤tUrl,
Host: host,
Header: headers,
}
}
func TestExternalAuthorizer_Authorize(t *testing.T) {
type args struct {
r *http.Request
userinfo user.Info
}
type httpMock struct {
url string
method string
status int
body string
}
type checks func(t *testing.T)
tests := []struct {
name string
args args
httpMock httpMock
checks checks
expectAllowed bool
expectReason string
expectErr bool
}{
{
name: "user is allowed",
args: args{
r: createRequest("host:80", true),
userinfo: &user.DefaultInfo{Name: "Test"},
},
httpMock: httpMock{
url: mockAuthUrl,
method: "POST",
status: 200,
body: "User Allowed",
},
checks: func(t *testing.T) {
// Verify that a call has been made in the backend.
calls := httpmock.GetCallCountInfo()[fmt.Sprintf("POST %v", mockAuthUrl)]
require.Equal(t, 1, calls)
},
expectAllowed: true,
expectReason: "",
expectErr: false,
},
{
name: "user is unauthorized",
args: args{
r: createRequest("host:80", true),
userinfo: &user.DefaultInfo{Name: "Test"},
},
httpMock: httpMock{
url: mockAuthUrl,
method: "POST",
status: 401,
body: "User Unauthorized",
},
checks: func(t *testing.T) {
// Verify that a call has been made in the backend.
calls := httpmock.GetCallCountInfo()[fmt.Sprintf("POST %v", mockAuthUrl)]
require.Equal(t, 1, calls)
},
expectAllowed: false,
expectReason: "User Unauthorized",
expectErr: false,
},
{
name: "authorization server exception",
args: args{
r: createRequest("host:80", true),
userinfo: &user.DefaultInfo{Name: "Test"},
},
httpMock: httpMock{
url: mockAuthUrl,
method: "POST",
status: 500,
body: "Internal server error",
},
checks: func(t *testing.T) {
// Verify that a call has been made in the backend.
calls := httpmock.GetCallCountInfo()[fmt.Sprintf("POST %v", mockAuthUrl)]
require.Equal(t, 1, calls)
},
expectAllowed: false,
expectReason: "",
expectErr: true,
},
{
name: "connection error",
args: args{
r: createRequest("host:80", true),
userinfo: &user.DefaultInfo{Name: "Test"},
},
httpMock: httpMock{
url: "http://wrongurl/auth",
method: "POST",
status: 500,
body: "Internal server error",
},
checks: func(t *testing.T) {
// Verify that a call has not been made in the backend.
calls := httpmock.GetCallCountInfo()[fmt.Sprintf("POST %v", mockAuthUrl)]
require.Equal(t, 0, calls)
},
expectAllowed: false,
expectReason: "Error while authorizing the request",
expectErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
e := ExternalAuthorizer{
url: mockAuthUrl,
}
// Mock HTTP.
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.httpMock.method, test.httpMock.url,
httpmock.NewStringResponder(test.httpMock.status, test.httpMock.body))
// Call the method.
gotAllowed, gotReason, err := e.Authorize(test.args.r, test.args.userinfo)
// Verify
if test.expectErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
require.Equal(t, test.expectAllowed, gotAllowed, "Authorize() gotAllowed = %v, expect %v", gotAllowed, test.expectAllowed)
require.Equal(t, test.expectReason, gotReason, "Authorize() gotReason = %v, expect %v", gotReason, test.expectReason)
if test.checks != nil {
test.checks(t)
}
})
}
}
func TestExternalAuthorizer_getRequestInfo(t *testing.T) {
type fields struct {
url string
}
type args struct {
r *http.Request
}
tests := []struct {
name string
fields fields
args args
expectRequest AuthorizationRequestInfo
}{
{
name: "request with host:443",
args: args{
r: createRequest("host:443", true),
},
expectRequest: AuthorizationRequestInfo{
Host: "host",
Port: 443,
Path: "/test",
Method: "GET",
},
},
{
name: "request with host without port",
args: args{
r: createRequest("host", true),
},
expectRequest: AuthorizationRequestInfo{
Host: "host",
Port: 80,
Path: "/test",
Method: "GET",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
e := ExternalAuthorizer{
url: test.fields.url,
}
gotRequest := e.getRequestInfo(test.args.r)
require.Equal(t, gotRequest, test.expectRequest,
"getRequestInfo() = %v, expect %v", gotRequest, test.expectRequest)
})
}
}
func TestExternalAuthorizer_getUserInfo(t *testing.T) {
type fields struct {
url string
}
type args struct {
r *http.Request
userinfo user.Info
}
nonJWTAuthorization := createRequest("host:80", false)
nonJWTAuthorization.Header.Add("Authorization", "Bearer: Test")
tests := []struct {
name string
fields fields
args args
expectInfo AuthorizationUserInfo
}{
{
name: "parse user info without JWT",
fields: fields{mockAuthUrl},
args: args{
r: createRequest("host:80", false),
userinfo: &user.DefaultInfo{
Name: "Test",
UID: "1",
Groups: []string{"test"},
Extra: map[string][]string{"test": {"test"}},
},
},
expectInfo: AuthorizationUserInfo{
Name: "Test",
Id: "1",
Groups: []string{"test"},
Extra: map[string][]string{"test": {"test"}},
Claims: nil,
},
},
{
name: "parse user info with JWT",
fields: fields{mockAuthUrl},
args: args{
r: createRequest("host:80", true),
userinfo: &user.DefaultInfo{
Name: "Test",
UID: "1",
Groups: []string{"test"},
Extra: map[string][]string{"test": {"test"}},
},
},
expectInfo: AuthorizationUserInfo{
Name: "Test",
Id: "1",
Groups: []string{"test"},
Extra: map[string][]string{"test": {"test"}},
Claims: map[string]interface{}{
"Issuer": "Issuer",
"Role": "Test",
"Username": "Test",
"exp": 1.667383642e+09,
"iat": 1.667383642e+09,
},
},
},
{
name: "parse user info with non JWT bearer token",
fields: fields{mockAuthUrl},
args: args{
r: nonJWTAuthorization,
userinfo: &user.DefaultInfo{
Name: "Test",
UID: "1",
Groups: []string{"test"},
Extra: map[string][]string{"test": {"test"}},
},
},
expectInfo: AuthorizationUserInfo{
Name: "Test",
Id: "1",
Groups: []string{"test"},
Extra: map[string][]string{"test": {"test"}},
Claims: nil,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
e := ExternalAuthorizer{
url: test.fields.url,
}
gotInfo := e.getUserInfo(test.args.r, test.args.userinfo)
require.Equal(t, gotInfo, test.expectInfo, "getUserInfo() gotInfo = %v, expect %v",
gotInfo, test.expectInfo)
})
}
}