| 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 cel |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
|
| 22 |
"github.com/google/cel-go/cel" |
| 23 |
|
| 24 |
"k8s.io/apimachinery/pkg/util/version" |
| 25 |
apiservercel "k8s.io/apiserver/pkg/cel" |
| 26 |
"k8s.io/apiserver/pkg/cel/environment" |
| 27 |
) |
| 28 |
|
| 29 |
const ( |
| 30 |
claimsVarName = "claims" |
| 31 |
userVarName = "user" |
| 32 |
) |
| 33 |
|
| 34 |
// compiler implements the Compiler interface. |
| 35 |
type compiler struct { |
| 36 |
// varEnvs is a map of CEL environments, keyed by the name of the CEL variable. |
| 37 |
// The CEL variable is available to the expression. |
| 38 |
// We have 2 environments, one for claims and one for user. |
| 39 |
varEnvs map[string]*environment.EnvSet |
| 40 |
} |
| 41 |
|
| 42 |
// NewDefaultCompiler returns a new Compiler following the default compatibility version. |
| 43 |
// Note: the compiler construction depends on feature gates and the compatibility version to be initialized. |
| 44 |
func NewDefaultCompiler() Compiler { |
| 45 |
return NewCompiler(environment.MustBaseEnvSet(environment.DefaultCompatibilityVersion(), true)) |
| 46 |
} |
| 47 |
|
| 48 |
// NewCompiler returns a new Compiler. |
| 49 |
func NewCompiler(env *environment.EnvSet) Compiler { |
| 50 |
return &compiler{ |
| 51 |
varEnvs: mustBuildEnvs(env), |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// CompileClaimsExpression compiles the given expressionAccessor into a CEL program that can be evaluated. |
| 56 |
// The claims CEL variable is available to the expression. |
| 57 |
func (c compiler) CompileClaimsExpression(expressionAccessor ExpressionAccessor) (CompilationResult, error) { |
| 58 |
return c.compile(expressionAccessor, claimsVarName) |
| 59 |
} |
| 60 |
|
| 61 |
// CompileUserExpression compiles the given expressionAccessor into a CEL program that can be evaluated. |
| 62 |
// The user CEL variable is available to the expression. |
| 63 |
func (c compiler) CompileUserExpression(expressionAccessor ExpressionAccessor) (CompilationResult, error) { |
| 64 |
return c.compile(expressionAccessor, userVarName) |
| 65 |
} |
| 66 |
|
| 67 |
func (c compiler) compile(expressionAccessor ExpressionAccessor, envVarName string) (CompilationResult, error) { |
| 68 |
resultError := func(errorString string, errType apiservercel.ErrorType) (CompilationResult, error) { |
| 69 |
return CompilationResult{}, &apiservercel.Error{ |
| 70 |
Type: errType, |
| 71 |
Detail: errorString, |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
env, err := c.varEnvs[envVarName].Env(environment.StoredExpressions) |
| 76 |
if err != nil { |
| 77 |
return resultError(fmt.Sprintf("unexpected error loading CEL environment: %v", err), apiservercel.ErrorTypeInternal) |
| 78 |
} |
| 79 |
|
| 80 |
ast, issues := env.Compile(expressionAccessor.GetExpression()) |
| 81 |
if issues != nil { |
| 82 |
return resultError("compilation failed: "+issues.String(), apiservercel.ErrorTypeInvalid) |
| 83 |
} |
| 84 |
|
| 85 |
found := false |
| 86 |
returnTypes := expressionAccessor.ReturnTypes() |
| 87 |
for _, returnType := range returnTypes { |
| 88 |
if ast.OutputType() == returnType || cel.AnyType == returnType { |
| 89 |
found = true |
| 90 |
break |
| 91 |
} |
| 92 |
} |
| 93 |
if !found { |
| 94 |
var reason string |
| 95 |
if len(returnTypes) == 1 { |
| 96 |
reason = fmt.Sprintf("must evaluate to %v", returnTypes[0].String()) |
| 97 |
} else { |
| 98 |
reason = fmt.Sprintf("must evaluate to one of %v", returnTypes) |
| 99 |
} |
| 100 |
|
| 101 |
return resultError(reason, apiservercel.ErrorTypeInvalid) |
| 102 |
} |
| 103 |
|
| 104 |
if _, err = cel.AstToCheckedExpr(ast); err != nil { |
| 105 |
// should be impossible since env.Compile returned no issues |
| 106 |
return resultError("unexpected compilation error: "+err.Error(), apiservercel.ErrorTypeInternal) |
| 107 |
} |
| 108 |
prog, err := env.Program(ast) |
| 109 |
if err != nil { |
| 110 |
return resultError("program instantiation failed: "+err.Error(), apiservercel.ErrorTypeInternal) |
| 111 |
} |
| 112 |
|
| 113 |
return CompilationResult{ |
| 114 |
Program: prog, |
| 115 |
AST: ast, |
| 116 |
ExpressionAccessor: expressionAccessor, |
| 117 |
}, nil |
| 118 |
} |
| 119 |
|
| 120 |
func buildUserType() *apiservercel.DeclType { |
| 121 |
field := func(name string, declType *apiservercel.DeclType, required bool) *apiservercel.DeclField { |
| 122 |
return apiservercel.NewDeclField(name, declType, required, nil, nil) |
| 123 |
} |
| 124 |
fields := func(fields ...*apiservercel.DeclField) map[string]*apiservercel.DeclField { |
| 125 |
result := make(map[string]*apiservercel.DeclField, len(fields)) |
| 126 |
for _, f := range fields { |
| 127 |
result[f.Name] = f |
| 128 |
} |
| 129 |
return result |
| 130 |
} |
| 131 |
|
| 132 |
return apiservercel.NewObjectType("kubernetes.UserInfo", fields( |
| 133 |
field("username", apiservercel.StringType, false), |
| 134 |
field("uid", apiservercel.StringType, false), |
| 135 |
field("groups", apiservercel.NewListType(apiservercel.StringType, -1), false), |
| 136 |
field("extra", apiservercel.NewMapType(apiservercel.StringType, apiservercel.NewListType(apiservercel.StringType, -1), -1), false), |
| 137 |
)) |
| 138 |
} |
| 139 |
|
| 140 |
func mustBuildEnvs(baseEnv *environment.EnvSet) map[string]*environment.EnvSet { |
| 141 |
buildEnvSet := func(envOpts []cel.EnvOption, declTypes []*apiservercel.DeclType) *environment.EnvSet { |
| 142 |
env, err := baseEnv.Extend(environment.VersionedOptions{ |
| 143 |
IntroducedVersion: version.MajorMinor(1, 0), |
| 144 |
EnvOptions: envOpts, |
| 145 |
DeclTypes: declTypes, |
| 146 |
}) |
| 147 |
if err != nil { |
| 148 |
panic(fmt.Sprintf("environment misconfigured: %v", err)) |
| 149 |
} |
| 150 |
return env |
| 151 |
} |
| 152 |
|
| 153 |
userType := buildUserType() |
| 154 |
claimsType := apiservercel.NewMapType(apiservercel.StringType, apiservercel.AnyType, -1) |
| 155 |
|
| 156 |
envs := make(map[string]*environment.EnvSet, 2) // build two environments, one for claims and one for user |
| 157 |
envs[claimsVarName] = buildEnvSet([]cel.EnvOption{cel.Variable(claimsVarName, claimsType.CelType())}, []*apiservercel.DeclType{claimsType}) |
| 158 |
envs[userVarName] = buildEnvSet([]cel.EnvOption{cel.Variable(userVarName, userType.CelType())}, []*apiservercel.DeclType{userType}) |
| 159 |
|
| 160 |
return envs |
| 161 |
} |