@@ -184,9 +184,38 @@ func dial(ctx context.Context, insecure bool, o *internal.DialSettings) (*grpc.C
184184 grpcOpts = append (grpcOpts , timeoutDialerOption )
185185 }
186186
187+ // NOTE(cbro): this is used only by the nightly mtls_smoketest and should
188+ // not otherwise be used. It will be removed or renamed at some point.
189+ if os .Getenv ("GOOGLE_API_USE_MTLS" ) == "always" {
190+ o .Endpoint = generateDefaultMtlsEndpoint (o .Endpoint )
191+ }
192+
187193 return grpc .DialContext (ctx , o .Endpoint , grpcOpts ... )
188194}
189195
196+ // generateDefaultMtlsEndpoint attempts to derive the mTLS version of the
197+ // defaultEndpoint via regex, and returns defaultEndpoint if unsuccessful.
198+ //
199+ // We need to applying the following 2 transformations:
200+ // 1. pubsub.googleapis.com to pubsub.mtls.googleapis.com
201+ // 2. pubsub.sandbox.googleapis.com to pubsub.mtls.sandbox.googleapis.com
202+ //
203+ // TODO(cbro): In the future, the mTLS endpoint will be read from Service Config
204+ // and passed in as defaultMtlsEndpoint instead of generated from defaultEndpoint,
205+ // and this function will be removed.
206+ func generateDefaultMtlsEndpoint (defaultEndpoint string ) string {
207+ var domains = []string {
208+ ".sandbox.googleapis.com" , // must come first because .googleapis.com is a substring
209+ ".googleapis.com" ,
210+ }
211+ for _ , domain := range domains {
212+ if strings .Contains (defaultEndpoint , domain ) {
213+ return strings .Replace (defaultEndpoint , domain , ".mtls" + domain , - 1 )
214+ }
215+ }
216+ return defaultEndpoint
217+ }
218+
190219func addOCStatsHandler (opts []grpc.DialOption , settings * internal.DialSettings ) []grpc.DialOption {
191220 if settings .TelemetryDisabled {
192221 return opts
0 commit comments