@@ -82,38 +82,34 @@ func (t *testCase) generateSubjectAccessReviews() []authv1.SubjectAccessReview {
82
82
// Initialize the list of SubjectAccessReview objects
83
83
sars := make ([]authv1.SubjectAccessReview , 0 )
84
84
85
- // expand the testcase data to generate a list of ResourceAttributes
85
+ // expand the testcaseData to generate a list of ResourceAttributes
86
86
resourceAttributes := t .expandResourceAttributes ()
87
87
88
- // expand the testcase data to generate a list of NonResourceAttributes
89
- // nonResourceAttributes := t.expandNonResourceAttributes()
88
+ // expand the testcaseData to generate a list of NonResourceAttributes
89
+ nonResourceAttributes := t .expandNonResourceAttributes ()
90
90
91
- // expand the testcase data to generate a list of SubjectAccessReview objects
92
- // based on the ResourceAttributes and NonResourceAttributes
93
- for _ , ra := range resourceAttributes {
94
- for _ , user := range t .data .users {
95
- for _ , group := range t .data .groups {
96
- sar := authv1.SubjectAccessReview {
97
- Spec : authv1.SubjectAccessReviewSpec {
98
- ResourceAttributes : & ra ,
99
- User : user ,
100
- Groups : group ,
101
- },
102
- }
103
- sars = append (sars , sar )
104
- }
91
+ // expand users and groups
92
+ userGroupExpansions := t .expandUsersAndGroups ()
93
+
94
+ // expand the ResourceAttributes, NonResourceAttributes and UserGroupExpansions
95
+ // to generate a list of SubjectAccessReviewSpec objects
96
+ sarSpecs := t .expandSubjectAccessReviewSpecs (resourceAttributes , nonResourceAttributes , userGroupExpansions )
97
+
98
+ // create the SubjectAccessReview objects based on the SubjectAccessReviewSpec objects
99
+ for _ , sarSpec := range sarSpecs {
100
+ sar := authv1.SubjectAccessReview {
101
+ Spec : sarSpec ,
105
102
}
103
+ sars = append (sars , sar )
106
104
}
107
105
return sars
108
106
}
109
107
110
- // expandResourceAttributes expands the testcase data to generate a list of ResourceAttributes
108
+ // expandResourceAttributes expands the testcaseData to generate a list of ResourceAttributes
111
109
func (t * testCase ) expandResourceAttributes () []authv1.ResourceAttributes {
112
110
// This will hold the expanded ResourceAttributes
113
111
ras := make ([]authv1.ResourceAttributes , 0 )
114
112
115
- // TODO: Convert this logic in a function similar to the way it is implemented
116
- // today to avoid code duplication
117
113
nsExpansions := make ([]authv1.ResourceAttributes , 0 )
118
114
// expand on namespaces
119
115
if len (t .data .namespaces ) > 0 {
@@ -267,7 +263,7 @@ func (t *testCase) expandResourceAttributes() []authv1.ResourceAttributes {
267
263
return ras
268
264
}
269
265
270
- // expandNonResourceAttributes expands the testcase data to generate a list of NonResourceAttributes
266
+ // expandNonResourceAttributes expands the testcaseData to generate a list of NonResourceAttributes
271
267
func (t * testCase ) expandNonResourceAttributes () []authv1.NonResourceAttributes {
272
268
// This will hold the expanded NonResourceAttributes
273
269
nras := make ([]authv1.NonResourceAttributes , 0 )
@@ -289,10 +285,17 @@ func (t *testCase) expandNonResourceAttributes() []authv1.NonResourceAttributes
289
285
verbExpansions := make ([]authv1.NonResourceAttributes , 0 )
290
286
if len (t .data .nonResourceVerbs ) > 0 {
291
287
for _ , verb := range t .data .nonResourceVerbs {
292
- for _ , nra := range nras {
293
- copy := nra
294
- copy .Verb = verb
295
- verbExpansions = append (verbExpansions , copy )
288
+ if len (nras ) > 0 {
289
+ for _ , nra := range nras {
290
+ copy := nra
291
+ copy .Verb = verb
292
+ verbExpansions = append (verbExpansions , copy )
293
+ }
294
+ } else {
295
+ nra := authv1.NonResourceAttributes {
296
+ Verb : verb ,
297
+ }
298
+ verbExpansions = append (verbExpansions , nra )
296
299
}
297
300
}
298
301
// we update the expanded list with verb expansions
@@ -302,6 +305,93 @@ func (t *testCase) expandNonResourceAttributes() []authv1.NonResourceAttributes
302
305
return nras
303
306
}
304
307
308
+ // expandUsersAndGroups expands the users and groups in the testcaseData to generate a list of SubjectAccessReviewSpecs
309
+ func (t * testCase ) expandUsersAndGroups () []authv1.SubjectAccessReviewSpec {
310
+ // This will hold the expanded SubjectAccessReviewSpec
311
+ sars := make ([]authv1.SubjectAccessReviewSpec , 0 )
312
+
313
+ // expand on users
314
+ userExpansions := make ([]authv1.SubjectAccessReviewSpec , 0 )
315
+ if len (t .data .users ) > 0 {
316
+ for _ , user := range t .data .users {
317
+ sar := authv1.SubjectAccessReviewSpec {
318
+ User : user ,
319
+ }
320
+ userExpansions = append (userExpansions , sar )
321
+ }
322
+ // we update the expanded list with user expansions
323
+ sars = userExpansions
324
+ }
325
+
326
+ // expand on groups
327
+ groupExpansions := make ([]authv1.SubjectAccessReviewSpec , 0 )
328
+ if len (t .data .groups ) > 0 {
329
+ for _ , group := range t .data .groups {
330
+ // If an expansion already took place, we need to copy the
331
+ // existing objects and change the groups
332
+ if len (sars ) > 0 {
333
+ for _ , sar := range sars {
334
+ copy := sar
335
+ copy .Groups = group
336
+ groupExpansions = append (groupExpansions , copy )
337
+ }
338
+ } else {
339
+ // If no expansion has taken place, we need to create a new object
340
+ sar := authv1.SubjectAccessReviewSpec {
341
+ Groups : group ,
342
+ }
343
+ groupExpansions = append (groupExpansions , sar )
344
+ }
345
+ }
346
+ // we update the expanded list with group expansions
347
+ sars = groupExpansions
348
+ }
349
+
350
+ return sars
351
+ }
352
+
353
+ // expandSubjectAccessReviewSpecs takes the expanded ResourceAttributes, NonResourceAttributes and
354
+ // UserGroupExpansions and generates a list of SubjectAccessReviewSpec objects
355
+ func (t * testCase ) expandSubjectAccessReviewSpecs (ras []authv1.ResourceAttributes , nras []authv1.NonResourceAttributes , userGroupExpansions []authv1.SubjectAccessReviewSpec ) []authv1.SubjectAccessReviewSpec {
356
+ sars := make ([]authv1.SubjectAccessReviewSpec , 0 )
357
+
358
+ // expand on ResourceAttributes if they are defined
359
+ rasExpansions := make ([]authv1.SubjectAccessReviewSpec , 0 )
360
+ if len (ras ) > 0 {
361
+ for _ , ra := range ras {
362
+ for _ , ug := range userGroupExpansions {
363
+ sar := authv1.SubjectAccessReviewSpec {
364
+ ResourceAttributes : & ra ,
365
+ User : ug .User ,
366
+ Groups : ug .Groups ,
367
+ }
368
+ rasExpansions = append (rasExpansions , sar )
369
+ }
370
+ }
371
+ // we update the expanded list with ResourceAttributes expansions
372
+ sars = rasExpansions
373
+ }
374
+
375
+ // expand on NonResourceAttributes if they are defined
376
+ nrasExpansions := make ([]authv1.SubjectAccessReviewSpec , 0 )
377
+ if len (nras ) > 0 {
378
+ for _ , nra := range nras {
379
+ for _ , ug := range userGroupExpansions {
380
+ sar := authv1.SubjectAccessReviewSpec {
381
+ NonResourceAttributes : & nra ,
382
+ User : ug .User ,
383
+ Groups : ug .Groups ,
384
+ }
385
+ nrasExpansions = append (nrasExpansions , sar )
386
+ }
387
+ }
388
+ // we update the expanded list with NonResourceAttributes expansions
389
+ sars = nrasExpansions
390
+ }
391
+
392
+ return sars
393
+ }
394
+
305
395
// createSubjectAccessReviews creates provided SubjectAccessReview objects in the cluster
306
396
func createSubjectAccessReviews (ctx context.Context , cs kubernetes.Interface , sars []authv1.SubjectAccessReview ) ([]authv1.SubjectAccessReview , error ) {
307
397
createdSars := make ([]authv1.SubjectAccessReview , 0 )
@@ -325,8 +415,6 @@ func createSubjectAccessReview(ctx context.Context, cs kubernetes.Interface, sar
325
415
// allowExpected is a boolean that determines if the expected result is 'allow' or 'deny'.
326
416
func (t * testCase ) evaluateOutput (createdSars []authv1.SubjectAccessReview , allowExpected bool ) {
327
417
328
- //TODO: check if it's safe to override the output object of the testcase like this
329
-
330
418
// Iterate over all the SubjectAccessReviews created and check for expecated result.
331
419
// We don't break the loop if a result doesn't match expectation since we want to
332
420
// capture all the failing SubjectAccessReviews for debugging.
0 commit comments