Skip to content

Commit 7dd4948

Browse files
authored
fix(bigtable): slighly change instruction for direct access (#14119)
1 parent d404777 commit 7dd4948

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

bigtable/direct_access_check.go

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ package bigtable
1818

1919
import (
2020
"context"
21-
"errors"
2221
"fmt"
23-
"os"
24-
"strconv"
2522
"strings"
2623

2724
"google.golang.org/api/option"
25+
"google.golang.org/api/option/internaloption"
2826
"google.golang.org/grpc"
2927
"google.golang.org/grpc/peer"
3028
)
@@ -34,17 +32,16 @@ const (
3432
directPathIPV4Prefix = "34.126"
3533
)
3634

37-
// This function attempts to establish a connection to the Bigtable instance using
38-
// Direct Access. It then checks if the underlying
35+
// CheckDirectAccessSupported attempts to establish a connection to the Bigtable instance
36+
// using Direct Access by enforcing internal gRPC options. It then checks if the underlying
3937
// gRPC connection is indeed using a DirectPath IP address.
4038
//
4139
// Prerequisites for successful Direct Access connectivity:
42-
// 1. The environment variable `CBT_ENABLE_DIRECTPATH` must be set to "true".
43-
// 2. The code must be running in a Google Cloud environment (e.g., GCE VM, GKE)
40+
// 1. The code must be running in a Google Cloud environment (e.g., GCE VM, GKE)
4441
// that is properly configured for Direct Access. This includes ensuring
4542
// that your routes and firewall rules allow egress traffic to the
4643
// Direct Access IP ranges: 34.126.0.0/18 and 2001:4860:8040::/42.
47-
// 3. The service account must have the necessary IAM permissions.
44+
// 2. The service account must have the necessary IAM permissions.
4845
//
4946
// Parameters:
5047
// - ctx: The context for the operation.
@@ -56,29 +53,14 @@ const (
5653
//
5754
// Returns:
5855
// - bool: True if DirectPath is successfully used for the connection, False otherwise.
59-
// - error: An error if the check could not be completed, or if DirectPath is not
60-
// enabled/configured. Specific error causes include:
61-
// - "CBT_ENABLE_DIRECTPATH=true is not set in env var": The required environment variable is missing.
56+
// - error: An error if the check could not be completed. Specific error causes include:
6257
// - Failure to create the Bigtable client (e.g., invalid project/instance).
6358
// - Failure during the PingAndWarm call (e.g., network issue, permissions).
6459
//
6560

6661
// CheckDirectAccessSupported verifies if Direct Access connectivity is enabled, configured,
6762
// and actively being used for the given Cloud Bigtable instance.
6863
func CheckDirectAccessSupported(ctx context.Context, project, instance, appProfile string, opts ...option.ClientOption) (bool, error) {
69-
// Check if env variable is set to true
70-
// Inside the function
71-
envVal := os.Getenv("CBT_ENABLE_DIRECTPATH")
72-
if envVal == "" {
73-
return false, errors.New("CBT_ENABLE_DIRECTPATH environment variable is not set")
74-
}
75-
isEnvEnabled, err := strconv.ParseBool(envVal)
76-
if err != nil {
77-
return false, fmt.Errorf("invalid value for CBT_ENABLE_DIRECTPATH: %s, must be true or false: %w", envVal, err)
78-
}
79-
if !isEnvEnabled {
80-
return false, errors.New("CBT_ENABLE_DIRECTPATH is not set to true")
81-
}
8264
isDirectPathUsed := false
8365
// Define the unary client interceptor
8466
interceptor := func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, callOpts ...grpc.CallOption) error {
@@ -108,6 +90,14 @@ func CheckDirectAccessSupported(ctx context.Context, project, instance, appProfi
10890
option.WithGRPCDialOption(grpc.WithUnaryInterceptor(interceptor)),
10991
}, opts...)
11092

93+
// Force DirectPath and ALTS using internal options
94+
allOpts = append(allOpts,
95+
internaloption.EnableDirectPath(true),
96+
internaloption.EnableDirectPathXds(),
97+
internaloption.AllowHardBoundTokens("ALTS"),
98+
internaloption.AllowNonDefaultServiceAccount(true),
99+
)
100+
111101
config := ClientConfig{
112102
AppProfile: appProfile,
113103
MetricsProvider: NoopMetricsProvider{},

bigtable/direct_access_check_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package bigtable
1919
import (
2020
"context"
2121
"log"
22-
"os"
2322
)
2423

2524
func ExampleCheckDirectAccessSupported() {
@@ -29,9 +28,6 @@ func ExampleCheckDirectAccessSupported() {
2928
instanceID := "my-instance"
3029
appProfileID := "default"
3130

32-
// Set the environment variable if not already set
33-
os.Setenv("CBT_ENABLE_DIRECTPATH", "true")
34-
3531
isDirectPath, err := CheckDirectAccessSupported(ctx, projectID, instanceID, appProfileID)
3632
if err != nil {
3733
log.Fatalf("DirectPath check failed: %v", err)

0 commit comments

Comments
 (0)