Skip to content

Commit 66e2269

Browse files
committed
cleanup pretty printing, add a comment
1 parent 6d59fb5 commit 66e2269

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

test/e2e/authorization.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ var _ = g.Describe("Authorization [RBAC] [Zalando]", func() {
247247
var tc testCase
248248
g.BeforeEach(func() {
249249
tc.data.groups = [][]string{
250+
// Collaborator groups can escalate privileges to their respective groups
251+
// so, we need to include the respective group in the list as well.
250252
{"CollaboratorPowerUser", "PowerUser"},
251253
{"CollaboratorManual", "Manual"},
252254
{"CollaboratorEmergency", "Emergency"},

test/e2e/authorization_utils.go

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -439,49 +439,34 @@ func (t *testCase) evaluateOutput(createdSars []authv1.SubjectAccessReview, allo
439439
// to help debug the RBAC test cases.
440440
func prettyPrintSAR(sar authv1.SubjectAccessReview) string {
441441

442+
// helper function to print the field values conditionally
443+
ifNotNil := func(k, v string) string {
444+
if v != "" {
445+
return "\n " + k + ": " + v
446+
}
447+
return ""
448+
}
449+
442450
str := "\nSubjectAccessReviewSpec:"
443451
// we print the field values conditionally since some fields might be empty
444452
// this helps in making the output more readable
445-
if ns := sar.Spec.ResourceAttributes.Namespace; ns != "" {
446-
str += "\n Namespace: " + ns
447-
}
448-
if verb := sar.Spec.ResourceAttributes.Verb; verb != "" {
449-
str += "\n Verb: " + verb
450-
}
451-
if group := sar.Spec.ResourceAttributes.Group; group != "" {
452-
str += "\n APIGroup: " + group
453-
}
454-
if resource := sar.Spec.ResourceAttributes.Resource; resource != "" {
455-
str += "\n Resource: " + resource
456-
}
457-
if subresource := sar.Spec.ResourceAttributes.Subresource; subresource != "" {
458-
str += "\n Subresource: " + subresource
459-
}
460-
if name := sar.Spec.ResourceAttributes.Name; name != "" {
461-
str += "\n Name: " + name
462-
}
453+
str += ifNotNil("Namespace", sar.Spec.ResourceAttributes.Namespace)
454+
str += ifNotNil("Verb", sar.Spec.ResourceAttributes.Verb)
455+
str += ifNotNil("Group", sar.Spec.ResourceAttributes.Group)
456+
str += ifNotNil("Resource", sar.Spec.ResourceAttributes.Resource)
457+
str += ifNotNil("Subresource", sar.Spec.ResourceAttributes.Subresource)
458+
str += ifNotNil("Name", sar.Spec.ResourceAttributes.Name)
463459
if sar.Spec.NonResourceAttributes != nil {
464-
if verb := sar.Spec.NonResourceAttributes.Verb; verb != "" {
465-
str += "\n NonResourceVerb: " + verb
466-
}
467-
if path := sar.Spec.NonResourceAttributes.Path; path != "" {
468-
str += "\n NonResourcePath: " + path
469-
}
470-
}
471-
if user := sar.Spec.User; user != "" {
472-
str += "\n User: " + user
473-
}
474-
if groups := sar.Spec.Groups; len(groups) > 0 {
475-
str += "\n Groups: " + strings.Join(groups, ",")
460+
str += ifNotNil("Path", sar.Spec.NonResourceAttributes.Path)
461+
str += ifNotNil("Verb", sar.Spec.NonResourceAttributes.Verb)
476462
}
463+
str += ifNotNil("User", sar.Spec.User)
464+
str += ifNotNil("Groups", strings.Join(sar.Spec.Groups, ","))
477465
str += "\nSubjectAccessReviewStatus:"
478466
// these fields are always present in the SubjectAccessReviewStatus
479467
str += "\n Allowed: " + strconv.FormatBool(sar.Status.Allowed)
480468
str += "\n Denied: " + strconv.FormatBool(sar.Status.Denied)
481-
482-
if reason := sar.Status.Reason; reason != "" {
483-
str += "\n Reason: " + reason
484-
}
469+
str += ifNotNil("Reason", sar.Status.Reason)
485470
str += "\n"
486471
return str
487472
}

0 commit comments

Comments
 (0)