@@ -546,23 +546,46 @@ var fullAuthority = []rbacv1.PolicyRule{
546
546
{Verbs : []string {"*" }, NonResourceURLs : []string {"*" }},
547
547
}
548
548
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.
549
560
func 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:\[("[^"]*")]}` )
552
565
553
566
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
+ }
554
572
555
- // Extract permissions
573
+ // Extract permissions using permRegex
556
574
permissions := []rbacv1.PolicyRule {}
557
575
permMatches := permRegex .FindAllStringSubmatch (ecError .Error (), - 1 )
558
576
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
+ }
559
581
permissions = append (permissions , rbacv1.PolicyRule {
560
582
APIGroups : []string {strings .Trim (match [1 ], `"` )},
561
583
Resources : []string {strings .Trim (match [2 ], `"` )},
562
584
Verbs : []string {strings .Trim (match [3 ], `"` )},
563
585
})
564
586
}
565
587
588
+ // Return the extracted permissions and the captured escalation message prefix as the error context
566
589
return permissions , errors .New (errMatches [0 ][1 ])
567
590
}
568
591
0 commit comments