-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
332 lines (316 loc) · 9.02 KB
/
main_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
328
329
330
331
332
package main
import (
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestParsePolicyCSV(t *testing.T) {
tests := []struct {
name string
policyCSV string
expectedUserToObjectPatternMapping map[string][]string
expectedGroupToObjectPatternMapping map[string][]string
}{
{
name: "Valid policy CSV with single team ALPHA READ ONLY",
policyCSV: `
g, [email protected], role:admin
g, [email protected], role:readonly
p, team-alpha-readonly, applications, get, alpha1-*, allow
p, team-alpha-readonly, applications, get, alpha2-*/*, allow
p, team-alpha-readonly, applications, get, alpha3-*, allow
p, team-alpha-readonly, applications, get, alpha4-*/*, allow
g, ALPHA READ ONLY, team-alpha-readonly
g, ALPHA READ ONLY_TW, team-alpha-readonly
g, [email protected], team-alpha-readonly
`,
expectedUserToObjectPatternMapping: map[string][]string{
"[email protected]": {
"*",
},
"[email protected]": {
"*",
},
"[email protected]": {
"alpha1-*",
"alpha2-*",
"alpha3-*",
"alpha4-*",
},
},
expectedGroupToObjectPatternMapping: map[string][]string{
"ALPHA READ ONLY": {
"alpha1-*",
"alpha2-*",
"alpha3-*",
"alpha4-*",
},
"ALPHA READ ONLY_TW": {
"alpha1-*",
"alpha2-*",
"alpha3-*",
"alpha4-*",
},
},
},
{
name: "Valid policy CSV with multiple teams ALPHA READ ONLY and BETA READ ONLY",
policyCSV: `
g, [email protected], role:admin
g, [email protected], role:readonly
p, team-alpha-readonly, applications, get, alpha1-*, allow
p, team-alpha-readonly, applications, get, alpha2-*, allow
p, team-alpha-readonly, applications, get, alpha3-*/*, allow
g, ALPHA READ ONLY, team-alpha-readonly
g, ALPHA READ ONLY_TW, team-alpha-readonly
p, team-beta-readonly, applications, get, beta-*/*, allow
g, BETA READ ONLY, team-beta-readonly
g, BETA READ ONLY_TW, team-beta-readonly
g, [email protected], team-beta-readonly
`,
expectedUserToObjectPatternMapping: map[string][]string{
"[email protected]": {
"*",
},
"[email protected]": {
"*",
},
"[email protected]": {
"beta-*",
},
},
expectedGroupToObjectPatternMapping: map[string][]string{
"ALPHA READ ONLY": {
"alpha1-*",
"alpha2-*",
"alpha3-*",
},
"ALPHA READ ONLY_TW": {
"alpha1-*",
"alpha2-*",
"alpha3-*",
},
"BETA READ ONLY": {
"beta-*",
},
"BETA READ ONLY_TW": {
"beta-*",
},
},
},
{
name: "Valid policy CSV with single group but without role",
policyCSV: `
g, GAMMA READ ONLY, team-gamma-readonly
`,
expectedUserToObjectPatternMapping: map[string][]string{},
expectedGroupToObjectPatternMapping: map[string][]string{
"GAMMA READ ONLY": {},
},
},
{
name: "Valid policy CSV with single role but without group",
policyCSV: `
p, team-gamma-readonly, applications, get, gamma-*, allow
`,
expectedUserToObjectPatternMapping: map[string][]string{},
expectedGroupToObjectPatternMapping: map[string][]string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
userToObjectPatternMapping, groupToObjectPatternMapping := parsePolicyCSV(tt.policyCSV)
if !reflect.DeepEqual(userToObjectPatternMapping, tt.expectedUserToObjectPatternMapping) {
t.Errorf("Expected user %v, but got user %v", tt.expectedUserToObjectPatternMapping, userToObjectPatternMapping)
}
if !reflect.DeepEqual(groupToObjectPatternMapping, tt.expectedGroupToObjectPatternMapping) {
t.Errorf("Expected group %v, but got group %v", tt.expectedGroupToObjectPatternMapping, groupToObjectPatternMapping)
}
})
}
}
func TestResolveObjectPatterns(t *testing.T) {
tests := []struct {
name string
email string
groups []string
userToObjectPatternMapping map[string][]string
groupToObjectPatternMapping map[string][]string
expectedPatterns map[string]struct{}
}{
{
name: "User with direct patterns",
email: "[email protected]",
groups: []string{},
userToObjectPatternMapping: map[string][]string{
"[email protected]": {"pattern1", "pattern2"},
},
groupToObjectPatternMapping: map[string][]string{},
expectedPatterns: map[string]struct{}{
"pattern1": {},
"pattern2": {},
},
},
{
name: "Group with patterns",
email: "[email protected]",
groups: []string{"group1"},
userToObjectPatternMapping: map[string][]string{
"[email protected]": {},
},
groupToObjectPatternMapping: map[string][]string{
"group1": {"pattern3", "pattern4"},
},
expectedPatterns: map[string]struct{}{
"pattern3": {},
"pattern4": {},
},
},
{
name: "User and groups with patterns",
email: "[email protected]",
groups: []string{"group1", "group2"},
userToObjectPatternMapping: map[string][]string{
"[email protected]": {"pattern5"},
},
groupToObjectPatternMapping: map[string][]string{
"group1": {"pattern6"},
"group2": {"pattern7"},
},
expectedPatterns: map[string]struct{}{
"pattern5": {},
"pattern6": {},
"pattern7": {},
},
},
{
name: "No patterns for user or groups",
email: "[email protected]",
groups: []string{"group3"},
userToObjectPatternMapping: map[string][]string{},
groupToObjectPatternMapping: map[string][]string{},
expectedPatterns: map[string]struct{}{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := resolveObjectPatterns(tt.email, tt.groups, tt.userToObjectPatternMapping, tt.groupToObjectPatternMapping)
if !reflect.DeepEqual(result, tt.expectedPatterns) {
t.Errorf("resolveObjectPatterns() = %v, want %v", result, tt.expectedPatterns)
}
})
}
}
func TestExtractToken(t *testing.T) {
tests := []struct {
name string
setupRequest func() *http.Request
expectedToken string
}{
{
name: "Valid Bearer Token in Authorization Header",
setupRequest: func() *http.Request {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Authorization", "Bearer valid_token")
return req
},
expectedToken: "valid_token",
},
{
name: "Token in Cookie",
setupRequest: func() *http.Request {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.AddCookie(&http.Cookie{Name: "argocd.token", Value: "cookie_token"})
return req
},
expectedToken: "cookie_token",
},
{
name: "No Token in Header or Cookie",
setupRequest: func() *http.Request {
req := httptest.NewRequest(http.MethodGet, "/", nil)
return req
},
expectedToken: "",
},
{
name: "Invalid Authorization Header",
setupRequest: func() *http.Request {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Authorization", "InvalidAuth valid_token")
return req
},
expectedToken: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := tt.setupRequest()
token := extractToken(req)
if token != tt.expectedToken {
t.Errorf("extractToken() = %q, want %q", token, tt.expectedToken)
}
})
}
}
func TestDecodeJWTPayload(t *testing.T) {
tests := []struct {
name string
token string
expected map[string]interface{}
expectingError bool
}{
{
name: "Valid JWT Token",
token: createTestJWT(map[string]interface{}{"email": "[email protected]", "role": "admin"}),
expected: map[string]interface{}{"email": "[email protected]", "role": "admin"},
expectingError: false,
},
{
name: "Invalid JWT Token Format",
token: "invalid.token",
expected: nil,
expectingError: true,
},
{
name: "Invalid Payload Encoding",
token: "header." + base64.RawURLEncoding.EncodeToString([]byte("invalid payload")) + ".signature",
expected: nil,
expectingError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
payload, err := decodeJWTPayload(tt.token)
if tt.expectingError && err == nil {
t.Errorf("Expected an error but got none")
}
if !tt.expectingError && err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !tt.expectingError && !compareMaps(payload, tt.expected) {
t.Errorf("Payload mismatch. Expected: %v, Got: %v", tt.expected, payload)
}
})
}
}
func createTestJWT(payload map[string]interface{}) string {
header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"HS256","typ":"JWT"}`))
payloadBytes, _ := json.Marshal(payload)
encodedPayload := base64.RawURLEncoding.EncodeToString(payloadBytes)
return header + "." + encodedPayload + ".signature"
}
func compareMaps(a, b map[string]interface{}) bool {
if len(a) != len(b) {
return false
}
for key, valueA := range a {
if valueB, exists := b[key]; !exists || valueA != valueB {
return false
}
}
return true
}