Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle cancelation while establishing ssh connection #832

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/ssh_terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ func sshConnect(p *SSHParams, addr string) error {
defer endSpin()
}

if err := sshClient.Connect(context.Background()); err != nil {
ctx := p.Ctx.Command.Context()
if err := sshClient.Connect(ctx); err != nil {
return errors.Wrap(err, "error connecting to SSH server")
}
defer sshClient.Close()
Expand All @@ -242,7 +243,7 @@ func sshConnect(p *SSHParams, addr string) error {
Mode: "xterm",
}

if err := sshClient.Shell(context.Background(), term, p.Cmd); err != nil {
if err := sshClient.Shell(ctx, term, p.Cmd); err != nil {
return errors.Wrap(err, "ssh shell")
}

Expand Down
39 changes: 33 additions & 6 deletions pkg/ssh/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func (c *Client) Close() error {
return nil
}

type connResp struct {
err error
conn ssh.Conn
client *ssh.Client
}

func (c *Client) Connect(ctx context.Context) error {
pubKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(c.Certificate))
if err != nil {
Expand Down Expand Up @@ -67,13 +73,34 @@ func (c *Client) Connect(ctx context.Context) error {
HostKeyAlgorithms: []string{ssh.KeyAlgoED25519},
}

conn, chans, reqs, err := ssh.NewClientConn(tcpConn, tcpConn.RemoteAddr().String(), conf)
if err != nil {
return err
}
respCh := make(chan connResp)

c.conn, c.client = conn, ssh.NewClient(conn, chans, reqs)
return nil
// ssh.NewClientConn doesn't take a context, so we need to handle cancelation on our end
go func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'd be far simpler instead of launching this in the background, and since you already have a reference to the underlying connection, to just close it on context cancellation instead of this.

conn, chans, reqs, err := ssh.NewClientConn(tcpConn, tcpConn.RemoteAddr().String(), conf)
if err != nil {
respCh <- connResp{err: err}
return
}

client := ssh.NewClient(conn, chans, reqs)

respCh <- connResp{nil, conn, client}
}()

for {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you looping?

select {
case <-ctx.Done():
return ctx.Err()
case resp := <-respCh:
if resp.err != nil {
return resp.err
}
c.conn = resp.conn
c.client = resp.client
return nil
}
}
}

func (c *Client) Shell(ctx context.Context, term *Terminal, cmd string) error {
Expand Down