@@ -546,23 +546,46 @@ var fullAuthority = []rbacv1.PolicyRule{
546546 {Verbs : []string {"*" }, NonResourceURLs : []string {"*" }},
547547}
548548
549+ // TODO: Investigate replacing this regex parsing with structured error handling once there are
550+ //
551+ // structured RBAC errors introduced by https://github.com/kubernetes/kubernetes/pull/130955.
552+ //
553+ // parseEscalationErrorForMissingRules attempts to extract specific RBAC permissions
554+ // that were denied due to escalation prevention from a given error's text.
555+ // It returns the list of extracted PolicyRules and an error.
556+ // Note: If parsing is successful, the returned error is derived from the *input* error's
557+ // message (specifically the part indicating the escalation attempt), not an error
558+ // encountered during the parsing process itself. If parsing fails due to an unexpected
559+ // error format, a distinct parsing error is returned.
549560func parseEscalationErrorForMissingRules (ecError error ) ([]rbacv1.PolicyRule , error ) {
550- errRegex := regexp .MustCompile (`(?s)^(user \".*\" \(groups=.*\) is attempting to grant RBAC permissions not currently held):.*?$` )
551- permRegex := regexp .MustCompile (`{APIGroups:\[("[^"]*")\], Resources:\[("[^"]*")\], Verbs:\[("[^"]*")\]}` )
561+ // errRegex captures the standard prefix of an escalation error message
562+ errRegex := regexp .MustCompile (`(?s)^(user ".*" \(groups=.*\) is attempting to grant RBAC permissions not currently held):.*?$` )
563+ // permRegex extracts the details (APIGroups, Resources, Verbs) of individual permissions listed within the error message
564+ permRegex := regexp .MustCompile (`{APIGroups:\[("[^"]*")], Resources:\[("[^"]*")], Verbs:\[("[^"]*")]}` )
552565
553566 errMatches := errRegex .FindAllStringSubmatch (ecError .Error (), - 1 )
567+ // Check if the main error message prefix was matched and captured
568+ if len (errMatches ) == 0 || len (errMatches [0 ]) < 2 {
569+ // The error format doesn't match the expected pattern for escalation errors
570+ return nil , fmt .Errorf ("failed to parse escalation error: unexpected format: %w" , ecError )
571+ }
554572
555- // Extract permissions
573+ // Extract permissions using permRegex
556574 permissions := []rbacv1.PolicyRule {}
557575 permMatches := permRegex .FindAllStringSubmatch (ecError .Error (), - 1 )
558576 for _ , match := range permMatches {
577+ // Ensure the match has the expected number of capture groups
578+ if len (match ) < 4 {
579+ continue // Skip malformed permission strings
580+ }
559581 permissions = append (permissions , rbacv1.PolicyRule {
560582 APIGroups : []string {strings .Trim (match [1 ], `"` )},
561583 Resources : []string {strings .Trim (match [2 ], `"` )},
562584 Verbs : []string {strings .Trim (match [3 ], `"` )},
563585 })
564586 }
565587
588+ // Return the extracted permissions and the captured escalation message prefix as the error context
566589 return permissions , errors .New (errMatches [0 ][1 ])
567590}
568591
0 commit comments