@@ -18,13 +18,11 @@ package bigtable
1818
1919import (
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.
6863func 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 {},
0 commit comments