Skip to content

Commit 860ae76

Browse files
authored
feat: Log DirectPath misconfiguration (#2105)
1 parent 88bccd9 commit 860ae76

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

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

+34
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
import java.util.concurrent.Executor;
6565
import java.util.concurrent.ScheduledExecutorService;
6666
import java.util.concurrent.TimeUnit;
67+
import java.util.logging.Level;
68+
import java.util.logging.Logger;
6769
import javax.annotation.Nullable;
6870
import javax.net.ssl.KeyManagerFactory;
6971
import org.threeten.bp.Duration;
@@ -82,6 +84,9 @@
8284
*/
8385
@InternalExtensionOnly
8486
public final class InstantiatingGrpcChannelProvider implements TransportChannelProvider {
87+
@VisibleForTesting
88+
static final Logger LOG = Logger.getLogger(InstantiatingGrpcChannelProvider.class.getName());
89+
8590
private static final String DIRECT_PATH_ENV_DISABLE_DIRECT_PATH =
8691
"GOOGLE_CLOUD_DISABLE_DIRECT_PATH";
8792
private static final String DIRECT_PATH_ENV_ENABLE_XDS = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS";
@@ -140,6 +145,7 @@ private InstantiatingGrpcChannelProvider(Builder builder) {
140145
builder.directPathServiceConfig == null
141146
? getDefaultDirectPathServiceConfig()
142147
: builder.directPathServiceConfig;
148+
logDirectPathMisconfig();
143149
}
144150

145151
/**
@@ -266,6 +272,33 @@ boolean isDirectPathXdsEnabled() {
266272
return false;
267273
}
268274

275+
private void logDirectPathMisconfig() {
276+
if (isDirectPathXdsEnabled()) {
277+
// Case 1: does not enable DirectPath
278+
if (!isDirectPathEnabled()) {
279+
LOG.log(
280+
Level.WARNING,
281+
"DirectPath is misconfigured. Please set the attemptDirectPath option along with the"
282+
+ " attemptDirectPathXds option.");
283+
} else {
284+
// Case 2: credential is not correctly set
285+
if (!isNonDefaultServiceAccountAllowed()) {
286+
LOG.log(
287+
Level.WARNING,
288+
"DirectPath is misconfigured. Please make sure the credential is an instance of "
289+
+ ComputeEngineCredentials.class.getName()
290+
+ " .");
291+
}
292+
// Case 3: not running on GCE
293+
if (!isOnComputeEngine()) {
294+
LOG.log(
295+
Level.WARNING,
296+
"DirectPath is misconfigured. DirectPath is only available in a GCE environment.");
297+
}
298+
}
299+
}
300+
}
301+
269302
private boolean isNonDefaultServiceAccountAllowed() {
270303
if (allowNonDefaultServiceAccount != null && allowNonDefaultServiceAccount) {
271304
return true;
@@ -275,6 +308,7 @@ private boolean isNonDefaultServiceAccountAllowed() {
275308

276309
// DirectPath should only be used on Compute Engine.
277310
// Notice Windows is supported for now.
311+
@VisibleForTesting
278312
static boolean isOnComputeEngine() {
279313
String osName = System.getProperty("os.name");
280314
if ("Linux".equals(osName)) {

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

+69
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
import java.util.concurrent.Executor;
5757
import java.util.concurrent.ScheduledExecutorService;
5858
import java.util.concurrent.ScheduledThreadPoolExecutor;
59+
import java.util.logging.Handler;
60+
import java.util.logging.LogRecord;
61+
import java.util.stream.Collectors;
5962
import javax.annotation.Nullable;
6063
import org.junit.Test;
6164
import org.junit.runner.RunWith;
@@ -503,4 +506,70 @@ protected Object getMtlsObjectFromTransportChannel(MtlsProvider provider)
503506
.build();
504507
return channelProvider.createMtlsChannelCredentials();
505508
}
509+
510+
@Test
511+
public void testLogDirectPathMisconfigAttrempDirectPathNotSet() {
512+
FakeLogHandler logHandler = new FakeLogHandler();
513+
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
514+
InstantiatingGrpcChannelProvider provider =
515+
InstantiatingGrpcChannelProvider.newBuilder().setAttemptDirectPathXds().build();
516+
assertThat(logHandler.getAllMessages())
517+
.contains(
518+
"DirectPath is misconfigured. Please set the attemptDirectPath option along with the"
519+
+ " attemptDirectPathXds option.");
520+
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
521+
}
522+
523+
@Test
524+
public void testLogDirectPathMisconfigWrongCredential() {
525+
FakeLogHandler logHandler = new FakeLogHandler();
526+
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
527+
InstantiatingGrpcChannelProvider provider =
528+
InstantiatingGrpcChannelProvider.newBuilder()
529+
.setAttemptDirectPathXds()
530+
.setAttemptDirectPath(true)
531+
.build();
532+
assertThat(logHandler.getAllMessages())
533+
.contains(
534+
"DirectPath is misconfigured. Please make sure the credential is an instance of"
535+
+ " com.google.auth.oauth2.ComputeEngineCredentials .");
536+
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
537+
}
538+
539+
@Test
540+
public void testLogDirectPathMisconfigNotOnGCE() {
541+
FakeLogHandler logHandler = new FakeLogHandler();
542+
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
543+
InstantiatingGrpcChannelProvider provider =
544+
InstantiatingGrpcChannelProvider.newBuilder()
545+
.setAttemptDirectPathXds()
546+
.setAttemptDirectPath(true)
547+
.setAllowNonDefaultServiceAccount(true)
548+
.build();
549+
if (!InstantiatingGrpcChannelProvider.isOnComputeEngine()) {
550+
assertThat(logHandler.getAllMessages())
551+
.contains(
552+
"DirectPath is misconfigured. DirectPath is only available in a GCE environment.");
553+
}
554+
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
555+
}
556+
557+
private static class FakeLogHandler extends Handler {
558+
List<LogRecord> records = new ArrayList<>();
559+
560+
@Override
561+
public void publish(LogRecord record) {
562+
records.add(record);
563+
}
564+
565+
@Override
566+
public void flush() {}
567+
568+
@Override
569+
public void close() throws SecurityException {}
570+
571+
List<String> getAllMessages() {
572+
return records.stream().map(LogRecord::getMessage).collect(Collectors.toList());
573+
}
574+
}
506575
}

0 commit comments

Comments
 (0)