Debian Codegraph @

pkg/authentication/cel/compile_test.go

raw
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 "reflect"
21 "strings"
22 "testing"
23
24 authenticationv1 "k8s.io/api/authentication/v1"
25 apiservercel "k8s.io/apiserver/pkg/cel"
26 )
27
28 func TestCompileClaimsExpression(t *testing.T) {
29 testCases := []struct {
30 name string
31 expressionAccessors []ExpressionAccessor
32 }{
33 {
34 name: "valid ClaimMappingCondition",
35 expressionAccessors: []ExpressionAccessor{
36 &ClaimMappingExpression{
37 Expression: "claims.foo",
38 },
39 },
40 },
41 {
42 name: "valid ClaimValidationCondition",
43 expressionAccessors: []ExpressionAccessor{
44 &ClaimValidationCondition{
45 Expression: "claims.foo == 'bar'",
46 },
47 },
48 },
49 {
50 name: "valid ExtraMapppingCondition",
51 expressionAccessors: []ExpressionAccessor{
52 &ExtraMappingExpression{
53 Expression: "claims.foo",
54 },
55 },
56 },
57 }
58
59 for _, tc := range testCases {
60 t.Run(tc.name, func(t *testing.T) {
61 for _, expressionAccessor := range tc.expressionAccessors {
62 _, err := NewDefaultCompiler().CompileClaimsExpression(expressionAccessor)
63 if err != nil {
64 t.Errorf("unexpected error: %v", err)
65 }
66 }
67 })
68 }
69 }
70
71 func TestCompileUserExpression(t *testing.T) {
72 testCases := []struct {
73 name string
74 expressionAccessors []ExpressionAccessor
75 }{
76 {
77 name: "valid UserValidationCondition",
78 expressionAccessors: []ExpressionAccessor{
79 &ExtraMappingExpression{
80 Expression: "user.username == 'bar'",
81 },
82 },
83 },
84 }
85
86 for _, tc := range testCases {
87 t.Run(tc.name, func(t *testing.T) {
88 for _, expressionAccessor := range tc.expressionAccessors {
89 _, err := NewDefaultCompiler().CompileUserExpression(expressionAccessor)
90 if err != nil {
91 t.Errorf("unexpected error: %v", err)
92 }
93 }
94 })
95 }
96 }
97
98 func TestCompileClaimsExpressionError(t *testing.T) {
99 testCases := []struct {
100 name string
101 expressionAccessors []ExpressionAccessor
102 wantErr string
103 }{
104 {
105 name: "invalid ClaimValidationCondition",
106 expressionAccessors: []ExpressionAccessor{
107 &ClaimValidationCondition{
108 Expression: "claims.foo",
109 },
110 },
111 wantErr: "must evaluate to bool",
112 },
113 {
114 name: "UserValidationCondition with wrong env",
115 expressionAccessors: []ExpressionAccessor{
116 &UserValidationCondition{
117 Expression: "user.username == 'foo'",
118 },
119 },
120 wantErr: `compilation failed: ERROR: <input>:1:1: undeclared reference to 'user' (in container '')`,
121 },
122 {
123 name: "invalid ClaimMappingCondition",
124 expressionAccessors: []ExpressionAccessor{
125 &ClaimMappingExpression{
126 Expression: "claims + 1",
127 },
128 },
129 wantErr: `compilation failed: ERROR: <input>:1:8: found no matching overload for '_+_' applied to '(map(string, any), int)'`,
130 },
131 }
132
133 for _, tc := range testCases {
134 t.Run(tc.name, func(t *testing.T) {
135 for _, expressionAccessor := range tc.expressionAccessors {
136 _, err := NewDefaultCompiler().CompileClaimsExpression(expressionAccessor)
137 if err == nil {
138 t.Errorf("expected error but got nil")
139 }
140 if !strings.Contains(err.Error(), tc.wantErr) {
141 t.Errorf("expected error to contain %q but got %q", tc.wantErr, err.Error())
142 }
143 }
144 })
145 }
146 }
147
148 func TestCompileUserExpressionError(t *testing.T) {
149 testCases := []struct {
150 name string
151 expressionAccessors []ExpressionAccessor
152 wantErr string
153 }{
154 {
155 name: "invalid UserValidationCondition",
156 expressionAccessors: []ExpressionAccessor{
157 &UserValidationCondition{
158 Expression: "user.username",
159 },
160 },
161 wantErr: "must evaluate to bool",
162 },
163 {
164 name: "ClamMappingCondition with wrong env",
165 expressionAccessors: []ExpressionAccessor{
166 &ClaimMappingExpression{
167 Expression: "claims.foo",
168 },
169 },
170 wantErr: `compilation failed: ERROR: <input>:1:1: undeclared reference to 'claims' (in container '')`,
171 },
172 {
173 name: "ExtraMappingCondition with wrong env",
174 expressionAccessors: []ExpressionAccessor{
175 &ExtraMappingExpression{
176 Expression: "claims.foo",
177 },
178 },
179 wantErr: `compilation failed: ERROR: <input>:1:1: undeclared reference to 'claims' (in container '')`,
180 },
181 {
182 name: "ClaimValidationCondition with wrong env",
183 expressionAccessors: []ExpressionAccessor{
184 &ClaimValidationCondition{
185 Expression: "claims.foo == 'bar'",
186 },
187 },
188 wantErr: `compilation failed: ERROR: <input>:1:1: undeclared reference to 'claims' (in container '')`,
189 },
190 {
191 name: "UserValidationCondition expression with unknown field",
192 expressionAccessors: []ExpressionAccessor{
193 &UserValidationCondition{
194 Expression: "user.unknown == 'foo'",
195 },
196 },
197 wantErr: `compilation failed: ERROR: <input>:1:5: undefined field 'unknown'`,
198 },
199 }
200
201 for _, tc := range testCases {
202 t.Run(tc.name, func(t *testing.T) {
203 for _, expressionAccessor := range tc.expressionAccessors {
204 _, err := NewDefaultCompiler().CompileUserExpression(expressionAccessor)
205 if err == nil {
206 t.Errorf("expected error but got nil")
207 }
208 if !strings.Contains(err.Error(), tc.wantErr) {
209 t.Errorf("expected error to contain %q but got %q", tc.wantErr, err.Error())
210 }
211 }
212 })
213 }
214 }
215
216 func TestBuildUserType(t *testing.T) {
217 userDeclType := buildUserType()
218 userType := reflect.TypeOf(authenticationv1.UserInfo{})
219
220 if len(userDeclType.Fields) != userType.NumField() {
221 t.Errorf("expected %d fields, got %d", userType.NumField(), len(userDeclType.Fields))
222 }
223
224 for i := 0; i < userType.NumField(); i++ {
225 field := userType.Field(i)
226 jsonTagParts := strings.Split(field.Tag.Get("json"), ",")
227 if len(jsonTagParts) < 1 {
228 t.Fatal("expected json tag to be present")
229 }
230 fieldName := jsonTagParts[0]
231
232 declField, ok := userDeclType.Fields[fieldName]
233 if !ok {
234 t.Errorf("expected field %q to be present", field.Name)
235 }
236 if nativeTypeToCELType(t, field.Type).CelType().Equal(declField.Type.CelType()).Value() != true {
237 t.Errorf("expected field %q to have type %v, got %v", field.Name, field.Type, declField.Type)
238 }
239 }
240 }
241
242 func nativeTypeToCELType(t *testing.T, nativeType reflect.Type) *apiservercel.DeclType {
243 switch nativeType {
244 case reflect.TypeOf(""):
245 return apiservercel.StringType
246 case reflect.TypeOf([]string{}):
247 return apiservercel.NewListType(apiservercel.StringType, -1)
248 case reflect.TypeOf(map[string]authenticationv1.ExtraValue{}):
249 return apiservercel.NewMapType(apiservercel.StringType, apiservercel.AnyType, -1)
250 default:
251 t.Fatalf("unsupported type %v", nativeType)
252 }
253 return nil
254 }