Skip to content

Commit 89d67bf

Browse files
sauravzgkannanjgithub
authored andcommitted
Fixup 12492: Eliminate GrpcService..Provider classes
1 parent 44a4d24 commit 89d67bf

7 files changed

Lines changed: 155 additions & 166 deletions

File tree

xds/src/main/java/io/grpc/xds/internal/extauthz/ExtAuthzConfigParser.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
import com.google.common.collect.ImmutableList;
2020
import io.envoyproxy.envoy.extensions.filters.http.ext_authz.v3.ExtAuthz;
2121
import io.grpc.internal.GrpcUtil;
22+
import io.grpc.xds.client.Bootstrapper.BootstrapInfo;
23+
import io.grpc.xds.client.Bootstrapper.ServerInfo;
2224
import io.grpc.xds.internal.MatcherParser;
2325
import io.grpc.xds.internal.grpcservice.GrpcServiceConfig;
2426
import io.grpc.xds.internal.grpcservice.GrpcServiceConfigParser;
2527
import io.grpc.xds.internal.grpcservice.GrpcServiceParseException;
26-
import io.grpc.xds.internal.grpcservice.GrpcServiceXdsContextProvider;
2728
import io.grpc.xds.internal.headermutations.HeaderMutationRulesParseException;
2829
import io.grpc.xds.internal.headermutations.HeaderMutationRulesParser;
2930

@@ -44,7 +45,7 @@ private ExtAuthzConfigParser() {}
4445
* @throws ExtAuthzParseException if the proto is invalid or contains unsupported features.
4546
*/
4647
public static ExtAuthzConfig parse(
47-
ExtAuthz extAuthzProto, GrpcServiceXdsContextProvider contextProvider)
48+
ExtAuthz extAuthzProto, BootstrapInfo bootstrapInfo, ServerInfo serverInfo)
4849
throws ExtAuthzParseException {
4950
if (!extAuthzProto.hasGrpcService()) {
5051
throw new ExtAuthzParseException(
@@ -53,7 +54,7 @@ public static ExtAuthzConfig parse(
5354
GrpcServiceConfig grpcServiceConfig;
5455
try {
5556
grpcServiceConfig =
56-
GrpcServiceConfigParser.parse(extAuthzProto.getGrpcService(), contextProvider);
57+
GrpcServiceConfigParser.parse(extAuthzProto.getGrpcService(), bootstrapInfo, serverInfo);
5758
} catch (GrpcServiceParseException e) {
5859
throw new ExtAuthzParseException("Failed to parse GrpcService config: " + e.getMessage(), e);
5960
}

xds/src/main/java/io/grpc/xds/internal/grpcservice/GrpcServiceConfigParser.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
import io.grpc.CompositeCallCredentials;
3030
import io.grpc.InsecureChannelCredentials;
3131
import io.grpc.Metadata;
32+
import io.grpc.NameResolverRegistry;
3233
import io.grpc.SecurityLevel;
3334
import io.grpc.alts.GoogleDefaultChannelCredentials;
3435
import io.grpc.auth.MoreCallCredentials;
3536
import io.grpc.xds.XdsChannelCredentials;
37+
import io.grpc.xds.client.Bootstrapper;
38+
import java.net.URI;
39+
import java.net.URISyntaxException;
3640
import java.time.Duration;
3741
import java.util.ArrayList;
3842
import java.util.Date;
@@ -71,15 +75,16 @@ private GrpcServiceConfigParser() {}
7175
* @return A {@link GrpcServiceConfig} instance.
7276
* @throws GrpcServiceParseException if the proto is invalid or uses unsupported features.
7377
*/
74-
public static GrpcServiceConfig parse(GrpcService grpcServiceProto,
75-
GrpcServiceXdsContextProvider contextProvider)
78+
public static GrpcServiceConfig parse(
79+
GrpcService grpcServiceProto, Bootstrapper.BootstrapInfo bootstrapInfo,
80+
Bootstrapper.ServerInfo serverInfo)
7681
throws GrpcServiceParseException {
7782
if (!grpcServiceProto.hasGoogleGrpc()) {
7883
throw new GrpcServiceParseException(
7984
"Unsupported: GrpcService must have GoogleGrpc, got: " + grpcServiceProto);
8085
}
8186
GrpcServiceConfig.GoogleGrpcConfig googleGrpcConfig =
82-
parseGoogleGrpcConfig(grpcServiceProto.getGoogleGrpc(), contextProvider);
87+
parseGoogleGrpcConfig(grpcServiceProto.getGoogleGrpc(), bootstrapInfo, serverInfo);
8388

8489
GrpcServiceConfig.Builder builder = GrpcServiceConfig.builder().googleGrpc(googleGrpcConfig);
8590

@@ -119,19 +124,41 @@ public static GrpcServiceConfig parse(GrpcService grpcServiceProto,
119124
* @throws GrpcServiceParseException if the proto is invalid.
120125
*/
121126
public static GrpcServiceConfig.GoogleGrpcConfig parseGoogleGrpcConfig(
122-
GrpcService.GoogleGrpc googleGrpcProto, GrpcServiceXdsContextProvider contextProvider)
127+
GrpcService.GoogleGrpc googleGrpcProto, Bootstrapper.BootstrapInfo bootstrapInfo,
128+
Bootstrapper.ServerInfo serverInfo)
123129
throws GrpcServiceParseException {
124130

125131
String targetUri = googleGrpcProto.getTargetUri();
126-
GrpcServiceXdsContext context = contextProvider.getContextForTarget(targetUri);
127132

128-
if (!context.isTargetUriSchemeSupported()) {
133+
AllowedGrpcServices allowedGrpcServices = bootstrapInfo.allowedGrpcServices()
134+
.filter(AllowedGrpcServices.class::isInstance)
135+
.map(AllowedGrpcServices.class::cast)
136+
.orElse(AllowedGrpcServices.empty());
137+
138+
boolean isTrustedControlPlane = serverInfo.isTrustedXdsServer();
139+
Optional<AllowedGrpcService> override =
140+
Optional.ofNullable(allowedGrpcServices.services().get(targetUri));
141+
142+
boolean isTargetUriSchemeSupported = false;
143+
try {
144+
URI uri = new URI(targetUri);
145+
String scheme = uri.getScheme();
146+
if (scheme == null) {
147+
scheme = NameResolverRegistry.getDefaultRegistry().getDefaultScheme();
148+
}
149+
if (scheme != null) {
150+
isTargetUriSchemeSupported =
151+
NameResolverRegistry.getDefaultRegistry().getProviderForScheme(scheme) != null;
152+
}
153+
} catch (URISyntaxException e) {
154+
// Fallback or ignore if not a valid URI
155+
}
156+
157+
if (!isTargetUriSchemeSupported) {
129158
throw new GrpcServiceParseException("Target URI scheme is not resolvable: " + targetUri);
130159
}
131160

132-
if (!context.isTrustedControlPlane()) {
133-
Optional<AllowedGrpcService> override =
134-
context.validAllowedGrpcService();
161+
if (!isTrustedControlPlane) {
135162
if (!override.isPresent()) {
136163
throw new GrpcServiceParseException(
137164
"Untrusted xDS server & URI not found in allowed_grpc_services: " + targetUri);

xds/src/main/java/io/grpc/xds/internal/grpcservice/GrpcServiceXdsContext.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

xds/src/main/java/io/grpc/xds/internal/grpcservice/GrpcServiceXdsContextProvider.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

xds/src/test/java/io/grpc/xds/internal/extauthz/ExtAuthzConfigParserTest.java

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.protobuf.Any;
2323
import com.google.protobuf.BoolValue;
2424
import io.envoyproxy.envoy.config.common.mutation_rules.v3.HeaderMutationRules;
25+
import io.envoyproxy.envoy.config.core.v3.GrpcService;
2526
import io.envoyproxy.envoy.config.core.v3.HeaderValue;
2627
import io.envoyproxy.envoy.config.core.v3.RuntimeFeatureFlag;
2728
import io.envoyproxy.envoy.config.core.v3.RuntimeFractionalPercent;
@@ -34,8 +35,12 @@
3435
import io.envoyproxy.envoy.type.v3.FractionalPercent;
3536
import io.envoyproxy.envoy.type.v3.FractionalPercent.DenominatorType;
3637
import io.grpc.Status;
38+
import io.grpc.xds.client.Bootstrapper.BootstrapInfo;
39+
import io.grpc.xds.client.Bootstrapper.ServerInfo;
40+
import io.grpc.xds.client.EnvoyProtoData.Node;
3741
import io.grpc.xds.internal.Matchers;
3842
import io.grpc.xds.internal.headermutations.HeaderMutationRulesConfig;
43+
import java.util.Collections;
3944
import org.junit.Before;
4045
import org.junit.Test;
4146
import org.junit.runner.RunWith;
@@ -49,13 +54,23 @@ public class ExtAuthzConfigParserTest {
4954
private static final Any FAKE_ACCESS_TOKEN_CALL_CREDS =
5055
Any.pack(AccessTokenCredentials.newBuilder().setToken("fake-token").build());
5156

57+
private static BootstrapInfo dummyBootstrapInfo() {
58+
return BootstrapInfo.builder()
59+
.servers(
60+
Collections.singletonList(ServerInfo.create("test_target", Collections.emptyMap())))
61+
.node(Node.newBuilder().build()).build();
62+
}
63+
64+
private static ServerInfo dummyServerInfo() {
65+
return ServerInfo.create("test_target", Collections.emptyMap(), false, true, false, false);
66+
}
67+
5268
private ExtAuthz.Builder extAuthzBuilder;
5369

5470
@Before
5571
public void setUp() {
5672
extAuthzBuilder = ExtAuthz.newBuilder()
57-
.setGrpcService(io.envoyproxy.envoy.config.core.v3.GrpcService.newBuilder()
58-
.setGoogleGrpc(io.envoyproxy.envoy.config.core.v3.GrpcService.GoogleGrpc.newBuilder()
73+
.setGrpcService(GrpcService.newBuilder().setGoogleGrpc(GrpcService.GoogleGrpc.newBuilder()
5974
.setTargetUri("test-cluster")
6075
.addChannelCredentialsPlugin(GOOGLE_DEFAULT_CHANNEL_CREDS)
6176
.addCallCredentialsPlugin(FAKE_ACCESS_TOKEN_CALL_CREDS).build())
@@ -67,7 +82,8 @@ public void parse_missingGrpcService_throws() {
6782
ExtAuthz extAuthz = ExtAuthz.newBuilder().build();
6883
try {
6984
ExtAuthzConfigParser.parse(extAuthz,
70-
io.grpc.xds.internal.grpcservice.GrpcServiceXdsContextTestUtil.dummyProvider());
85+
dummyBootstrapInfo(),
86+
dummyServerInfo());
7187
fail("Expected ExtAuthzParseException");
7288
} catch (ExtAuthzParseException e) {
7389
assertThat(e).hasMessageThat()
@@ -78,11 +94,12 @@ public void parse_missingGrpcService_throws() {
7894
@Test
7995
public void parse_invalidGrpcService_throws() {
8096
ExtAuthz extAuthz = ExtAuthz.newBuilder()
81-
.setGrpcService(io.envoyproxy.envoy.config.core.v3.GrpcService.newBuilder().build())
97+
.setGrpcService(GrpcService.newBuilder().build())
8298
.build();
8399
try {
84100
ExtAuthzConfigParser.parse(extAuthz,
85-
io.grpc.xds.internal.grpcservice.GrpcServiceXdsContextTestUtil.dummyProvider());
101+
dummyBootstrapInfo(),
102+
dummyServerInfo());
86103
fail("Expected ExtAuthzParseException");
87104
} catch (ExtAuthzParseException e) {
88105
assertThat(e).hasMessageThat().startsWith("Failed to parse GrpcService config:");
@@ -97,7 +114,8 @@ public void parse_invalidAllowExpression_throws() {
97114
.build();
98115
try {
99116
ExtAuthzConfigParser.parse(extAuthz,
100-
io.grpc.xds.internal.grpcservice.GrpcServiceXdsContextTestUtil.dummyProvider());
117+
dummyBootstrapInfo(),
118+
dummyServerInfo());
101119
fail("Expected ExtAuthzParseException");
102120
} catch (ExtAuthzParseException e) {
103121
assertThat(e).hasMessageThat().startsWith("Invalid regex pattern for allow_expression:");
@@ -112,7 +130,8 @@ public void parse_invalidDisallowExpression_throws() {
112130
.build();
113131
try {
114132
ExtAuthzConfigParser.parse(extAuthz,
115-
io.grpc.xds.internal.grpcservice.GrpcServiceXdsContextTestUtil.dummyProvider());
133+
dummyBootstrapInfo(),
134+
dummyServerInfo());
116135
fail("Expected ExtAuthzParseException");
117136
} catch (ExtAuthzParseException e) {
118137
assertThat(e).hasMessageThat().startsWith("Invalid regex pattern for disallow_expression:");
@@ -149,7 +168,8 @@ public void parse_success() throws ExtAuthzParseException {
149168
.build();
150169

151170
ExtAuthzConfig config = ExtAuthzConfigParser.parse(extAuthz,
152-
io.grpc.xds.internal.grpcservice.GrpcServiceXdsContextTestUtil.dummyProvider());
171+
dummyBootstrapInfo(),
172+
dummyServerInfo());
153173

154174
assertThat(config.grpcService().googleGrpc().target()).isEqualTo("test-cluster");
155175
assertThat(config.grpcService().timeout().get().getSeconds()).isEqualTo(5);
@@ -178,7 +198,8 @@ public void parse_saneDefaults() throws ExtAuthzParseException {
178198
ExtAuthz extAuthz = extAuthzBuilder.build();
179199

180200
ExtAuthzConfig config = ExtAuthzConfigParser.parse(extAuthz,
181-
io.grpc.xds.internal.grpcservice.GrpcServiceXdsContextTestUtil.dummyProvider());
201+
dummyBootstrapInfo(),
202+
dummyServerInfo());
182203

183204
assertThat(config.failureModeAllow()).isFalse();
184205
assertThat(config.failureModeAllowHeaderAdd()).isFalse();
@@ -199,7 +220,8 @@ public void parse_headerMutationRules_allowExpressionOnly() throws ExtAuthzParse
199220
.build();
200221

201222
ExtAuthzConfig config = ExtAuthzConfigParser.parse(extAuthz,
202-
io.grpc.xds.internal.grpcservice.GrpcServiceXdsContextTestUtil.dummyProvider());
223+
dummyBootstrapInfo(),
224+
dummyServerInfo());
203225

204226
assertThat(config.decoderHeaderMutationRules().isPresent()).isTrue();
205227
HeaderMutationRulesConfig rules = config.decoderHeaderMutationRules().get();
@@ -215,7 +237,8 @@ public void parse_headerMutationRules_disallowExpressionOnly() throws ExtAuthzPa
215237
.build()).build();
216238

217239
ExtAuthzConfig config = ExtAuthzConfigParser.parse(extAuthz,
218-
io.grpc.xds.internal.grpcservice.GrpcServiceXdsContextTestUtil.dummyProvider());
240+
dummyBootstrapInfo(),
241+
dummyServerInfo());
219242

220243
assertThat(config.decoderHeaderMutationRules().isPresent()).isTrue();
221244
HeaderMutationRulesConfig rules = config.decoderHeaderMutationRules().get();
@@ -231,7 +254,8 @@ public void parse_filterEnabled_hundred() throws ExtAuthzParseException {
231254
.build();
232255

233256
ExtAuthzConfig config = ExtAuthzConfigParser.parse(extAuthz,
234-
io.grpc.xds.internal.grpcservice.GrpcServiceXdsContextTestUtil.dummyProvider());
257+
dummyBootstrapInfo(),
258+
dummyServerInfo());
235259

236260
assertThat(config.filterEnabled()).isEqualTo(Matchers.FractionMatcher.create(25, 100));
237261
}
@@ -245,7 +269,8 @@ public void parse_filterEnabled_million() throws ExtAuthzParseException {
245269
.build();
246270

247271
ExtAuthzConfig config = ExtAuthzConfigParser.parse(extAuthz,
248-
io.grpc.xds.internal.grpcservice.GrpcServiceXdsContextTestUtil.dummyProvider());
272+
dummyBootstrapInfo(),
273+
dummyServerInfo());
249274

250275
assertThat(config.filterEnabled())
251276
.isEqualTo(Matchers.FractionMatcher.create(123456, 1_000_000));
@@ -260,7 +285,8 @@ public void parse_filterEnabled_unrecognizedDenominator() {
260285

261286
try {
262287
ExtAuthzConfigParser.parse(extAuthz,
263-
io.grpc.xds.internal.grpcservice.GrpcServiceXdsContextTestUtil.dummyProvider());
288+
dummyBootstrapInfo(),
289+
dummyServerInfo());
264290
fail("Expected ExtAuthzParseException");
265291
} catch (ExtAuthzParseException e) {
266292
assertThat(e).hasMessageThat().isEqualTo("Unknown denominator type: UNRECOGNIZED");

0 commit comments

Comments
 (0)