@@ -22,6 +22,7 @@ import (
22
22
"github.com/google/cel-go/cel"
23
23
"github.com/google/cel-go/common/types/ref"
24
24
25
+ authorizationv1 "k8s.io/api/authorization/v1"
25
26
"k8s.io/apimachinery/pkg/util/version"
26
27
apiservercel "k8s.io/apiserver/pkg/cel"
27
28
"k8s.io/apiserver/pkg/cel/environment"
@@ -143,6 +144,7 @@ func mustBuildEnv(baseEnv *environment.EnvSet) *environment.EnvSet {
143
144
}
144
145
145
146
// buildRequestType generates a DeclType for SubjectAccessReviewSpec.
147
+ // if attributes are added here, also add to convertObjectToUnstructured.
146
148
func buildRequestType (field func (name string , declType * apiservercel.DeclType , required bool ) * apiservercel.DeclField , fields func (fields ... * apiservercel.DeclField ) map [string ]* apiservercel.DeclField ) * apiservercel.DeclType {
147
149
resourceAttributesType := buildResourceAttributesType (field , fields )
148
150
nonResourceAttributesType := buildNonResourceAttributesType (field , fields )
@@ -157,6 +159,7 @@ func buildRequestType(field func(name string, declType *apiservercel.DeclType, r
157
159
}
158
160
159
161
// buildResourceAttributesType generates a DeclType for ResourceAttributes.
162
+ // if attributes are added here, also add to convertObjectToUnstructured.
160
163
func buildResourceAttributesType (field func (name string , declType * apiservercel.DeclType , required bool ) * apiservercel.DeclField , fields func (fields ... * apiservercel.DeclField ) map [string ]* apiservercel.DeclField ) * apiservercel.DeclType {
161
164
return apiservercel .NewObjectType ("kubernetes.ResourceAttributes" , fields (
162
165
field ("namespace" , apiservercel .StringType , false ),
@@ -170,9 +173,42 @@ func buildResourceAttributesType(field func(name string, declType *apiservercel.
170
173
}
171
174
172
175
// buildNonResourceAttributesType generates a DeclType for NonResourceAttributes.
176
+ // if attributes are added here, also add to convertObjectToUnstructured.
173
177
func buildNonResourceAttributesType (field func (name string , declType * apiservercel.DeclType , required bool ) * apiservercel.DeclField , fields func (fields ... * apiservercel.DeclField ) map [string ]* apiservercel.DeclField ) * apiservercel.DeclType {
174
178
return apiservercel .NewObjectType ("kubernetes.NonResourceAttributes" , fields (
175
179
field ("path" , apiservercel .StringType , false ),
176
180
field ("verb" , apiservercel .StringType , false ),
177
181
))
178
182
}
183
+
184
+ func convertObjectToUnstructured (obj * authorizationv1.SubjectAccessReviewSpec ) map [string ]interface {} {
185
+ // Construct version containing every SubjectAccessReview user and string attribute field, even omitempty ones, for evaluation by CEL
186
+ extra := obj .Extra
187
+ if extra == nil {
188
+ extra = map [string ]authorizationv1.ExtraValue {}
189
+ }
190
+ ret := map [string ]interface {}{
191
+ "user" : obj .User ,
192
+ "groups" : obj .Groups ,
193
+ "uid" : string (obj .UID ),
194
+ "extra" : extra ,
195
+ }
196
+ if obj .ResourceAttributes != nil {
197
+ ret ["resourceAttributes" ] = map [string ]string {
198
+ "namespace" : obj .ResourceAttributes .Namespace ,
199
+ "verb" : obj .ResourceAttributes .Verb ,
200
+ "group" : obj .ResourceAttributes .Group ,
201
+ "version" : obj .ResourceAttributes .Version ,
202
+ "resource" : obj .ResourceAttributes .Resource ,
203
+ "subresource" : obj .ResourceAttributes .Subresource ,
204
+ "name" : obj .ResourceAttributes .Name ,
205
+ }
206
+ }
207
+ if obj .NonResourceAttributes != nil {
208
+ ret ["nonResourceAttributes" ] = map [string ]string {
209
+ "verb" : obj .NonResourceAttributes .Verb ,
210
+ "path" : obj .NonResourceAttributes .Path ,
211
+ }
212
+ }
213
+ return ret
214
+ }
0 commit comments