Provide a worker decryption function to proxy handlers (#2784)
This commit is contained in:
6 files changed
+62
-22
No files matched your search
@@ -28,6 +28,9 @@ var (
|
||||
GetHandler = tcpOnly
|
||||
)
|
||||
|
||||
// DecryptFn decrypts the provided bytes into a proto.Message
|
||||
type DecryptFn func(ctx context.Context, from []byte, to proto.Message) error
|
||||
|
||||
// ProxyConnFn is called after the call to ConnectConnection on the cluster.
|
||||
// ProxyConnFn blocks until the specific request that is being proxied is finished
|
||||
type ProxyConnFn func(ctx context.Context)
|
||||
@@ -37,7 +40,7 @@ type ProxyConnFn func(ctx context.Context)
|
||||
// be nil. If there is no error ProxyConnFn must be set. When Handler has
|
||||
// returned, it is expected that the initial connection to the endpoint has been
|
||||
// established.
|
||||
type Handler func(context.Context, net.Conn, *ProxyDialer, string, *anypb.Any) (ProxyConnFn, error)
|
||||
type Handler func(context.Context, DecryptFn, net.Conn, *ProxyDialer, string, *anypb.Any) (ProxyConnFn, error)
|
||||
|
||||
func RegisterHandler(protocol string, handler Handler) error {
|
||||
_, loaded := handlers.LoadOrStore(protocol, handler)
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
func TestRegisterHandler(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
|
||||
fn := func(context.Context, net.Conn, *ProxyDialer, string, *anypb.Any) (ProxyConnFn, error) {
|
||||
fn := func(context.Context, DecryptFn, net.Conn, *ProxyDialer, string, *anypb.Any) (ProxyConnFn, error) {
|
||||
return nil, nil
|
||||
}
|
||||
oldHandler := handlers
|
||||
@@ -37,7 +37,7 @@ func TestRegisterHandler(t *testing.T) {
|
||||
|
||||
func TestAlwaysTcpGetHandler(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
fn := func(context.Context, net.Conn, *ProxyDialer, string, *anypb.Any) (ProxyConnFn, error) {
|
||||
fn := func(context.Context, DecryptFn, net.Conn, *ProxyDialer, string, *anypb.Any) (ProxyConnFn, error) {
|
||||
return nil, nil
|
||||
}
|
||||
oldHandler := handlers
|
||||
@@ -45,9 +45,7 @@ func TestAlwaysTcpGetHandler(t *testing.T) {
|
||||
handlers = oldHandler
|
||||
})
|
||||
handlers = sync.Map{}
|
||||
|
||||
_, err := tcpOnly("wid", nil)
|
||||
require.Error(err)
|
||||
assert.ErrorIs(err, ErrUnknownProtocol)
|
||||
|
||||
require.NoError(RegisterHandler("tcp", fn))
|
||||
|
||||
@@ -24,7 +24,7 @@ func init() {
|
||||
// handleProxy returns a ProxyConnFn which starts the copy between the
|
||||
// connections and blocks until an error (EOF on happy path) is received on
|
||||
// either connection.
|
||||
func handleProxy(ctx context.Context, conn net.Conn, out *proxy.ProxyDialer, connId string, _ *anypb.Any) (proxy.ProxyConnFn, error) {
|
||||
func handleProxy(ctx context.Context, _ proxy.DecryptFn, conn net.Conn, out *proxy.ProxyDialer, connId string, _ *anypb.Any) (proxy.ProxyConnFn, error) {
|
||||
const op = "tcp.HandleProxy"
|
||||
switch {
|
||||
case conn == nil:
|
||||
|
||||
@@ -85,7 +85,7 @@ func TestHandleProxy_Errors(t *testing.T) {
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
fn, err := handleProxy(context.Background(), tc.conn, tc.dialer, tc.connId, tc.protocolCtx)
|
||||
fn, err := handleProxy(context.Background(), nil, tc.conn, tc.dialer, tc.connId, tc.protocolCtx)
|
||||
if tc.wantError {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, fn)
|
||||
@@ -163,7 +163,7 @@ func TestHandleTcpProxyV1(t *testing.T) {
|
||||
conn := websocket.NetConn(ctx, proxyConn, websocket.MessageBinary)
|
||||
|
||||
go func() {
|
||||
fn, err := handleProxy(ctx, conn, tDial, resp.GetConnectionId(), resp.GetProtocolContext())
|
||||
fn, err := handleProxy(ctx, nil, conn, tDial, resp.GetConnectionId(), resp.GetProtocolContext())
|
||||
t.Cleanup(func() {
|
||||
// Use of the t.Cleanup is so we can check the state of the returned
|
||||
// error since it isn't valid to call `t.FailNow()` from a goroutine.
|
||||
|
||||
Reference in new issue
Block a user