| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package compiler |
| 16 |
|
| 17 |
import ( |
| 18 |
"reflect" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/google/cel-go/cel" |
| 22 |
"github.com/google/cel-go/common/env" |
| 23 |
"github.com/google/cel-go/ext" |
| 24 |
"github.com/google/cel-go/policy" |
| 25 |
|
| 26 |
celpb "cel.dev/expr" |
| 27 |
configpb "cel.dev/expr/conformance" |
| 28 |
structpb "google.golang.org/protobuf/types/known/structpb" |
| 29 |
) |
| 30 |
|
| 31 |
func TestEnvironmentFileCompareTextprotoAndYAML(t *testing.T) { |
| 32 |
t.Run("compare textproto and yaml environment files", func(t *testing.T) { |
| 33 |
pbEnv := testEnvProto() |
| 34 |
protoConfig, err := configFromEnvProto(t, pbEnv) |
| 35 |
if err != nil { |
| 36 |
t.Fatalf("configFromEnvProto(%v) failed: %v", pbEnv, err) |
| 37 |
} |
| 38 |
config, err := parseEnv(t, "yaml_config", "testdata/config.yaml") |
| 39 |
if err != nil { |
| 40 |
t.Fatalf("parseEnv(%s) failed: %v", "testdata/config.yaml", err) |
| 41 |
} |
| 42 |
if protoConfig.Container != config.Container { |
| 43 |
t.Fatalf("Container got %q, wanted %q", protoConfig.Container, config.Container) |
| 44 |
} |
| 45 |
if !reflect.DeepEqual(protoConfig.Imports, config.Imports) { |
| 46 |
t.Fatalf("Imports got %v, wanted %v", protoConfig.Imports, config.Imports) |
| 47 |
} |
| 48 |
if !reflect.DeepEqual(protoConfig.StdLib, config.StdLib) { |
| 49 |
t.Fatalf("StdLib got %v, wanted %v", protoConfig.StdLib, config.StdLib) |
| 50 |
} |
| 51 |
if len(protoConfig.Extensions) != len(config.Extensions) { |
| 52 |
t.Fatalf("Extensions count got %d, wanted %d", len(protoConfig.Extensions), len(config.Extensions)) |
| 53 |
} |
| 54 |
for _, protoConfigExt := range protoConfig.Extensions { |
| 55 |
found := false |
| 56 |
for _, configExt := range config.Extensions { |
| 57 |
if reflect.DeepEqual(protoConfigExt, configExt) { |
| 58 |
found = true |
| 59 |
break |
| 60 |
} |
| 61 |
} |
| 62 |
if !found { |
| 63 |
t.Fatalf("Extensions got %v, wanted %v", protoConfig.Extensions, config.Extensions) |
| 64 |
} |
| 65 |
} |
| 66 |
if !reflect.DeepEqual(protoConfig.ContextVariable, config.ContextVariable) { |
| 67 |
t.Fatalf("ContextVariable got %v, wanted %v", protoConfig.ContextVariable, config.ContextVariable) |
| 68 |
} |
| 69 |
if len(protoConfig.Variables) != len(config.Variables) { |
| 70 |
t.Fatalf("Variables count got %d, wanted %d", len(protoConfig.Variables), len(config.Variables)) |
| 71 |
} else { |
| 72 |
for i, v := range protoConfig.Variables { |
| 73 |
for j, p := range v.TypeDesc.Params { |
| 74 |
if p.TypeName == "google.protobuf.Any" && |
| 75 |
config.Variables[i].TypeDesc.Params[j].TypeName == "dyn" { |
| 76 |
p.TypeName = "dyn" |
| 77 |
} |
| 78 |
} |
| 79 |
if !reflect.DeepEqual(v, config.Variables[i]) { |
| 80 |
t.Fatalf("Variables[%d] not equal, got %v, wanted %v", i, v, config.Variables[i]) |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
if len(protoConfig.Functions) != len(config.Functions) { |
| 85 |
t.Fatalf("Functions count got %d, wanted %d", len(protoConfig.Functions), len(config.Functions)) |
| 86 |
} else { |
| 87 |
for i, f := range protoConfig.Functions { |
| 88 |
if !reflect.DeepEqual(f, config.Functions[i]) { |
| 89 |
t.Fatalf("Functions[%d] not equal, got %v, wanted %v", i, f, config.Functions[i]) |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
if len(protoConfig.Features) != len(config.Features) { |
| 94 |
t.Fatalf("Features count got %d, wanted %d", len(protoConfig.Features), len(config.Features)) |
| 95 |
} else { |
| 96 |
for i, f := range protoConfig.Features { |
| 97 |
if !reflect.DeepEqual(f, config.Features[i]) { |
| 98 |
t.Fatalf("Features[%d] not equal, got %v, wanted %v", i, f, config.Features[i]) |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
if len(protoConfig.Validators) != len(config.Validators) { |
| 103 |
t.Fatalf("Validators count got %d, wanted %d", len(protoConfig.Validators), len(config.Validators)) |
| 104 |
} else { |
| 105 |
for i, v := range protoConfig.Validators { |
| 106 |
if !reflect.DeepEqual(v, config.Validators[i]) { |
| 107 |
t.Fatalf("Validators[%d] not equal, got %v, wanted %v", i, v, config.Validators[i]) |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
}) |
| 112 |
} |
| 113 |
|
| 114 |
func testEnvProto() *configpb.Environment { |
| 115 |
return &configpb.Environment{ |
| 116 |
Name: "test-environment", |
| 117 |
Description: "Test environment", |
| 118 |
Container: "google.expr", |
| 119 |
Imports: []*configpb.Environment_Import{ |
| 120 |
{Name: "google.expr.proto3.test.TestAllTypes"}, |
| 121 |
}, |
| 122 |
Stdlib: &configpb.LibrarySubset{ |
| 123 |
IncludeMacros: []string{"has", "exists"}, |
| 124 |
IncludeFunctions: []*celpb.Decl{ |
| 125 |
{ |
| 126 |
Name: "_==_", |
| 127 |
DeclKind: &celpb.Decl_Function{ |
| 128 |
Function: &celpb.Decl_FunctionDecl{ |
| 129 |
Overloads: []*celpb.Decl_FunctionDecl_Overload{ |
| 130 |
{ |
| 131 |
OverloadId: "equals", |
| 132 |
Params: []*celpb.Type{ |
| 133 |
{ |
| 134 |
TypeKind: &celpb.Type_Primitive{ |
| 135 |
Primitive: celpb.Type_STRING, |
| 136 |
}, |
| 137 |
}, |
| 138 |
{ |
| 139 |
TypeKind: &celpb.Type_Primitive{ |
| 140 |
Primitive: celpb.Type_STRING, |
| 141 |
}, |
| 142 |
}, |
| 143 |
}, |
| 144 |
ResultType: &celpb.Type{ |
| 145 |
TypeKind: &celpb.Type_Primitive{ |
| 146 |
Primitive: celpb.Type_BOOL, |
| 147 |
}, |
| 148 |
}, |
| 149 |
}, |
| 150 |
}, |
| 151 |
}, |
| 152 |
}, |
| 153 |
}, |
| 154 |
{ |
| 155 |
Name: "_||_", |
| 156 |
DeclKind: &celpb.Decl_Function{ |
| 157 |
Function: &celpb.Decl_FunctionDecl{ |
| 158 |
Overloads: []*celpb.Decl_FunctionDecl_Overload{ |
| 159 |
{ |
| 160 |
OverloadId: "logical_or", |
| 161 |
Params: []*celpb.Type{ |
| 162 |
{ |
| 163 |
TypeKind: &celpb.Type_Primitive{ |
| 164 |
Primitive: celpb.Type_BOOL, |
| 165 |
}, |
| 166 |
}, |
| 167 |
{ |
| 168 |
TypeKind: &celpb.Type_Primitive{ |
| 169 |
Primitive: celpb.Type_BOOL, |
| 170 |
}, |
| 171 |
}, |
| 172 |
}, |
| 173 |
ResultType: &celpb.Type{ |
| 174 |
TypeKind: &celpb.Type_Primitive{ |
| 175 |
Primitive: celpb.Type_BOOL, |
| 176 |
}, |
| 177 |
}, |
| 178 |
}, |
| 179 |
}, |
| 180 |
}, |
| 181 |
}, |
| 182 |
}, |
| 183 |
}, |
| 184 |
}, |
| 185 |
Extensions: []*configpb.Extension{ |
| 186 |
{ |
| 187 |
Name: "lists", |
| 188 |
Version: "latest", |
| 189 |
}, |
| 190 |
{ |
| 191 |
Name: "sets", |
| 192 |
Version: "latest", |
| 193 |
}, |
| 194 |
}, |
| 195 |
Declarations: []*celpb.Decl{ |
| 196 |
{ |
| 197 |
Name: "destination.ip", |
| 198 |
DeclKind: &celpb.Decl_Ident{ |
| 199 |
Ident: &celpb.Decl_IdentDecl{ |
| 200 |
Type: &celpb.Type{ |
| 201 |
TypeKind: &celpb.Type_Primitive{ |
| 202 |
Primitive: celpb.Type_STRING, |
| 203 |
}, |
| 204 |
}, |
| 205 |
}, |
| 206 |
}, |
| 207 |
}, |
| 208 |
{ |
| 209 |
Name: "origin.ip", |
| 210 |
DeclKind: &celpb.Decl_Ident{ |
| 211 |
Ident: &celpb.Decl_IdentDecl{ |
| 212 |
Type: &celpb.Type{ |
| 213 |
TypeKind: &celpb.Type_Primitive{ |
| 214 |
Primitive: celpb.Type_STRING, |
| 215 |
}, |
| 216 |
}, |
| 217 |
}, |
| 218 |
}, |
| 219 |
}, |
| 220 |
{ |
| 221 |
Name: "spec.restricted_destinations", |
| 222 |
DeclKind: &celpb.Decl_Ident{ |
| 223 |
Ident: &celpb.Decl_IdentDecl{ |
| 224 |
Type: &celpb.Type{ |
| 225 |
TypeKind: &celpb.Type_ListType_{ |
| 226 |
ListType: &celpb.Type_ListType{ |
| 227 |
ElemType: &celpb.Type{ |
| 228 |
TypeKind: &celpb.Type_Primitive{ |
| 229 |
Primitive: celpb.Type_STRING, |
| 230 |
}, |
| 231 |
}, |
| 232 |
}, |
| 233 |
}, |
| 234 |
}, |
| 235 |
}, |
| 236 |
}, |
| 237 |
}, |
| 238 |
{ |
| 239 |
Name: "spec.origin", |
| 240 |
DeclKind: &celpb.Decl_Ident{ |
| 241 |
Ident: &celpb.Decl_IdentDecl{ |
| 242 |
Type: &celpb.Type{ |
| 243 |
TypeKind: &celpb.Type_Primitive{ |
| 244 |
Primitive: celpb.Type_STRING, |
| 245 |
}, |
| 246 |
}, |
| 247 |
}, |
| 248 |
}, |
| 249 |
}, |
| 250 |
{ |
| 251 |
Name: "request", |
| 252 |
DeclKind: &celpb.Decl_Ident{ |
| 253 |
Ident: &celpb.Decl_IdentDecl{ |
| 254 |
Type: &celpb.Type{ |
| 255 |
TypeKind: &celpb.Type_MapType_{ |
| 256 |
MapType: &celpb.Type_MapType{ |
| 257 |
KeyType: &celpb.Type{ |
| 258 |
TypeKind: &celpb.Type_Primitive{ |
| 259 |
Primitive: celpb.Type_STRING, |
| 260 |
}, |
| 261 |
}, |
| 262 |
ValueType: &celpb.Type{ |
| 263 |
TypeKind: &celpb.Type_WellKnown{ |
| 264 |
WellKnown: celpb.Type_ANY, |
| 265 |
}, |
| 266 |
}, |
| 267 |
}, |
| 268 |
}, |
| 269 |
}, |
| 270 |
}, |
| 271 |
}, |
| 272 |
}, |
| 273 |
{ |
| 274 |
Name: "resource", |
| 275 |
DeclKind: &celpb.Decl_Ident{ |
| 276 |
Ident: &celpb.Decl_IdentDecl{ |
| 277 |
Type: &celpb.Type{ |
| 278 |
TypeKind: &celpb.Type_MapType_{ |
| 279 |
MapType: &celpb.Type_MapType{ |
| 280 |
KeyType: &celpb.Type{ |
| 281 |
TypeKind: &celpb.Type_Primitive{ |
| 282 |
Primitive: celpb.Type_STRING, |
| 283 |
}, |
| 284 |
}, |
| 285 |
ValueType: &celpb.Type{ |
| 286 |
TypeKind: &celpb.Type_WellKnown{ |
| 287 |
WellKnown: celpb.Type_ANY, |
| 288 |
}, |
| 289 |
}, |
| 290 |
}, |
| 291 |
}, |
| 292 |
}, |
| 293 |
}, |
| 294 |
}, |
| 295 |
}, |
| 296 |
{ |
| 297 |
Name: "locationCode", |
| 298 |
DeclKind: &celpb.Decl_Function{ |
| 299 |
Function: &celpb.Decl_FunctionDecl{ |
| 300 |
Overloads: []*celpb.Decl_FunctionDecl_Overload{ |
| 301 |
{ |
| 302 |
OverloadId: "locationCode_string", |
| 303 |
Params: []*celpb.Type{ |
| 304 |
{ |
| 305 |
TypeKind: &celpb.Type_Primitive{ |
| 306 |
Primitive: celpb.Type_STRING, |
| 307 |
}, |
| 308 |
}, |
| 309 |
}, |
| 310 |
ResultType: &celpb.Type{ |
| 311 |
TypeKind: &celpb.Type_Primitive{ |
| 312 |
Primitive: celpb.Type_STRING, |
| 313 |
}, |
| 314 |
}, |
| 315 |
}, |
| 316 |
}, |
| 317 |
}, |
| 318 |
}, |
| 319 |
}, |
| 320 |
}, |
| 321 |
Validators: []*configpb.Validator{ |
| 322 |
{Name: "cel.validator.duration"}, |
| 323 |
{ |
| 324 |
Name: "cel.validator.comprehension_nesting_limit", |
| 325 |
Config: map[string]*structpb.Value{ |
| 326 |
"limit": structpb.NewNumberValue(2), |
| 327 |
}, |
| 328 |
}, |
| 329 |
}, |
| 330 |
Features: []*configpb.Feature{ |
| 331 |
{ |
| 332 |
Name: "cel.feature.macro_call_tracking", |
| 333 |
Enabled: true, |
| 334 |
}, |
| 335 |
}, |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
func configFromEnvProto(t *testing.T, pbEnv *configpb.Environment) (*env.Config, error) { |
| 340 |
t.Helper() |
| 341 |
envConfig, fileDescriptorSet, err := envProtoToConfig(pbEnv) |
| 342 |
if err != nil { |
| 343 |
return nil, err |
| 344 |
} |
| 345 |
var envOpts []cel.EnvOption |
| 346 |
if fileDescriptorSet != nil { |
| 347 |
envOpts = append(envOpts, cel.TypeDescs(fileDescriptorSet)) |
| 348 |
} |
| 349 |
envOpts = append(envOpts, cel.FromConfig(envConfig, ext.ExtensionOptionFactory)) |
| 350 |
return envOptionToConfig(t, envConfig.Name, envOpts...) |
| 351 |
} |
| 352 |
|
| 353 |
func parseEnv(t *testing.T, name, path string) (*env.Config, error) { |
| 354 |
t.Helper() |
| 355 |
opts := EnvironmentFile(path) |
| 356 |
return envOptionToConfig(t, name, opts) |
| 357 |
} |
| 358 |
|
| 359 |
func envOptionToConfig(t *testing.T, name string, opts ...cel.EnvOption) (*env.Config, error) { |
| 360 |
t.Helper() |
| 361 |
e, err := cel.NewCustomEnv(opts...) |
| 362 |
if err != nil { |
| 363 |
return nil, err |
| 364 |
} |
| 365 |
conf, err := e.ToConfig(name) |
| 366 |
if err != nil { |
| 367 |
return nil, err |
| 368 |
} |
| 369 |
return conf, nil |
| 370 |
} |
| 371 |
|
| 372 |
func TestFileExpressionCustomPolicyParser(t *testing.T) { |
| 373 |
t.Run("test file expression custom policy parser", func(t *testing.T) { |
| 374 |
envOpt := EnvironmentFile("../../policy/testdata/k8s/config.yaml") |
| 375 |
parserOpt := policy.ParserOption(func(p *policy.Parser) (*policy.Parser, error) { |
| 376 |
p.TagVisitor = policy.K8sTestTagHandler() |
| 377 |
return p, nil |
| 378 |
}) |
| 379 |
compilerOpts := []any{envOpt, parserOpt} |
| 380 |
compiler, err := NewCompiler(compilerOpts...) |
| 381 |
if err != nil { |
| 382 |
t.Fatalf("NewCompiler() failed: %v", err) |
| 383 |
} |
| 384 |
policyFile := &FileExpression{ |
| 385 |
Path: "../../policy/testdata/k8s/policy.yaml", |
| 386 |
} |
| 387 |
k8sAst, _, err := policyFile.CreateAST(compiler) |
| 388 |
if err != nil { |
| 389 |
t.Fatalf("CreateAST() failed: %v", err) |
| 390 |
} |
| 391 |
if k8sAst == nil { |
| 392 |
t.Fatalf("CreateAST() returned nil ast") |
| 393 |
} |
| 394 |
}) |
| 395 |
} |
| 396 |
|
| 397 |
func TestRawExpressionCreateAst(t *testing.T) { |
| 398 |
t.Run("test raw expression create ast", func(t *testing.T) { |
| 399 |
envOpt := EnvironmentFile("testdata/config.yaml") |
| 400 |
compiler, err := NewCompiler(envOpt) |
| 401 |
if err != nil { |
| 402 |
t.Fatalf("NewCompiler() failed: %v", err) |
| 403 |
} |
| 404 |
rawExpr := &RawExpression{ |
| 405 |
Value: "locationCode(destination.ip)==locationCode(origin.ip)", |
| 406 |
} |
| 407 |
ast, _, err := rawExpr.CreateAST(compiler) |
| 408 |
if err != nil { |
| 409 |
t.Fatalf("CreateAST() failed: %v", err) |
| 410 |
} |
| 411 |
if ast == nil { |
| 412 |
t.Fatalf("CreateAST() returned nil ast") |
| 413 |
} |
| 414 |
}) |
| 415 |
} |