Skip to content

Commit

Permalink
ssh: Recreate connection on retries in setupProxy
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Christophe Fergeau <[email protected]>
  • Loading branch information
cfergeau committed Jan 15, 2024
1 parent 8912b78 commit feda120
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions pkg/sshclient/ssh_forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit feda120

Please sign in to comment.