Skip to content

Commit

Permalink
transport/grpc: support GOOGLE_API_USE_MTLS=always for mtls_smoketest (
Browse files Browse the repository at this point in the history
…#534)

This will swap "foo.googleapis.com" to "foo.mtls.googleapis.com".
It is used by mtls_smoketest.
  • Loading branch information
broady authored Jun 16, 2020
1 parent 4d381d7 commit 4f567d4
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions transport/grpc/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,38 @@ func dial(ctx context.Context, insecure bool, o *internal.DialSettings) (*grpc.C
grpcOpts = append(grpcOpts, timeoutDialerOption)
}

// NOTE(cbro): this is used only by the nightly mtls_smoketest and should
// not otherwise be used. It will be removed or renamed at some point.
if os.Getenv("GOOGLE_API_USE_MTLS") == "always" {
o.Endpoint = generateDefaultMtlsEndpoint(o.Endpoint)
}

return grpc.DialContext(ctx, o.Endpoint, grpcOpts...)
}

// generateDefaultMtlsEndpoint attempts to derive the mTLS version of the
// defaultEndpoint via regex, and returns defaultEndpoint if unsuccessful.
//
// We need to applying the following 2 transformations:
// 1. pubsub.googleapis.com to pubsub.mtls.googleapis.com
// 2. pubsub.sandbox.googleapis.com to pubsub.mtls.sandbox.googleapis.com
//
// TODO(cbro): In the future, the mTLS endpoint will be read from Service Config
// and passed in as defaultMtlsEndpoint instead of generated from defaultEndpoint,
// and this function will be removed.
func generateDefaultMtlsEndpoint(defaultEndpoint string) string {
var domains = []string{
".sandbox.googleapis.com", // must come first because .googleapis.com is a substring
".googleapis.com",
}
for _, domain := range domains {
if strings.Contains(defaultEndpoint, domain) {
return strings.Replace(defaultEndpoint, domain, ".mtls"+domain, -1)
}
}
return defaultEndpoint
}

func addOCStatsHandler(opts []grpc.DialOption, settings *internal.DialSettings) []grpc.DialOption {
if settings.TelemetryDisabled {
return opts
Expand Down

0 comments on commit 4f567d4

Please sign in to comment.