Skip to content

Commit

Permalink
fix: use TLS in gRPC client
Browse files Browse the repository at this point in the history
Enable TLS and certificate checking in the gRPC client when
communicating with remote hosts.
  • Loading branch information
hperl committed Aug 18, 2022
1 parent 5b3e731 commit 4310cfb
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cmd/client/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"context"
"fmt"
"net/url"
"os"
"strings"
"time"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)

Expand Down Expand Up @@ -62,7 +64,20 @@ func Conn(ctx context.Context, remote string) (*grpc.ClientConn, error) {

ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return grpc.DialContext(ctx, remote, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock(), grpc.WithDisableHealthCheck())
return grpc.DialContext(ctx, remote,
grpc.WithTransportCredentials(transportCredentials(remote)),
grpc.WithBlock(),
grpc.WithDisableHealthCheck())
}

func transportCredentials(remote string) credentials.TransportCredentials {
u, err := url.Parse(remote)
if err == nil && (u.Hostname() == "127.0.0.1" || u.Hostname() == "localhost") {
return insecure.NewCredentials()
}

// Defaults to the default host root CA bundle
return credentials.NewTLS(nil)
}

func RegisterRemoteURLFlags(flags *pflag.FlagSet) {
Expand Down

0 comments on commit 4310cfb

Please sign in to comment.