@@ -105,6 +105,7 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
105105 @ Nullable private final Credentials credentials ;
106106 @ Nullable private final ChannelPrimer channelPrimer ;
107107 @ Nullable private final Boolean attemptDirectPath ;
108+ @ Nullable private final Boolean allowNonDefaultServiceAccount ;
108109 @ VisibleForTesting final ImmutableMap <String , ?> directPathServiceConfig ;
109110 @ Nullable private final MtlsProvider mtlsProvider ;
110111
@@ -129,6 +130,7 @@ private InstantiatingGrpcChannelProvider(Builder builder) {
129130 this .credentials = builder .credentials ;
130131 this .channelPrimer = builder .channelPrimer ;
131132 this .attemptDirectPath = builder .attemptDirectPath ;
133+ this .allowNonDefaultServiceAccount = builder .allowNonDefaultServiceAccount ;
132134 this .directPathServiceConfig =
133135 builder .directPathServiceConfig == null
134136 ? getDefaultDirectPathServiceConfig ()
@@ -274,6 +276,13 @@ private boolean isDirectPathEnabled(String serviceAddress) {
274276 return false ;
275277 }
276278
279+ private boolean isNonDefaultServiceAccountAllowed () {
280+ if (allowNonDefaultServiceAccount != null && allowNonDefaultServiceAccount ) {
281+ return true ;
282+ }
283+ return credentials instanceof ComputeEngineCredentials ;
284+ }
285+
277286 // DirectPath should only be used on Compute Engine.
278287 // Notice Windows is supported for now.
279288 static boolean isOnComputeEngine () {
@@ -323,7 +332,7 @@ private ManagedChannel createSingleChannel() throws IOException, GeneralSecurity
323332
324333 // TODO(weiranf): Add API in ComputeEngineCredentials to check default service account.
325334 if (isDirectPathEnabled (serviceAddress )
326- && credentials instanceof ComputeEngineCredentials
335+ && isNonDefaultServiceAccountAllowed ()
327336 && isOnComputeEngine ()) {
328337 builder = ComputeEngineChannelBuilder .forAddress (serviceAddress , port );
329338 // Set default keepAliveTime and keepAliveTimeout when directpath environment is enabled.
@@ -435,6 +444,7 @@ public static final class Builder {
435444 @ Nullable private Credentials credentials ;
436445 @ Nullable private ChannelPrimer channelPrimer ;
437446 @ Nullable private Boolean attemptDirectPath ;
447+ @ Nullable private Boolean allowNonDefaultServiceAccount ;
438448 @ Nullable private ImmutableMap <String , ?> directPathServiceConfig ;
439449
440450 private Builder () {
@@ -459,6 +469,7 @@ private Builder(InstantiatingGrpcChannelProvider provider) {
459469 this .credentials = provider .credentials ;
460470 this .channelPrimer = provider .channelPrimer ;
461471 this .attemptDirectPath = provider .attemptDirectPath ;
472+ this .allowNonDefaultServiceAccount = provider .allowNonDefaultServiceAccount ;
462473 this .directPathServiceConfig = provider .directPathServiceConfig ;
463474 this .mtlsProvider = provider .mtlsProvider ;
464475 }
@@ -654,6 +665,13 @@ public Builder setAttemptDirectPath(boolean attemptDirectPath) {
654665 return this ;
655666 }
656667
668+ /** Whether allow non-default service account for DirectPath. */
669+ @ InternalApi ("For internal use by google-cloud-java clients only" )
670+ public Builder setAllowNonDefaultServiceAccount (boolean allowNonDefaultServiceAccount ) {
671+ this .allowNonDefaultServiceAccount = allowNonDefaultServiceAccount ;
672+ return this ;
673+ }
674+
657675 /**
658676 * Sets a service config for direct path. If direct path is not enabled, the provided service
659677 * config will be ignored.
0 commit comments