Skip to content

Commit

Permalink
Add TLS option
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-jones committed Dec 6, 2024
1 parent 978093a commit 5793957
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"log/slog"
"os"
Expand All @@ -11,13 +13,17 @@ import (
_ "github.com/joho/godotenv/autoload"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"

echov1 "github.com/e-flux-platform/echo-grpc/gen/go/road/echo/v1"
)

func main() {
var serverAddr string
var (
serverAddr string
useTLS bool
)

app := &cli.App{
Flags: []cli.Flag{
Expand All @@ -27,12 +33,17 @@ func main() {
Destination: &serverAddr,
Required: true,
},
&cli.BoolFlag{
Name: "tls",
EnvVars: []string{"TLS"},
Destination: &useTLS,
},
},
Action: func(cCtx *cli.Context) error {
ctx, cancel := signal.NotifyContext(cCtx.Context, syscall.SIGTERM, syscall.SIGINT)
defer cancel()

return run(ctx, serverAddr, cCtx.Args().First())
return run(ctx, serverAddr, useTLS, cCtx.Args().First())
},
}

Expand All @@ -42,8 +53,19 @@ func main() {
}
}

func run(ctx context.Context, serverAddr, message string) error {
conn, err := grpc.NewClient(serverAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
func run(ctx context.Context, serverAddr string, useTLS bool, message string) error {
var transportCredentials credentials.TransportCredentials
if useTLS {
rootCAs, _ := x509.SystemCertPool()
tlsConfig := &tls.Config{
RootCAs: rootCAs,
}
transportCredentials = credentials.NewTLS(tlsConfig)
} else {
transportCredentials = insecure.NewCredentials()
}

conn, err := grpc.NewClient(serverAddr, grpc.WithTransportCredentials(transportCredentials))
if err != nil {
return err
}
Expand Down

0 comments on commit 5793957

Please sign in to comment.