Skip to content
Snippets Groups Projects
Commit 9ef5fce1 authored by Jonah Husson's avatar Jonah Husson
Browse files

Update to use modified client, add params for relevant options, enable use of tls with web conn

parent c4074043
No related branches found
No related tags found
3 merge requests!39Merge release into master,!34Update to use modified client, add params for relevant options, enable use of tls with web conn,!32Project/channels
...@@ -11,6 +11,7 @@ package connect ...@@ -11,6 +11,7 @@ package connect
import ( import (
"crypto/tls" "crypto/tls"
"fmt"
"github.com/golang/protobuf/ptypes/any" "github.com/golang/protobuf/ptypes/any"
"github.com/improbable-eng/grpc-web/go/grpcweb" "github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/pkg/errors" "github.com/pkg/errors"
...@@ -230,10 +231,30 @@ func (c *ProtoComms) ServeWithWeb() { ...@@ -230,10 +231,30 @@ func (c *ProtoComms) ServeWithWeb() {
httpServer := grpcweb.WrapServer(grpcServer, httpServer := grpcweb.WrapServer(grpcServer,
grpcweb.WithOriginFunc(func(origin string) bool { return true })) grpcweb.WithOriginFunc(func(origin string) bool { return true }))
// This blocks for the lifetime of the listener. // This blocks for the lifetime of the listener.
if TestingOnlyDisableTLS && c.privateKey == nil {
fmt.Println("no tls")
if err := http.Serve(l, httpServer); err != nil { if err := http.Serve(l, httpServer); err != nil {
// Cannot panic here due to shared net.Listener // Cannot panic here due to shared net.Listener
jww.ERROR.Printf("Failed to serve HTTP: %v", err) jww.ERROR.Printf("Failed to serve HTTP: %v", err)
} }
} else {
fmt.Println("tls")
tlsConf := &tls.Config{}
tlsConf.NextProtos = append(tlsConf.NextProtos, "http/1.1")
tlsConf.Certificates = make([]tls.Certificate, 1)
tlsConf.InsecureSkipVerify = true
var err error
tlsConf.Certificates[0], err = tls.X509KeyPair(c.pubKeyPem, rsa.CreatePrivateKeyPem(c.privateKey))
if err != nil {
jww.FATAL.Panicf("Failed to load tls key: %+v", err)
}
tlsLis := tls.NewListener(l, tlsConf)
if err := http.Serve(tlsLis, httpServer); err != nil {
// Cannot panic here due to shared net.Listener
jww.ERROR.Printf("Failed to serve HTTP: %v", err)
}
}
jww.INFO.Printf("Shutting down HTTP server listener") jww.INFO.Printf("Shutting down HTTP server listener")
} }
listenGRPC := func(l net.Listener) { listenGRPC := func(l net.Listener) {
......
package connect package connect
import ( import (
"github.com/ktr0731/grpc-web-go-client/grpcweb" "git.xx.network/elixxir/grpc-web-go-client/grpcweb"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
......
...@@ -2,27 +2,29 @@ package connect ...@@ -2,27 +2,29 @@ package connect
import ( import (
"context" "context"
"fmt" "gitlab.com/xx_network/comms/connect/token"
"github.com/improbable-eng/grpc-web/go/grpcweb"
pb "gitlab.com/xx_network/comms/messages" pb "gitlab.com/xx_network/comms/messages"
"gitlab.com/xx_network/comms/testkeys"
"gitlab.com/xx_network/crypto/csprng" "gitlab.com/xx_network/crypto/csprng"
"gitlab.com/xx_network/crypto/signature/rsa"
"gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/utils"
"google.golang.org/grpc" "google.golang.org/grpc"
"net" "net"
"net/http"
"testing" "testing"
"time" "time"
) )
type TestGenericServer struct { type TestGenericServer struct {
resp string
} }
func (ts *TestGenericServer) AuthenticateToken(context.Context, *pb.AuthenticatedMessage) (*pb.Ack, error) { func (ts *TestGenericServer) AuthenticateToken(context.Context, *pb.AuthenticatedMessage) (*pb.Ack, error) {
return &pb.Ack{}, nil return &pb.Ack{Error: ts.resp}, nil
} }
func (ts *TestGenericServer) RequestToken(context.Context, *pb.Ping) (*pb.AssignToken, error) { func (ts *TestGenericServer) RequestToken(context.Context, *pb.Ping) (*pb.AssignToken, error) {
return &pb.AssignToken{Token: []byte("testtoken")}, nil return &pb.AssignToken{Token: []byte(ts.resp)}, nil
} }
func TestWebConnection(t *testing.T) { func TestWebConnection(t *testing.T) {
...@@ -47,13 +49,93 @@ func TestWebConnection(t *testing.T) { ...@@ -47,13 +49,93 @@ func TestWebConnection(t *testing.T) {
} }
go func() { go func() {
s := grpc.NewServer()
pb.RegisterGenericServer(s, &TestGenericServer{resp: "response"})
pc := ProtoComms{
networkId: id.NewIdFromString("zezima", id.User, t),
disableAuth: true,
tokens: token.NewMap(),
Manager: newManager(),
netListener: lis,
grpcServer: s,
}
pc.ServeWithWeb()
}()
time.Sleep(time.Second * 5)
err = h.connect()
if err != nil {
t.Fatal(err)
}
ctx, cancel := h.GetMessagingContext()
defer cancel()
resp := &pb.Ack{}
err = h.connection.GetWebConn().Invoke(ctx, "/messages.Generic/AuthenticateToken", &pb.AuthenticatedMessage{}, resp)
if err != nil {
t.Fatal(err)
}
t.Log(resp.Error)
}
func TestWebConnection_TLS(t *testing.T) {
addr := "0.0.0.0:11420"
certBytes, err := utils.ReadFile(testkeys.GetNodeCertPath())
if err != nil {
t.Fatal(err)
}
keyBytes, err := utils.ReadFile(testkeys.GetNodeKeyPath())
if err != nil {
t.Fatal(err)
}
lis, err := net.Listen("tcp", addr)
if err != nil {
t.Fatal(err)
}
rng := csprng.NewSystemRNG()
hostId, err := id.NewRandomID(rng, id.User)
if err != nil {
t.Fatal(err)
}
hostParams := GetDefaultHostParams()
TestingOnlyDisableTLS = true
hostParams.ConnectionType = Web
h, err := newHost(hostId, addr, certBytes, hostParams)
if err != nil {
t.Fatal(err)
}
s := grpc.NewServer() s := grpc.NewServer()
pb.RegisterGenericServer(s, &TestGenericServer{}) pb.RegisterGenericServer(s, &TestGenericServer{})
ws := grpcweb.WrapServer(s, grpcweb.WithOriginFunc(func(origin string) bool { return true }))
if err := http.Serve(lis, ws); err != nil { pk, err := rsa.LoadPrivateKeyFromPem(keyBytes)
fmt.Println(err) if err != nil {
t.Errorf("failed to serve: %v", err) t.Fatal(err)
} }
salt := make([]byte, 8)
_, err = rng.Read(salt)
if err != nil {
t.Fatal(err)
}
go func() {
pc := ProtoComms{
networkId: id.NewIdFromString("zezima", id.User, t),
privateKey: pk,
disableAuth: false,
tokens: token.NewMap(),
Manager: newManager(),
netListener: lis,
grpcServer: s,
pubKeyPem: certBytes,
salt: nil,
}
pc.ServeWithWeb()
}() }()
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
...@@ -65,10 +147,10 @@ func TestWebConnection(t *testing.T) { ...@@ -65,10 +147,10 @@ func TestWebConnection(t *testing.T) {
ctx, cancel := h.GetMessagingContext() ctx, cancel := h.GetMessagingContext()
defer cancel() defer cancel()
// TODO: This fails with RequestToken, seemingly because Ping has no actual contents. Throws an EOF error when attempting to parse response. Need to look into this in client repo. resp := &pb.Ack{}
resp := &pb.AssignToken{} err = h.connection.GetWebConn().Invoke(ctx, "/messages.Generic/AuthenticateToken", &pb.AuthenticatedMessage{}, resp)
err = h.connection.GetWebConn().Invoke(ctx, "/messages.Generic/RequestToken", &pb.Ping{}, resp)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
...@@ -3,7 +3,7 @@ package connect ...@@ -3,7 +3,7 @@ package connect
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/ktr0731/grpc-web-go-client/grpcweb" "git.xx.network/elixxir/grpc-web-go-client/grpcweb"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/connectivity" "google.golang.org/grpc/connectivity"
......
...@@ -59,6 +59,7 @@ type HostParams struct { ...@@ -59,6 +59,7 @@ type HostParams struct {
// ConnectionType describes the method for the underlying host connection // ConnectionType describes the method for the underlying host connection
ConnectionType ConnectionType ConnectionType ConnectionType
WebParams WebConnParam
} }
// GetDefaultHostParams Get default set of host params // GetDefaultHostParams Get default set of host params
......
...@@ -2,12 +2,19 @@ package connect ...@@ -2,12 +2,19 @@ package connect
import ( import (
"fmt" "fmt"
"github.com/ktr0731/grpc-web-go-client/grpcweb" "git.xx.network/elixxir/grpc-web-go-client/grpcweb"
"github.com/pkg/errors" "github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"google.golang.org/grpc" "google.golang.org/grpc"
"time"
) )
type WebConnParam struct {
TlsHandshakeTimeout time.Duration
IdleConnTimeout time.Duration
ExpectContinueTimeout time.Duration
}
// webConn implements the Connection interface // webConn implements the Connection interface
type webConn struct { type webConn struct {
h *Host h *Host
...@@ -42,7 +49,7 @@ func (wc *webConn) connectWebHelper() (err error) { ...@@ -42,7 +49,7 @@ func (wc *webConn) connectWebHelper() (err error) {
// Configure TLS options // Configure TLS options
var securityDial grpcweb.DialOption var securityDial grpcweb.DialOption
if wc.h.credentials != nil { if wc.h.credentials != nil {
securityDial = grpcweb.WithTransportCredentials(wc.h.credentials) securityDial = grpcweb.WithTlsCertificate(wc.h.certificate)
} else if TestingOnlyDisableTLS { } else if TestingOnlyDisableTLS {
jww.WARN.Printf("Connecting to %v without TLS!", wc.h.GetAddress()) jww.WARN.Printf("Connecting to %v without TLS!", wc.h.GetAddress())
securityDial = grpcweb.WithInsecure() securityDial = grpcweb.WithInsecure()
...@@ -70,8 +77,10 @@ func (wc *webConn) connectWebHelper() (err error) { ...@@ -70,8 +77,10 @@ func (wc *webConn) connectWebHelper() (err error) {
//ctx, cancel := newContext(time.Duration(backoffTime) * time.Millisecond) //ctx, cancel := newContext(time.Duration(backoffTime) * time.Millisecond)
dialOpts := []grpcweb.DialOption{ dialOpts := []grpcweb.DialOption{
// grpc.WithBlock(), grpcweb.WithIdleConnTimeout(wc.h.params.WebParams.IdleConnTimeout),
// grpc.WithKeepaliveParams(wc.h.params.KaClientOpts), grpcweb.WithExpectContinueTimeout(wc.h.params.WebParams.ExpectContinueTimeout),
grpcweb.WithTlsHandshakeTimeout(wc.h.params.WebParams.TlsHandshakeTimeout),
grpcweb.WithInsecureTlsVerification(),
grpcweb.WithDefaultCallOptions(), // grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32)), grpcweb.WithDefaultCallOptions(), // grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32)),
securityDial, securityDial,
} }
...@@ -108,11 +117,10 @@ func (wc *webConn) connectWebHelper() (err error) { ...@@ -108,11 +117,10 @@ func (wc *webConn) connectWebHelper() (err error) {
// Close handles closing the http connection. // Close handles closing the http connection.
func (wc *webConn) Close() error { func (wc *webConn) Close() error {
// TODO this needs work on the grpc-web-go-client side
if wc.connection == nil { if wc.connection == nil {
return nil return nil
} }
return nil return wc.connection.Close()
} }
...@@ -123,7 +131,9 @@ func (wc *webConn) disconnect() { ...@@ -123,7 +131,9 @@ func (wc *webConn) disconnect() {
// connection. In that case, we should not close a connection which does not // connection. In that case, we should not close a connection which does not
// exist // exist
if wc.connection != nil { if wc.connection != nil {
// TODO webconn cannot close yet, this needs work on that side if err := wc.connection.Close(); err != nil {
jww.FATAL.Panicf("Failed to disconnect web client: %+v", err)
}
wc.connection = nil wc.connection = nil
} }
...@@ -132,9 +142,8 @@ func (wc *webConn) disconnect() { ...@@ -132,9 +142,8 @@ func (wc *webConn) disconnect() {
// isAlive returns true if the webConn is non-nil and alive // isAlive returns true if the webConn is non-nil and alive
// must already be under the connectionMux // must already be under the connectionMux
func (wc *webConn) isAlive() bool { func (wc *webConn) isAlive() bool {
// TODO this cannot be determined until grpcweb clients have a persistent connection
if wc.connection == nil { if wc.connection == nil {
return false return false
} }
return true return wc.connection.IsAlive()
} }
...@@ -3,17 +3,23 @@ module gitlab.com/xx_network/comms ...@@ -3,17 +3,23 @@ module gitlab.com/xx_network/comms
go 1.13 go 1.13
require ( require (
git.xx.network/elixxir/grpc-web-go-client v0.0.0-20220825220914-af7341034631
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/golang/protobuf v1.5.2 github.com/golang/protobuf v1.5.2
github.com/improbable-eng/grpc-web v0.15.0 github.com/improbable-eng/grpc-web v0.15.0
github.com/ktr0731/grpc-web-go-client v0.2.8 github.com/json-iterator/go v1.1.12 // indirect
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/rs/cors v1.8.2 // indirect
github.com/soheilhy/cmux v0.1.5 github.com/soheilhy/cmux v0.1.5
github.com/spf13/jwalterweatherman v1.1.0 github.com/spf13/jwalterweatherman v1.1.0
github.com/stretchr/testify v1.8.0 // indirect
gitlab.com/xx_network/crypto v0.0.5-0.20220729193517-1e5e96f39f6e gitlab.com/xx_network/crypto v0.0.5-0.20220729193517-1e5e96f39f6e
gitlab.com/xx_network/primitives v0.0.4-0.20220712193914-aebd8544396e gitlab.com/xx_network/primitives v0.0.4-0.20220712193914-aebd8544396e
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c
google.golang.org/grpc v1.38.0 google.golang.org/grpc v1.49.0
google.golang.org/protobuf v1.26.0 google.golang.org/protobuf v1.28.1
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
nhooyr.io/websocket v1.8.7 // indirect
) )
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment