Skip to content

Instantly share code, notes, and snippets.

@marzocchi
Last active October 11, 2023 11:12
Show Gist options
  • Save marzocchi/c4d3e2254853c5ff02b420044e796aea to your computer and use it in GitHub Desktop.
Save marzocchi/c4d3e2254853c5ff02b420044e796aea to your computer and use it in GitHub Desktop.
Connect to a gRPC server with TLS (without a certificate, key pair)
FROM golang:alpine AS builder
RUN apk --no-cache add build-base git mercurial gcc
ADD . /go/src/grpc-client-test
RUN cd /go/src/grpc-client-test && go build -o grpc-client-test main.go
FROM alpine
COPY --from=builder /go/src/grpc-client-test /grpc-client-test
ENV GRPC_GO_LOG_SEVERITY_LEVEL=info
ENV GRPC_GO_LOG_VERBOSITY_LEVEL=6
ENTRYPOINT ["/grpc-client-test"]
package main
import (
"context"
"crypto/tls"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/health/grpc_health_v1"
"os"
"path"
"time"
)
func main() {
prog := path.Base(os.Args[0])
if len(os.Args) < 2 {
_, _ = fmt.Fprintf(os.Stderr, "usage: %s HOST:PORT\n", prog)
os.Exit(1)
}
addr := os.Args[1]
creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: false})
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
conn, err := grpc.DialContext(ctx, addr, grpc.WithTransportCredentials(creds))
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s: %s\n", prog, err)
os.Exit(1)
}
defer conn.Close()
client := grpc_health_v1.NewHealthClient(conn)
rsp, err := client.Check(context.Background(), &grpc_health_v1.HealthCheckRequest{})
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s: %s\n", prog, err)
os.Exit(1)
}
fmt.Printf("Status: %s\n", rsp.Status)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment