| 1 |
/* |
| 2 |
Copyright 2023 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package library |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"reflect" |
| 23 |
"strings" |
| 24 |
|
| 25 |
"k8s.io/apimachinery/pkg/fields" |
| 26 |
"k8s.io/apimachinery/pkg/labels" |
| 27 |
genericfeatures "k8s.io/apiserver/pkg/features" |
| 28 |
utilfeature "k8s.io/apiserver/pkg/util/feature" |
| 29 |
|
| 30 |
"github.com/google/cel-go/cel" |
| 31 |
"github.com/google/cel-go/common/types" |
| 32 |
"github.com/google/cel-go/common/types/ref" |
| 33 |
|
| 34 |
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation" |
| 35 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 36 |
"k8s.io/apiserver/pkg/authentication/serviceaccount" |
| 37 |
"k8s.io/apiserver/pkg/authentication/user" |
| 38 |
"k8s.io/apiserver/pkg/authorization/authorizer" |
| 39 |
) |
| 40 |
|
| 41 |
// Authz provides a CEL function library extension for performing authorization checks. |
| 42 |
// Note that authorization checks are only supported for CEL expression fields in the API |
| 43 |
// where an 'authorizer' variable is provided to the CEL expression. See the |
| 44 |
// documentation of API fields where CEL expressions are used to learn if the 'authorizer' |
| 45 |
// variable is provided. |
| 46 |
// |
| 47 |
// path |
| 48 |
// |
| 49 |
// Returns a PathCheck configured to check authorization for a non-resource request |
| 50 |
// path (e.g. /healthz). If path is an empty string, an error is returned. |
| 51 |
// Note that the leading '/' is not required. |
| 52 |
// |
| 53 |
// <Authorizer>.path(<string>) <PathCheck> |
| 54 |
// |
| 55 |
// Examples: |
| 56 |
// |
| 57 |
// authorizer.path('/healthz') // returns a PathCheck for the '/healthz' API path |
| 58 |
// authorizer.path('') // results in "path must not be empty" error |
| 59 |
// authorizer.path(' ') // results in "path must not be empty" error |
| 60 |
// |
| 61 |
// group |
| 62 |
// |
| 63 |
// Returns a GroupCheck configured to check authorization for the API resources for |
| 64 |
// a particular API group. |
| 65 |
// Note that authorization checks are only supported for CEL expression fields in the API |
| 66 |
// where an 'authorizer' variable is provided to the CEL expression. Check the |
| 67 |
// documentation of API fields where CEL expressions are used to learn if the 'authorizer' |
| 68 |
// variable is provided. |
| 69 |
// |
| 70 |
// <Authorizer>.group(<string>) <GroupCheck> |
| 71 |
// |
| 72 |
// Examples: |
| 73 |
// |
| 74 |
// authorizer.group('apps') // returns a GroupCheck for the 'apps' API group |
| 75 |
// authorizer.group('') // returns a GroupCheck for the core API group |
| 76 |
// authorizer.group('example.com') // returns a GroupCheck for the custom resources in the 'example.com' API group |
| 77 |
// |
| 78 |
// serviceAccount |
| 79 |
// |
| 80 |
// Returns an Authorizer configured to check authorization for the provided service account namespace and name. |
| 81 |
// If the name is not a valid DNS subdomain string (as defined by RFC 1123), an error is returned. |
| 82 |
// If the namespace is not a valid DNS label (as defined by RFC 1123), an error is returned. |
| 83 |
// |
| 84 |
// <Authorizer>.serviceAccount(<string>, <string>) <Authorizer> |
| 85 |
// |
| 86 |
// Examples: |
| 87 |
// |
| 88 |
// authorizer.serviceAccount('default', 'myserviceaccount') // returns an Authorizer for the service account with namespace 'default' and name 'myserviceaccount' |
| 89 |
// authorizer.serviceAccount('not@a#valid!namespace', 'validname') // returns an error |
| 90 |
// authorizer.serviceAccount('valid.example.com', 'invalid@*name') // returns an error |
| 91 |
// |
| 92 |
// resource |
| 93 |
// |
| 94 |
// Returns a ResourceCheck configured to check authorization for a particular API resource. |
| 95 |
// Note that the provided resource string should be a lower case plural name of a Kubernetes API resource. |
| 96 |
// |
| 97 |
// <GroupCheck>.resource(<string>) <ResourceCheck> |
| 98 |
// |
| 99 |
// Examples: |
| 100 |
// |
| 101 |
// authorizer.group('apps').resource('deployments') // returns a ResourceCheck for the 'deployments' resources in the 'apps' group. |
| 102 |
// authorizer.group('').resource('pods') // returns a ResourceCheck for the 'pods' resources in the core group. |
| 103 |
// authorizer.group('apps').resource('') // results in "resource must not be empty" error |
| 104 |
// authorizer.group('apps').resource(' ') // results in "resource must not be empty" error |
| 105 |
// |
| 106 |
// subresource |
| 107 |
// |
| 108 |
// Returns a ResourceCheck configured to check authorization for a particular subresource of an API resource. |
| 109 |
// If subresource is set to "", the subresource field of this ResourceCheck is considered unset. |
| 110 |
// |
| 111 |
// <ResourceCheck>.subresource(<string>) <ResourceCheck> |
| 112 |
// |
| 113 |
// Examples: |
| 114 |
// |
| 115 |
// authorizer.group('').resource('pods').subresource('status') // returns a ResourceCheck the 'status' subresource of 'pods' |
| 116 |
// authorizer.group('apps').resource('deployments').subresource('scale') // returns a ResourceCheck the 'scale' subresource of 'deployments' |
| 117 |
// authorizer.group('example.com').resource('widgets').subresource('scale') // returns a ResourceCheck for the 'scale' subresource of the 'widgets' custom resource |
| 118 |
// authorizer.group('example.com').resource('widgets').subresource('') // returns a ResourceCheck for the 'widgets' resource. |
| 119 |
// |
| 120 |
// namespace |
| 121 |
// |
| 122 |
// Returns a ResourceCheck configured to check authorization for a particular namespace. |
| 123 |
// For cluster scoped resources, namespace() does not need to be called; namespace defaults |
| 124 |
// to "", which is the correct namespace value to use to check cluster scoped resources. |
| 125 |
// If namespace is set to "", the ResourceCheck will check authorization for the cluster scope. |
| 126 |
// |
| 127 |
// <ResourceCheck>.namespace(<string>) <ResourceCheck> |
| 128 |
// |
| 129 |
// Examples: |
| 130 |
// |
| 131 |
// authorizer.group('apps').resource('deployments').namespace('test') // returns a ResourceCheck for 'deployments' in the 'test' namespace |
| 132 |
// authorizer.group('').resource('pods').namespace('default') // returns a ResourceCheck for 'pods' in the 'default' namespace |
| 133 |
// authorizer.group('').resource('widgets').namespace('') // returns a ResourceCheck for 'widgets' in the cluster scope |
| 134 |
// |
| 135 |
// name |
| 136 |
// |
| 137 |
// Returns a ResourceCheck configured to check authorization for a particular resource name. |
| 138 |
// If name is set to "", the name field of this ResourceCheck is considered unset. |
| 139 |
// |
| 140 |
// <ResourceCheck>.name(<name>) <ResourceCheck> |
| 141 |
// |
| 142 |
// Examples: |
| 143 |
// |
| 144 |
// authorizer.group('apps').resource('deployments').namespace('test').name('backend') // returns a ResourceCheck for the 'backend' 'deployments' resource in the 'test' namespace |
| 145 |
// authorizer.group('apps').resource('deployments').namespace('test').name('') // returns a ResourceCheck for the 'deployments' resource in the 'test' namespace |
| 146 |
// |
| 147 |
// check |
| 148 |
// |
| 149 |
// For PathCheck, checks if the principal (user or service account) that sent the request is authorized for the HTTP request verb of the path. |
| 150 |
// For ResourceCheck, checks if the principal (user or service account) that sent the request is authorized for the API verb and the configured authorization checks of the ResourceCheck. |
| 151 |
// The check operation can be expensive, particularly in clusters using the webhook authorization mode. |
| 152 |
// |
| 153 |
// <PathCheck>.check(<check>) <Decision> |
| 154 |
// <ResourceCheck>.check(<check>) <Decision> |
| 155 |
// |
| 156 |
// Examples: |
| 157 |
// |
| 158 |
// authorizer.group('').resource('pods').namespace('default').check('create') // Checks if the principal (user or service account) is authorized create pods in the 'default' namespace. |
| 159 |
// authorizer.path('/healthz').check('get') // Checks if the principal (user or service account) is authorized to make HTTP GET requests to the /healthz API path. |
| 160 |
// |
| 161 |
// allowed |
| 162 |
// |
| 163 |
// Returns true if the authorizer's decision for the check is "allow". Note that if the authorizer's decision is |
| 164 |
// "no opinion", that the 'allowed' function will return false. |
| 165 |
// |
| 166 |
// <Decision>.allowed() <bool> |
| 167 |
// |
| 168 |
// Examples: |
| 169 |
// |
| 170 |
// authorizer.group('').resource('pods').namespace('default').check('create').allowed() // Returns true if the principal (user or service account) is allowed create pods in the 'default' namespace. |
| 171 |
// authorizer.path('/healthz').check('get').allowed() // Returns true if the principal (user or service account) is allowed to make HTTP GET requests to the /healthz API path. |
| 172 |
// |
| 173 |
// reason |
| 174 |
// |
| 175 |
// Returns a string reason for the authorization decision |
| 176 |
// |
| 177 |
// <Decision>.reason() <string> |
| 178 |
// |
| 179 |
// Examples: |
| 180 |
// |
| 181 |
// authorizer.path('/healthz').check('GET').reason() |
| 182 |
// |
| 183 |
// errored |
| 184 |
// |
| 185 |
// Returns true if the authorization check resulted in an error. |
| 186 |
// |
| 187 |
// <Decision>.errored() <bool> |
| 188 |
// |
| 189 |
// Examples: |
| 190 |
// |
| 191 |
// authorizer.group('').resource('pods').namespace('default').check('create').errored() // Returns true if the authorization check resulted in an error |
| 192 |
// |
| 193 |
// error |
| 194 |
// |
| 195 |
// If the authorization check resulted in an error, returns the error. Otherwise, returns the empty string. |
| 196 |
// |
| 197 |
// <Decision>.error() <string> |
| 198 |
// |
| 199 |
// Examples: |
| 200 |
// |
| 201 |
// authorizer.group('').resource('pods').namespace('default').check('create').error() |
| 202 |
// |
| 203 |
// fieldSelector |
| 204 |
// |
| 205 |
// Takes a string field selector, parses it to field selector requirements, and includes it in the authorization check. |
| 206 |
// If the field selector does not parse successfully, no field selector requirements are included in the authorization check. |
| 207 |
// Added in Kubernetes 1.31+, Authz library version 1. |
| 208 |
// |
| 209 |
// <ResourceCheck>.fieldSelector(<string>) <ResourceCheck> |
| 210 |
// |
| 211 |
// Examples: |
| 212 |
// |
| 213 |
// authorizer.group('').resource('pods').fieldSelector('spec.nodeName=mynode').check('list').allowed() |
| 214 |
// |
| 215 |
// labelSelector (added in v1, Kubernetes 1.31+) |
| 216 |
// |
| 217 |
// Takes a string label selector, parses it to label selector requirements, and includes it in the authorization check. |
| 218 |
// If the label selector does not parse successfully, no label selector requirements are included in the authorization check. |
| 219 |
// Added in Kubernetes 1.31+, Authz library version 1. |
| 220 |
// |
| 221 |
// <ResourceCheck>.labelSelector(<string>) <ResourceCheck> |
| 222 |
// |
| 223 |
// Examples: |
| 224 |
// |
| 225 |
// authorizer.group('').resource('pods').labelSelector('app=example').check('list').allowed() |
| 226 |
func Authz() cel.EnvOption { |
| 227 |
return cel.Lib(authzLib) |
| 228 |
} |
| 229 |
|
| 230 |
var authzLib = &authz{} |
| 231 |
|
| 232 |
type authz struct{} |
| 233 |
|
| 234 |
func (*authz) LibraryName() string { |
| 235 |
return "kubernetes.authz" |
| 236 |
} |
| 237 |
|
| 238 |
func (*authz) Types() []*cel.Type { |
| 239 |
return []*cel.Type{ |
| 240 |
AuthorizerType, |
| 241 |
PathCheckType, |
| 242 |
GroupCheckType, |
| 243 |
ResourceCheckType, |
| 244 |
DecisionType} |
| 245 |
} |
| 246 |
|
| 247 |
func (*authz) declarations() map[string][]cel.FunctionOpt { |
| 248 |
return authzLibraryDecls |
| 249 |
} |
| 250 |
|
| 251 |
var authzLibraryDecls = map[string][]cel.FunctionOpt{ |
| 252 |
"path": { |
| 253 |
cel.MemberOverload("authorizer_path", []*cel.Type{AuthorizerType, cel.StringType}, PathCheckType, |
| 254 |
cel.BinaryBinding(authorizerPath))}, |
| 255 |
"group": { |
| 256 |
cel.MemberOverload("authorizer_group", []*cel.Type{AuthorizerType, cel.StringType}, GroupCheckType, |
| 257 |
cel.BinaryBinding(authorizerGroup))}, |
| 258 |
"serviceAccount": { |
| 259 |
cel.MemberOverload("authorizer_serviceaccount", []*cel.Type{AuthorizerType, cel.StringType, cel.StringType}, AuthorizerType, |
| 260 |
cel.FunctionBinding(authorizerServiceAccount))}, |
| 261 |
"resource": { |
| 262 |
cel.MemberOverload("groupcheck_resource", []*cel.Type{GroupCheckType, cel.StringType}, ResourceCheckType, |
| 263 |
cel.BinaryBinding(groupCheckResource))}, |
| 264 |
"subresource": { |
| 265 |
cel.MemberOverload("resourcecheck_subresource", []*cel.Type{ResourceCheckType, cel.StringType}, ResourceCheckType, |
| 266 |
cel.BinaryBinding(resourceCheckSubresource))}, |
| 267 |
"namespace": { |
| 268 |
cel.MemberOverload("resourcecheck_namespace", []*cel.Type{ResourceCheckType, cel.StringType}, ResourceCheckType, |
| 269 |
cel.BinaryBinding(resourceCheckNamespace))}, |
| 270 |
"name": { |
| 271 |
cel.MemberOverload("resourcecheck_name", []*cel.Type{ResourceCheckType, cel.StringType}, ResourceCheckType, |
| 272 |
cel.BinaryBinding(resourceCheckName))}, |
| 273 |
"check": { |
| 274 |
cel.MemberOverload("pathcheck_check", []*cel.Type{PathCheckType, cel.StringType}, DecisionType, |
| 275 |
cel.BinaryBinding(pathCheckCheck)), |
| 276 |
cel.MemberOverload("resourcecheck_check", []*cel.Type{ResourceCheckType, cel.StringType}, DecisionType, |
| 277 |
cel.BinaryBinding(resourceCheckCheck))}, |
| 278 |
"errored": { |
| 279 |
cel.MemberOverload("decision_errored", []*cel.Type{DecisionType}, cel.BoolType, |
| 280 |
cel.UnaryBinding(decisionErrored))}, |
| 281 |
"error": { |
| 282 |
cel.MemberOverload("decision_error", []*cel.Type{DecisionType}, cel.StringType, |
| 283 |
cel.UnaryBinding(decisionError))}, |
| 284 |
"allowed": { |
| 285 |
cel.MemberOverload("decision_allowed", []*cel.Type{DecisionType}, cel.BoolType, |
| 286 |
cel.UnaryBinding(decisionAllowed))}, |
| 287 |
"reason": { |
| 288 |
cel.MemberOverload("decision_reason", []*cel.Type{DecisionType}, cel.StringType, |
| 289 |
cel.UnaryBinding(decisionReason))}, |
| 290 |
} |
| 291 |
|
| 292 |
func (*authz) CompileOptions() []cel.EnvOption { |
| 293 |
options := make([]cel.EnvOption, 0, len(authzLibraryDecls)) |
| 294 |
for name, overloads := range authzLibraryDecls { |
| 295 |
options = append(options, cel.Function(name, overloads...)) |
| 296 |
} |
| 297 |
return options |
| 298 |
} |
| 299 |
|
| 300 |
func (*authz) ProgramOptions() []cel.ProgramOption { |
| 301 |
return []cel.ProgramOption{} |
| 302 |
} |
| 303 |
|
| 304 |
// AuthzSelectors provides a CEL function library extension for adding fieldSelector and |
| 305 |
// labelSelector filters to authorization checks. This requires the Authz library. |
| 306 |
// See documentation of the Authz library for use and availability of the authorizer variable. |
| 307 |
// |
| 308 |
// fieldSelector |
| 309 |
// |
| 310 |
// Takes a string field selector, parses it to field selector requirements, and includes it in the authorization check. |
| 311 |
// If the field selector does not parse successfully, no field selector requirements are included in the authorization check. |
| 312 |
// Added in Kubernetes 1.31+. |
| 313 |
// |
| 314 |
// <ResourceCheck>.fieldSelector(<string>) <ResourceCheck> |
| 315 |
// |
| 316 |
// Examples: |
| 317 |
// |
| 318 |
// authorizer.group('').resource('pods').fieldSelector('spec.nodeName=mynode').check('list').allowed() |
| 319 |
// |
| 320 |
// labelSelector |
| 321 |
// |
| 322 |
// Takes a string label selector, parses it to label selector requirements, and includes it in the authorization check. |
| 323 |
// If the label selector does not parse successfully, no label selector requirements are included in the authorization check. |
| 324 |
// Added in Kubernetes 1.31+. |
| 325 |
// |
| 326 |
// <ResourceCheck>.labelSelector(<string>) <ResourceCheck> |
| 327 |
// |
| 328 |
// Examples: |
| 329 |
// |
| 330 |
// authorizer.group('').resource('pods').labelSelector('app=example').check('list').allowed() |
| 331 |
func AuthzSelectors() cel.EnvOption { |
| 332 |
return cel.Lib(authzSelectorsLib) |
| 333 |
} |
| 334 |
|
| 335 |
var authzSelectorsLib = &authzSelectors{} |
| 336 |
|
| 337 |
type authzSelectors struct{} |
| 338 |
|
| 339 |
func (*authzSelectors) LibraryName() string { |
| 340 |
return "kubernetes.authzSelectors" |
| 341 |
} |
| 342 |
|
| 343 |
func (*authzSelectors) Types() []*cel.Type { |
| 344 |
return []*cel.Type{ResourceCheckType} |
| 345 |
} |
| 346 |
|
| 347 |
func (*authzSelectors) declarations() map[string][]cel.FunctionOpt { |
| 348 |
return authzSelectorsLibraryDecls |
| 349 |
} |
| 350 |
|
| 351 |
var authzSelectorsLibraryDecls = map[string][]cel.FunctionOpt{ |
| 352 |
"fieldSelector": { |
| 353 |
cel.MemberOverload("authorizer_fieldselector", []*cel.Type{ResourceCheckType, cel.StringType}, ResourceCheckType, |
| 354 |
cel.BinaryBinding(resourceCheckFieldSelector))}, |
| 355 |
"labelSelector": { |
| 356 |
cel.MemberOverload("authorizer_labelselector", []*cel.Type{ResourceCheckType, cel.StringType}, ResourceCheckType, |
| 357 |
cel.BinaryBinding(resourceCheckLabelSelector))}, |
| 358 |
} |
| 359 |
|
| 360 |
func (*authzSelectors) CompileOptions() []cel.EnvOption { |
| 361 |
options := make([]cel.EnvOption, 0, len(authzSelectorsLibraryDecls)) |
| 362 |
for name, overloads := range authzSelectorsLibraryDecls { |
| 363 |
options = append(options, cel.Function(name, overloads...)) |
| 364 |
} |
| 365 |
return options |
| 366 |
} |
| 367 |
|
| 368 |
func (*authzSelectors) ProgramOptions() []cel.ProgramOption { |
| 369 |
return []cel.ProgramOption{} |
| 370 |
} |
| 371 |
|
| 372 |
func authorizerPath(arg1, arg2 ref.Val) ref.Val { |
| 373 |
authz, ok := arg1.(authorizerVal) |
| 374 |
if !ok { |
| 375 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 376 |
} |
| 377 |
|
| 378 |
path, ok := arg2.Value().(string) |
| 379 |
if !ok { |
| 380 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 381 |
} |
| 382 |
|
| 383 |
if len(strings.TrimSpace(path)) == 0 { |
| 384 |
return types.NewErr("path must not be empty") |
| 385 |
} |
| 386 |
|
| 387 |
return authz.pathCheck(path) |
| 388 |
} |
| 389 |
|
| 390 |
func authorizerGroup(arg1, arg2 ref.Val) ref.Val { |
| 391 |
authz, ok := arg1.(authorizerVal) |
| 392 |
if !ok { |
| 393 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 394 |
} |
| 395 |
|
| 396 |
group, ok := arg2.Value().(string) |
| 397 |
if !ok { |
| 398 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 399 |
} |
| 400 |
|
| 401 |
return authz.groupCheck(group) |
| 402 |
} |
| 403 |
|
| 404 |
func authorizerServiceAccount(args ...ref.Val) ref.Val { |
| 405 |
argn := len(args) |
| 406 |
if argn != 3 { |
| 407 |
return types.NoSuchOverloadErr() |
| 408 |
} |
| 409 |
|
| 410 |
authz, ok := args[0].(authorizerVal) |
| 411 |
if !ok { |
| 412 |
return types.MaybeNoSuchOverloadErr(args[0]) |
| 413 |
} |
| 414 |
|
| 415 |
namespace, ok := args[1].Value().(string) |
| 416 |
if !ok { |
| 417 |
return types.MaybeNoSuchOverloadErr(args[1]) |
| 418 |
} |
| 419 |
|
| 420 |
name, ok := args[2].Value().(string) |
| 421 |
if !ok { |
| 422 |
return types.MaybeNoSuchOverloadErr(args[2]) |
| 423 |
} |
| 424 |
|
| 425 |
if errors := apimachineryvalidation.ValidateServiceAccountName(name, false); len(errors) > 0 { |
| 426 |
return types.NewErr("Invalid service account name") |
| 427 |
} |
| 428 |
if errors := apimachineryvalidation.ValidateNamespaceName(namespace, false); len(errors) > 0 { |
| 429 |
return types.NewErr("Invalid service account namespace") |
| 430 |
} |
| 431 |
return authz.serviceAccount(namespace, name) |
| 432 |
} |
| 433 |
|
| 434 |
func groupCheckResource(arg1, arg2 ref.Val) ref.Val { |
| 435 |
groupCheck, ok := arg1.(groupCheckVal) |
| 436 |
if !ok { |
| 437 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 438 |
} |
| 439 |
|
| 440 |
resource, ok := arg2.Value().(string) |
| 441 |
if !ok { |
| 442 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 443 |
} |
| 444 |
|
| 445 |
if len(strings.TrimSpace(resource)) == 0 { |
| 446 |
return types.NewErr("resource must not be empty") |
| 447 |
} |
| 448 |
return groupCheck.resourceCheck(resource) |
| 449 |
} |
| 450 |
|
| 451 |
func resourceCheckSubresource(arg1, arg2 ref.Val) ref.Val { |
| 452 |
resourceCheck, ok := arg1.(resourceCheckVal) |
| 453 |
if !ok { |
| 454 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 455 |
} |
| 456 |
|
| 457 |
subresource, ok := arg2.Value().(string) |
| 458 |
if !ok { |
| 459 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 460 |
} |
| 461 |
|
| 462 |
result := resourceCheck |
| 463 |
result.subresource = subresource |
| 464 |
return result |
| 465 |
} |
| 466 |
|
| 467 |
func resourceCheckFieldSelector(arg1, arg2 ref.Val) ref.Val { |
| 468 |
resourceCheck, ok := arg1.(resourceCheckVal) |
| 469 |
if !ok { |
| 470 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 471 |
} |
| 472 |
|
| 473 |
fieldSelector, ok := arg2.Value().(string) |
| 474 |
if !ok { |
| 475 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 476 |
} |
| 477 |
|
| 478 |
result := resourceCheck |
| 479 |
result.fieldSelector = fieldSelector |
| 480 |
return result |
| 481 |
} |
| 482 |
|
| 483 |
func resourceCheckLabelSelector(arg1, arg2 ref.Val) ref.Val { |
| 484 |
resourceCheck, ok := arg1.(resourceCheckVal) |
| 485 |
if !ok { |
| 486 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 487 |
} |
| 488 |
|
| 489 |
labelSelector, ok := arg2.Value().(string) |
| 490 |
if !ok { |
| 491 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 492 |
} |
| 493 |
|
| 494 |
result := resourceCheck |
| 495 |
result.labelSelector = labelSelector |
| 496 |
return result |
| 497 |
} |
| 498 |
|
| 499 |
func resourceCheckNamespace(arg1, arg2 ref.Val) ref.Val { |
| 500 |
resourceCheck, ok := arg1.(resourceCheckVal) |
| 501 |
if !ok { |
| 502 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 503 |
} |
| 504 |
|
| 505 |
namespace, ok := arg2.Value().(string) |
| 506 |
if !ok { |
| 507 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 508 |
} |
| 509 |
|
| 510 |
result := resourceCheck |
| 511 |
result.namespace = namespace |
| 512 |
return result |
| 513 |
} |
| 514 |
|
| 515 |
func resourceCheckName(arg1, arg2 ref.Val) ref.Val { |
| 516 |
resourceCheck, ok := arg1.(resourceCheckVal) |
| 517 |
if !ok { |
| 518 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 519 |
} |
| 520 |
|
| 521 |
name, ok := arg2.Value().(string) |
| 522 |
if !ok { |
| 523 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 524 |
} |
| 525 |
|
| 526 |
result := resourceCheck |
| 527 |
result.name = name |
| 528 |
return result |
| 529 |
} |
| 530 |
|
| 531 |
func pathCheckCheck(arg1, arg2 ref.Val) ref.Val { |
| 532 |
pathCheck, ok := arg1.(pathCheckVal) |
| 533 |
if !ok { |
| 534 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 535 |
} |
| 536 |
|
| 537 |
httpRequestVerb, ok := arg2.Value().(string) |
| 538 |
if !ok { |
| 539 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 540 |
} |
| 541 |
|
| 542 |
return pathCheck.Authorize(context.TODO(), httpRequestVerb) |
| 543 |
} |
| 544 |
|
| 545 |
func resourceCheckCheck(arg1, arg2 ref.Val) ref.Val { |
| 546 |
resourceCheck, ok := arg1.(resourceCheckVal) |
| 547 |
if !ok { |
| 548 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 549 |
} |
| 550 |
|
| 551 |
apiVerb, ok := arg2.Value().(string) |
| 552 |
if !ok { |
| 553 |
return types.MaybeNoSuchOverloadErr(arg1) |
| 554 |
} |
| 555 |
|
| 556 |
return resourceCheck.Authorize(context.TODO(), apiVerb) |
| 557 |
} |
| 558 |
|
| 559 |
func decisionErrored(arg ref.Val) ref.Val { |
| 560 |
decision, ok := arg.(decisionVal) |
| 561 |
if !ok { |
| 562 |
return types.MaybeNoSuchOverloadErr(arg) |
| 563 |
} |
| 564 |
|
| 565 |
return types.Bool(decision.err != nil) |
| 566 |
} |
| 567 |
|
| 568 |
func decisionError(arg ref.Val) ref.Val { |
| 569 |
decision, ok := arg.(decisionVal) |
| 570 |
if !ok { |
| 571 |
return types.MaybeNoSuchOverloadErr(arg) |
| 572 |
} |
| 573 |
|
| 574 |
if decision.err == nil { |
| 575 |
return types.String("") |
| 576 |
} |
| 577 |
return types.String(decision.err.Error()) |
| 578 |
} |
| 579 |
|
| 580 |
func decisionAllowed(arg ref.Val) ref.Val { |
| 581 |
decision, ok := arg.(decisionVal) |
| 582 |
if !ok { |
| 583 |
return types.MaybeNoSuchOverloadErr(arg) |
| 584 |
} |
| 585 |
|
| 586 |
return types.Bool(decision.authDecision == authorizer.DecisionAllow) |
| 587 |
} |
| 588 |
|
| 589 |
func decisionReason(arg ref.Val) ref.Val { |
| 590 |
decision, ok := arg.(decisionVal) |
| 591 |
if !ok { |
| 592 |
return types.MaybeNoSuchOverloadErr(arg) |
| 593 |
} |
| 594 |
|
| 595 |
return types.String(decision.reason) |
| 596 |
} |
| 597 |
|
| 598 |
var ( |
| 599 |
AuthorizerType = cel.ObjectType("kubernetes.authorization.Authorizer") |
| 600 |
PathCheckType = cel.ObjectType("kubernetes.authorization.PathCheck") |
| 601 |
GroupCheckType = cel.ObjectType("kubernetes.authorization.GroupCheck") |
| 602 |
ResourceCheckType = cel.ObjectType("kubernetes.authorization.ResourceCheck") |
| 603 |
DecisionType = cel.ObjectType("kubernetes.authorization.Decision") |
| 604 |
) |
| 605 |
|
| 606 |
// Resource represents an API resource |
| 607 |
type Resource interface { |
| 608 |
// GetName returns the name of the object as presented in the request. On a CREATE operation, the client |
| 609 |
// may omit name and rely on the server to generate the name. If that is the case, this method will return |
| 610 |
// the empty string |
| 611 |
GetName() string |
| 612 |
// GetNamespace is the namespace associated with the request (if any) |
| 613 |
GetNamespace() string |
| 614 |
// GetResource is the name of the resource being requested. This is not the kind. For example: pods |
| 615 |
GetResource() schema.GroupVersionResource |
| 616 |
// GetSubresource is the name of the subresource being requested. This is a different resource, scoped to the parent resource, but it may have a different kind. |
| 617 |
// For instance, /pods has the resource "pods" and the kind "Pod", while /pods/foo/status has the resource "pods", the sub resource "status", and the kind "Pod" |
| 618 |
// (because status operates on pods). The binding resource for a pod though may be /pods/foo/binding, which has resource "pods", subresource "binding", and kind "Binding". |
| 619 |
GetSubresource() string |
| 620 |
} |
| 621 |
|
| 622 |
func NewAuthorizerVal(userInfo user.Info, authorizer authorizer.Authorizer) ref.Val { |
| 623 |
return authorizerVal{receiverOnlyObjectVal: receiverOnlyVal(AuthorizerType), userInfo: userInfo, authAuthorizer: authorizer} |
| 624 |
} |
| 625 |
|
| 626 |
func NewResourceAuthorizerVal(userInfo user.Info, authorizer authorizer.Authorizer, requestResource Resource) ref.Val { |
| 627 |
a := authorizerVal{receiverOnlyObjectVal: receiverOnlyVal(AuthorizerType), userInfo: userInfo, authAuthorizer: authorizer} |
| 628 |
resource := requestResource.GetResource() |
| 629 |
g := a.groupCheck(resource.Group) |
| 630 |
r := g.resourceCheck(resource.Resource) |
| 631 |
r.subresource = requestResource.GetSubresource() |
| 632 |
r.namespace = requestResource.GetNamespace() |
| 633 |
r.name = requestResource.GetName() |
| 634 |
return r |
| 635 |
} |
| 636 |
|
| 637 |
type authorizerVal struct { |
| 638 |
receiverOnlyObjectVal |
| 639 |
userInfo user.Info |
| 640 |
authAuthorizer authorizer.Authorizer |
| 641 |
} |
| 642 |
|
| 643 |
func (a authorizerVal) pathCheck(path string) pathCheckVal { |
| 644 |
return pathCheckVal{receiverOnlyObjectVal: receiverOnlyVal(PathCheckType), authorizer: a, path: path} |
| 645 |
} |
| 646 |
|
| 647 |
func (a authorizerVal) groupCheck(group string) groupCheckVal { |
| 648 |
return groupCheckVal{receiverOnlyObjectVal: receiverOnlyVal(GroupCheckType), authorizer: a, group: group} |
| 649 |
} |
| 650 |
|
| 651 |
func (a authorizerVal) serviceAccount(namespace, name string) authorizerVal { |
| 652 |
sa := &serviceaccount.ServiceAccountInfo{Name: name, Namespace: namespace} |
| 653 |
return authorizerVal{ |
| 654 |
receiverOnlyObjectVal: receiverOnlyVal(AuthorizerType), |
| 655 |
userInfo: sa.UserInfo(), |
| 656 |
authAuthorizer: a.authAuthorizer, |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
type pathCheckVal struct { |
| 661 |
receiverOnlyObjectVal |
| 662 |
authorizer authorizerVal |
| 663 |
path string |
| 664 |
} |
| 665 |
|
| 666 |
func (a pathCheckVal) Authorize(ctx context.Context, verb string) ref.Val { |
| 667 |
attr := &authorizer.AttributesRecord{ |
| 668 |
Path: a.path, |
| 669 |
Verb: verb, |
| 670 |
User: a.authorizer.userInfo, |
| 671 |
} |
| 672 |
|
| 673 |
decision, reason, err := a.authorizer.authAuthorizer.Authorize(ctx, attr) |
| 674 |
return newDecision(decision, err, reason) |
| 675 |
} |
| 676 |
|
| 677 |
type groupCheckVal struct { |
| 678 |
receiverOnlyObjectVal |
| 679 |
authorizer authorizerVal |
| 680 |
group string |
| 681 |
} |
| 682 |
|
| 683 |
func (g groupCheckVal) resourceCheck(resource string) resourceCheckVal { |
| 684 |
return resourceCheckVal{receiverOnlyObjectVal: receiverOnlyVal(ResourceCheckType), groupCheck: g, resource: resource} |
| 685 |
} |
| 686 |
|
| 687 |
type resourceCheckVal struct { |
| 688 |
receiverOnlyObjectVal |
| 689 |
groupCheck groupCheckVal |
| 690 |
resource string |
| 691 |
subresource string |
| 692 |
namespace string |
| 693 |
name string |
| 694 |
fieldSelector string |
| 695 |
labelSelector string |
| 696 |
} |
| 697 |
|
| 698 |
func (a resourceCheckVal) Authorize(ctx context.Context, verb string) ref.Val { |
| 699 |
attr := &authorizer.AttributesRecord{ |
| 700 |
ResourceRequest: true, |
| 701 |
APIGroup: a.groupCheck.group, |
| 702 |
APIVersion: "*", |
| 703 |
Resource: a.resource, |
| 704 |
Subresource: a.subresource, |
| 705 |
Namespace: a.namespace, |
| 706 |
Name: a.name, |
| 707 |
Verb: verb, |
| 708 |
User: a.groupCheck.authorizer.userInfo, |
| 709 |
} |
| 710 |
|
| 711 |
if utilfeature.DefaultFeatureGate.Enabled(genericfeatures.AuthorizeWithSelectors) { |
| 712 |
if len(a.fieldSelector) > 0 { |
| 713 |
selector, err := fields.ParseSelector(a.fieldSelector) |
| 714 |
if err != nil { |
| 715 |
attr.FieldSelectorRequirements, attr.FieldSelectorParsingErr = nil, err |
| 716 |
} else { |
| 717 |
attr.FieldSelectorRequirements, attr.FieldSelectorParsingErr = selector.Requirements(), nil |
| 718 |
} |
| 719 |
} |
| 720 |
if len(a.labelSelector) > 0 { |
| 721 |
requirements, err := labels.ParseToRequirements(a.labelSelector) |
| 722 |
if err != nil { |
| 723 |
attr.LabelSelectorRequirements, attr.LabelSelectorParsingErr = nil, err |
| 724 |
} else { |
| 725 |
attr.LabelSelectorRequirements, attr.LabelSelectorParsingErr = requirements, nil |
| 726 |
} |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
decision, reason, err := a.groupCheck.authorizer.authAuthorizer.Authorize(ctx, attr) |
| 731 |
return newDecision(decision, err, reason) |
| 732 |
} |
| 733 |
|
| 734 |
func newDecision(authDecision authorizer.Decision, err error, reason string) decisionVal { |
| 735 |
return decisionVal{receiverOnlyObjectVal: receiverOnlyVal(DecisionType), authDecision: authDecision, err: err, reason: reason} |
| 736 |
} |
| 737 |
|
| 738 |
type decisionVal struct { |
| 739 |
receiverOnlyObjectVal |
| 740 |
err error |
| 741 |
authDecision authorizer.Decision |
| 742 |
reason string |
| 743 |
} |
| 744 |
|
| 745 |
// receiverOnlyObjectVal provides an implementation of ref.Val for |
| 746 |
// any object type that has receiver functions but does not expose any fields to |
| 747 |
// CEL. |
| 748 |
type receiverOnlyObjectVal struct { |
| 749 |
typeValue *types.Type |
| 750 |
} |
| 751 |
|
| 752 |
// receiverOnlyVal returns a receiverOnlyObjectVal for the given type. |
| 753 |
func receiverOnlyVal(objectType *cel.Type) receiverOnlyObjectVal { |
| 754 |
return receiverOnlyObjectVal{typeValue: types.NewTypeValue(objectType.String())} |
| 755 |
} |
| 756 |
|
| 757 |
// ConvertToNative implements ref.Val.ConvertToNative. |
| 758 |
func (a receiverOnlyObjectVal) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 759 |
return nil, fmt.Errorf("type conversion error from '%s' to '%v'", a.typeValue.String(), typeDesc) |
| 760 |
} |
| 761 |
|
| 762 |
// ConvertToType implements ref.Val.ConvertToType. |
| 763 |
func (a receiverOnlyObjectVal) ConvertToType(typeVal ref.Type) ref.Val { |
| 764 |
switch typeVal { |
| 765 |
case a.typeValue: |
| 766 |
return a |
| 767 |
case types.TypeType: |
| 768 |
return a.typeValue |
| 769 |
} |
| 770 |
return types.NewErr("type conversion error from '%s' to '%s'", a.typeValue, typeVal) |
| 771 |
} |
| 772 |
|
| 773 |
// Equal implements ref.Val.Equal. |
| 774 |
func (a receiverOnlyObjectVal) Equal(other ref.Val) ref.Val { |
| 775 |
o, ok := other.(receiverOnlyObjectVal) |
| 776 |
if !ok { |
| 777 |
return types.MaybeNoSuchOverloadErr(other) |
| 778 |
} |
| 779 |
return types.Bool(a == o) |
| 780 |
} |
| 781 |
|
| 782 |
// Type implements ref.Val.Type. |
| 783 |
func (a receiverOnlyObjectVal) Type() ref.Type { |
| 784 |
return a.typeValue |
| 785 |
} |
| 786 |
|
| 787 |
// Value implements ref.Val.Value. |
| 788 |
func (a receiverOnlyObjectVal) Value() any { |
| 789 |
return types.NoSuchOverloadErr() |
| 790 |
} |