Skip to content

Commit 47fcb60

Browse files
trgeigerbentito
authored andcommitted
Improve permission regexp matching
Now handles multiple values in any of APIGroups, Resources, or Verbs. Adds small utility function for trimming and splitting those values into a string slice. Signed-off-by: Tayler Geiger <[email protected]>
1 parent 364bfb9 commit 47fcb60

File tree

1 file changed

+16
-4
lines changed
  • internal/operator-controller/authorization

1 file changed

+16
-4
lines changed

internal/operator-controller/authorization/rbac.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ func parseEscalationErrorForMissingRules(ecError error) ([]rbacv1.PolicyRule, er
563563
// Group 2: Optional resolution errors
564564
errRegex := regexp.MustCompile(`(?s)^user ".*" \(groups=.*\) is attempting to grant RBAC permissions not currently held: (.*)(?:; resolution errors: (.*))?$`)
565565
// permRegex extracts the details (APIGroups, Resources, Verbs) of individual permissions listed within the error message
566-
permRegex := regexp.MustCompile(`{APIGroups:\[("[^"]*")], Resources:\[("[^"]*")], Verbs:\[("[^"]*")]}`)
566+
permRegex := regexp.MustCompile(`{APIGroups:\[([^\]]*)\], Resources:\[([^\]]*)\], Verbs:\[([^\]]*)\]}`)
567567

568568
errString := ecError.Error()
569569
errMatches := errRegex.FindStringSubmatch(errString) // Use FindStringSubmatch for single match expected
@@ -587,9 +587,9 @@ func parseEscalationErrorForMissingRules(ecError error) ([]rbacv1.PolicyRule, er
587587
continue // Skip malformed permission strings
588588
}
589589
permissions = append(permissions, rbacv1.PolicyRule{
590-
APIGroups: []string{strings.Trim(match[1], `"`)},
591-
Resources: []string{strings.Trim(match[2], `"`)},
592-
Verbs: []string{strings.Trim(match[3], `"`)},
590+
APIGroups: splitAndTrim(match[1]),
591+
Resources: splitAndTrim(match[2]),
592+
Verbs: splitAndTrim(match[3]),
593593
})
594594
}
595595

@@ -603,6 +603,18 @@ func parseEscalationErrorForMissingRules(ecError error) ([]rbacv1.PolicyRule, er
603603
return permissions, errors.New(errMsg)
604604
}
605605

606+
func splitAndTrim(input string) []string {
607+
parts := strings.Split(input, ",")
608+
609+
output := make([]string, 0, len(parts))
610+
for _, part := range parts {
611+
trimmed := strings.TrimSpace(part)
612+
trimmed = strings.Trim(trimmed, `"`)
613+
output = append(output, trimmed)
614+
}
615+
return output
616+
}
617+
606618
func hasAggregationRule(clusterRole *rbacv1.ClusterRole) bool {
607619
// Currently, an aggregation rule is considered present only if it has one or more selectors.
608620
// An empty slice of ClusterRoleSelectors means no selectors were provided,

0 commit comments

Comments
 (0)