From 89287b68a240f818e9ae70a6395b1d72e21ee236 Mon Sep 17 00:00:00 2001 From: Mohan Li <67390330+mohanli-ml@users.noreply.github.com> Date: Wed, 9 Dec 2020 12:04:03 -0800 Subject: [PATCH] fix(transport/grpc): check Compute Engine environment for DirectPath (#781) Add Compute Engine environment check since DirectPath can only be used on Compute Engine. --- transport/grpc/dial.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/transport/grpc/dial.go b/transport/grpc/dial.go index f8a6ca29981..727a5beff1b 100644 --- a/transport/grpc/dial.go +++ b/transport/grpc/dial.go @@ -14,6 +14,7 @@ import ( "log" "strings" + "cloud.google.com/go/compute/metadata" "go.opencensus.io/plugin/ocgrpc" "golang.org/x/oauth2" "google.golang.org/api/internal" @@ -137,7 +138,7 @@ func dial(ctx context.Context, insecure bool, o *internal.DialSettings) (*grpc.C // * The endpoint is a host:port (or dns:///host:port). // * Credentials are obtained via GCE metadata server, using the default // service account. - if o.EnableDirectPath && checkDirectPathEndPoint(endpoint) && isTokenSourceDirectPathCompatible(creds.TokenSource) { + if o.EnableDirectPath && checkDirectPathEndPoint(endpoint) && isTokenSourceDirectPathCompatible(creds.TokenSource) && metadata.OnGCE() { if !strings.HasPrefix(endpoint, "dns:///") { endpoint = "dns:///" + endpoint } @@ -186,7 +187,7 @@ func dial(ctx context.Context, insecure bool, o *internal.DialSettings) (*grpc.C // point when isDirectPathEnabled will default to true, we guard it by // the Directpath env var for now once we can introspect user defined // dialer (https://github.com/grpc/grpc-go/issues/2795). - if timeoutDialerOption != nil && o.EnableDirectPath && checkDirectPathEndPoint(endpoint) { + if timeoutDialerOption != nil && o.EnableDirectPath && checkDirectPathEndPoint(endpoint) && metadata.OnGCE() { grpcOpts = append(grpcOpts, timeoutDialerOption) }