Skip to content

Commit 5bae116

Browse files
committed
add all expansions in SAR generation
1 parent cdc0ee7 commit 5bae116

File tree

1 file changed

+115
-27
lines changed

1 file changed

+115
-27
lines changed

test/e2e/authorization_utils.go

Lines changed: 115 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,38 +82,34 @@ func (t *testCase) generateSubjectAccessReviews() []authv1.SubjectAccessReview {
8282
// Initialize the list of SubjectAccessReview objects
8383
sars := make([]authv1.SubjectAccessReview, 0)
8484

85-
// expand the testcase data to generate a list of ResourceAttributes
85+
// expand the testcaseData to generate a list of ResourceAttributes
8686
resourceAttributes := t.expandResourceAttributes()
8787

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()
9090

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,
105102
}
103+
sars = append(sars, sar)
106104
}
107105
return sars
108106
}
109107

110-
// expandResourceAttributes expands the testcase data to generate a list of ResourceAttributes
108+
// expandResourceAttributes expands the testcaseData to generate a list of ResourceAttributes
111109
func (t *testCase) expandResourceAttributes() []authv1.ResourceAttributes {
112110
// This will hold the expanded ResourceAttributes
113111
ras := make([]authv1.ResourceAttributes, 0)
114112

115-
// TODO: Convert this logic in a function similar to the way it is implemented
116-
// today to avoid code duplication
117113
nsExpansions := make([]authv1.ResourceAttributes, 0)
118114
// expand on namespaces
119115
if len(t.data.namespaces) > 0 {
@@ -267,7 +263,7 @@ func (t *testCase) expandResourceAttributes() []authv1.ResourceAttributes {
267263
return ras
268264
}
269265

270-
// expandNonResourceAttributes expands the testcase data to generate a list of NonResourceAttributes
266+
// expandNonResourceAttributes expands the testcaseData to generate a list of NonResourceAttributes
271267
func (t *testCase) expandNonResourceAttributes() []authv1.NonResourceAttributes {
272268
// This will hold the expanded NonResourceAttributes
273269
nras := make([]authv1.NonResourceAttributes, 0)
@@ -289,10 +285,17 @@ func (t *testCase) expandNonResourceAttributes() []authv1.NonResourceAttributes
289285
verbExpansions := make([]authv1.NonResourceAttributes, 0)
290286
if len(t.data.nonResourceVerbs) > 0 {
291287
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)
296299
}
297300
}
298301
// we update the expanded list with verb expansions
@@ -302,6 +305,93 @@ func (t *testCase) expandNonResourceAttributes() []authv1.NonResourceAttributes
302305
return nras
303306
}
304307

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+
305395
// createSubjectAccessReviews creates provided SubjectAccessReview objects in the cluster
306396
func createSubjectAccessReviews(ctx context.Context, cs kubernetes.Interface, sars []authv1.SubjectAccessReview) ([]authv1.SubjectAccessReview, error) {
307397
createdSars := make([]authv1.SubjectAccessReview, 0)
@@ -325,8 +415,6 @@ func createSubjectAccessReview(ctx context.Context, cs kubernetes.Interface, sar
325415
// allowExpected is a boolean that determines if the expected result is 'allow' or 'deny'.
326416
func (t *testCase) evaluateOutput(createdSars []authv1.SubjectAccessReview, allowExpected bool) {
327417

328-
//TODO: check if it's safe to override the output object of the testcase like this
329-
330418
// Iterate over all the SubjectAccessReviews created and check for expecated result.
331419
// We don't break the loop if a result doesn't match expectation since we want to
332420
// capture all the failing SubjectAccessReviews for debugging.

0 commit comments

Comments
 (0)