From d8f4d70d7b8955b1f971993460bc3eca6dc124fa Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Fri, 12 Jan 2024 11:21:09 -0500 Subject: [PATCH] ssh: Recreate connection on retries in setupProxy The previous fix was not working as expected, as the ssh go code will close the underlying connection when there's a failure. This was causing the retries for CreateBastion() to fail, as after the first failure it would try to use a closed connection. This commit recreates the connection each time before calling CreateBastion() to fix this. This also simplifies the code. --- pkg/sshclient/ssh_forwarder.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/pkg/sshclient/ssh_forwarder.go b/pkg/sshclient/ssh_forwarder.go index 75c43991d..b994f24d9 100644 --- a/pkg/sshclient/ssh_forwarder.go +++ b/pkg/sshclient/ssh_forwarder.go @@ -166,12 +166,11 @@ func setupProxy(ctx context.Context, socketURI *url.URL, dest *url.URL, identity return conn, err } - conn, err := initialConnection(ctx, connectFunc) - if err != nil { - return &SSHForward{}, err - } - createBastion := func() (*Bastion, error) { + conn, err := connectFunc(ctx, nil) + if err != nil { + return nil, err + } return CreateBastion(dest, passphrase, identity, conn, connectFunc) } bastion, err := retry(ctx, createBastion, "Waiting for sshd") @@ -215,13 +214,6 @@ loop: return returnVal, fmt.Errorf("timeout: %w", err) } -func initialConnection(ctx context.Context, connectFunc ConnectCallback) (net.Conn, error) { - retryFunc := func() (net.Conn, error) { - return connectFunc(ctx, nil) - } - return retry(ctx, retryFunc, "Waiting for sshd socket") -} - func acceptConnection(ctx context.Context, listener net.Listener, bastion *Bastion, socketURI *url.URL) error { con, err := listener.Accept() if err != nil {