Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use TLS in gRPC client #988

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
"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 {
host, _, err := net.SplitHostPort(remote)
if err == nil && (host == "127.0.0.1" || host == "localhost") {
return insecure.NewCredentials()
}

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

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