Skip to content

Commit 9f4a76a

Browse files
authored
fix(bigtable): allow non-default service accounts for session DirectAccess (#14926)
### Problem Workloads running on Compute Engine or GKE with a non-default service account (for example, GKE Workload Identity Federation or explicit service account credentials) cannot establish DirectPath (`DirectAccess`) connections on the session-based protocol path (`ShimImpl` and `GaxDirectAccessChannelProvider`), even when classic unary RPCs on the same client successfully use DirectPath. Instead, session channels fall back to CloudPath (`GaxBasicChannelProvider`) and report `direct_access/compatible=0`. ### Root Cause `ShimImpl.create` and both factory methods in `GaxDirectAccessChannelProvider` (`create` and `createWithFallback`) called `.setAttemptDirectPath(true).setAttemptDirectPathXds()` without setting `.setAllowNonDefaultServiceAccount(true)`. Without `setAllowNonDefaultServiceAccount(true)`, `InstantiatingGrpcChannelProvider.isCredentialDirectPathCompatible()` only accepts `ComputeEngineCredentials` and returns `false` for other credential types. Consequently, `directAccessProvider.canUseDirectPath()` returns `false` in `GaxDirectAccessChannelProvider`, causing `GaxDirectAccessChannelProvider.createWithFallback` to immediately return a CloudPath `GaxBasicChannelProvider`. ### Fix - Enable `.setAllowNonDefaultServiceAccount(true)` on the DirectAccess `InstantiatingGrpcChannelProvider` builders in `ShimImpl.create`, `GaxDirectAccessChannelProvider.create`, and `GaxDirectAccessChannelProvider.createWithFallback`. ### Testing - Added `GaxDirectAccessChannelProviderTest` verifying that `create` and `createWithFallback` return `GaxDirectAccessChannelProvider` on a GCE Linux environment when configured with non-`ComputeEngineCredentials` credentials.
1 parent 8535344 commit 9f4a76a

3 files changed

Lines changed: 106 additions & 2 deletions

File tree

‎java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/compat/GaxDirectAccessChannelProvider.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ public class GaxDirectAccessChannelProvider implements ChannelProvider {
3636
public static ChannelProvider create(
3737
InstantiatingGrpcChannelProvider outer, @Nullable Credentials credentials) {
3838
InstantiatingGrpcChannelProvider directAccessProvider =
39-
outer.toBuilder().setAttemptDirectPath(true).setAttemptDirectPathXds().build();
39+
outer.toBuilder()
40+
.setAttemptDirectPath(true)
41+
.setAttemptDirectPathXds()
42+
.setAllowNonDefaultServiceAccount(true)
43+
.build();
4044

4145
InstantiatingGrpcChannelProvider cloudPathProvider =
4246
outer.toBuilder().setAttemptDirectPath(false).build();
@@ -52,7 +56,11 @@ public static ChannelProvider create(
5256
public static ChannelProvider createWithFallback(
5357
InstantiatingGrpcChannelProvider outer, @Nullable Credentials credentials) {
5458
InstantiatingGrpcChannelProvider directAccessProvider =
55-
outer.toBuilder().setAttemptDirectPath(true).setAttemptDirectPathXds().build();
59+
outer.toBuilder()
60+
.setAttemptDirectPath(true)
61+
.setAttemptDirectPathXds()
62+
.setAllowNonDefaultServiceAccount(true)
63+
.build();
5664

5765
InstantiatingGrpcChannelProvider cloudPathProvider =
5866
outer.toBuilder().setAttemptDirectPath(false).build();

‎java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/compat/ShimImpl.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public static Shim create(
129129
.setChannelPoolSettings(ChannelPoolSettings.staticallySized(1))
130130
.setAttemptDirectPath(true)
131131
.setAttemptDirectPathXds()
132+
.setAllowNonDefaultServiceAccount(true)
132133
.build();
133134

134135
if (transportProvider.needsEndpoint()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.bigtable.data.v2.internal.compat;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
import static org.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.withSettings;
21+
22+
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
23+
import com.google.auth.Credentials;
24+
import com.google.bigtable.v2.FeatureFlags;
25+
import com.google.cloud.bigtable.data.v2.internal.api.ChannelProviders.ChannelProvider;
26+
import java.lang.reflect.Field;
27+
import org.junit.jupiter.api.AfterEach;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
31+
class GaxDirectAccessChannelProviderTest {
32+
33+
private String originalOsName;
34+
private String originalProductName;
35+
36+
@BeforeEach
37+
void setUp() throws Exception {
38+
originalOsName = System.getProperty("os.name");
39+
originalProductName =
40+
(String) getStaticField(InstantiatingGrpcChannelProvider.class, "systemProductName");
41+
}
42+
43+
@AfterEach
44+
void tearDown() throws Exception {
45+
if (originalOsName != null) {
46+
System.setProperty("os.name", originalOsName);
47+
}
48+
setStaticField(
49+
InstantiatingGrpcChannelProvider.class, "systemProductName", originalProductName);
50+
}
51+
52+
@Test
53+
void create_withNonDefaultServiceAccountOnGce_enablesDirectAccess() throws Exception {
54+
System.setProperty("os.name", "Linux");
55+
setStaticField(
56+
InstantiatingGrpcChannelProvider.class, "systemProductName", "Google Compute Engine");
57+
58+
Credentials nonDefaultSaCredentials =
59+
mock(Credentials.class, withSettings().withoutAnnotations());
60+
InstantiatingGrpcChannelProvider outer =
61+
InstantiatingGrpcChannelProvider.newBuilder()
62+
.setEndpoint("bigtable.googleapis.com:443")
63+
.setCredentials(nonDefaultSaCredentials)
64+
.build();
65+
66+
ChannelProvider directChannelProvider =
67+
GaxDirectAccessChannelProvider.create(outer, nonDefaultSaCredentials);
68+
assertThat(directChannelProvider).isInstanceOf(GaxDirectAccessChannelProvider.class);
69+
FeatureFlags flags =
70+
directChannelProvider.updateFeatureFlags(FeatureFlags.getDefaultInstance());
71+
assertThat(flags.getDirectAccessRequested()).isTrue();
72+
assertThat(flags.getTrafficDirectorEnabled()).isTrue();
73+
assertThat(directChannelProvider.getFallback()).isEmpty();
74+
75+
ChannelProvider fallbackChannelProvider =
76+
GaxDirectAccessChannelProvider.createWithFallback(outer, nonDefaultSaCredentials);
77+
assertThat(fallbackChannelProvider).isInstanceOf(GaxDirectAccessChannelProvider.class);
78+
assertThat(fallbackChannelProvider.getFallback()).isPresent();
79+
assertThat(fallbackChannelProvider.getFallback().get())
80+
.isInstanceOf(GaxBasicChannelProvider.class);
81+
}
82+
83+
private static Object getStaticField(Class<?> clazz, String fieldName) throws Exception {
84+
Field field = clazz.getDeclaredField(fieldName);
85+
field.setAccessible(true);
86+
return field.get(null);
87+
}
88+
89+
private static void setStaticField(Class<?> clazz, String fieldName, Object value)
90+
throws Exception {
91+
Field field = clazz.getDeclaredField(fieldName);
92+
field.setAccessible(true);
93+
field.set(null, value);
94+
}
95+
}

0 commit comments

Comments
 (0)