test(cluster): split tests between parallel and sequential packages (#5390)
This commit is contained in:
1 parent
1b6b22ef4f
commit
98bb4c8c31
17 files changed
+164
-133
No files matched your search
@@ -0,0 +1,13 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
/*
|
||||
This package includes a set of tests that run sequentially.
|
||||
A test should be added to this package if it needs to be completely
|
||||
isolated from other tests. Newly added tests should not enable the
|
||||
testing parallel option. Please include a comment in the unit test
|
||||
that explains why the test must be ran sequentially. Tests that can
|
||||
be ran in parallel should be moved to the adjacent "parallel" package.
|
||||
*/
|
||||
|
||||
package sequential
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package sequential
|
||||
|
||||
import (
|
||||
// Enable tcp target support.
|
||||
_ "github.com/hashicorp/boundary/internal/daemon/controller/handlers/targets/tcp"
|
||||
_ "github.com/hashicorp/boundary/internal/target/tcp"
|
||||
)
|
||||
@@ -0,0 +1,399 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package sequential
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
"github.com/hashicorp/boundary/internal/cmd/config"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller"
|
||||
tg "github.com/hashicorp/boundary/internal/daemon/controller/handlers/targets"
|
||||
"github.com/hashicorp/boundary/internal/daemon/worker"
|
||||
"github.com/hashicorp/boundary/internal/event"
|
||||
"github.com/hashicorp/boundary/internal/session"
|
||||
"github.com/hashicorp/boundary/internal/tests/helper"
|
||||
"github.com/hashicorp/dawdle"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// timeoutBurdenType details our "burden cases" for the session
|
||||
// cleanup tests.
|
||||
//
|
||||
// There are 3 burden cases:
|
||||
//
|
||||
// * default: This case simulates normal default operation where both
|
||||
// worker and controller generally are timing out connections at
|
||||
// generally the same interval. In reality, this is not necessarily
|
||||
// going to be the case, but it's hard to test individual cases when
|
||||
// both settings are the same.
|
||||
//
|
||||
// * worker: This case assumes the worker is the source of truth for
|
||||
// controller state. Here, the controller's grace period is
|
||||
// increased to a high factor over the default to ensure that the
|
||||
// worker is managing the lifecycle of a connection and will properly
|
||||
// unclaim it closed once the connection resumes, ensuring the
|
||||
// connection is marked as closed on the worker.
|
||||
type timeoutBurdenType string
|
||||
|
||||
const (
|
||||
timeoutBurdenTypeDefault timeoutBurdenType = "default"
|
||||
timeoutBurdenTypeWorker timeoutBurdenType = "worker"
|
||||
)
|
||||
|
||||
var timeoutBurdenCases = []timeoutBurdenType{timeoutBurdenTypeDefault, timeoutBurdenTypeWorker}
|
||||
|
||||
func controllerGracePeriod(ty timeoutBurdenType) time.Duration {
|
||||
if ty == timeoutBurdenTypeWorker {
|
||||
return helper.DefaultWorkerStatusGracePeriod * 10
|
||||
}
|
||||
|
||||
return helper.DefaultWorkerStatusGracePeriod
|
||||
}
|
||||
|
||||
func workerGracePeriod(ty timeoutBurdenType) time.Duration {
|
||||
return helper.DefaultWorkerStatusGracePeriod
|
||||
}
|
||||
|
||||
// TestSessionCleanup is the main test for session cleanup, and
|
||||
// dispatches to the individual subtests.
|
||||
func TestSessionCleanup(t *testing.T) {
|
||||
for _, burdenCase := range timeoutBurdenCases {
|
||||
burdenCase := burdenCase
|
||||
t.Run(string(burdenCase), func(t *testing.T) {
|
||||
t.Run("single_controller", testWorkerSessionCleanupSingle(burdenCase))
|
||||
t.Run("multi_controller", testWorkerSessionCleanupMulti(burdenCase))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testWorkerSessionCleanupSingle(burdenCase timeoutBurdenType) func(t *testing.T) {
|
||||
const op = "cluster.testWorkerSessionCleanupSingle"
|
||||
return func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
// This prevents us from running tests in parallel.
|
||||
tg.SetupSuiteTargetFilters(t)
|
||||
logger := hclog.New(&hclog.LoggerOptions{
|
||||
Name: t.Name(),
|
||||
Level: hclog.Trace,
|
||||
})
|
||||
|
||||
conf, err := config.DevController()
|
||||
require.NoError(err)
|
||||
|
||||
pl, err := net.Listen("tcp", "[::1]:0")
|
||||
require.NoError(err)
|
||||
c1 := controller.NewTestController(t, &controller.TestControllerOpts{
|
||||
Config: conf,
|
||||
InitialResourcesSuffix: "1234567890",
|
||||
Logger: logger.Named("c1"),
|
||||
PublicClusterAddr: pl.Addr().String(),
|
||||
WorkerStatusGracePeriodDuration: controllerGracePeriod(burdenCase),
|
||||
})
|
||||
|
||||
helper.ExpectWorkers(t, c1)
|
||||
|
||||
// Wire up the testing proxies
|
||||
require.Len(c1.ClusterAddrs(), 1)
|
||||
proxy, err := dawdle.NewProxy("tcp", "", c1.ClusterAddrs()[0],
|
||||
dawdle.WithListener(pl),
|
||||
dawdle.WithRbufSize(256),
|
||||
dawdle.WithWbufSize(256),
|
||||
)
|
||||
require.NoError(err)
|
||||
t.Cleanup(func() {
|
||||
_ = proxy.Close()
|
||||
})
|
||||
require.NotEmpty(t, proxy.ListenerAddr())
|
||||
|
||||
w1 := worker.NewTestWorker(t, &worker.TestWorkerOpts{
|
||||
WorkerAuthKms: c1.Config().WorkerAuthKms,
|
||||
InitialUpstreams: []string{proxy.ListenerAddr()},
|
||||
Logger: logger.Named("w1"),
|
||||
SuccessfulStatusGracePeriodDuration: workerGracePeriod(burdenCase),
|
||||
})
|
||||
|
||||
helper.ExpectWorkers(t, c1, w1)
|
||||
|
||||
// Use an independent context for test things that take a context so
|
||||
// that we aren't tied to any timeouts in the controller, etc. This
|
||||
// can interfere with some of the test operations.
|
||||
ctx := context.Background()
|
||||
|
||||
// Connect target
|
||||
client := c1.Client()
|
||||
client.SetToken(c1.Token().Token)
|
||||
tcl := targets.NewClient(client)
|
||||
tgt, err := tcl.Read(ctx, "ttcp_1234567890")
|
||||
require.NoError(err)
|
||||
require.NotNil(tgt)
|
||||
|
||||
// Create test server, update default port on target
|
||||
ts := helper.NewTestTcpServer(t)
|
||||
require.NotNil(t, ts)
|
||||
t.Cleanup(ts.Close)
|
||||
tgt, err = tcl.Update(ctx, tgt.Item.Id, tgt.Item.Version, targets.WithTcpTargetDefaultPort(ts.Port()), targets.WithSessionConnectionLimit(-1))
|
||||
require.NoError(err)
|
||||
require.NotNil(tgt)
|
||||
|
||||
// Authorize and connect
|
||||
sess := helper.NewTestSession(ctx, t, tcl, "ttcp_1234567890")
|
||||
sConn := sess.Connect(ctx, t)
|
||||
|
||||
// Run initial send/receive test, make sure things are working
|
||||
event.WriteSysEvent(ctx, op, "running initial send/recv test")
|
||||
sConn.TestSendRecvAll(t)
|
||||
|
||||
// Kill the link
|
||||
event.WriteSysEvent(ctx, op, "pausing controller/worker link")
|
||||
proxy.Pause()
|
||||
|
||||
// Wait for failure connection state (depends on burden case)
|
||||
switch burdenCase {
|
||||
case timeoutBurdenTypeWorker:
|
||||
// Wait on worker, then check controller
|
||||
sess.ExpectConnectionStateOnWorker(ctx, t, w1, session.StatusClosed)
|
||||
sess.ExpectConnectionStateOnController(ctx, t, c1.Controller().ConnectionRepoFn, session.StatusConnected)
|
||||
|
||||
default:
|
||||
// Should be closed on both worker and controller. Wait on
|
||||
// worker then check controller.
|
||||
sess.ExpectConnectionStateOnWorker(ctx, t, w1, session.StatusClosed)
|
||||
sess.ExpectConnectionStateOnController(ctx, t, c1.Controller().ConnectionRepoFn, session.StatusClosed)
|
||||
}
|
||||
|
||||
sConn.TestSendRecvFail(t)
|
||||
|
||||
// Resume the connection, and reconnect.
|
||||
event.WriteSysEvent(ctx, op, "resuming controller/worker link")
|
||||
proxy.Resume()
|
||||
err = w1.Worker().WaitForNextSuccessfulStatusUpdate()
|
||||
require.NoError(err)
|
||||
|
||||
// Do something post-reconnect depending on burden case. Note in
|
||||
// the default case, both worker and controller should be
|
||||
// relatively in sync, so we don't worry about these
|
||||
// post-reconnection assertions.
|
||||
switch burdenCase {
|
||||
case timeoutBurdenTypeWorker:
|
||||
// If we are expecting the worker to be the source of truth of
|
||||
// a connection status, ensure that our old session's
|
||||
// connections are actually closed now that the worker is
|
||||
// properly reporting in again.
|
||||
sess.ExpectConnectionStateOnController(ctx, t, c1.Controller().ConnectionRepoFn, session.StatusClosed)
|
||||
}
|
||||
|
||||
// Proceed with new connection test
|
||||
event.WriteSysEvent(ctx, op, "connecting to new session after resuming controller/worker link")
|
||||
sess = helper.NewTestSession(ctx, t, tcl, "ttcp_1234567890") // re-assign, other connection will close in t.Cleanup()
|
||||
sConn = sess.Connect(ctx, t)
|
||||
sConn.TestSendRecvAll(t)
|
||||
}
|
||||
}
|
||||
|
||||
func testWorkerSessionCleanupMulti(burdenCase timeoutBurdenType) func(t *testing.T) {
|
||||
const op = "cluster.testWorkerSessionCleanupMulti"
|
||||
return func(t *testing.T) {
|
||||
// This prevents us from running tests in parallel.
|
||||
tg.SetupSuiteTargetFilters(t)
|
||||
require := require.New(t)
|
||||
logger := hclog.New(&hclog.LoggerOptions{
|
||||
Name: t.Name(),
|
||||
Level: hclog.Trace,
|
||||
})
|
||||
|
||||
// ******************
|
||||
// ** Controller 1 **
|
||||
// ******************
|
||||
conf1, err := config.DevController()
|
||||
require.NoError(err)
|
||||
|
||||
pl1, err := net.Listen("tcp", "[::1]:0")
|
||||
require.NoError(err)
|
||||
c1 := controller.NewTestController(t, &controller.TestControllerOpts{
|
||||
Config: conf1,
|
||||
InitialResourcesSuffix: "1234567890",
|
||||
Logger: logger.Named("c1"),
|
||||
PublicClusterAddr: pl1.Addr().String(),
|
||||
WorkerStatusGracePeriodDuration: controllerGracePeriod(burdenCase),
|
||||
})
|
||||
|
||||
// ******************
|
||||
// ** Controller 2 **
|
||||
// ******************
|
||||
pl2, err := net.Listen("tcp", "[::1]:0")
|
||||
require.NoError(err)
|
||||
c2 := c1.AddClusterControllerMember(t, &controller.TestControllerOpts{
|
||||
Logger: logger.Named("c2"),
|
||||
PublicClusterAddr: pl2.Addr().String(),
|
||||
WorkerStatusGracePeriodDuration: controllerGracePeriod(burdenCase),
|
||||
})
|
||||
|
||||
wg := new(sync.WaitGroup)
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
helper.ExpectWorkers(t, c1)
|
||||
}()
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
helper.ExpectWorkers(t, c2)
|
||||
}()
|
||||
wg.Wait()
|
||||
|
||||
// *************
|
||||
// ** Proxy 1 **
|
||||
// *************
|
||||
require.Len(c1.ClusterAddrs(), 1)
|
||||
p1, err := dawdle.NewProxy("tcp", "", c1.ClusterAddrs()[0],
|
||||
dawdle.WithListener(pl1),
|
||||
dawdle.WithRbufSize(256),
|
||||
dawdle.WithWbufSize(256),
|
||||
)
|
||||
require.NoError(err)
|
||||
t.Cleanup(func() {
|
||||
_ = p1.Close()
|
||||
})
|
||||
require.NotEmpty(t, p1.ListenerAddr())
|
||||
|
||||
// *************
|
||||
// ** Proxy 2 **
|
||||
// *************
|
||||
require.Len(c2.ClusterAddrs(), 1)
|
||||
p2, err := dawdle.NewProxy("tcp", "", c2.ClusterAddrs()[0],
|
||||
dawdle.WithListener(pl2),
|
||||
dawdle.WithRbufSize(256),
|
||||
dawdle.WithWbufSize(256),
|
||||
)
|
||||
require.NoError(err)
|
||||
t.Cleanup(func() {
|
||||
_ = p2.Close()
|
||||
})
|
||||
require.NotEmpty(t, p2.ListenerAddr())
|
||||
|
||||
// ************
|
||||
// ** Worker **
|
||||
// ************
|
||||
w1 := worker.NewTestWorker(t, &worker.TestWorkerOpts{
|
||||
WorkerAuthKms: c1.Config().WorkerAuthKms,
|
||||
InitialUpstreams: []string{p1.ListenerAddr(), p2.ListenerAddr()},
|
||||
Logger: logger.Named("w1"),
|
||||
SuccessfulStatusGracePeriodDuration: workerGracePeriod(burdenCase),
|
||||
})
|
||||
// Worker needs some extra time to become ready, otherwise for a
|
||||
// currently-unknown reason the next successful status update fails
|
||||
// because it's not sent before the context times out.
|
||||
time.Sleep(5 * time.Second)
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
helper.ExpectWorkers(t, c1, w1)
|
||||
}()
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
helper.ExpectWorkers(t, c2, w1)
|
||||
}()
|
||||
wg.Wait()
|
||||
|
||||
// Use an independent context for test things that take a context so
|
||||
// that we aren't tied to any timeouts in the controller, etc. This
|
||||
// can interfere with some of the test operations.
|
||||
ctx := context.Background()
|
||||
|
||||
// Connect target
|
||||
client := c1.Client()
|
||||
client.SetToken(c1.Token().Token)
|
||||
tcl := targets.NewClient(client)
|
||||
tgt, err := tcl.Read(ctx, "ttcp_1234567890")
|
||||
require.NoError(err)
|
||||
require.NotNil(tgt)
|
||||
|
||||
// Create test server, update default port on target
|
||||
ts := helper.NewTestTcpServer(t)
|
||||
require.NotNil(ts)
|
||||
t.Cleanup(ts.Close)
|
||||
tgt, err = tcl.Update(ctx, tgt.Item.Id, tgt.Item.Version, targets.WithTcpTargetDefaultPort(ts.Port()), targets.WithSessionConnectionLimit(-1))
|
||||
require.NoError(err)
|
||||
require.NotNil(tgt)
|
||||
|
||||
// Authorize and connect
|
||||
sess := helper.NewTestSession(ctx, t, tcl, "ttcp_1234567890")
|
||||
sConn := sess.Connect(ctx, t)
|
||||
|
||||
// Run initial send/receive test, make sure things are working
|
||||
event.WriteSysEvent(ctx, op, "running initial send/recv test")
|
||||
sConn.TestSendRecvAll(t)
|
||||
|
||||
// Kill connection to first controller, and run test again, should
|
||||
// pass, deferring to other controller. Wait for the next
|
||||
// successful status report to ensure this.
|
||||
event.WriteSysEvent(ctx, op, "pausing link to controller #1")
|
||||
p1.Pause()
|
||||
err = w1.Worker().WaitForNextSuccessfulStatusUpdate()
|
||||
require.NoError(err)
|
||||
sConn.TestSendRecvAll(t)
|
||||
|
||||
// Resume first controller, pause second. This one should work too.
|
||||
event.WriteSysEvent(ctx, op, "pausing link to controller #2, resuming #1")
|
||||
p1.Resume()
|
||||
p2.Pause()
|
||||
err = w1.Worker().WaitForNextSuccessfulStatusUpdate()
|
||||
require.NoError(err)
|
||||
sConn.TestSendRecvAll(t)
|
||||
|
||||
// Kill the first controller connection again. This one should fail
|
||||
// due to lack of any connection.
|
||||
event.WriteSysEvent(ctx, op, "pausing link to controller #1 again, both connections should be offline")
|
||||
p1.Pause()
|
||||
|
||||
// Wait for failure connection state (depends on burden case)
|
||||
switch burdenCase {
|
||||
case timeoutBurdenTypeWorker:
|
||||
// Wait on worker, then check controller
|
||||
sess.ExpectConnectionStateOnWorker(ctx, t, w1, session.StatusClosed)
|
||||
sess.ExpectConnectionStateOnController(ctx, t, c1.Controller().ConnectionRepoFn, session.StatusConnected)
|
||||
|
||||
default:
|
||||
// Should be closed on both worker and controller. Wait on
|
||||
// worker then check controller.
|
||||
sess.ExpectConnectionStateOnWorker(ctx, t, w1, session.StatusClosed)
|
||||
sess.ExpectConnectionStateOnController(ctx, t, c1.Controller().ConnectionRepoFn, session.StatusClosed)
|
||||
}
|
||||
|
||||
// Run send/receive test again to check expected connection-level
|
||||
// behavior
|
||||
sConn.TestSendRecvFail(t)
|
||||
|
||||
// Finally resume both, try again. Should behave as per normal.
|
||||
event.WriteSysEvent(ctx, op, "resuming connections to both controllers")
|
||||
p1.Resume()
|
||||
p2.Resume()
|
||||
err = w1.Worker().WaitForNextSuccessfulStatusUpdate()
|
||||
require.NoError(err)
|
||||
|
||||
// Do something post-reconnect depending on burden case. Note in
|
||||
// the default case, both worker and controller should be
|
||||
// relatively in sync, so we don't worry about these
|
||||
// post-reconnection assertions.
|
||||
switch burdenCase {
|
||||
case timeoutBurdenTypeWorker:
|
||||
// If we are expecting the worker to be the source of truth of
|
||||
// a connection status, ensure that our old session's
|
||||
// connections are actually closed now that the worker is
|
||||
// properly reporting in again.
|
||||
sess.ExpectConnectionStateOnController(ctx, t, c1.Controller().ConnectionRepoFn, session.StatusClosed)
|
||||
}
|
||||
|
||||
// Proceed with new connection test
|
||||
event.WriteSysEvent(ctx, op, "connecting to new session after resuming controller/worker link")
|
||||
sess = helper.NewTestSession(ctx, t, tcl, "ttcp_1234567890") // re-assign, other connection will close in t.Cleanup()
|
||||
sConn = sess.Connect(ctx, t)
|
||||
sConn.TestSendRecvAll(t)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package sequential
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/api/sessions"
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
"github.com/hashicorp/boundary/internal/cmd/config"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller"
|
||||
tg "github.com/hashicorp/boundary/internal/daemon/controller/handlers/targets"
|
||||
"github.com/hashicorp/boundary/internal/daemon/worker"
|
||||
"github.com/hashicorp/boundary/internal/tests/helper"
|
||||
"github.com/hashicorp/dawdle"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWorkerBytesUpDown(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
// This prevents us from running tests in parallel.
|
||||
tg.SetupSuiteTargetFilters(t)
|
||||
|
||||
logger := hclog.New(&hclog.LoggerOptions{
|
||||
Name: t.Name(),
|
||||
Level: hclog.Trace,
|
||||
})
|
||||
|
||||
conf, err := config.DevController(config.WithIPv6Enabled(true))
|
||||
require.NoError(err)
|
||||
|
||||
pl, err := net.Listen("tcp", "[::1]:")
|
||||
require.NoError(err)
|
||||
|
||||
// update cluster listener to utilize proxy listener address
|
||||
for _, l := range conf.Listeners {
|
||||
if l.Purpose[0] == "cluster" {
|
||||
l.Address = pl.Addr().String()
|
||||
}
|
||||
}
|
||||
|
||||
c1 := controller.NewTestController(t, &controller.TestControllerOpts{
|
||||
Config: conf,
|
||||
InitialResourcesSuffix: "1234567890",
|
||||
Logger: logger.Named("c1"),
|
||||
WorkerStatusGracePeriodDuration: helper.DefaultWorkerStatusGracePeriod,
|
||||
})
|
||||
|
||||
helper.ExpectWorkers(t, c1)
|
||||
|
||||
// Wire up the testing proxies
|
||||
require.Len(c1.ClusterAddrs(), 1)
|
||||
proxy, err := dawdle.NewProxy("tcp", "", c1.ClusterAddrs()[0],
|
||||
dawdle.WithListener(pl),
|
||||
dawdle.WithRbufSize(256),
|
||||
dawdle.WithWbufSize(256),
|
||||
)
|
||||
require.NoError(err)
|
||||
require.NotEmpty(t, proxy.ListenerAddr())
|
||||
t.Cleanup(func() { _ = proxy.Close() })
|
||||
|
||||
w1 := worker.NewTestWorker(t, &worker.TestWorkerOpts{
|
||||
WorkerAuthKms: c1.Config().WorkerAuthKms,
|
||||
InitialUpstreams: []string{proxy.ListenerAddr()},
|
||||
Logger: logger.Named("w1"),
|
||||
SuccessfulStatusGracePeriodDuration: helper.DefaultSuccessfulStatusGracePeriod,
|
||||
EnableIPv6: true,
|
||||
})
|
||||
|
||||
helper.ExpectWorkers(t, c1, w1)
|
||||
|
||||
// Use an independent context for test things that take a context so
|
||||
// that we aren't tied to any timeouts in the controller, etc. This
|
||||
// can interfere with some of the test operations.
|
||||
ctx := context.Background()
|
||||
|
||||
// Connect target
|
||||
client := c1.Client()
|
||||
client.SetToken(c1.Token().Token)
|
||||
tcl := targets.NewClient(client)
|
||||
tgt, err := tcl.Read(ctx, "ttcp_1234567890")
|
||||
require.NoError(err)
|
||||
require.NotNil(tgt)
|
||||
|
||||
// Create test server, update default port on target
|
||||
ts := helper.NewTestTcpServer(t)
|
||||
require.NotNil(t, ts)
|
||||
t.Cleanup(ts.Close)
|
||||
tgt, err = tcl.Update(ctx, tgt.Item.Id, tgt.Item.Version, targets.WithTcpTargetDefaultPort(ts.Port()), targets.WithSessionConnectionLimit(-1))
|
||||
require.NoError(err)
|
||||
require.NotNil(tgt)
|
||||
|
||||
// Authorize a session, connect and send/recv some traffic
|
||||
workerInfo := []*targets.WorkerInfo{
|
||||
{
|
||||
Address: w1.ProxyAddrs()[0],
|
||||
},
|
||||
}
|
||||
sess := helper.NewTestSession(ctx, t, tcl, "ttcp_1234567890", helper.WithWorkerInfo(workerInfo))
|
||||
conn := sess.Connect(ctx, t)
|
||||
conn.TestSendRecvAll(t)
|
||||
|
||||
// Wait for next status and then check DB for bytes up and down
|
||||
helper.ExpectWorkers(t, c1, w1)
|
||||
|
||||
dbConns1, err := c1.ConnectionsRepo().ListConnectionsBySessionId(ctx, sess.SessionId)
|
||||
require.NoError(err)
|
||||
require.Len(dbConns1, 1)
|
||||
require.NotZero(dbConns1[0].BytesUp)
|
||||
require.NotZero(dbConns1[0].BytesDown)
|
||||
|
||||
// Also check the connection data via the API
|
||||
sc := sessions.NewClient(c1.Client())
|
||||
apiRes1, err := sc.Read(ctx, sess.SessionId)
|
||||
require.NoError(err)
|
||||
require.Len(apiRes1.Item.Connections, 1)
|
||||
require.NotZero(apiRes1.Item.Connections[0].BytesUp)
|
||||
require.NotZero(apiRes1.Item.Connections[0].BytesDown)
|
||||
|
||||
// Send/recv some more traffic, close connection, wait for the next status
|
||||
// update and check everything again.
|
||||
conn.TestSendRecvAll(t)
|
||||
require.NoError(conn.Close())
|
||||
helper.ExpectWorkers(t, c1, w1)
|
||||
|
||||
dbConns2, err := c1.ConnectionsRepo().ListConnectionsBySessionId(ctx, sess.SessionId)
|
||||
require.NoError(err)
|
||||
require.Len(dbConns2, 1)
|
||||
require.Greater(dbConns2[0].BytesUp, dbConns1[0].BytesUp)
|
||||
require.Greater(dbConns2[0].BytesDown, dbConns1[0].BytesDown)
|
||||
require.NotEmpty(dbConns2[0].ClosedReason)
|
||||
|
||||
apiRes2, err := sc.Read(ctx, sess.SessionId)
|
||||
require.NoError(err)
|
||||
require.Len(apiRes2.Item.Connections, 1)
|
||||
require.Greater(apiRes2.Item.Connections[0].BytesUp, apiRes1.Item.Connections[0].BytesUp)
|
||||
require.Greater(apiRes2.Item.Connections[0].BytesDown, apiRes1.Item.Connections[0].BytesDown)
|
||||
require.NotEmpty(apiRes2.Item.Connections[0].ClosedReason)
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package sequential
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
"github.com/hashicorp/boundary/internal/cmd/config"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller"
|
||||
tg "github.com/hashicorp/boundary/internal/daemon/controller/handlers/targets"
|
||||
"github.com/hashicorp/boundary/internal/daemon/worker"
|
||||
"github.com/hashicorp/boundary/internal/event"
|
||||
"github.com/hashicorp/boundary/internal/tests/helper"
|
||||
"github.com/hashicorp/dawdle"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWorkerSessionProxyMultipleConnections(t *testing.T) {
|
||||
const op = "cluster.TestWorkerSessionMultipleConnections"
|
||||
|
||||
// This prevents us from running tests in parallel.
|
||||
tg.SetupSuiteTargetFilters(t)
|
||||
|
||||
require := require.New(t)
|
||||
logger := hclog.New(&hclog.LoggerOptions{
|
||||
Name: t.Name(),
|
||||
Level: hclog.Trace,
|
||||
})
|
||||
|
||||
pl, err := net.Listen("tcp", "[::1]:")
|
||||
require.NoError(err)
|
||||
|
||||
conf, err := config.DevController(config.WithIPv6Enabled(true))
|
||||
require.NoError(err)
|
||||
|
||||
// update cluster listener to utilize proxy listener address
|
||||
for _, l := range conf.Listeners {
|
||||
if l.Purpose[0] == "cluster" {
|
||||
l.Address = pl.Addr().String()
|
||||
}
|
||||
}
|
||||
|
||||
c1 := controller.NewTestController(t, &controller.TestControllerOpts{
|
||||
Config: conf,
|
||||
InitialResourcesSuffix: "1234567890",
|
||||
Logger: logger.Named("c1"),
|
||||
WorkerStatusGracePeriodDuration: helper.DefaultWorkerStatusGracePeriod,
|
||||
})
|
||||
|
||||
helper.ExpectWorkers(t, c1)
|
||||
|
||||
// Wire up the testing proxies
|
||||
require.Len(c1.ClusterAddrs(), 1)
|
||||
proxy, err := dawdle.NewProxy("tcp", "", c1.ClusterAddrs()[0],
|
||||
dawdle.WithListener(pl),
|
||||
dawdle.WithRbufSize(256),
|
||||
dawdle.WithWbufSize(256),
|
||||
)
|
||||
require.NoError(err)
|
||||
t.Cleanup(func() { _ = proxy.Close() })
|
||||
require.NotEmpty(t, proxy.ListenerAddr())
|
||||
|
||||
w1 := worker.NewTestWorker(t, &worker.TestWorkerOpts{
|
||||
WorkerAuthKms: c1.Config().WorkerAuthKms,
|
||||
InitialUpstreams: []string{proxy.ListenerAddr()},
|
||||
Logger: logger.Named("w1"),
|
||||
SuccessfulStatusGracePeriodDuration: helper.DefaultWorkerStatusGracePeriod,
|
||||
EnableIPv6: true,
|
||||
})
|
||||
|
||||
helper.ExpectWorkers(t, c1, w1)
|
||||
|
||||
// Use an independent context for test things that take a context so
|
||||
// that we aren't tied to any timeouts in the controller, etc. This
|
||||
// can interfere with some of the test operations.
|
||||
ctx := context.Background()
|
||||
|
||||
// Connect target
|
||||
client := c1.Client()
|
||||
client.SetToken(c1.Token().Token)
|
||||
tcl := targets.NewClient(client)
|
||||
tgt, err := tcl.Read(ctx, "ttcp_1234567890")
|
||||
require.NoError(err)
|
||||
require.NotNil(tgt)
|
||||
|
||||
// Create test server, update default port on target
|
||||
ts := helper.NewTestTcpServer(t)
|
||||
require.NotNil(t, ts)
|
||||
t.Cleanup(ts.Close)
|
||||
t.Logf("test server listening on port %d", ts.Port())
|
||||
|
||||
tgt, err = tcl.Update(ctx, tgt.Item.Id, tgt.Item.Version, targets.WithTcpTargetDefaultPort(ts.Port()), targets.WithSessionConnectionLimit(-1))
|
||||
require.NoError(err)
|
||||
require.NotNil(tgt)
|
||||
|
||||
// Authorize and connect
|
||||
workerInfo := []*targets.WorkerInfo{
|
||||
{
|
||||
Address: w1.ProxyAddrs()[0],
|
||||
},
|
||||
}
|
||||
sess := helper.NewTestSession(ctx, t, tcl, "ttcp_1234567890", helper.WithWorkerInfo(workerInfo))
|
||||
sConn := sess.Connect(ctx, t)
|
||||
|
||||
// Run initial send/receive test, make sure things are working
|
||||
event.WriteSysEvent(ctx, op, "running initial send/recv test for initial connection")
|
||||
sConn.TestSendRecvAll(t)
|
||||
|
||||
// Create another connection for this session
|
||||
sConn2 := sess.Connect(ctx, t)
|
||||
// Ensure second connection works
|
||||
event.WriteSysEvent(ctx, op, "running initial send/recv test for second connection")
|
||||
sConn2.TestSendRecvAll(t)
|
||||
|
||||
// Create another connection for this session
|
||||
sConn3 := sess.Connect(ctx, t)
|
||||
// Ensure second connection works
|
||||
event.WriteSysEvent(ctx, op, "running initial send/recv test for third connection")
|
||||
sConn3.TestSendRecvAll(t)
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package sequential
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
"github.com/hashicorp/boundary/internal/cmd/config"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller"
|
||||
ct "github.com/hashicorp/boundary/internal/daemon/controller/handlers/targets"
|
||||
"github.com/hashicorp/boundary/internal/daemon/worker"
|
||||
"github.com/hashicorp/boundary/internal/tests/helper"
|
||||
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/targets"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/mr-tron/base58"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func TestWorkerTagging(t *testing.T) {
|
||||
logger := hclog.New(&hclog.LoggerOptions{
|
||||
Level: hclog.Trace,
|
||||
})
|
||||
|
||||
conf, err := config.DevController()
|
||||
require.NoError(t, err)
|
||||
|
||||
c1 := controller.NewTestController(t, &controller.TestControllerOpts{
|
||||
Config: conf,
|
||||
InitialResourcesSuffix: "1234567890",
|
||||
Logger: logger.Named("c1"),
|
||||
})
|
||||
|
||||
ctx := c1.Context()
|
||||
|
||||
// No workers yet
|
||||
helper.ExpectWorkers(t, c1)
|
||||
|
||||
// Ensure target is valid
|
||||
client := c1.Client()
|
||||
client.SetToken(c1.Token().Token)
|
||||
tcl := targets.NewClient(client)
|
||||
tgt, err := tcl.Read(ctx, "ttcp_1234567890")
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, tgt)
|
||||
|
||||
// Worker 1
|
||||
conf, err = config.DevWorker()
|
||||
require.NoError(t, err)
|
||||
conf.Worker.Name = "test_worker_1"
|
||||
conf.Worker.Tags = map[string][]string{
|
||||
"region": {"east"},
|
||||
"foo": {"bar"},
|
||||
}
|
||||
w1 := worker.NewTestWorker(t, &worker.TestWorkerOpts{
|
||||
Config: conf,
|
||||
WorkerAuthKms: c1.Config().WorkerAuthKms,
|
||||
InitialUpstreams: c1.ClusterAddrs(),
|
||||
Logger: logger.Named("w1"),
|
||||
})
|
||||
w1Addr := w1.ProxyAddrs()[0]
|
||||
|
||||
// Worker 2
|
||||
conf, err = config.DevWorker()
|
||||
require.NoError(t, err)
|
||||
conf.Worker.Name = "test_worker_2"
|
||||
conf.Worker.Tags = map[string][]string{
|
||||
"region": {"west"},
|
||||
"az": {"one", "two", "three"},
|
||||
}
|
||||
w2 := worker.NewTestWorker(t, &worker.TestWorkerOpts{
|
||||
Config: conf,
|
||||
WorkerAuthKms: c1.Config().WorkerAuthKms,
|
||||
InitialUpstreams: c1.ClusterAddrs(),
|
||||
Logger: logger.Named("w2"),
|
||||
})
|
||||
w2Addr := w2.ProxyAddrs()[0]
|
||||
|
||||
// Worker 3
|
||||
conf, err = config.DevWorker()
|
||||
require.NoError(t, err)
|
||||
conf.Worker.Name = "test_worker_3"
|
||||
conf.Worker.Tags = map[string][]string{
|
||||
"region": {"west"},
|
||||
"az": {"one", "three"},
|
||||
}
|
||||
w3 := worker.NewTestWorker(t, &worker.TestWorkerOpts{
|
||||
Config: conf,
|
||||
WorkerAuthKms: c1.Config().WorkerAuthKms,
|
||||
InitialUpstreams: c1.ClusterAddrs(),
|
||||
Logger: logger.Named("w3"),
|
||||
})
|
||||
w3Addr := w3.ProxyAddrs()[0]
|
||||
|
||||
helper.ExpectWorkers(t, c1, w1, w2, w3)
|
||||
|
||||
// Ensure we are using the OSS filter, which uses egress only for worker selection
|
||||
oldAuthFun := ct.AuthorizeSessionWorkerFilterFn
|
||||
ct.AuthorizeSessionWorkerFilterFn = ct.AuthorizeSessionWithWorkerFilter
|
||||
validateIngressFn := ct.ValidateIngressWorkerFilterFn
|
||||
ct.ValidateIngressWorkerFilterFn = ct.IngressWorkerFilterUnsupported
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
egressFilter string
|
||||
ingressFilter string
|
||||
expWorkers []string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "base case",
|
||||
expWorkers: []string{w1Addr, w2Addr, w3Addr},
|
||||
},
|
||||
{
|
||||
name: "name and region",
|
||||
egressFilter: `"/name" matches "test_worker_[13]" and "west" in "/tags/region"`,
|
||||
expWorkers: []string{w3Addr},
|
||||
},
|
||||
{
|
||||
name: "name and az",
|
||||
egressFilter: `"/name" matches "test_worker_[23]" and "three" in "/tags/az"`,
|
||||
expWorkers: []string{w2Addr, w3Addr},
|
||||
},
|
||||
{
|
||||
name: "key not found doesn't error",
|
||||
egressFilter: `"bar" in "/tags/foo"`,
|
||||
expWorkers: []string{w1Addr},
|
||||
},
|
||||
{
|
||||
name: "ingress filter invalid",
|
||||
ingressFilter: `"/name" matches "test_worker_[13]" and "west" in "/tags/region"`,
|
||||
expWorkers: []string{w3Addr},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
require, assert := require.New(t), assert.New(t)
|
||||
|
||||
opts := []targets.Option{targets.WithAutomaticVersioning(true), targets.WithDescription(tc.name)}
|
||||
if tc.egressFilter != "" {
|
||||
opts = append(opts, targets.WithEgressWorkerFilter(tc.egressFilter))
|
||||
}
|
||||
if tc.ingressFilter != "" {
|
||||
opts = append(opts, targets.WithIngressWorkerFilter(tc.ingressFilter))
|
||||
}
|
||||
tgt, err := tcl.Update(ctx, "ttcp_1234567890", 0, opts...)
|
||||
if tc.wantErr {
|
||||
require.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
require.NotNil(tgt)
|
||||
|
||||
// Fetch the session and decode the token
|
||||
sar, err := tcl.AuthorizeSession(ctx, "ttcp_1234567890")
|
||||
require.NoError(err)
|
||||
sat := sar.GetItem().(*targets.SessionAuthorization).AuthorizationToken
|
||||
decodedSat, err := base58.FastBase58Decoding(sat)
|
||||
require.NoError(err)
|
||||
var sad pb.SessionAuthorizationData
|
||||
require.NoError(proto.Unmarshal(decodedSat, &sad))
|
||||
|
||||
var addrs []string
|
||||
for _, worker := range sad.GetWorkerInfo() {
|
||||
addrs = append(addrs, worker.GetAddress())
|
||||
}
|
||||
assert.ElementsMatch(tc.expWorkers, addrs)
|
||||
})
|
||||
}
|
||||
ct.AuthorizeSessionWorkerFilterFn = oldAuthFun
|
||||
ct.ValidateIngressWorkerFilterFn = validateIngressFn
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package sequential
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
apiproxy "github.com/hashicorp/boundary/api/proxy"
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
"github.com/hashicorp/boundary/globals"
|
||||
"github.com/hashicorp/boundary/internal/cmd/config"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller"
|
||||
tg "github.com/hashicorp/boundary/internal/daemon/controller/handlers/targets"
|
||||
"github.com/hashicorp/boundary/internal/daemon/worker"
|
||||
"github.com/hashicorp/boundary/internal/event"
|
||||
"github.com/hashicorp/boundary/internal/tests/helper"
|
||||
"github.com/hashicorp/go-cleanhttp"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/stretchr/testify/require"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
func TestCustomX509Verification_Client(t *testing.T) {
|
||||
req := require.New(t)
|
||||
ctx := context.Background()
|
||||
ec := event.TestEventerConfig(t, "TestWorkerReplay", event.TestWithObservationSink(t), event.TestWithSysSink(t))
|
||||
testLock := &sync.Mutex{}
|
||||
// This prevents us from running tests in parallel.
|
||||
tg.SetupSuiteTargetFilters(t)
|
||||
logger := hclog.New(&hclog.LoggerOptions{
|
||||
Mutex: testLock,
|
||||
Name: "test",
|
||||
})
|
||||
req.NoError(event.InitSysEventer(logger, testLock, "use-TestCustomX509Verification", event.WithEventerConfig(&ec.EventerConfig)))
|
||||
|
||||
conf, err := config.DevController()
|
||||
conf.Eventing = &ec.EventerConfig
|
||||
req.NoError(err)
|
||||
c1 := controller.NewTestController(t, &controller.TestControllerOpts{
|
||||
Config: conf,
|
||||
InitialResourcesSuffix: "1234567890",
|
||||
Logger: logger.Named("c1"),
|
||||
})
|
||||
|
||||
conf, err = config.DevWorker()
|
||||
conf.Eventing = &ec.EventerConfig
|
||||
req.NoError(err)
|
||||
w1 := worker.NewTestWorker(t, &worker.TestWorkerOpts{
|
||||
WorkerAuthKms: c1.Config().WorkerAuthKms,
|
||||
InitialUpstreams: c1.ClusterAddrs(),
|
||||
Logger: logger.Named("w1"),
|
||||
})
|
||||
|
||||
// Give time for it to connect
|
||||
helper.ExpectWorkers(t, c1, w1)
|
||||
|
||||
// Connect target
|
||||
client := c1.Client()
|
||||
client.SetToken(c1.Token().Token)
|
||||
tcl := targets.NewClient(client)
|
||||
tgt, err := tcl.Read(ctx, "ttcp_1234567890")
|
||||
req.NoError(err)
|
||||
req.NotNil(tgt)
|
||||
|
||||
// Create test server, update default port on target
|
||||
ts := helper.NewTestTcpServer(t)
|
||||
require.NotNil(t, ts)
|
||||
t.Cleanup(ts.Close)
|
||||
tgt, err = tcl.Update(ctx, tgt.Item.Id, tgt.Item.Version, targets.WithTcpTargetDefaultPort(ts.Port()), targets.WithSessionConnectionLimit(-1))
|
||||
req.NoError(err)
|
||||
req.NotNil(tgt)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
certPool *x509.CertPool
|
||||
dnsName *bytes.Buffer
|
||||
wantErrContains string
|
||||
}{
|
||||
{
|
||||
name: "base",
|
||||
},
|
||||
{
|
||||
name: "modified cert pool",
|
||||
certPool: x509.NewCertPool(),
|
||||
wantErrContains: "signed by unknown authority",
|
||||
},
|
||||
{
|
||||
name: "modified dns name",
|
||||
dnsName: bytes.NewBuffer([]byte("foobar")),
|
||||
wantErrContains: "not foobar",
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
sar, err := tcl.AuthorizeSession(ctx, "ttcp_1234567890")
|
||||
require.NoError(err)
|
||||
require.NotNil(sar)
|
||||
|
||||
sessAuth, err := sar.GetSessionAuthorization()
|
||||
require.NoError(err)
|
||||
sessAuthzData, err := sessAuth.GetSessionAuthorizationData()
|
||||
require.NoError(err)
|
||||
require.LessOrEqual(1, len(sessAuthzData.WorkerInfo))
|
||||
tlsConf := apiproxy.TestClientTlsConfig(t, sessAuth.AuthorizationToken, apiproxy.WithWorkerHost(sessAuth.SessionId))
|
||||
|
||||
certPool := tc.certPool
|
||||
if certPool == nil {
|
||||
parsedCert, err := x509.ParseCertificate(sessAuthzData.Certificate)
|
||||
require.NoError(err)
|
||||
certPool = x509.NewCertPool()
|
||||
certPool.AddCert(parsedCert)
|
||||
}
|
||||
|
||||
dnsName := sessAuth.SessionId
|
||||
if tc.dnsName != nil {
|
||||
dnsName = tc.dnsName.String()
|
||||
}
|
||||
|
||||
verifyOpts := x509.VerifyOptions{
|
||||
DNSName: dnsName,
|
||||
Roots: certPool,
|
||||
KeyUsages: []x509.ExtKeyUsage{
|
||||
x509.ExtKeyUsageClientAuth,
|
||||
x509.ExtKeyUsageServerAuth,
|
||||
},
|
||||
}
|
||||
tlsConf.VerifyConnection = func(cs tls.ConnectionState) error {
|
||||
// Go will not run this without at least one peer certificate, but
|
||||
// doesn't hurt to check
|
||||
if len(cs.PeerCertificates) == 0 {
|
||||
return errors.New("no peer certificates provided")
|
||||
}
|
||||
_, err := cs.PeerCertificates[0].Verify(verifyOpts)
|
||||
return err
|
||||
}
|
||||
|
||||
transport := cleanhttp.DefaultTransport()
|
||||
transport.DisableKeepAlives = false
|
||||
// This isn't/shouldn't used anyways really because the connection is
|
||||
// hijacked, just setting for completeness
|
||||
transport.IdleConnTimeout = 0
|
||||
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
dialer := &tls.Dialer{Config: tlsConf}
|
||||
return dialer.DialContext(ctx, network, addr)
|
||||
}
|
||||
|
||||
conn, _, err := websocket.Dial(
|
||||
ctx,
|
||||
fmt.Sprintf("ws://%s/v1/proxy", sessAuthzData.WorkerInfo[0].Address),
|
||||
&websocket.DialOptions{
|
||||
HTTPClient: &http.Client{
|
||||
Transport: transport,
|
||||
},
|
||||
Subprotocols: []string{globals.TcpProxyV1},
|
||||
},
|
||||
)
|
||||
if conn != nil {
|
||||
defer conn.Close(websocket.StatusNormalClosure, "done")
|
||||
}
|
||||
switch tc.wantErrContains != "" {
|
||||
case true:
|
||||
require.Error(err)
|
||||
require.Contains(err.Error(), tc.wantErrContains)
|
||||
default:
|
||||
require.NoError(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomX509Verification_Server(t *testing.T) {
|
||||
t.Skip("These tests are currently not working and will need further investigation")
|
||||
|
||||
ec := event.TestEventerConfig(t, "TestCustomX509Verification_Server", event.TestWithObservationSink(t), event.TestWithSysSink(t))
|
||||
testLock := &sync.Mutex{}
|
||||
logger := hclog.New(&hclog.LoggerOptions{
|
||||
Mutex: testLock,
|
||||
Name: "test",
|
||||
Level: hclog.Trace,
|
||||
})
|
||||
require.NoError(t, event.InitSysEventer(logger, testLock, "use-TestCustomX509Verification_Server", event.WithEventerConfig(&ec.EventerConfig)))
|
||||
t.Cleanup(func() { event.TestResetSystEventer(t) })
|
||||
t.Cleanup(func() {
|
||||
all, err := io.ReadAll(ec.AllEvents)
|
||||
require.NoError(t, err)
|
||||
t.Log(string(all))
|
||||
})
|
||||
|
||||
t.Run("bad cert pool", testCustomX509Verification_Server(ec, x509.NewCertPool(), "", "bad certificate"))
|
||||
t.Run("bad dns name", testCustomX509Verification_Server(ec, nil, "foobar", "bad certificate"))
|
||||
}
|
||||
|
||||
func testCustomX509Verification_Server(ec event.TestConfig, certPool *x509.CertPool, dnsName, wantErrContains string) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
req := require.New(t)
|
||||
ctx := context.Background()
|
||||
// This prevents us from running tests in parallel.
|
||||
tg.SetupSuiteTargetFilters(t)
|
||||
|
||||
conf, err := config.DevController()
|
||||
conf.Eventing = &ec.EventerConfig
|
||||
req.NoError(err)
|
||||
c1 := controller.NewTestController(t, &controller.TestControllerOpts{
|
||||
Config: conf,
|
||||
InitialResourcesSuffix: "1234567890",
|
||||
})
|
||||
|
||||
conf, err = config.DevWorker()
|
||||
conf.Eventing = &ec.EventerConfig
|
||||
req.NoError(err)
|
||||
w1 := worker.NewTestWorker(t, &worker.TestWorkerOpts{
|
||||
WorkerAuthKms: c1.Config().WorkerAuthKms,
|
||||
InitialUpstreams: c1.ClusterAddrs(),
|
||||
})
|
||||
w1.Worker().TestOverrideX509VerifyCertPool = certPool
|
||||
w1.Worker().TestOverrideX509VerifyDnsName = dnsName
|
||||
|
||||
// Give time for it to connect
|
||||
helper.ExpectWorkers(t, c1, w1)
|
||||
|
||||
// Connect target
|
||||
client := c1.Client()
|
||||
client.SetToken(c1.Token().Token)
|
||||
tcl := targets.NewClient(client)
|
||||
tgt, err := tcl.Read(ctx, "ttcp_1234567890")
|
||||
req.NoError(err)
|
||||
req.NotNil(tgt)
|
||||
|
||||
// Create test server, update default port on target
|
||||
ts := helper.NewTestTcpServer(t)
|
||||
require.NotNil(t, ts)
|
||||
t.Cleanup(ts.Close)
|
||||
tgt, err = tcl.Update(ctx, tgt.Item.Id, tgt.Item.Version, targets.WithTcpTargetDefaultPort(ts.Port()), targets.WithSessionConnectionLimit(-1))
|
||||
req.NoError(err)
|
||||
req.NotNil(tgt)
|
||||
|
||||
sar, err := tcl.AuthorizeSession(ctx, "ttcp_1234567890")
|
||||
req.NoError(err)
|
||||
req.NotNil(sar)
|
||||
sessAuth, err := sar.GetSessionAuthorization()
|
||||
req.NoError(err)
|
||||
sessAuthzData, err := sessAuth.GetSessionAuthorizationData()
|
||||
req.NoError(err)
|
||||
req.LessOrEqual(1, len(sessAuthzData.WorkerInfo))
|
||||
tlsConf := apiproxy.TestClientTlsConfig(t, sessAuth.AuthorizationToken, apiproxy.WithWorkerHost(sessAuth.SessionId))
|
||||
|
||||
transport := cleanhttp.DefaultTransport()
|
||||
transport.DisableKeepAlives = false
|
||||
// This isn't/shouldn't used anyways really because the connection is
|
||||
// hijacked, just setting for completeness
|
||||
transport.IdleConnTimeout = 0
|
||||
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
dialer := &tls.Dialer{Config: tlsConf}
|
||||
return dialer.DialContext(ctx, network, addr)
|
||||
}
|
||||
|
||||
conn, _, err := websocket.Dial(
|
||||
ctx,
|
||||
fmt.Sprintf("wss://%s/v1/proxy", sessAuthzData.WorkerInfo[0].Address),
|
||||
&websocket.DialOptions{
|
||||
HTTPClient: &http.Client{
|
||||
Transport: transport,
|
||||
},
|
||||
Subprotocols: []string{globals.TcpProxyV1},
|
||||
},
|
||||
)
|
||||
if conn != nil {
|
||||
defer conn.Close(websocket.StatusNormalClosure, "done")
|
||||
}
|
||||
switch wantErrContains != "" {
|
||||
case true:
|
||||
req.Error(err)
|
||||
req.Contains(err.Error(), wantErrContains)
|
||||
default:
|
||||
req.NoError(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user