@@ -62,47 +62,45 @@ func NewSARAuthorizer(kubeClient kubernetes.Interface) *SARAuthorizer {
6262 }
6363}
6464
65- func (s * SARAuthorizer ) AuthorizeRequest (ctx context.Context , req any ) error {
65+ func (s * SARAuthorizer ) AuthorizeRequest (ctx context.Context , req any ) (authz. Decision , string , error ) {
6666 pReq , ok := req .(* pbv1.PublishRequest )
6767 if ! ok {
68- return fmt .Errorf ("unsupported request type %T" , req )
68+ return authz . DecisionDeny , "unsupported request type" , fmt .Errorf ("unsupported request type %T" , req )
6969 }
7070
7171 eventsType , err := types .ParseCloudEventsType (pReq .Event .Type )
7272 if err != nil {
73- return err
73+ return authz . DecisionDeny , "invalid event type" , err
7474 }
7575
7676 // the event of grpc publish request is the original cloudevent data, we need a `ce-` prefix
7777 // to get the event attribute
7878 clusterAttr , ok := pReq .Event .Attributes [fmt .Sprintf ("ce-%s" , types .ExtensionClusterName )]
7979 if ! ok {
80- return fmt .Errorf ("missing ce-clustername in event attributes, %v" , pReq .Event .Attributes )
80+ return authz . DecisionDeny , "missing cluster name" , fmt .Errorf ("missing ce-clustername in event attributes, %v" , pReq .Event .Attributes )
8181 }
8282
83- if err := s .authorize (ctx , clusterAttr .GetCeString (), * eventsType ); err != nil {
84- return err
85- }
86- return nil
83+ decision , reason , err := s .authorize (ctx , clusterAttr .GetCeString (), * eventsType )
84+ return decision , reason , err
8785}
8886
89- func (s * SARAuthorizer ) AuthorizeStream (ctx context.Context , ss grpc.ServerStream , info * grpc.StreamServerInfo ) (grpc.ServerStream , error ) {
87+ func (s * SARAuthorizer ) AuthorizeStream (ctx context.Context , ss grpc.ServerStream , info * grpc.StreamServerInfo ) (authz. Decision , string , grpc.ServerStream , error ) {
9088 if info .IsClientStream {
91- return ss , nil
89+ return authz . DecisionAllow , "client stream allowed" , ss , nil
9290 }
9391
9492 if info .FullMethod != pbv1 .CloudEventService_Subscribe_FullMethodName {
95- return nil , fmt .Errorf ("unsupported service full method %s for SARAuthorizer" , info .FullMethod )
93+ return authz . DecisionDeny , "unsupported method" , nil , fmt .Errorf ("unsupported service full method %s for SARAuthorizer" , info .FullMethod )
9694 }
9795
9896 var req pbv1.SubscriptionRequest
9997 if err := ss .RecvMsg (& req ); err != nil {
100- return nil , err
98+ return authz . DecisionDeny , "failed to receive message" , nil , err
10199 }
102100
103101 eventDataType , err := types .ParseCloudEventsDataType (req .DataType )
104102 if err != nil {
105- return nil , err
103+ return authz . DecisionDeny , "invalid data type" , nil , err
106104 }
107105
108106 eventsType := types.CloudEventsType {
@@ -111,34 +109,36 @@ func (s *SARAuthorizer) AuthorizeStream(ctx context.Context, ss grpc.ServerStrea
111109 Action : types .WatchRequestAction ,
112110 }
113111
114- if err := s .authorize (ss .Context (), req .ClusterName , eventsType ); err != nil {
115- return nil , err
112+ decision , reason , err := s .authorize (ss .Context (), req .ClusterName , eventsType )
113+ if err != nil {
114+ return decision , reason , nil , err
116115 }
117116
118- return & wrappedAuthorizedStream {ServerStream : ss , authorizedReq : & req }, nil
117+ return decision , reason , & wrappedAuthorizedStream {ServerStream : ss , authorizedReq : & req }, nil
119118}
120119
121- func (s * SARAuthorizer ) authorize (ctx context.Context , cluster string , eventsType types.CloudEventsType ) error {
120+ func (s * SARAuthorizer ) authorize (ctx context.Context , cluster string , eventsType types.CloudEventsType ) (authz. Decision , string , error ) {
122121 user , groups , err := userInfo (ctx )
123122 if err != nil {
124- return err
123+ return authz . DecisionDeny , "failed to extract user info" , err
125124 }
126125
127126 sar , err := toSubjectAccessReview (cluster , user , groups , eventsType )
128127 if err != nil {
129- return err
128+ return authz . DecisionDeny , "failed to create SAR" , err
130129 }
131130
132131 created , err := s .kubeClient .AuthorizationV1 ().SubjectAccessReviews ().Create (
133132 ctx , sar , metav1.CreateOptions {})
134133 if err != nil {
135- return err
134+ return authz . DecisionDeny , "SAR request failed" , err
136135 }
137136 if ! created .Status .Allowed {
138- return fmt .Errorf ("the event %s is not allowed, (cluster=%s, sar=%v, reason=%v)" ,
137+ reason := fmt .Sprintf ("access denied for event %s (cluster=%s, reason=%s)" , eventsType , cluster , created .Status .Reason )
138+ return authz .DecisionDeny , reason , fmt .Errorf ("the event %s is not allowed, (cluster=%s, sar=%v, reason=%v)" ,
139139 eventsType , cluster , sar .Spec , created .Status )
140140 }
141- return nil
141+ return authz . DecisionAllow , "access granted by SAR" , nil
142142}
143143
144144func userInfo (ctx context.Context ) (user string , groups []string , err error ) {
0 commit comments