Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit 4a7713a

Browse files
feat: pass a CallCredentials to grpc-java for DirectPath (#1488)
* feat: pass a CallCredentials to grpc-java for DirectPath Co-authored-by: Chanseok Oh <chanseok@google.com>
1 parent 73d078e commit 4a7713a

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

‎gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@
4747
import com.google.common.collect.ImmutableList;
4848
import com.google.common.collect.ImmutableMap;
4949
import com.google.common.io.Files;
50+
import io.grpc.CallCredentials;
5051
import io.grpc.ChannelCredentials;
5152
import io.grpc.Grpc;
5253
import io.grpc.ManagedChannel;
5354
import io.grpc.ManagedChannelBuilder;
5455
import io.grpc.TlsChannelCredentials;
55-
import io.grpc.alts.ComputeEngineChannelBuilder;
56+
import io.grpc.alts.GoogleDefaultChannelCredentials;
57+
import io.grpc.auth.MoreCallCredentials;
5658
import java.io.File;
5759
import java.io.IOException;
5860
import java.nio.charset.StandardCharsets;
@@ -336,13 +338,16 @@ private ManagedChannel createSingleChannel() throws IOException, GeneralSecurity
336338
if (isDirectPathEnabled(serviceAddress)
337339
&& isNonDefaultServiceAccountAllowed()
338340
&& isOnComputeEngine()) {
341+
CallCredentials callCreds = MoreCallCredentials.from(credentials);
342+
ChannelCredentials channelCreds =
343+
GoogleDefaultChannelCredentials.newBuilder().callCredentials(callCreds).build();
339344
isDirectPathXdsEnabled = Boolean.parseBoolean(envProvider.getenv(DIRECT_PATH_ENV_ENABLE_XDS));
340345
if (isDirectPathXdsEnabled) {
341346
// google-c2p resolver target must not have a port number
342347
builder =
343-
ComputeEngineChannelBuilder.forTarget("google-c2p-experimental:///" + serviceAddress);
348+
Grpc.newChannelBuilder("google-c2p-experimental:///" + serviceAddress, channelCreds);
344349
} else {
345-
builder = ComputeEngineChannelBuilder.forAddress(serviceAddress, port);
350+
builder = Grpc.newChannelBuilderForAddress(serviceAddress, port, channelCreds);
346351
builder.defaultServiceConfig(directPathServiceConfig);
347352
}
348353
// Set default keepAliveTime and keepAliveTimeout when directpath environment is enabled.

‎gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,20 +263,9 @@ public void testWithGCECredentials() throws IOException {
263263
ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1);
264264
executor.shutdown();
265265

266-
ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> channelConfigurator =
267-
channelBuilder -> {
268-
if (InstantiatingGrpcChannelProvider.isOnComputeEngine()) {
269-
assertThat(channelBuilder).isInstanceOf(ComputeEngineChannelBuilder.class);
270-
} else {
271-
assertThat(channelBuilder).isNotInstanceOf(ComputeEngineChannelBuilder.class);
272-
}
273-
return channelBuilder;
274-
};
275-
276266
TransportChannelProvider provider =
277267
InstantiatingGrpcChannelProvider.newBuilder()
278268
.setAttemptDirectPath(true)
279-
.setChannelConfigurator(channelConfigurator)
280269
.build()
281270
.withExecutor((Executor) executor)
282271
.withHeaders(Collections.<String, String>emptyMap())

0 commit comments

Comments
 (0)