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

Commit 656b613

Browse files
authored
fix: check Compute Engine environment for DirectPath (#1250)
1 parent d455da2 commit 656b613

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

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

+27-1
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444
import com.google.common.base.Preconditions;
4545
import com.google.common.collect.ImmutableList;
4646
import com.google.common.collect.ImmutableMap;
47+
import com.google.common.io.CharStreams;
4748
import io.grpc.ManagedChannel;
4849
import io.grpc.ManagedChannelBuilder;
4950
import io.grpc.alts.ComputeEngineChannelBuilder;
5051
import java.io.IOException;
52+
import java.io.InputStreamReader;
5153
import java.util.Map;
5254
import java.util.concurrent.Executor;
5355
import java.util.concurrent.ScheduledExecutorService;
@@ -74,6 +76,8 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
7476
static final long DIRECT_PATH_KEEP_ALIVE_TIMEOUT_SECONDS = 20;
7577
// reduce the thundering herd problem of too many channels trying to (re)connect at the same time
7678
static final int MAX_POOL_SIZE = 1000;
79+
static final String GCE_PRODUCTION_NAME_PRIOR_2016 = "Google";
80+
static final String GCE_PRODUCTION_NAME_AFTER_2016 = "Google Compute Engine";
7781

7882
private final int processorCount;
7983
private final Executor executor;
@@ -234,6 +238,26 @@ private boolean isDirectPathEnabled(String serviceAddress) {
234238
return false;
235239
}
236240

241+
// DirectPath should only be used on Compute Engine.
242+
// Notice Windows is supported for now.
243+
static boolean isOnComputeEngine() {
244+
String osName = System.getProperty("os.name");
245+
if ("Linux".equals(osName)) {
246+
String cmd = "cat /sys/class/dmi/id/product_name";
247+
try {
248+
Process process = Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", cmd});
249+
process.waitFor();
250+
String result =
251+
CharStreams.toString(new InputStreamReader(process.getInputStream(), "UTF-8"));
252+
return result.contains(GCE_PRODUCTION_NAME_PRIOR_2016)
253+
|| result.contains(GCE_PRODUCTION_NAME_AFTER_2016);
254+
} catch (IOException | InterruptedException e) {
255+
return false;
256+
}
257+
}
258+
return false;
259+
}
260+
237261
private ManagedChannel createSingleChannel() throws IOException {
238262
GrpcHeaderInterceptor headerInterceptor =
239263
new GrpcHeaderInterceptor(headerProvider.getHeaders());
@@ -250,7 +274,9 @@ private ManagedChannel createSingleChannel() throws IOException {
250274
ManagedChannelBuilder builder;
251275

252276
// TODO(weiranf): Add API in ComputeEngineCredentials to check default service account.
253-
if (isDirectPathEnabled(serviceAddress) && credentials instanceof ComputeEngineCredentials) {
277+
if (isDirectPathEnabled(serviceAddress)
278+
&& credentials instanceof ComputeEngineCredentials
279+
&& isOnComputeEngine()) {
254280
builder = ComputeEngineChannelBuilder.forAddress(serviceAddress, port);
255281
// Set default keepAliveTime and keepAliveTimeout when directpath environment is enabled.
256282
// Will be overridden by user defined values if any.

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ public void testWithGCECredentials() throws IOException {
219219
ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> channelConfigurator =
220220
new ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder>() {
221221
public ManagedChannelBuilder apply(ManagedChannelBuilder channelBuilder) {
222-
assertThat(channelBuilder instanceof ComputeEngineChannelBuilder).isTrue();
222+
if (InstantiatingGrpcChannelProvider.isOnComputeEngine()) {
223+
assertThat(channelBuilder instanceof ComputeEngineChannelBuilder).isTrue();
224+
} else {
225+
assertThat(channelBuilder instanceof ComputeEngineChannelBuilder).isFalse();
226+
}
223227
return channelBuilder;
224228
}
225229
};
@@ -234,7 +238,11 @@ public ManagedChannelBuilder apply(ManagedChannelBuilder channelBuilder) {
234238
.withEndpoint("localhost:8080");
235239

236240
assertThat(provider.needsCredentials()).isTrue();
237-
provider = provider.withCredentials(ComputeEngineCredentials.create());
241+
if (InstantiatingGrpcChannelProvider.isOnComputeEngine()) {
242+
provider = provider.withCredentials(ComputeEngineCredentials.create());
243+
} else {
244+
provider = provider.withCredentials(CloudShellCredentials.create(3000));
245+
}
238246
assertThat(provider.needsCredentials()).isFalse();
239247

240248
provider.getTransportChannel().shutdownNow();

0 commit comments

Comments
 (0)