feat(worker): net.Conn implementation to count Bytes Read and Written

This commit introduces a new net.Conn implementation (countingConn) that
will keep track of bytes read and written for a particular client
connection. This is then implemented into the worker code using
interface composition.

Refs: #2501
This commit is contained in:
Hugo authored and Hugo Vieira committed 2022-10-26 17:43:04 +01:00
1 parent f114e20c6d
commit 1bb80624b8
7 files changed
+261 -17

No files matched your search

+1 -2
View File
@@ -7,7 +7,6 @@ import (
"sync"
"github.com/hashicorp/boundary/internal/daemon/worker/session"
"nhooyr.io/websocket"
)
// Config provides the core parameters needed for a worker to create a proxy between
@@ -20,7 +19,7 @@ type Config struct {
// there are any load balancers or proxies between the user and the worker,
// then it will be the address of the last one before the worker.
ClientAddress *net.TCPAddr
ClientConn *websocket.Conn
ClientConn net.Conn
RemoteEndpoint string
Session session.Session
+5 -5
View File
@@ -35,7 +35,7 @@ func TestConfigValidate(t *testing.T) {
{
name: "missing-client-address",
conf: Config{
ClientConn: conn,
ClientConn: websocket.NetConn(context.Background(), conn, websocket.MessageBinary),
RemoteEndpoint: "tcp://remote",
Session: si,
ConnectionId: "connection-id",
@@ -58,7 +58,7 @@ func TestConfigValidate(t *testing.T) {
name: "missing-remote-endpoint",
conf: Config{
ClientAddress: clientAddr,
ClientConn: conn,
ClientConn: websocket.NetConn(context.Background(), conn, websocket.MessageBinary),
Session: si,
ConnectionId: "connection-id",
},
@@ -69,7 +69,7 @@ func TestConfigValidate(t *testing.T) {
name: "missing-session",
conf: Config{
ClientAddress: clientAddr,
ClientConn: conn,
ClientConn: websocket.NetConn(context.Background(), conn, websocket.MessageBinary),
RemoteEndpoint: "tcp://remote",
ConnectionId: "connection-id",
},
@@ -80,7 +80,7 @@ func TestConfigValidate(t *testing.T) {
name: "missing-connection-id",
conf: Config{
ClientAddress: clientAddr,
ClientConn: conn,
ClientConn: websocket.NetConn(context.Background(), conn, websocket.MessageBinary),
RemoteEndpoint: "tcp://remote",
Session: si,
},
@@ -91,7 +91,7 @@ func TestConfigValidate(t *testing.T) {
name: "valid",
conf: Config{
ClientAddress: clientAddr,
ClientConn: conn,
ClientConn: websocket.NetConn(context.Background(), conn, websocket.MessageBinary),
RemoteEndpoint: "tcp://remote",
Session: si,
ConnectionId: "connection-id",
+4 -8
View File
@@ -10,7 +10,6 @@ import (
"github.com/hashicorp/boundary/internal/daemon/worker/proxy"
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
"nhooyr.io/websocket"
)
func init() {
@@ -59,22 +58,19 @@ func handleProxy(ctx context.Context, conf proxy.Config, _ ...proxy.Option) erro
return fmt.Errorf("error marking connection as connected: %w", err)
}
// Get a wrapped net.Conn so we can use io.Copy
netConn := websocket.NetConn(ctx, conn, websocket.MessageBinary)
connWg := new(sync.WaitGroup)
connWg.Add(2)
go func() {
defer connWg.Done()
_, _ = io.Copy(netConn, tcpRemoteConn)
_ = netConn.Close()
_, _ = io.Copy(conn, tcpRemoteConn)
_ = conn.Close()
_ = tcpRemoteConn.Close()
}()
go func() {
defer connWg.Done()
_, _ = io.Copy(tcpRemoteConn, netConn)
_, _ = io.Copy(tcpRemoteConn, conn)
_ = tcpRemoteConn.Close()
_ = netConn.Close()
_ = conn.Close()
}()
connWg.Wait()
return nil
+2 -1
View File
@@ -85,9 +85,10 @@ func TestHandleTcpProxyV1(t *testing.T) {
_, _, err = s.RequestAuthorizeConnection(ctx, "workerid", connCancelFn)
require.NoError(err)
conn := websocket.NetConn(ctx, proxyConn, websocket.MessageBinary)
conf := proxy.Config{
ClientAddress: clientAddr,
ClientConn: proxyConn,
ClientConn: conn,
RemoteEndpoint: fmt.Sprintf("tcp://localhost:%d", port),
Session: s,
ConnectionId: "mock-connection",