johanbrandhorst / grpc-gateway-boilerplate

All the boilerplate you need to get started with writing grpc-gateway powered REST services in Go
MIT License
479 stars 83 forks source link

Use without TLS #1

Closed fi0 closed 5 years ago

fi0 commented 5 years ago

Can I run this without TLS?

johanbrandhorst commented 5 years ago

Sure you can, just comment out L66 in main.go and use grpc.WithInsecure() when calling the gRPC backend on L84.

Also stop serving with a TLSConfig on L116 and use gwServer.ListenAndServe instead on L122.

MiniXC commented 4 years ago

With the current version, I have tried the following:

I get the following warnings:

WARNING: 2020/09/29 09:33:52 [core] grpc: addrConn.createTransport failed to connect to {0.0.0.0:10000 0.0.0.0:10000 <nil> 0 <nil>}. Err: connection error: desc = "transport: authentication handshake failed: tls: first record does not look like a TLS handshake". Reconnecting...
INFO: 2020/09/29 09:33:52 [core] Subchannel Connectivity change to TRANSIENT_FAILURE
INFO: 2020/09/29 09:33:52 [core] Channel Connectivity change to TRANSIENT_FAILURE
WARNING: 2020/09/29 09:33:52 [core] grpc: Server.Serve failed to create ServerTransport:  connection error: desc = "transport: http2Server.HandleStreams received bogus greeting from client: \"\\x16\\x03\\x01\\x00\\xf7\\x01\\x00\\x00\\xf3\\x03\\x03\\xc2\\xde\\x01\\xa7m\\xbb92WҦP\\xc7\""

Sorry if this a simple mistake on my part, I'm fairly new to grpc and go.

johanbrandhorst commented 4 years ago

Against current master:

diff --git a/gateway/gateway.go b/gateway/gateway.go
index bccae44..d97c09d 100644
--- a/gateway/gateway.go
+++ b/gateway/gateway.go
@@ -13,7 +13,6 @@ import (
    "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
    "github.com/rakyll/statik/fs"
    "google.golang.org/grpc"
-   "google.golang.org/grpc/credentials"
    "google.golang.org/grpc/grpclog"

    "github.com/johanbrandhorst/grpc-gateway-boilerplate/insecure"
@@ -48,7 +47,8 @@ func Run(dialAddr string) error {
    conn, err := grpc.DialContext(
        context.Background(),
        dialAddr,
-       grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(insecure.CertPool, "")),
+       //grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(insecure.CertPool, "")),
+       grpc.WithInsecure(),
        grpc.WithBlock(),
    )
    if err != nil {
diff --git a/main.go b/main.go
index 41926fa..a25f3cc 100644
--- a/main.go
+++ b/main.go
@@ -9,11 +9,9 @@ import (

    "github.com/rakyll/statik/fs"
    "google.golang.org/grpc"
-   "google.golang.org/grpc/credentials"
    "google.golang.org/grpc/grpclog"

    "github.com/johanbrandhorst/grpc-gateway-boilerplate/gateway"
-   "github.com/johanbrandhorst/grpc-gateway-boilerplate/insecure"
    pbExample "github.com/johanbrandhorst/grpc-gateway-boilerplate/proto"
    "github.com/johanbrandhorst/grpc-gateway-boilerplate/server"

@@ -46,13 +44,13 @@ func main() {
    }

    s := grpc.NewServer(
-       // TODO: Replace with your own certificate!
-       grpc.Creds(credentials.NewServerTLSFromCert(&insecure.Cert)),
+   // TODO: Replace with your own certificate!
+   //grpc.Creds(credentials.NewServerTLSFromCert(&insecure.Cert)),
    )
    pbExample.RegisterUserServiceServer(s, server.New())

    // Serve gRPC Server
-   log.Info("Serving gRPC on https://", addr)
+   log.Info("Serving gRPC on http://", addr)
    go func() {
        log.Fatal(s.Serve(lis))
    }()
$ SERVE_HTTP=true go run main.go 
INFO: 2020/09/29 11:08:25 Serving gRPC on http://0.0.0.0:10000
INFO: 2020/09/29 11:08:25 [core] parsed scheme: "dns"
INFO: 2020/09/29 11:08:25 [core] ccResolverWrapper: sending update to cc: {[{0.0.0.0:10000  <nil> 0 <nil>}] <nil> <nil>}
INFO: 2020/09/29 11:08:25 [core] ClientConn switching balancer to "pick_first"
INFO: 2020/09/29 11:08:25 [core] Channel switches to new LB policy "pick_first"
INFO: 2020/09/29 11:08:25 [core] Subchannel Connectivity change to CONNECTING
INFO: 2020/09/29 11:08:25 [core] Subchannel picks a new address "0.0.0.0:10000" to connect
INFO: 2020/09/29 11:08:25 [core] Channel Connectivity change to CONNECTING
INFO: 2020/09/29 11:08:25 [core] Subchannel Connectivity change to READY
INFO: 2020/09/29 11:08:25 [core] Channel Connectivity change to READY
INFO: 2020/09/29 11:08:25 Serving gRPC-Gateway and OpenAPI Documentation on http://0.0.0.0:11000
MiniXC commented 4 years ago

I will try this, thanks.