Skip to content

Commit 5592211

Browse files
authored
Enable xDS credentials (#424)
* Enable xDS credentials This change should be relatively straightforward. It is a noop outside of the context of xDS (as demonstrated by the fact that the tests all pass), but it enables xDS-provided certificates (i.e. the ones that would be provided/specified in GRPC_XDS_BOOTSTRAP). See proposal [A29](https://github.com/grpc/proposal/blob/master/A29-xds-tls-security.md#go) for additional detail. * Only enable xds credentials if the target is an xDS target * Update after merge
1 parent 184c8f7 commit 5592211

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

grpcurl.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"google.golang.org/grpc"
2929
"google.golang.org/grpc/credentials"
3030
"google.golang.org/grpc/credentials/insecure"
31+
xdsCredentials "google.golang.org/grpc/credentials/xds"
3132
"google.golang.org/grpc/metadata"
3233
protov2 "google.golang.org/protobuf/proto"
3334
"google.golang.org/protobuf/types/descriptorpb"
@@ -609,6 +610,21 @@ func ServerTransportCredentials(cacertFile, serverCertFile, serverKeyFile string
609610
// and blocking until the returned connection is ready. If the given credentials are nil, the
610611
// connection will be insecure (plain-text).
611612
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+
612628
// grpc.Dial doesn't provide any information on permanent connection errors (like
613629
// TLS handshake failures). So in order to provide good error messages, we need a
614630
// 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
624640

625641
// custom credentials and dialer will notify on error via the
626642
// 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,
632646
}
647+
633648
dialer := func(ctx context.Context, address string) (net.Conn, error) {
634649
// NB: We *could* handle the TLS handshake ourselves, in the custom
635650
// dialer (instead of customizing both the dialer and the credentials).
@@ -655,13 +670,8 @@ func BlockingDial(ctx context.Context, network, address string, creds credential
655670
opts = append([]grpc.DialOption{grpc.FailOnNonTempDialError(true)}, opts...)
656671
// But we don't want caller to be able to override these two, so we put
657672
// 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))
659674

660-
if creds == nil {
661-
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
662-
} else {
663-
opts = append(opts, grpc.WithTransportCredentials(creds))
664-
}
665675
conn, err := grpc.DialContext(ctx, address, opts...)
666676
var res interface{}
667677
if err != nil {

0 commit comments

Comments
 (0)