99 "strings"
1010 "time"
1111
12+ "github.com/kcp-dev/logicalcluster/v3"
1213 openfgav1 "github.com/openfga/api/proto/openfga/v1"
1314 corev1alpha1 "github.com/platform-mesh/account-operator/api/v1alpha1"
1415 "github.com/prometheus/client_golang/prometheus"
@@ -20,13 +21,15 @@ import (
2021
2122 "github.com/platform-mesh/golang-commons/logger"
2223
24+ "github.com/platform-mesh/rebac-authz-webhook/pkg/mapperprovider"
2325 "github.com/platform-mesh/rebac-authz-webhook/pkg/util"
2426)
2527
2628type AuthorizationHandler struct {
2729 fga openfgav1.OpenFGAServiceClient
2830 accountInfoName string
2931 mgr mcmanager.Manager
32+ mps * mapperprovider.MapperProviders
3033}
3134
3235var (
@@ -37,12 +40,13 @@ var (
3740 })
3841)
3942
40- func NewAuthorizationHandler (fga openfgav1.OpenFGAServiceClient , mgr mcmanager.Manager , accountInfoName string ) (* AuthorizationHandler , error ) {
43+ func NewAuthorizationHandler (fga openfgav1.OpenFGAServiceClient , mgr mcmanager.Manager , accountInfoName string , mps * mapperprovider. MapperProviders ) (* AuthorizationHandler , error ) {
4144
4245 return & AuthorizationHandler {
4346 fga : fga ,
4447 accountInfoName : accountInfoName ,
4548 mgr : mgr ,
49+ mps : mps ,
4650 }, nil
4751}
4852
@@ -116,6 +120,12 @@ func (a *AuthorizationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
116120 return
117121 }
118122
123+ log = log .ChildLogger ("resourceAttributes" , sar .Spec .ResourceAttributes .String ()).
124+ ChildLogger ("group" , sar .Spec .ResourceAttributes .Group ).
125+ ChildLogger ("resource" , sar .Spec .ResourceAttributes .Resource ).
126+ ChildLogger ("subresource" , sar .Spec .ResourceAttributes .Subresource ).
127+ ChildLogger ("verb" , sar .Spec .ResourceAttributes .Verb )
128+
119129 log .Debug ().Str ("sar" , fmt .Sprintf ("%+v" , sar )).Msg ("Received SubjectAccessReview" )
120130
121131 // For resource attributes, we need to get the store ID
@@ -126,12 +136,6 @@ func (a *AuthorizationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
126136 return
127137 }
128138
129- log = log .ChildLogger ("resourceAttributes" , sar .Spec .ResourceAttributes .String ()).
130- ChildLogger ("group" , sar .Spec .ResourceAttributes .Group ).
131- ChildLogger ("resource" , sar .Spec .ResourceAttributes .Resource ).
132- ChildLogger ("subresource" , sar .Spec .ResourceAttributes .Subresource ).
133- ChildLogger ("verb" , sar .Spec .ResourceAttributes .Verb )
134-
135139 group := util .CapGroupToRelationLength (sar , 50 )
136140 group = strings .ReplaceAll (group , "." , "_" )
137141 relation := sar .Spec .ResourceAttributes .Verb
@@ -142,34 +146,30 @@ func (a *AuthorizationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
142146 clusterName = clusterNames [0 ]
143147 }
144148 }
149+ log = log .ChildLogger ("clusterName" , clusterName )
145150
146151 var namespaced bool
147152 var gvk schema.GroupVersionKind
148153
149- cluster , err := a .mgr .GetCluster (r .Context (), clusterName )
150- if err != nil {
151- log .Error ().Err (err ).Str ("cluster" , clusterName ).Msg ("error getting cluster" )
152- noOpinion (w , sar )
153- return
154- }
155-
156- restMapper := cluster .GetRESTMapper ()
157- if err != nil {
154+ restMapper , ok := a .mps .GetMapper (logicalcluster .Name (clusterName ))
155+ if ! ok {
158156 log .Error ().Err (err ).Msg ("error getting provider" )
159157 noOpinion (w , sar )
160158 return
161159 }
162160
163- gvk , err = restMapper . KindFor ( schema.GroupVersionResource {
161+ gvr := schema.GroupVersionResource {
164162 Group : sar .Spec .ResourceAttributes .Group ,
165163 Resource : sar .Spec .ResourceAttributes .Resource ,
166164 Version : sar .Spec .ResourceAttributes .Version ,
167- })
165+ }
166+ gvk , err = restMapper .KindFor (gvr )
168167 if err != nil {
169- log .Error ().Err (err ).Msg ("error getting GVK" )
168+ log .Error ().Err (err ).Str ( "gvr" , fmt . Sprintf ( "%+v" , gvr )). Msg ("error getting GVK" )
170169 noOpinion (w , sar )
171170 return
172171 }
172+ log .Debug ().Str ("gvr" , fmt .Sprintf ("%+v" , gvr )).Str ("gvk" , fmt .Sprintf ("%+v" , gvk )).Msg ("Got GVK" )
173173
174174 namespaced , err = apiutil .IsGVKNamespaced (gvk , restMapper )
175175 if err != nil {
@@ -178,15 +178,14 @@ func (a *AuthorizationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
178178 return
179179 }
180180
181- groupForType := strings .ReplaceAll (sar .Spec .ResourceAttributes .Group , "." , "_" )
182181 resourceType := sar .Spec .ResourceAttributes .Resource
183182
184183 if singularResource , err := restMapper .ResourceSingularizer (sar .Spec .ResourceAttributes .Resource ); err == nil {
185184 resourceType = singularResource
186185 log .Debug ().Str ("resource" , sar .Spec .ResourceAttributes .Resource ).Str ("singular" , resourceType ).Msg ("Converted resource to singular form" )
187186 }
188187
189- objectType := fmt .Sprintf ("%s_%s" , groupForType , resourceType )
188+ objectType := fmt .Sprintf ("%s_%s" , group , resourceType )
190189
191190 longestObjectType := fmt .Sprintf ("create_%ss" , objectType )
192191 if len (longestObjectType ) > 50 {
@@ -233,7 +232,7 @@ func (a *AuthorizationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
233232 }
234233 }
235234
236- log .Debug ().Str ("object" , object ).Str ("relation" , relation ).Any ("contextualTuples" , contextualTuples ).Msg ("ruleless mode, using contextual tuples " )
235+ log .Debug ().Str ("object" , object ).Str ("relation" , relation ).Any ("contextualTuples" , contextualTuples ).Msg ("check call elements " )
237236
238237 if a .fga == nil {
239238 log .Warn ().Msg ("FGA client is nil, returning no opinion" )
@@ -260,7 +259,7 @@ func (a *AuthorizationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
260259 noOpinion (w , sar )
261260 return
262261 }
263- log .Info ().Bool ("allowed" , res .Allowed ).Str ("user" , sar .Spec .User ).Str ("object" , object ).Str ("relation" , relation ).Msg ("sar response" )
262+ log .Info ().Str ("allowed" , fmt . Sprintf ( "%t" , res .Allowed ) ).Str ("user" , sar .Spec .User ).Str ("object" , object ).Str ("relation" , relation ).Msg ("sar response" )
264263 if ! res .Allowed {
265264 noOpinion (w , sar )
266265 return
0 commit comments