-
Notifications
You must be signed in to change notification settings - Fork 4k
xds: CEL implementation #12770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shivaspeaks
wants to merge
7
commits into
grpc:master
Choose a base branch
from
shivaspeaks:cel
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
xds: CEL implementation #12770
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4801530
xds: CEL implementation
shivaspeaks d0febc2
remove registry based approach for attributes and resolve comments
shivaspeaks c4c31d0
checkstyle on javadoc
shivaspeaks b9452ae
checkstyle on javadoc
shivaspeaks ce4e16c
resolve comments
shivaspeaks bd4cb56
resolve comments
shivaspeaks 9ac8f95
resolve comments
shivaspeaks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
xds/src/main/java/io/grpc/xds/internal/matcher/CelCommon.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| * 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 com.google.common.collect.ImmutableSet; | ||
| import dev.cel.common.CelAbstractSyntaxTree; | ||
| import dev.cel.common.CelOptions; | ||
| import dev.cel.common.ast.CelReference; | ||
| import dev.cel.runtime.CelRuntime; | ||
| import dev.cel.runtime.CelRuntimeFactory; | ||
| import dev.cel.runtime.CelStandardFunctions; | ||
| import dev.cel.runtime.CelStandardFunctions.StandardFunction; | ||
| import dev.cel.runtime.standard.AddOperator.AddOverload; | ||
| import java.util.Map; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Shared utilities for CEL-based matchers and extractors. | ||
| */ | ||
| final class CelCommon { | ||
| private static final CelOptions CEL_OPTIONS = CelOptions.newBuilder() | ||
| .enableComprehension(false) | ||
| .maxRegexProgramSize(100) | ||
| .build(); | ||
| private static final String REQUEST_VARIABLE = "request"; | ||
| private static final CelStandardFunctions FUNCTIONS = | ||
| CelStandardFunctions.newBuilder() | ||
| .filterFunctions((func, over) -> { | ||
| if (func == StandardFunction.STRING) { | ||
| return false; | ||
| } | ||
| if (func == StandardFunction.ADD) { | ||
| return !over.equals(AddOverload.ADD_STRING) | ||
| && !over.equals(AddOverload.ADD_LIST); | ||
| } | ||
| return true; | ||
| }) | ||
| .build(); | ||
|
|
||
|
|
||
|
|
||
| private static final ImmutableSet<String> ALLOWED_EXACT_OVERLOAD_IDS = ImmutableSet.of( | ||
| "equals", "not_equals", "logical_and", "logical_or", "logical_not"); | ||
|
|
||
| /** | ||
| * Regular expression pattern to validate internal CEL overload IDs. | ||
| * | ||
| * <p>Standard CEL operators and conversion functions often have empty names in the | ||
| * AST and are identified solely by their overload IDs (e.g., {@code equals} for | ||
| * {@code ==}, {@code divide_int64} for {@code /}). | ||
| * | ||
| * <p>This pattern matches allowed overload IDs by their prefixes (e.g., | ||
| * {@code divide}, {@code size}), optionally followed by numeric types | ||
| * (e.g., {@code int64}) and type-specific suffixes (e.g., {@code _string}, | ||
| * {@code _int64}). | ||
| */ | ||
| private static final Pattern ALLOWED_OVERLOAD_ID_PREFIX_PATTERN = Pattern.compile( | ||
| "^(size|matches|contains|startsWith|endsWith|starts_with|ends_with|" | ||
| + "timestamp|duration|in|index|has|int|uint|double|string|bytes|bool|" | ||
| + "less|less_equals|greater|greater_equals|" | ||
| + "add|subtract|multiply|divide|modulo|negate)" | ||
| + "[0-9]*(_.*)?$"); | ||
|
shivaspeaks marked this conversation as resolved.
|
||
|
|
||
| static final CelRuntime RUNTIME = CelRuntimeFactory.standardCelRuntimeBuilder() | ||
| .setStandardEnvironmentEnabled(false) | ||
| .setStandardFunctions(FUNCTIONS) | ||
| .setOptions(CEL_OPTIONS) | ||
| .build(); | ||
|
|
||
| private CelCommon() {} | ||
|
|
||
| /** | ||
| * Validates that the AST only references the allowed variable ("request") | ||
| * and supported functions as defined in gRFC A106. | ||
| */ | ||
| static void checkAllowedReferences(CelAbstractSyntaxTree ast) { | ||
| for (Map.Entry<Long, CelReference> entry : ast.getReferenceMap().entrySet()) { | ||
| CelReference ref = entry.getValue(); | ||
|
|
||
| // Check for variables (where overloadIds is empty) | ||
| if (!ref.value().isPresent() && ref.overloadIds().isEmpty()) { | ||
| if (!REQUEST_VARIABLE.equals(ref.name())) { | ||
| throw new IllegalArgumentException( | ||
| "CEL expression references unknown variable: " + ref.name()); | ||
| } | ||
| } else if (!ref.overloadIds().isEmpty()) { | ||
| String name = ref.name(); | ||
| if (name.isEmpty()) { | ||
| boolean allowed = false; | ||
| for (String id : ref.overloadIds()) { | ||
| if (id.equals("add_string") || id.equals("add_list") || id.endsWith("_to_string")) { | ||
| allowed = false; | ||
| break; | ||
| } | ||
| if (ALLOWED_EXACT_OVERLOAD_IDS.contains(id) | ||
| || ALLOWED_OVERLOAD_ID_PREFIX_PATTERN.matcher(id).matches()) { | ||
| allowed = true; | ||
| break; | ||
|
shivaspeaks marked this conversation as resolved.
|
||
| } | ||
| } | ||
| if (!allowed) { | ||
| throw new IllegalArgumentException( | ||
| "CEL expression references unknown function with overload IDs: " | ||
| + ref.overloadIds()); | ||
| } | ||
| } else { | ||
| // Standard conversion functions (like string(x)) are named in the AST. | ||
| // We must explicitly reject 'string' here since it's disabled in the environment. | ||
| if (name.equals("string")) { | ||
| throw new IllegalArgumentException( | ||
| "CEL expression references unknown function with overload IDs: " | ||
| + ref.overloadIds()); | ||
| } | ||
| throw new IllegalArgumentException( | ||
| "CEL expression references unsupported named function: " + name); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
88 changes: 88 additions & 0 deletions
88
xds/src/main/java/io/grpc/xds/internal/matcher/CelStringExtractor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * 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.types.SimpleType; | ||
| import dev.cel.runtime.CelEvaluationException; | ||
| import dev.cel.runtime.CelRuntime; | ||
| import dev.cel.runtime.CelVariableResolver; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Executes compiled CEL expressions that extract a string. | ||
| */ | ||
| public final class CelStringExtractor { | ||
| private final CelRuntime.Program program; | ||
| @Nullable | ||
| private final String defaultValue; | ||
|
|
||
| private CelStringExtractor(CelRuntime.Program program, @Nullable String defaultValue) { | ||
| this.program = program; | ||
| this.defaultValue = defaultValue; | ||
| } | ||
|
|
||
| /** | ||
| * Compiles the AST into a CelStringExtractor with an optional default value. | ||
| * Throws an Exception if evaluation fails during compilation setup. | ||
| */ | ||
| public static CelStringExtractor compile(CelAbstractSyntaxTree ast, @Nullable String defaultValue) | ||
| throws CelEvaluationException { | ||
| if (ast.getResultType() != SimpleType.STRING && ast.getResultType() != SimpleType.DYN) { | ||
| throw new IllegalArgumentException( | ||
| "CEL expression must evaluate to string, got: " + ast.getResultType()); | ||
| } | ||
| CelCommon.checkAllowedReferences(ast); | ||
| CelRuntime.Program program = CelCommon.RUNTIME.createProgram(ast); | ||
| return new CelStringExtractor(program, defaultValue); | ||
| } | ||
|
|
||
| /** | ||
| * Compiles the AST into a CelStringExtractor with no default value. | ||
| * Throws an Exception if evaluation fails during compilation setup. | ||
| */ | ||
| public static CelStringExtractor compile(CelAbstractSyntaxTree ast) | ||
| throws CelEvaluationException { | ||
| return compile(ast, null); | ||
| } | ||
|
|
||
| /** | ||
| * Evaluates the CEL expression and returns the string result. | ||
| * Returns the default value if the result is not a string or if evaluation | ||
| * fails. | ||
| */ | ||
| public String extract(Object input) throws CelEvaluationException { | ||
| if (input instanceof CelVariableResolver) { | ||
| try { | ||
| Object result = program.eval((CelVariableResolver) input); | ||
|
|
||
| if (result instanceof String) { | ||
| return (String) result; | ||
| } | ||
| } catch (CelEvaluationException e) { | ||
| if (defaultValue == null) { | ||
| throw e; | ||
| } | ||
| } | ||
| } else if (defaultValue == null) { | ||
| throw new CelEvaluationException( | ||
| "Unsupported input type for CEL evaluation: " | ||
| + (input == null ? "null" : input.getClass().getName())); | ||
| } | ||
|
shivaspeaks marked this conversation as resolved.
|
||
| return defaultValue; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Negative tests need to be added by creating AST and creating runtime with these disallowed expression types.