forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCelCommon.java
More file actions
66 lines (59 loc) · 2.33 KB
/
CelCommon.java
File metadata and controls
66 lines (59 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright 2026 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc.xds.internal.matcher;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelOptions;
import dev.cel.common.types.SimpleType;
import dev.cel.compiler.CelCompiler;
import dev.cel.compiler.CelCompilerFactory;
import dev.cel.runtime.CelRuntime;
import dev.cel.runtime.CelRuntimeFactory;
/**
* Shared utilities for CEL-based matchers and extractors.
*/
final class CelCommon {
private static final CelOptions CEL_OPTIONS = CelOptions.newBuilder()
.enableComprehension(false)
.enableStringConversion(false)
.enableStringConcatenation(false)
.enableListConcatenation(false)
.maxRegexProgramSize(100)
.build();
static final CelCompiler COMPILER = CelCompilerFactory.standardCelCompilerBuilder()
.addVar("request", SimpleType.DYN)
.setOptions(CEL_OPTIONS)
.build();
static final CelRuntime RUNTIME = CelRuntimeFactory.standardCelRuntimeBuilder()
.setOptions(CEL_OPTIONS)
.build();
private CelCommon() {}
static void checkAllowedVariables(CelAbstractSyntaxTree ast) {
// Basic validation to ensure only supported variables (request) are used.
// This iterates over the reference map generated by the type checker.
for (java.util.Map.Entry<Long, dev.cel.common.ast.CelReference> entry :
ast.getReferenceMap().entrySet()) {
dev.cel.common.ast.CelReference ref = entry.getValue();
// If overload_id is empty, it's a variable reference or type name.
// We only support "request".
if (ref.value() == null && ref.overloadIds().isEmpty()) {
if (!"request".equals(ref.name())) {
throw new IllegalArgumentException(
"CEL expression references unknown variable: " + ref.name());
}
}
}
}
}