Skip to content

xds: Expose filter names to filter instances #11971

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

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion xds/src/main/java/io/grpc/xds/FaultFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public boolean isClientFilter() {
}

@Override
public FaultFilter newInstance() {
public FaultFilter newInstance(String name) {
Copy link
Member

Choose a reason for hiding this comment

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

Right. We are not using name here but just to follow the interface we are doing this. I have ditto commit in local.. :)

return INSTANCE;
}

Expand Down
2 changes: 1 addition & 1 deletion xds/src/main/java/io/grpc/xds/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ default boolean isServerFilter() {
* <li>Filter name+typeUrl in FilterChain's HCM.http_filters.</li>
* </ol>
*/
Filter newInstance();
Filter newInstance(String name);

/**
* Parses the top-level filter config from raw proto message. The message may be either a {@link
Expand Down
2 changes: 1 addition & 1 deletion xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public boolean isClientFilter() {
}

@Override
public GcpAuthenticationFilter newInstance() {
public GcpAuthenticationFilter newInstance(String name) {
return new GcpAuthenticationFilter();
}

Expand Down
2 changes: 1 addition & 1 deletion xds/src/main/java/io/grpc/xds/InternalRbacFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
throw new IllegalArgumentException(
String.format("Failed to parse Rbac policy: %s", filterConfig.errorDetail));
}
return new RbacFilter.Provider().newInstance()
return new RbacFilter.Provider().newInstance("internalRbacFilter")

Check warning on line 36 in xds/src/main/java/io/grpc/xds/InternalRbacFilter.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/InternalRbacFilter.java#L36

Added line #L36 was not covered by tests
.buildServerInterceptor(filterConfig.config, null);
}
}
2 changes: 1 addition & 1 deletion xds/src/main/java/io/grpc/xds/RbacFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public boolean isServerFilter() {
}

@Override
public RbacFilter newInstance() {
public RbacFilter newInstance(String name) {
return INSTANCE;
}

Expand Down
2 changes: 1 addition & 1 deletion xds/src/main/java/io/grpc/xds/RouterFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public boolean isServerFilter() {
}

@Override
public RouterFilter newInstance() {
public RouterFilter newInstance(String name) {
return INSTANCE;
}

Expand Down
3 changes: 2 additions & 1 deletion xds/src/main/java/io/grpc/xds/XdsNameResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@ private void updateActiveFilters(@Nullable List<NamedFilterConfig> filterConfigs

Filter.Provider provider = filterRegistry.get(typeUrl);
checkNotNull(provider, "provider %s", typeUrl);
Filter filter = activeFilters.computeIfAbsent(filterKey, k -> provider.newInstance());
Filter filter = activeFilters.computeIfAbsent(
filterKey, k -> provider.newInstance(namedFilter.name));
checkNotNull(filter, "filter %s", filterKey);
filtersToShutdown.remove(filterKey);
}
Expand Down
3 changes: 2 additions & 1 deletion xds/src/main/java/io/grpc/xds/XdsServerWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ private void updateActiveFiltersForChain(

Filter.Provider provider = filterRegistry.get(typeUrl);
checkNotNull(provider, "provider %s", typeUrl);
Filter filter = chainFilters.computeIfAbsent(filterKey, k -> provider.newInstance());
Filter filter = chainFilters.computeIfAbsent(
filterKey, k -> provider.newInstance(namedFilter.name));
checkNotNull(filter, "filter %s", filterKey);
filtersToShutdown.remove(filterKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ public boolean isClientFilter() {
}

@Override
public TestFilter newInstance() {
public TestFilter newInstance(String name) {
return new TestFilter();
}

Expand Down
10 changes: 6 additions & 4 deletions xds/src/test/java/io/grpc/xds/RbacFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public class RbacFilterTest {
StringMatcher.newBuilder().setExact("/" + PATH).setIgnoreCase(true).build();
private static final RbacFilter.Provider FILTER_PROVIDER = new RbacFilter.Provider();

private final String name = "theFilterName";

@Test
public void filterType_serverOnly() {
assertThat(FILTER_PROVIDER.isClientFilter()).isFalse();
Expand Down Expand Up @@ -259,7 +261,7 @@ public void testAuthorizationInterceptor() {
OrMatcher.create(AlwaysTrueMatcher.INSTANCE));
AuthConfig authconfig = AuthConfig.create(Collections.singletonList(policyMatcher),
GrpcAuthorizationEngine.Action.ALLOW);
FILTER_PROVIDER.newInstance().buildServerInterceptor(RbacConfig.create(authconfig), null)
FILTER_PROVIDER.newInstance(name).buildServerInterceptor(RbacConfig.create(authconfig), null)
.interceptCall(mockServerCall, new Metadata(), mockHandler);
verify(mockHandler, never()).startCall(eq(mockServerCall), any(Metadata.class));
ArgumentCaptor<Status> captor = ArgumentCaptor.forClass(Status.class);
Expand All @@ -271,7 +273,7 @@ public void testAuthorizationInterceptor() {

authconfig = AuthConfig.create(Collections.singletonList(policyMatcher),
GrpcAuthorizationEngine.Action.DENY);
FILTER_PROVIDER.newInstance().buildServerInterceptor(RbacConfig.create(authconfig), null)
FILTER_PROVIDER.newInstance(name).buildServerInterceptor(RbacConfig.create(authconfig), null)
.interceptCall(mockServerCall, new Metadata(), mockHandler);
verify(mockHandler).startCall(eq(mockServerCall), any(Metadata.class));
}
Expand Down Expand Up @@ -322,7 +324,7 @@ public void overrideConfig() {
RbacConfig override = FILTER_PROVIDER.parseFilterConfigOverride(Any.pack(rbacPerRoute)).config;
assertThat(override).isEqualTo(RbacConfig.create(null));
ServerInterceptor interceptor =
FILTER_PROVIDER.newInstance().buildServerInterceptor(original, override);
FILTER_PROVIDER.newInstance(name).buildServerInterceptor(original, override);
assertThat(interceptor).isNull();

policyMatcher = PolicyMatcher.create("policy-matcher-override",
Expand All @@ -332,7 +334,7 @@ public void overrideConfig() {
GrpcAuthorizationEngine.Action.ALLOW);
override = RbacConfig.create(authconfig);

FILTER_PROVIDER.newInstance().buildServerInterceptor(original, override)
FILTER_PROVIDER.newInstance(name).buildServerInterceptor(original, override)
.interceptCall(mockServerCall, new Metadata(), mockHandler);
verify(mockHandler).startCall(eq(mockServerCall), any(Metadata.class));
verify(mockServerCall).getAttributes();
Expand Down
2 changes: 1 addition & 1 deletion xds/src/test/java/io/grpc/xds/StatefulFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public boolean isServerFilter() {
}

@Override
public synchronized StatefulFilter newInstance() {
public synchronized StatefulFilter newInstance(String name) {
StatefulFilter filter = new StatefulFilter(counter++);
instances.put(filter.idx, filter);
return filter;
Expand Down
2 changes: 1 addition & 1 deletion xds/src/test/java/io/grpc/xds/XdsNameResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void setUp() {
// Lenient: suppress [MockitoHint] Unused warning, only used in resolved_fault* tests.
lenient()
.doReturn(new FaultFilter(mockRandom, new AtomicLong()))
.when(faultFilterProvider).newInstance();
.when(faultFilterProvider).newInstance(any(String.class));

FilterRegistry filterRegistry = FilterRegistry.newRegistry().register(
ROUTER_FILTER_PROVIDER,
Expand Down
4 changes: 2 additions & 2 deletions xds/src/test/java/io/grpc/xds/XdsServerWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ public void run() {
Filter.Provider filterProvider = mock(Filter.Provider.class);
when(filterProvider.typeUrls()).thenReturn(new String[]{"filter-type-url"});
when(filterProvider.isServerFilter()).thenReturn(true);
when(filterProvider.newInstance()).thenReturn(filter);
when(filterProvider.newInstance(any(String.class))).thenReturn(filter);
filterRegistry.register(filterProvider);

FilterConfig f0 = mock(FilterConfig.class);
Expand Down Expand Up @@ -1208,7 +1208,7 @@ public void run() {
Filter.Provider filterProvider = mock(Filter.Provider.class);
when(filterProvider.typeUrls()).thenReturn(new String[]{"filter-type-url"});
when(filterProvider.isServerFilter()).thenReturn(true);
when(filterProvider.newInstance()).thenReturn(filter);
when(filterProvider.newInstance(any(String.class))).thenReturn(filter);
filterRegistry.register(filterProvider);

FilterConfig f0 = mock(FilterConfig.class);
Expand Down