@@ -438,23 +438,50 @@ func (t *testCase) evaluateOutput(createdSars []authv1.SubjectAccessReview, allo
438
438
// prettyPrintSAR pretty prints the SubjectAccessReview object. This is used
439
439
// to help debug the RBAC test cases.
440
440
func prettyPrintSAR (sar authv1.SubjectAccessReview ) string {
441
- str := "SubjectAccessReviewSpec:"
442
- str += "\n Namespace: " + sar .Spec .ResourceAttributes .Namespace
443
- str += "\n Verb: " + sar .Spec .ResourceAttributes .Verb
444
- str += "\n APIGroup: " + sar .Spec .ResourceAttributes .Group
445
- str += "\n Resource: " + sar .Spec .ResourceAttributes .Resource
446
- str += "\n Subresource: " + sar .Spec .ResourceAttributes .Subresource
447
- str += "\n Name: " + sar .Spec .ResourceAttributes .Name
441
+
442
+ str := "\n SubjectAccessReviewSpec:"
443
+ // we print the field values conditionally since some fields might be empty
444
+ // 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
+ }
448
463
if sar .Spec .NonResourceAttributes != nil {
449
- str += "\n NonResourcePath: " + sar .Spec .NonResourceAttributes .Path
450
- str += "\n NonResourceVerb: " + sar .Spec .NonResourceAttributes .Verb
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 , "," )
451
476
}
452
- str += "\n User: " + sar .Spec .User
453
- str += "\n Groups: " + strings .Join (sar .Spec .Groups , "," )
454
477
str += "\n SubjectAccessReviewStatus:"
478
+ // these fields are always present in the SubjectAccessReviewStatus
455
479
str += "\n Allowed: " + strconv .FormatBool (sar .Status .Allowed )
456
480
str += "\n Denied: " + strconv .FormatBool (sar .Status .Denied )
457
- str += "\n Reason: " + sar .Status .Reason
481
+
482
+ if reason := sar .Status .Reason ; reason != "" {
483
+ str += "\n Reason: " + reason
484
+ }
458
485
str += "\n "
459
486
return str
460
487
}
0 commit comments