Skip to content
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f472c63
xds: Implementation of Unified Matcher and CEL Integration
shivaspeaks Feb 3, 2026
99aeef1
fix dependency issue
shivaspeaks Feb 3, 2026
db4d885
add some unit tests to increase coverage
shivaspeaks Feb 3, 2026
33eff21
add some more unit tests
shivaspeaks Feb 4, 2026
e28c8f5
fix unchecked issue
shivaspeaks Feb 4, 2026
78895db
add some more unit tests
shivaspeaks Feb 4, 2026
19989eb
fix/add tests
shivaspeaks Feb 4, 2026
118d7af
remove not required tests
shivaspeaks Feb 4, 2026
77b01b9
add tests
shivaspeaks Feb 4, 2026
310677d
add tests
shivaspeaks Feb 4, 2026
49031b4
add tests
shivaspeaks Feb 4, 2026
5a31706
add some tests
shivaspeaks Feb 5, 2026
c62d193
change dependency on dev.cel:cel
shivaspeaks Feb 12, 2026
0c2d771
Subset dev.cel environment in CelCommon and add regression test
shivaspeaks Feb 12, 2026
91d6bea
Address comments
shivaspeaks Feb 16, 2026
22f65ca
remove dependency from dev.cel:cel
shivaspeaks Feb 18, 2026
5c04ac3
add ANTLR
shivaspeaks Feb 18, 2026
82d9a8b
remove cel compiler from xds/build.gradle
shivaspeaks Feb 18, 2026
7b50380
Refactor StringMatcher parsing logic
shivaspeaks Feb 25, 2026
4b626ad
address comments and create registries
shivaspeaks Feb 27, 2026
c5bf128
address comments
shivaspeaks Mar 2, 2026
c0b2b1d
upgrade cel
shivaspeaks Mar 2, 2026
e07d612
add dev.cel:common
shivaspeaks Mar 2, 2026
020f9ff
add dev.cel:common
shivaspeaks Mar 2, 2026
f20c5f0
add unit tests for exactMatchMap case
shivaspeaks Mar 4, 2026
0bd57e9
add unit tests
shivaspeaks Mar 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ IO_GRPC_GRPC_JAVA_ARTIFACTS = [
"com.google.re2j:re2j:1.8",
"com.google.s2a.proto.v2:s2a-proto:0.1.3",
"com.google.truth:truth:1.4.5",
"dev.cel:cel:0.11.1",
"com.squareup.okhttp:okhttp:2.7.5",
"com.squareup.okio:okio:2.10.0", # 3.0+ needs swapping to -jvm; need work to avoid flag-day
"io.netty:netty-buffer:4.1.130.Final",
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ checkstyle = "com.puppycrawl.tools:checkstyle:10.26.1"
# checkstyle 10.0+ requires Java 11+
# See https://checkstyle.sourceforge.io/releasenotes_old_8-35_10-26.html#Release_10.0
# checkForUpdates: checkstylejava8:9.+
cel = "dev.cel:cel:0.11.1"
checkstylejava8 = "com.puppycrawl.tools:checkstyle:9.3"
commons-math3 = "org.apache.commons:commons-math3:3.6.1"
conscrypt = "org.conscrypt:conscrypt-openjdk-uber:2.5.2"
Expand Down
1 change: 1 addition & 0 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ IO_GRPC_GRPC_JAVA_ARTIFACTS = [
"com.google.re2j:re2j:1.8",
"com.google.s2a.proto.v2:s2a-proto:0.1.3",
"com.google.truth:truth:1.4.5",
"dev.cel:cel:0.11.1",
"com.squareup.okhttp:okhttp:2.7.5",
"com.squareup.okio:okio:2.10.0", # 3.0+ needs swapping to -jvm; need work to avoid flag-day
"io.netty:netty-buffer:4.1.130.Final",
Expand Down
3 changes: 3 additions & 0 deletions xds/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ java_library(
artifact("com.google.errorprone:error_prone_annotations"),
artifact("com.google.guava:guava"),
artifact("com.google.re2j:re2j"),
artifact("dev.cel:cel"),
artifact("io.netty:netty-buffer"),
artifact("io.netty:netty-codec"),
artifact("io.netty:netty-common"),
Expand Down Expand Up @@ -101,6 +102,8 @@ JAR_JAR_RULES = [
"rule io.envoyproxy.** io.grpc.xds.shaded.io.envoyproxy.@1",
"rule udpa.annotations.** io.grpc.xds.shaded.udpa.annotations.@1",
"rule xds.annotations.** io.grpc.xds.shaded.xds.annotations.@1",
"rule dev.cel.** io.grpc.xds.shaded.dev.cel.@1",
"rule cel.** io.grpc.xds.shaded.cel.@1",
]

jar_jar(
Expand Down
7 changes: 7 additions & 0 deletions xds/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ dependencies {
libraries.re2j,
libraries.auto.value.annotations,
libraries.protobuf.java.util
implementation(libraries.cel) {
exclude group: 'com.google.protobuf', module: 'protobuf-java'
}
def nettyDependency = implementation project(':grpc-netty')

testImplementation project(':grpc-api')
Expand Down Expand Up @@ -175,13 +178,15 @@ tasks.named("javadoc").configure {
exclude 'io/grpc/xds/XdsNameResolverProvider.java'
exclude 'io/grpc/xds/internal/**'
exclude 'io/grpc/xds/Internal*'
exclude 'dev/cel/**'
}

def prefixName = 'io.grpc.xds'
tasks.named("shadowJar").configure {
archiveClassifier = null
dependencies {
include(project(':grpc-xds'))
include(dependency('dev.cel:.*'))
}
// Relocated packages commonly need exclusions in jacocoTestReport and javadoc
// Keep in sync with BUILD.bazel's JAR_JAR_RULES
Expand All @@ -198,6 +203,8 @@ tasks.named("shadowJar").configure {
// TODO: missing java_package option in .proto
relocate 'udpa.annotations', "${prefixName}.shaded.udpa.annotations"
relocate 'xds.annotations', "${prefixName}.shaded.xds.annotations"
relocate 'dev.cel', "${prefixName}.shaded.dev.cel"
relocate 'cel', "${prefixName}.shaded.cel"
exclude "**/*.proto"
}

Expand Down
2 changes: 1 addition & 1 deletion xds/src/main/java/io/grpc/xds/internal/MatcherParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static Matchers.StringMatcher parseStringMatcher(
return Matchers.StringMatcher.forSafeRegEx(
Pattern.compile(proto.getSafeRegex().getRegex()));
case CONTAINS:
return Matchers.StringMatcher.forContains(proto.getContains());
return Matchers.StringMatcher.forContains(proto.getContains(), proto.getIgnoreCase());
case MATCHPATTERN_NOT_SET:
default:
throw new IllegalArgumentException(
Expand Down
13 changes: 10 additions & 3 deletions xds/src/main/java/io/grpc/xds/internal/Matchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,15 @@ public static StringMatcher forSafeRegEx(Pattern regEx) {
}

/** The input string should contain this substring. */
public static StringMatcher forContains(String contains) {
public static StringMatcher forContains(String contains, boolean ignoreCase) {
checkNotNull(contains, "contains");
return StringMatcher.create(null, null, null, null, contains,
false/* doesn't matter */);
ignoreCase);
}

/** The input string should contain this substring. */
public static StringMatcher forContains(String contains) {
return forContains(contains, false);
}

/** Returns the matching result for this string. */
Expand All @@ -281,7 +286,9 @@ public boolean matches(String args) {
? args.toLowerCase(Locale.ROOT).endsWith(suffix().toLowerCase(Locale.ROOT))
: args.endsWith(suffix());
} else if (contains() != null) {
return args.contains(contains());
return ignoreCase()
? args.toLowerCase(Locale.ROOT).contains(contains().toLowerCase(Locale.ROOT))
: args.contains(contains());
}
return regEx().matches(args);
}
Expand Down
95 changes: 95 additions & 0 deletions xds/src/main/java/io/grpc/xds/internal/matcher/CelCommon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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)
.maxRegexProgramSize(100)
.build();

private static final dev.cel.checker.CelStandardDeclarations DECLARATIONS =
dev.cel.checker.CelStandardDeclarations.newBuilder()
.filterFunctions((func, over) -> {
if (func == dev.cel.checker.CelStandardDeclarations.StandardFunction.STRING) {
return false;
}
if (func == dev.cel.checker.CelStandardDeclarations.StandardFunction.ADD) {
String id = over.celOverloadDecl().overloadId();
return !id.equals("add_string") && !id.equals("add_list");
}
return true;
})
.build();

private static final dev.cel.runtime.CelStandardFunctions FUNCTIONS =
dev.cel.runtime.CelStandardFunctions.newBuilder()
.filterFunctions((func, over) -> {
if (func == dev.cel.runtime.CelStandardFunctions.StandardFunction.STRING) {
return false;
}
if (func == dev.cel.runtime.CelStandardFunctions.StandardFunction.ADD) {
return !over.equals(
(Object) dev.cel.runtime.standard.AddOperator.AddOverload.ADD_STRING)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we needing to cast to Object? If you're working around ErrorProne, then something seems broken and casting to Object is just hiding it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it was an errorProne workaround.
over is statically known as a StandardOverload interface, but ADD_STRING is an AddOverload enum. ErrorProne threw an [EqualsIncompatibleType] build error so we need to cast the ADD_STRING enum specifically to (Object) before passing it into equals.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ADD_STRING is a CelStandardOverload, which isn't a StandardOverload. It seems ErrorProne is correct; these will never be equal.

You should comment cases like this. If this were a bug in ErrorProne we'd probably want to file a bug with ErrorProne, but even if not, the comment helps us know when it is no longer necessary. (We don't have to guess why it was done, so we can confirm if that is no longer the case.)

It looks like that has been adjusted on cel-java master, as the function is now passed CelStandardOverload.

@sergiitk, the interface change looks like a breaking API change. That's surprising since I thought we were using stable APIs. Is this PR using an API that it shouldn't be, was this a known change that just hadn't been released yet, or should we be concerned about the actual stability of the API?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a relatively new API that didn't exist when API stabilization doc was made (go/cel-api-stabilization). It's stable now (I'll update the doc when I get a chance). Reason why I suggested this API is that it follows the recommended pattern for how environment subsetting is supposed to be done across all CEL stacks.

As for EP -- I'm surprised it's actually flagging this (internally, it doesn't). AddOperator enum implements CelStandardOverload, so they should equal. My only guess is that CelStandardOverload is a functional interface, so theoretically it's possible to provide a lambda that's not equals comparable. Though we control the standard overload implementations, so in practice this won't happen. Checking for reference identity should likely get around the issue over == AddOverload.ADD_STRING

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, this is the new setup flow. I didn't look where this FUNCTIONS constant was used. That's fine; I'm less surprised that changed. (And yes, it's good to add it to the doc.)

I'm surprised it's actually flagging this (internally, it doesn't)

I don't think it'd get flagged on master either. It only looked to be a problem because we're still using 0.11.1; in 0.11.1 I agree with ErrorProne that the equals() will always return false.

&& !over.equals(
(Object) dev.cel.runtime.standard.AddOperator.AddOverload.ADD_LIST);
}
return true;
})
.build();

static final CelCompiler COMPILER = CelCompilerFactory.standardCelCompilerBuilder()
.setStandardEnvironmentEnabled(false)
.setStandardDeclarations(DECLARATIONS)
.addVar("request", SimpleType.DYN)
.setOptions(CEL_OPTIONS)
.build();

static final CelRuntime RUNTIME = CelRuntimeFactory.standardCelRuntimeBuilder()
.setStandardEnvironmentEnabled(false)
.setStandardFunctions(FUNCTIONS)
.setOptions(CEL_OPTIONS)
.build();

private CelCommon() {}

static void checkAllowedVariables(CelAbstractSyntaxTree ast) {
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().isPresent() && ref.overloadIds().isEmpty()) {
if (!"request".equals(ref.name())) {
throw new IllegalArgumentException(
"CEL expression references unknown variable: " + ref.name());
}
}
}
}
}
71 changes: 71 additions & 0 deletions xds/src/main/java/io/grpc/xds/internal/matcher/CelMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.CelValidationException;
import dev.cel.common.types.SimpleType;
import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelRuntime;

/**
* Executes compiled CEL expressions.
*/
public final class CelMatcher {
private final CelRuntime.Program program;

private CelMatcher(CelRuntime.Program program) {
this.program = program;
}

/**
* Compiles the AST into a CelMatcher.
*/
public static CelMatcher compile(CelAbstractSyntaxTree ast)
throws CelValidationException, CelEvaluationException {
if (ast.getResultType() != SimpleType.BOOL) {
throw new IllegalArgumentException(
"CEL expression must evaluate to boolean, got: " + ast.getResultType());
}
CelCommon.checkAllowedVariables(ast);
CelRuntime.Program program = CelCommon.RUNTIME.createProgram(ast);
return new CelMatcher(program);
}

/**
* Evaluates the CEL expression against the input activation.
*/
public boolean match(Object input) throws CelEvaluationException {
Object result;
if (input instanceof dev.cel.runtime.CelVariableResolver) {
result = program.eval((dev.cel.runtime.CelVariableResolver) input);
} else if (input instanceof java.util.Map) {
@SuppressWarnings("unchecked")
java.util.Map<String, ?> mapInput = (java.util.Map<String, ?>) input;
result = program.eval(mapInput);
} else {
throw new CelEvaluationException(
"Unsupported input type for CEL evaluation: " + input.getClass().getName());
}

if (result instanceof Boolean) {
return (Boolean) result;
}
throw new CelEvaluationException(
"CEL expression must evaluate to boolean, got: " + result.getClass().getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.CelValidationException;
import dev.cel.common.types.SimpleType;
import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelRuntime;

/**
* Executes compiled CEL expressions that extract a string.
*/
public final class CelStringExtractor {
private final CelRuntime.Program program;

private CelStringExtractor(CelRuntime.Program program) {
this.program = program;
}

/**
* Compiles the AST into a CelStringExtractor.
*/
public static CelStringExtractor compile(CelAbstractSyntaxTree ast)
throws CelValidationException, CelEvaluationException {
if (ast.getResultType() != SimpleType.STRING && ast.getResultType() != SimpleType.DYN) {
throw new IllegalArgumentException(
"CEL expression must evaluate to string, got: " + ast.getResultType());
}
CelCommon.checkAllowedVariables(ast);
CelRuntime.Program program = CelCommon.RUNTIME.createProgram(ast);
return new CelStringExtractor(program);
}

/**
* Compiles the CEL expression string into a CelStringExtractor.
*/
public static CelStringExtractor compile(String expression)
throws CelValidationException, CelEvaluationException {
CelAbstractSyntaxTree ast = CelCommon.COMPILER.compile(expression).getAst();
return compile(ast);
}

/**
* Evaluates the CEL expression against the input activation and returns the string result.
* Returns null if the result is not a string.
*/
public String extract(Object input) throws CelEvaluationException {
Object result;
if (input instanceof dev.cel.runtime.CelVariableResolver) {
result = program.eval((dev.cel.runtime.CelVariableResolver) input);
} else if (input instanceof java.util.Map) {
@SuppressWarnings("unchecked")
java.util.Map<String, ?> mapInput = (java.util.Map<String, ?>) input;
result = program.eval(mapInput);
} else {
throw new CelEvaluationException(
"Unsupported input type for CEL evaluation: " + input.getClass().getName());
}

if (result instanceof String) {
return (String) result;
}
return null;
}
}
Loading