Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transport/grpc): check Compute Engine environment for DirectPath #781

Merged
merged 3 commits into from Dec 9, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions transport/grpc/dial.go
Expand Up @@ -11,7 +11,9 @@ import (
"context"
"crypto/tls"
"errors"
"io/ioutil"
"log"
"runtime"
"strings"

"go.opencensus.io/plugin/ocgrpc"
Expand All @@ -28,6 +30,11 @@ import (
_ "google.golang.org/grpc/balancer/grpclb"
)

const (
GCEProductionNamePrior2016 = "Google"
GCEProductionNameAfter2016 = "Google Compute Engine"
)

// Set at init time by dial_appengine.go. If nil, we're not on App Engine.
var appengineDialerHook func(context.Context) grpc.DialOption

Expand Down Expand Up @@ -137,7 +144,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) && isOnComputeEngine() {
if !strings.HasPrefix(endpoint, "dns:///") {
endpoint = "dns:///" + endpoint
}
Expand Down Expand Up @@ -186,7 +193,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) && isOnComputeEngine() {
codyoss marked this conversation as resolved.
Show resolved Hide resolved
grpcOpts = append(grpcOpts, timeoutDialerOption)
}

Expand Down Expand Up @@ -247,6 +254,20 @@ func isTokenSourceDirectPathCompatible(ts oauth2.TokenSource) bool {
return true
}

func isOnComputeEngine() bool {
// DirectPath should only be used on Compute Engine.
// Notice Windows is not supported for now.
if runtime.GOOS == "linux" {
file, err := ioutil.ReadFile("/sys/class/dmi/id/product_name")
if err != nil {
log.Print("Fail to check Compute Engine, and DirectPath will not be used.")
return false
}
return strings.Contains(string(file), GCEProductionNamePrior2016) || strings.Contains(string(file), GCEProductionNameAfter2016)
}
return false
}

func checkDirectPathEndPoint(endpoint string) bool {
// Only [dns:///]host[:port] is supported, not other schemes (e.g., "tcp://" or "unix://").
// Also don't try direct path if the user has chosen an alternate name resolver
Expand Down