@@ -28,6 +28,7 @@ import (
28
28
"google.golang.org/grpc"
29
29
"google.golang.org/grpc/credentials"
30
30
"google.golang.org/grpc/credentials/insecure"
31
+ xdsCredentials "google.golang.org/grpc/credentials/xds"
31
32
"google.golang.org/grpc/metadata"
32
33
protov2 "google.golang.org/protobuf/proto"
33
34
"google.golang.org/protobuf/types/descriptorpb"
@@ -609,6 +610,21 @@ func ServerTransportCredentials(cacertFile, serverCertFile, serverKeyFile string
609
610
// and blocking until the returned connection is ready. If the given credentials are nil, the
610
611
// connection will be insecure (plain-text).
611
612
func BlockingDial (ctx context.Context , network , address string , creds credentials.TransportCredentials , opts ... grpc.DialOption ) (* grpc.ClientConn , error ) {
613
+ if creds == nil {
614
+ creds = insecure .NewCredentials ()
615
+ }
616
+
617
+ var err error
618
+ if strings .HasPrefix (address , "xds:///" ) {
619
+ // The xds:/// prefix is used to signal to the gRPC client to use an xDS server to resolve the
620
+ // target. The relevant credentials will be automatically pulled from the GRPC_XDS_BOOTSTRAP or
621
+ // GRPC_XDS_BOOTSTRAP_CONFIG env vars.
622
+ creds , err = xdsCredentials .NewClientCredentials (xdsCredentials.ClientOptions {FallbackCreds : creds })
623
+ if err != nil {
624
+ return nil , err
625
+ }
626
+ }
627
+
612
628
// grpc.Dial doesn't provide any information on permanent connection errors (like
613
629
// TLS handshake failures). So in order to provide good error messages, we need a
614
630
// custom dialer that can provide that info. That means we manage the TLS handshake.
@@ -624,12 +640,11 @@ func BlockingDial(ctx context.Context, network, address string, creds credential
624
640
625
641
// custom credentials and dialer will notify on error via the
626
642
// writeResult function
627
- if creds != nil {
628
- creds = & errSignalingCreds {
629
- TransportCredentials : creds ,
630
- writeResult : writeResult ,
631
- }
643
+ creds = & errSignalingCreds {
644
+ TransportCredentials : creds ,
645
+ writeResult : writeResult ,
632
646
}
647
+
633
648
dialer := func (ctx context.Context , address string ) (net.Conn , error ) {
634
649
// NB: We *could* handle the TLS handshake ourselves, in the custom
635
650
// dialer (instead of customizing both the dialer and the credentials).
@@ -655,13 +670,8 @@ func BlockingDial(ctx context.Context, network, address string, creds credential
655
670
opts = append ([]grpc.DialOption {grpc .FailOnNonTempDialError (true )}, opts ... )
656
671
// But we don't want caller to be able to override these two, so we put
657
672
// them *after* the explicitly provided options.
658
- opts = append (opts , grpc .WithBlock (), grpc .WithContextDialer (dialer ))
673
+ opts = append (opts , grpc .WithBlock (), grpc .WithContextDialer (dialer ), grpc . WithTransportCredentials ( creds ) )
659
674
660
- if creds == nil {
661
- opts = append (opts , grpc .WithTransportCredentials (insecure .NewCredentials ()))
662
- } else {
663
- opts = append (opts , grpc .WithTransportCredentials (creds ))
664
- }
665
675
conn , err := grpc .DialContext (ctx , address , opts ... )
666
676
var res interface {}
667
677
if err != nil {
0 commit comments