@@ -52,6 +52,14 @@ func NewRBACPreAuthorizer(cl client.Client) PreAuthorizer {
5252 }
5353}
5454
55+ // PreAuthorize validates whether the current user/request satisfies the necessary permissions
56+ // as defined by the RBAC policy. It examines the user’s roles, resource identifiers, and
57+ // the intended action to determine if the operation is allowed.
58+ //
59+ // Return Value:
60+ // - nil: indicates that the authorization check passed and the operation is permitted.
61+ // - non-nil error: indicates that the authorization failed (either due to insufficient permissions
62+ // or an error encountered during the check), the error provides a slice of several failures at once.
5563func (a * rbacPreAuthorizer ) PreAuthorize (ctx context.Context , manifestManager user.Info , manifestReader io.Reader ) (map [string ][]rbacv1.PolicyRule , error ) {
5664 dm , err := a .decodeManifest (manifestReader )
5765 if err != nil {
@@ -81,6 +89,10 @@ func (a *rbacPreAuthorizer) PreAuthorize(ctx context.Context, manifestManager us
8189 }
8290
8391 for ns , nsMissingRules := range missingRules {
92+ // NOTE: Although CompactRules is defined to return an error, its current implementation
93+ // never produces a non-nil error. This is because all operations within the function are
94+ // designed to succeed under current conditions. In the future, if more complex rule validations
95+ // are introduced, this behavior may change and proper error handling will be required.
8496 if compactMissingRules , err := validation .CompactRules (nsMissingRules ); err == nil {
8597 missingRules [ns ] = compactMissingRules
8698 }
@@ -120,7 +132,15 @@ func (a *rbacPreAuthorizer) decodeManifest(manifestReader io.Reader) (*decodedMa
120132 gvk := uObj .GroupVersionKind ()
121133 restMapping , err := a .restMapper .RESTMapping (gvk .GroupKind (), gvk .Version )
122134 if err != nil {
123- errs = append (errs , fmt .Errorf ("could not get REST mapping for object %d in manifest with GVK %s: %w" , i , gvk , err ))
135+ var objName string
136+ if name := uObj .GetName (); name != "" {
137+ objName = fmt .Sprintf (" (name: %s)" , name )
138+ }
139+
140+ errs = append (
141+ errs ,
142+ fmt .Errorf ("could not get REST mapping for object %d in manifest with GVK %s%s: %w" , i , gvk , objName , err ),
143+ )
124144 continue
125145 }
126146
@@ -183,8 +203,11 @@ func (a *rbacPreAuthorizer) authorizeAttributesRecords(ctx context.Context, attr
183203}
184204
185205func (a * rbacPreAuthorizer ) attributesAllowed (ctx context.Context , attributesRecord authorizer.AttributesRecord ) (bool , error ) {
186- decision , _ , err := a .authorizer .Authorize (ctx , attributesRecord )
206+ decision , reason , err := a .authorizer .Authorize (ctx , attributesRecord )
187207 if err != nil {
208+ if reason != "" {
209+ return false , fmt .Errorf ("%s: %w" , reason , err )
210+ }
188211 return false , err
189212 }
190213 return decision == authorizer .DecisionAllow , nil
@@ -452,6 +475,9 @@ var fullAuthority = []rbacv1.PolicyRule{
452475}
453476
454477func hasAggregationRule (clusterRole * rbacv1.ClusterRole ) bool {
478+ // Currently, an aggregation rule is considered present only if it has one or more selectors.
479+ // An empty slice of ClusterRoleSelectors means no selectors were provided,
480+ // which does NOT imply "match all."
455481 return clusterRole .AggregationRule != nil && len (clusterRole .AggregationRule .ClusterRoleSelectors ) > 0
456482}
457483
0 commit comments