Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(pg): cancel queries over an SSH tunnel when their context ends (#21592)

zchpeter3a466f488145
diff --git a/backend/common/testcontainer/shared.go b/backend/common/testcontainer/shared.go
index b0d17c46fe..8be82c1ab6 100644
--- a/backend/common/testcontainer/shared.go
+++ b/backend/common/testcontainer/shared.go
@@ -118,6 +118,12 @@ func NewPg17Database(t testing.TB) (string, *sql.DB) {
return createPgDatabase(t, SharedPg17Container(t))
}
+// NewTLSPgDatabase is NewPgDatabase on the shared TLS PostgreSQL container.
+func NewTLSPgDatabase(t testing.TB) (string, *sql.DB) {
+ t.Helper()
+ return createPgDatabase(t, SharedTLSPgContainer(t))
+}
+
func createPgDatabase(t testing.TB, container *Container) (string, *sql.DB) {
t.Helper()
name := newDatabaseName()
diff --git a/backend/common/testcontainer/ssh.go b/backend/common/testcontainer/ssh.go
new file mode 100644
index 0000000000..82d685669a
--- /dev/null
+++ b/backend/common/testcontainer/ssh.go
@@ -0,0 +1,157 @@
+package testcontainer
+
+import (
+ "bytes"
+ "crypto/ed25519"
+ "crypto/rand"
+ "encoding/pem"
+ "io"
+ "net"
+ "strconv"
+ "sync"
+ "testing"
+
+ "github.com/pkg/errors"
+ "github.com/stretchr/testify/require"
+ "golang.org/x/crypto/ssh"
+)
+
+// SSHServer is an in-process SSH bastion for tests that reach a database
+// through an SSH tunnel. It runs in the test binary rather than in a container,
+// so the addresses it forwards to are the ones the test itself can dial.
+type SSHServer struct {
+ host string
+ port string
+ privateKey string
+}
+
+func (*SSHServer) GetUser() string { return "bytebase" }
+
+func (s *SSHServer) GetHost() string { return s.host }
+func (s *SSHServer) GetPort() string { return s.port }
+
+// GetPrivateKey is the PEM-encoded key the server accepts, for any user.
+func (s *SSHServer) GetPrivateKey() string { return s.privateKey }
+
+// NewSSHServer starts an SSH server that forwards TCP connections and stops it
+// when the test ends.
+func NewSSHServer(t testing.TB) *SSHServer {
+ t.Helper()
+
+ _, hostKey, err := ed25519.GenerateKey(rand.Reader)
+ require.NoError(t, err)
+ hostSigner, err := ssh.NewSignerFromKey(hostKey)
+ require.NoError(t, err)
+
+ clientPublicKey, clientKey, err := ed25519.GenerateKey(rand.Reader)
+ require.NoError(t, err)
+ authorizedKey, err := ssh.NewPublicKey(clientPublicKey)
+ require.NoError(t, err)
+ clientKeyPEM, err := ssh.MarshalPrivateKey(clientKey, "")
+ require.NoError(t, err)
+
+ config := &ssh.ServerConfig{
+ PublicKeyCallback: func(_ ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
+ if !bytes.Equal(key.Marshal(), authorizedKey.Marshal()) {
+ return nil, errors.New("unknown public key")
+ }
+ return nil, nil
+ },
+ }
+ config.AddHostKey(hostSigner)
+
+ host, port := NewTCPServer(t, func(conn net.Conn) { serveSSHConn(conn, config) })
+ return &SSHServer{host: host, port: port, privateKey: string(pem.EncodeToMemory(clientKeyPEM))}
+}
+
+// NewTCPServer listens on a loopback port and runs handle on each connection it
+// accepts. A connection stays open after handle returns, until the test ends,
+// so a handle that does nothing is a server that never answers.
+func NewTCPServer(t testing.TB, handle func(net.Conn)) (host, port string) {
+ t.Helper()
+
+ listener, err := net.Listen("tcp", "127.0.0.1:0")
+ require.NoError(t, err)
+ host, port, err = net.SplitHostPort(listener.Addr().String())
+ require.NoError(t, err)
+
+ var mu sync.Mutex
+ var conns []net.Conn
+ t.Cleanup(func() {
+ _ = listener.Close()
+ mu.Lock()
+ defer mu.Unlock()
+ for _, conn := range conns {
+ _ = conn.Close()
+ }
+ })
+
+ go func() {
+ for {
+ conn, err := listener.Accept()
+ if err != nil {
+ return
+ }
+ mu.Lock()
+ conns = append(conns, conn)
+ mu.Unlock()
+ go handle(conn)
+ }
+ }()
+ return host, port
+}
+
+func serveSSHConn(conn net.Conn, config *ssh.ServerConfig) {
+ _, channels, requests, err := ssh.NewServerConn(conn, config)
+ if err != nil {
+ return
+ }
+ go ssh.DiscardRequests(requests)
+ for newChannel := range channels {
+ if newChannel.ChannelType() != "direct-tcpip" {
+ _ = newChannel.Reject(ssh.UnknownChannelType, "only direct-tcpip is supported")
+ continue
+ }
+ go forwardSSHChannel(newChannel)
+ }
+}
+
+// forwardSSHChannel serves one direct-tcpip channel as sshd does: it dials the
+// requested address and closes both sides once either one ends.
+func forwardSSHChannel(newChannel ssh.NewChannel) {
+ // RFC 4254 section 7.2.
+ var request struct {
+ DestAddr string
+ DestPort uint32
+ OriginAddr string
+ OriginPort uint32
+ }
+ if err := ssh.Unmarshal(newChannel.ExtraData(), &request); err != nil {
+ _ = newChannel.Reject(ssh.ConnectionFailed, err.Error())
+ return
+ }
+ target, err := net.Dial("tcp", net.JoinHostPort(request.DestAddr, strconv.FormatUint(uint64(request.DestPort), 10)))
+ if err != nil {
+ _ = newChannel.Reject(ssh.ConnectionFailed, err.Error())
+ return
+ }
+ defer target.Close()
+
+ channel, requests, err := newChannel.Accept()
+ if err != nil {
+ return
+ }
+ defer channel.Close()
+ go ssh.DiscardRequests(requests)
+
+ done := make(chan struct{}, 2)
+ go func() {
+ _, _ = io.Copy(channel, target)
+ done <- struct{}{}
+ }()
+ go func() {
+ _, _ = io.Copy(target, channel)
+ done <- struct{}{}
+ }()
+ <-done
+}
diff --git a/backend/plugin/db/cockroachdb/cockroachdb.go b/backend/plugin/db/cockroachdb/cockroachdb.go
index 1aa46849e7..00d9a751aa 100644
--- a/backend/plugin/db/cockroachdb/cockroachdb.go
+++ b/backend/plugin/db/cockroachdb/cockroachdb.go
@@ -75,12 +75,8 @@ func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionC
}
d.sshClient = sshClient
- pgxConnConfig.DialFunc = func(_ context.Context, network, addr string) (net.Conn, error) {
- conn, err := sshClient.Dial(network, addr)
- if err != nil {
- return nil, err
- }
- return &util.NoDeadlineConn{Conn: conn}, nil
+ pgxConnConfig.DialFunc = func(ctx context.Context, network, addr string) (net.Conn, error) {
+ return util.DialSSHTunnel(ctx, sshClient, network, addr)
}
}
diff --git a/backend/plugin/db/cockroachdb/ssh_test.go b/backend/plugin/db/cockroachdb/ssh_test.go
new file mode 100644
index 0000000000..9eed618281
--- /dev/null
+++ b/backend/plugin/db/cockroachdb/ssh_test.go
@@ -0,0 +1,48 @@
+package cockroachdb
+
+import (
+ "context"
+ "net"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/db"
+)
+
+func TestPingThroughSSHTunnelReturnsWhenContextEnds(t *testing.T) {
+ t.Parallel()
+ sshServer := testcontainer.NewSSHServer(t)
+ // The server accepts the connection and never answers the startup message.
+ host, port := testcontainer.NewTCPServer(t, func(net.Conn) {})
+
+ background := context.Background()
+ driver, err := (&Driver{}).Open(background, storepb.Engine_COCKROACHDB, db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Host: host,
+ Port: port,
+ Username: "bytebase",
+ SshHost: sshServer.GetHost(),
+ SshPort: sshServer.GetPort(),
+ SshUser: sshServer.GetUser(),
+ SshPrivateKey: sshServer.GetPrivateKey(),
+ },
+ })
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = driver.Close(background) })
+
+ ctx, cancel := context.WithTimeout(background, 500*time.Millisecond)
+ defer cancel()
+ pinged := make(chan error, 1)
+ go func() { pinged <- driver.Ping(ctx) }()
+
+ select {
+ case err := <-pinged:
+ require.ErrorIs(t, err, context.DeadlineExceeded)
+ case <-time.After(10 * time.Second):
+ require.FailNow(t, "Ping was still blocked long after its context ended")
+ }
+}
diff --git a/backend/plugin/db/pg/cancel_test.go b/backend/plugin/db/pg/cancel_test.go
new file mode 100644
index 0000000000..14aa5184e4
--- /dev/null
+++ b/backend/plugin/db/pg/cancel_test.go
@@ -0,0 +1,125 @@
+package pg
+
+import (
+ "context"
+ "database/sql"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
+ "github.com/bytebase/bytebase/backend/plugin/db"
+)
+
+func TestQueryConnStopsServerQueryWhenContextEnds(t *testing.T) {
+ t.Parallel()
+
+ tests := []struct {
+ name string
+ sshTunnel bool
+ tls bool
+ // timeout is the context deadline; zero cancels the context instead.
+ timeout time.Duration
+ wantErr string
+ }{
+ {name: "direct, canceled", wantErr: "context canceled"},
+ {name: "direct, deadline exceeded", timeout: 2 * time.Second, wantErr: "context deadline exceeded"},
+ {name: "ssh tunnel, canceled", sshTunnel: true, wantErr: "context canceled"},
+ {name: "ssh tunnel, deadline exceeded", sshTunnel: true, timeout: 2 * time.Second, wantErr: "context deadline exceeded"},
+ {name: "ssh tunnel with TLS, canceled", sshTunnel: true, tls: true, wantErr: "context canceled"},
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+ var pgContainer *testcontainer.Container
+ var dbName string
+ var rawDB *sql.DB
+ if tc.tls {
+ pgContainer = testcontainer.SharedTLSPgContainer(t)
+ dbName, rawDB = testcontainer.NewTLSPgDatabase(t)
+ } else {
+ pgContainer = testcontainer.SharedPgContainer(t)
+ dbName, rawDB = testcontainer.NewPgDatabase(t)
+ }
+ dataSource := &storepb.DataSource{
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
+ Username: "postgres",
+ UseSsl: tc.tls,
+ }
+ if tc.sshTunnel {
+ sshServer := testcontainer.NewSSHServer(t)
+ dataSource.SshHost = sshServer.GetHost()
+ dataSource.SshPort = sshServer.GetPort()
+ dataSource.SshUser = sshServer.GetUser()
+ dataSource.SshPrivateKey = sshServer.GetPrivateKey()
+ }
+
+ background := context.Background()
+ driver, err := (&Driver{}).Open(background, storepb.Engine_POSTGRES, db.ConnectionConfig{
+ DataSource: dataSource,
+ Password: "root-password",
+ ConnectionContext: db.ConnectionContext{DatabaseName: dbName},
+ })
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = driver.Close(background) })
+
+ conn, err := driver.GetDB().Conn(background)
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = conn.Close() })
+ // Runs first, so a query that outlived its context cannot hold up the closes above.
+ t.Cleanup(func() {
+ _, err := rawDB.Exec(`SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = $1 AND pid <> pg_backend_pid()`, dbName)
+ require.NoError(t, err)
+ })
+
+ ctx, cancel := context.WithCancel(background)
+ if tc.timeout > 0 {
+ ctx, cancel = context.WithTimeout(background, tc.timeout)
+ }
+ defer cancel()
+
+ done := make(chan []*v1pb.QueryResult, 1)
+ go func() {
+ results, err := driver.QueryConn(ctx, conn, "SELECT pg_sleep(30);", db.QueryContext{MaximumSQLResultSize: 1 << 30})
+ if err != nil {
+ results = []*v1pb.QueryResult{{Error: err.Error()}}
+ }
+ done <- results
+ }()
+
+ const runningQueries = `state = 'active' AND query LIKE '%pg_sleep%'`
+ require.Eventually(t, func() bool { return countSessions(t, rawDB, dbName, runningQueries) == 1 }, 10*time.Second, 20*time.Millisecond,
+ "the query never started on the server")
+ if tc.timeout == 0 {
+ cancel()
+ }
+
+ select {
+ case results := <-done:
+ require.Len(t, results, 1)
+ require.Contains(t, results[0].Error, tc.wantErr)
+ case <-time.After(10 * time.Second):
+ require.FailNow(t, "QueryConn was still blocked 10s after the query started")
+ }
+ require.Eventually(t, func() bool { return countSessions(t, rawDB, dbName, runningQueries) == 0 }, 10*time.Second, 20*time.Millisecond,
+ "the query kept running on the server after its context ended")
+ require.Eventually(t, func() bool { return countSessions(t, rawDB, dbName, `application_name = 'bytebase'`) == 0 }, 10*time.Second, 20*time.Millisecond,
+ "the canceled connection stayed open on the server")
+
+ require.NoError(t, driver.Ping(background), "the driver must reconnect once the canceled connection is discarded")
+ })
+ }
+}
+
+// countSessions counts the sessions in the database, other than the one asking, that match the pg_stat_activity condition.
+func countSessions(t *testing.T, rawDB *sql.DB, dbName, condition string) int {
+ t.Helper()
+ var count int
+ err := rawDB.QueryRow(`SELECT count(*) FROM pg_stat_activity WHERE datname = $1 AND pid <> pg_backend_pid() AND `+condition, dbName).Scan(&count)
+ require.NoError(t, err)
+ return count
+}
diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.go
index a5780295fe..90d024c6f4 100644
--- a/backend/plugin/db/pg/pg.go
+++ b/backend/plugin/db/pg/pg.go
@@ -94,12 +94,8 @@ func (d *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Connectio
}
d.sshClient = sshClient
- pgxConnConfig.DialFunc = func(_ context.Context, network, addr string) (net.Conn, error) {
- conn, err := sshClient.Dial(network, addr)
- if err != nil {
- return nil, err
- }
- return &util.NoDeadlineConn{Conn: conn}, nil
+ pgxConnConfig.DialFunc = func(ctx context.Context, network, addr string) (net.Conn, error) {
+ return util.DialSSHTunnel(ctx, sshClient, network, addr)
}
}
diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.go
index 8a707f05dc..5b9fee3995 100644
--- a/backend/plugin/db/redshift/redshift.go
+++ b/backend/plugin/db/redshift/redshift.go
@@ -106,12 +106,8 @@ func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionC
}
d.sshClient = sshClient
- pgxConnConfig.DialFunc = func(_ context.Context, network, addr string) (net.Conn, error) {
- conn, err := sshClient.Dial(network, addr)
- if err != nil {
- return nil, err
- }
- return &util.NoDeadlineConn{Conn: conn}, nil
+ pgxConnConfig.DialFunc = func(ctx context.Context, network, addr string) (net.Conn, error) {
+ return util.DialSSHTunnel(ctx, sshClient, network, addr)
}
}
d.databaseName = config.ConnectionContext.DatabaseName
diff --git a/backend/plugin/db/redshift/ssh_test.go b/backend/plugin/db/redshift/ssh_test.go
new file mode 100644
index 0000000000..18339cb752
--- /dev/null
+++ b/backend/plugin/db/redshift/ssh_test.go
@@ -0,0 +1,48 @@
+package redshift
+
+import (
+ "context"
+ "net"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/db"
+)
+
+func TestPingThroughSSHTunnelReturnsWhenContextEnds(t *testing.T) {
+ t.Parallel()
+ sshServer := testcontainer.NewSSHServer(t)
+ // The server accepts the connection and never answers the startup message.
+ host, port := testcontainer.NewTCPServer(t, func(net.Conn) {})
+
+ background := context.Background()
+ driver, err := (&Driver{}).Open(background, storepb.Engine_REDSHIFT, db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Host: host,
+ Port: port,
+ Username: "bytebase",
+ SshHost: sshServer.GetHost(),
+ SshPort: sshServer.GetPort(),
+ SshUser: sshServer.GetUser(),
+ SshPrivateKey: sshServer.GetPrivateKey(),
+ },
+ })
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = driver.Close(background) })
+
+ ctx, cancel := context.WithTimeout(background, 500*time.Millisecond)
+ defer cancel()
+ pinged := make(chan error, 1)
+ go func() { pinged <- driver.Ping(ctx) }()
+
+ select {
+ case err := <-pinged:
+ require.ErrorIs(t, err, context.DeadlineExceeded)
+ case <-time.After(10 * time.Second):
+ require.FailNow(t, "Ping was still blocked long after its context ended")
+ }
+}
diff --git a/backend/plugin/db/util/ssh.go b/backend/plugin/db/util/ssh.go
index f5327208ad..16b356bf90 100644
--- a/backend/plugin/db/util/ssh.go
+++ b/backend/plugin/db/util/ssh.go
@@ -2,9 +2,12 @@
package util
import (
+ "context"
"fmt"
+ "io"
"net"
"os"
+ "sync"
"time"
"golang.org/x/crypto/ssh"
@@ -60,8 +63,79 @@ func init() {
}
}
+// NoDeadlineConn ignores deadlines, so a read or write blocked on it cannot be
+// interrupted. A driver that relies on deadlines dials with DialSSHTunnel.
type NoDeadlineConn struct{ net.Conn }
func (*NoDeadlineConn) SetDeadline(time.Time) error { return nil }
func (*NoDeadlineConn) SetReadDeadline(time.Time) error { return nil }
func (*NoDeadlineConn) SetWriteDeadline(time.Time) error { return nil }
+
+// DialSSHTunnel dials addr through client. Unlike the ssh channel it wraps, the
+// connection it returns honors deadlines and reports addr as its remote
+// address. A driver needs both to stop a query whose context ended: pgx sets a
+// deadline to release the blocked read, then dials RemoteAddr to send the
+// cancel request.
+func DialSSHTunnel(ctx context.Context, client *ssh.Client, network, addr string) (net.Conn, error) {
+ channel, err := client.DialContext(ctx, network, addr)
+ if err != nil {
+ return nil, err
+ }
+ return newSSHTunnelConn(channel, sshTunnelAddr{network: network, address: addr}), nil
+}
+
+// sshTunnelFlushTimeout bounds how long Close waits for a peer that stopped
+// reading to take the bytes Write already accepted.
+const sshTunnelFlushTimeout = time.Second
+
+// sshTunnelConn relays an ssh channel through a net.Pipe. The caller reads and
+// writes the near end of the pipe, which is what supplies the deadlines.
+type sshTunnelConn struct {
+ net.Conn
+ channel net.Conn
+ remote net.Addr
+
+ // flushed is closed once the channel has every accepted write and is closed.
+ flushed chan struct{}
+ closeOnce sync.Once
+}
+
+func newSSHTunnelConn(channel net.Conn, remote net.Addr) *sshTunnelConn {
+ near, far := net.Pipe()
+ c := &sshTunnelConn{Conn: near, channel: channel, remote: remote, flushed: make(chan struct{})}
+ go func() {
+ _, _ = io.Copy(far, channel)
+ _ = far.Close()
+ }()
+ go func() {
+ defer close(c.flushed)
+ _, _ = io.Copy(channel, far)
+ _ = far.Close()
+ _ = channel.Close()
+ }()
+ return c
+}
+
+func (c *sshTunnelConn) LocalAddr() net.Addr { return c.channel.LocalAddr() }
+func (c *sshTunnelConn) RemoteAddr() net.Addr { return c.remote }
+
+// Close closes the channel after the writes accepted so far, as closing a TCP
+// socket still sends what is buffered.
+func (c *sshTunnelConn) Close() error {
+ c.closeOnce.Do(func() {
+ _ = c.Conn.Close()
+ timer := time.NewTimer(sshTunnelFlushTimeout)
+ defer timer.Stop()
+ select {
+ case <-c.flushed:
+ case <-timer.C:
+ _ = c.channel.Close()
+ }
+ })
+ return nil
+}
+
+type sshTunnelAddr struct{ network, address string }
+
+func (a sshTunnelAddr) Network() string { return a.network }
+func (a sshTunnelAddr) String() string { return a.address }
diff --git a/backend/plugin/db/util/ssh_test.go b/backend/plugin/db/util/ssh_test.go
new file mode 100644
index 0000000000..8484422bda
--- /dev/null
+++ b/backend/plugin/db/util/ssh_test.go
@@ -0,0 +1,188 @@
+package util
+
+import (
+ "context"
+ "crypto/rand"
+ "io"
+ "net"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/require"
+ "golang.org/x/crypto/ssh"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+)
+
+func newSSHClient(t *testing.T) *ssh.Client {
+ t.Helper()
+ sshServer := testcontainer.NewSSHServer(t)
+ client, err := GetSSHClient(&storepb.DataSource{
+ SshHost: sshServer.GetHost(),
+ SshPort: sshServer.GetPort(),
+ SshUser: sshServer.GetUser(),
+ SshPrivateKey: sshServer.GetPrivateKey(),
+ })
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = client.Close() })
+ return client
+}
+
+// openTunnel returns a tunneled connection to a TCP server that runs handle
+// on it, with the address the tunnel was asked to reach.
+func openTunnel(t *testing.T, handle func(net.Conn)) (net.Conn, string) {
+ t.Helper()
+ addr := net.JoinHostPort(testcontainer.NewTCPServer(t, handle))
+ conn, err := DialSSHTunnel(context.Background(), newSSHClient(t), "tcp", addr)
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = conn.Close() })
+ return conn, addr
+}
+
+func silent(net.Conn) {}
+
+func echo(conn net.Conn) { _, _ = io.Copy(conn, conn) }
+
+// within fails the test unless f returns in time, so a call the connection
+// fails to interrupt reads as a failure rather than a hung test binary.
+func within[T any](t *testing.T, timeout time.Duration, f func() T) T {
+ t.Helper()
+ done := make(chan T, 1)
+ go func() { done <- f() }()
+ select {
+ case v := <-done:
+ return v
+ case <-time.After(timeout):
+ require.FailNow(t, "the call was still blocked", "after %s", timeout)
+ var zero T
+ return zero
+ }
+}
+
+func requireTimeout(t *testing.T, err error) {
+ t.Helper()
+ var netErr net.Error
+ require.ErrorAs(t, err, &netErr)
+ require.True(t, netErr.Timeout(), "want a timeout, got %v", err)
+}
+
+func TestSSHTunnelConnRelaysBothDirections(t *testing.T) {
+ t.Parallel()
+ conn, _ := openTunnel(t, echo)
+
+ // Larger than the ssh channel window, so the relay has to keep pace in both directions.
+ sent := make([]byte, 4<<20)
+ _, err := rand.Read(sent)
+ require.NoError(t, err)
+ go func() { _, _ = conn.Write(sent) }()
+
+ received := make([]byte, len(sent))
+ err = within(t, 30*time.Second, func() error {
+ _, err := io.ReadFull(conn, received)
+ return err
+ })
+ require.NoError(t, err)
+ require.Equal(t, sent, received)
+}
+
+func TestSSHTunnelConnReportsDialedAddress(t *testing.T) {
+ t.Parallel()
+ conn, addr := openTunnel(t, silent)
+
+ require.Equal(t, "tcp", conn.RemoteAddr().Network())
+ require.Equal(t, addr, conn.RemoteAddr().String())
+}
+
+func TestSSHTunnelConnReadDeadlineInterruptsBlockedRead(t *testing.T) {
+ t.Parallel()
+ conn, _ := openTunnel(t, echo)
+
+ require.NoError(t, conn.SetReadDeadline(time.Now().Add(100*time.Millisecond)))
+ err := within(t, 10*time.Second, func() error {
+ _, err := conn.Read(make([]byte, 1))
+ return err
+ })
+ requireTimeout(t, err)
+
+ require.NoError(t, conn.SetReadDeadline(time.Time{}))
+ _, err = conn.Write([]byte("ping"))
+ require.NoError(t, err)
+ reply := make([]byte, 4)
+ _, err = io.ReadFull(conn, reply)
+ require.NoError(t, err, "the connection must stay usable once the deadline is cleared")
+ require.Equal(t, "ping", string(reply))
+}
+
+func TestSSHTunnelConnWriteDeadlineInterruptsBlockedWrite(t *testing.T) {
+ t.Parallel()
+ conn, _ := openTunnel(t, silent)
+
+ require.NoError(t, conn.SetWriteDeadline(time.Now().Add(500*time.Millisecond)))
+ chunk := make([]byte, 1<<20)
+ err := within(t, 30*time.Second, func() error {
+ // The peer never reads, so the write blocks once the buffers on the way to it fill up.
+ for {
+ if _, err := conn.Write(chunk); err != nil {
+ return err
+ }
+ }
+ })
+ requireTimeout(t, err)
+}
+
+func TestSSHTunnelConnCloseInterruptsBlockedRead(t *testing.T) {
+ t.Parallel()
+ conn, _ := openTunnel(t, silent)
+
+ readErr := make(chan error, 1)
+ go func() {
+ _, err := conn.Read(make([]byte, 1))
+ readErr <- err
+ }()
+ require.NoError(t, conn.Close())
+ require.Error(t, within(t, 10*time.Second, func() error { return <-readErr }))
+}
+
+func TestSSHTunnelConnCloseDeliversAcceptedWrites(t *testing.T) {
+ t.Parallel()
+ received := make(chan []byte, 1)
+ conn, _ := openTunnel(t, func(conn net.Conn) {
+ data, _ := io.ReadAll(conn)
+ received <- data
+ })
+
+ sent := make([]byte, 256<<10)
+ _, err := rand.Read(sent)
+ require.NoError(t, err)
+ _, err = conn.Write(sent)
+ require.NoError(t, err)
+ require.NoError(t, conn.Close())
+
+ require.Equal(t, sent, within(t, 10*time.Second, func() []byte { return <-received }),
+ "the peer must see every accepted byte, then the end of the stream")
+}
+
+func TestSSHTunnelConnReadsEOFWhenPeerCloses(t *testing.T) {
+ t.Parallel()
+ conn, _ := openTunnel(t, func(conn net.Conn) {
+ _, _ = conn.Write([]byte("bye"))
+ _ = conn.Close()
+ })
+
+ var data []byte
+ err := within(t, 10*time.Second, func() (err error) {
+ data, err = io.ReadAll(conn)
+ return err
+ })
+ require.NoError(t, err)
+ require.Equal(t, "bye", string(data))
+}
+
+func TestDialSSHTunnelReturnsWhenContextEnds(t *testing.T) {
+ t.Parallel()
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+ _, err := DialSSHTunnel(ctx, newSSHClient(t), "tcp", net.JoinHostPort(testcontainer.NewTCPServer(t, silent)))
+ require.ErrorIs(t, err, context.Canceled)
+}