Last active
October 11, 2023 11:12
-
-
Save marzocchi/c4d3e2254853c5ff02b420044e796aea to your computer and use it in GitHub Desktop.
Connect to a gRPC server with TLS (without a certificate, key pair)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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