| 1 |
/* |
| 2 |
Copyright 2024 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 cel |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"github.com/google/cel-go/interpreter" |
| 23 |
"math" |
| 24 |
"time" |
| 25 |
|
| 26 |
admissionv1 "k8s.io/api/admission/v1" |
| 27 |
v1 "k8s.io/api/core/v1" |
| 28 |
"k8s.io/apiserver/pkg/admission" |
| 29 |
"k8s.io/apiserver/pkg/cel" |
| 30 |
"k8s.io/apiserver/pkg/cel/library" |
| 31 |
) |
| 32 |
|
| 33 |
// newActivation creates an activation for CEL admission plugins from the given request, admission chain and |
| 34 |
// variable binding information. |
| 35 |
func newActivation(compositionCtx CompositionContext, versionedAttr *admission.VersionedAttributes, request *admissionv1.AdmissionRequest, inputs OptionalVariableBindings, namespace *v1.Namespace) (*evaluationActivation, error) { |
| 36 |
oldObjectVal, err := objectToResolveVal(versionedAttr.VersionedOldObject) |
| 37 |
if err != nil { |
| 38 |
return nil, fmt.Errorf("failed to prepare oldObject variable for evaluation: %w", err) |
| 39 |
} |
| 40 |
objectVal, err := objectToResolveVal(versionedAttr.VersionedObject) |
| 41 |
if err != nil { |
| 42 |
return nil, fmt.Errorf("failed to prepare object variable for evaluation: %w", err) |
| 43 |
} |
| 44 |
var paramsVal, authorizerVal, requestResourceAuthorizerVal any |
| 45 |
if inputs.VersionedParams != nil { |
| 46 |
paramsVal, err = objectToResolveVal(inputs.VersionedParams) |
| 47 |
if err != nil { |
| 48 |
return nil, fmt.Errorf("failed to prepare params variable for evaluation: %w", err) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
if inputs.Authorizer != nil { |
| 53 |
authorizerVal = library.NewAuthorizerVal(versionedAttr.GetUserInfo(), inputs.Authorizer) |
| 54 |
requestResourceAuthorizerVal = library.NewResourceAuthorizerVal(versionedAttr.GetUserInfo(), inputs.Authorizer, versionedAttr) |
| 55 |
} |
| 56 |
|
| 57 |
requestVal, err := convertObjectToUnstructured(request) |
| 58 |
if err != nil { |
| 59 |
return nil, fmt.Errorf("failed to prepare request variable for evaluation: %w", err) |
| 60 |
} |
| 61 |
namespaceVal, err := objectToResolveVal(namespace) |
| 62 |
if err != nil { |
| 63 |
return nil, fmt.Errorf("failed to prepare namespace variable for evaluation: %w", err) |
| 64 |
} |
| 65 |
va := &evaluationActivation{ |
| 66 |
object: objectVal, |
| 67 |
oldObject: oldObjectVal, |
| 68 |
params: paramsVal, |
| 69 |
request: requestVal.Object, |
| 70 |
namespace: namespaceVal, |
| 71 |
authorizer: authorizerVal, |
| 72 |
requestResourceAuthorizer: requestResourceAuthorizerVal, |
| 73 |
} |
| 74 |
|
| 75 |
// composition is an optional feature that only applies for ValidatingAdmissionPolicy and MutatingAdmissionPolicy. |
| 76 |
if compositionCtx != nil { |
| 77 |
va.variables = compositionCtx.Variables(va) |
| 78 |
} |
| 79 |
return va, nil |
| 80 |
} |
| 81 |
|
| 82 |
type evaluationActivation struct { |
| 83 |
object, oldObject, params, request, namespace, authorizer, requestResourceAuthorizer, variables interface{} |
| 84 |
} |
| 85 |
|
| 86 |
// ResolveName returns a value from the activation by qualified name, or false if the name |
| 87 |
// could not be found. |
| 88 |
func (a *evaluationActivation) ResolveName(name string) (interface{}, bool) { |
| 89 |
switch name { |
| 90 |
case ObjectVarName: |
| 91 |
return a.object, true |
| 92 |
case OldObjectVarName: |
| 93 |
return a.oldObject, true |
| 94 |
case ParamsVarName: |
| 95 |
return a.params, true // params may be null |
| 96 |
case RequestVarName: |
| 97 |
return a.request, true |
| 98 |
case NamespaceVarName: |
| 99 |
return a.namespace, true |
| 100 |
case AuthorizerVarName: |
| 101 |
return a.authorizer, a.authorizer != nil |
| 102 |
case RequestResourceAuthorizerVarName: |
| 103 |
return a.requestResourceAuthorizer, a.requestResourceAuthorizer != nil |
| 104 |
case VariableVarName: // variables always present |
| 105 |
return a.variables, true |
| 106 |
default: |
| 107 |
return nil, false |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// Parent returns the parent of the current activation, may be nil. |
| 112 |
// If non-nil, the parent will be searched during resolve calls. |
| 113 |
func (a *evaluationActivation) Parent() interpreter.Activation { |
| 114 |
return nil |
| 115 |
} |
| 116 |
|
| 117 |
// Evaluate runs a compiled CEL admission plugin expression using the provided activation and CEL |
| 118 |
// runtime cost budget. |
| 119 |
func (a *evaluationActivation) Evaluate(ctx context.Context, compositionCtx CompositionContext, compilationResult CompilationResult, remainingBudget int64) (EvaluationResult, int64, error) { |
| 120 |
var evaluation = EvaluationResult{} |
| 121 |
if compilationResult.ExpressionAccessor == nil { // in case of placeholder |
| 122 |
return evaluation, remainingBudget, nil |
| 123 |
} |
| 124 |
|
| 125 |
evaluation.ExpressionAccessor = compilationResult.ExpressionAccessor |
| 126 |
if compilationResult.Error != nil { |
| 127 |
evaluation.Error = &cel.Error{ |
| 128 |
Type: cel.ErrorTypeInvalid, |
| 129 |
Detail: fmt.Sprintf("compilation error: %v", compilationResult.Error), |
| 130 |
Cause: compilationResult.Error, |
| 131 |
} |
| 132 |
return evaluation, remainingBudget, nil |
| 133 |
} |
| 134 |
if compilationResult.Program == nil { |
| 135 |
evaluation.Error = &cel.Error{ |
| 136 |
Type: cel.ErrorTypeInternal, |
| 137 |
Detail: "unexpected internal error compiling expression", |
| 138 |
} |
| 139 |
return evaluation, remainingBudget, nil |
| 140 |
} |
| 141 |
t1 := time.Now() |
| 142 |
evalResult, evalDetails, err := compilationResult.Program.ContextEval(ctx, a) |
| 143 |
// budget may be spent due to lazy evaluation of composited variables |
| 144 |
if compositionCtx != nil { |
| 145 |
compositionCost := compositionCtx.GetAndResetCost() |
| 146 |
if compositionCost > remainingBudget { |
| 147 |
return evaluation, -1, &cel.Error{ |
| 148 |
Type: cel.ErrorTypeInvalid, |
| 149 |
Detail: "validation failed due to running out of cost budget, no further validation rules will be run", |
| 150 |
Cause: cel.ErrOutOfBudget, |
| 151 |
} |
| 152 |
} |
| 153 |
remainingBudget -= compositionCost |
| 154 |
} |
| 155 |
elapsed := time.Since(t1) |
| 156 |
evaluation.Elapsed = elapsed |
| 157 |
if evalDetails == nil { |
| 158 |
return evaluation, -1, &cel.Error{ |
| 159 |
Type: cel.ErrorTypeInternal, |
| 160 |
Detail: fmt.Sprintf("runtime cost could not be calculated for expression: %v, no further expression will be run", compilationResult.ExpressionAccessor.GetExpression()), |
| 161 |
} |
| 162 |
} else { |
| 163 |
rtCost := evalDetails.ActualCost() |
| 164 |
if rtCost == nil { |
| 165 |
return evaluation, -1, &cel.Error{ |
| 166 |
Type: cel.ErrorTypeInvalid, |
| 167 |
Detail: fmt.Sprintf("runtime cost could not be calculated for expression: %v, no further expression will be run", compilationResult.ExpressionAccessor.GetExpression()), |
| 168 |
Cause: cel.ErrOutOfBudget, |
| 169 |
} |
| 170 |
} else { |
| 171 |
if *rtCost > math.MaxInt64 || int64(*rtCost) > remainingBudget { |
| 172 |
return evaluation, -1, &cel.Error{ |
| 173 |
Type: cel.ErrorTypeInvalid, |
| 174 |
Detail: "validation failed due to running out of cost budget, no further validation rules will be run", |
| 175 |
Cause: cel.ErrOutOfBudget, |
| 176 |
} |
| 177 |
} |
| 178 |
remainingBudget -= int64(*rtCost) |
| 179 |
} |
| 180 |
} |
| 181 |
if err != nil { |
| 182 |
evaluation.Error = &cel.Error{ |
| 183 |
Type: cel.ErrorTypeInvalid, |
| 184 |
Detail: fmt.Sprintf("expression '%v' resulted in error: %v", compilationResult.ExpressionAccessor.GetExpression(), err), |
| 185 |
} |
| 186 |
} else { |
| 187 |
evaluation.EvalResult = evalResult |
| 188 |
} |
| 189 |
return evaluation, remainingBudget, nil |
| 190 |
} |