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

Added ability for RequestSubsystem to be cancelled. #3456

Merged
merged 1 commit into from
Mar 20, 2020
Merged
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
30 changes: 29 additions & 1 deletion lib/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,28 @@ func (n *NodeAddr) ProxyFormat() string {
return strings.Join(parts, "@")
}

// requestSubsystem sends a subsystem request on the session. If the passed
// in context is canceled first, unblocks.
func requestSubsystem(ctx context.Context, session *ssh.Session, name string) error {
errCh := make(chan error)

go func() {
er := session.RequestSubsystem(name)
errCh <- er
}()

select {
case err := <-errCh:
return trace.Wrap(err)
case <-ctx.Done():
err := session.Close()
if err != nil {
log.Debugf("Failed to close session: %v.", err)
}
return trace.Wrap(ctx.Err())
}
}

// ConnectToNode connects to the ssh server via Proxy.
// It returns connected and authenticated NodeClient
func (proxy *ProxyClient) ConnectToNode(ctx context.Context, nodeAddress NodeAddr, user string, quiet bool) (*NodeClient, error) {
Expand Down Expand Up @@ -610,8 +632,14 @@ func (proxy *ProxyClient) ConnectToNode(ctx context.Context, nodeAddress NodeAdd
}
}

err = proxySession.RequestSubsystem("proxy:" + nodeAddress.ProxyFormat())
err = requestSubsystem(ctx, proxySession, "proxy:"+nodeAddress.ProxyFormat())
if err != nil {
// If the user pressed Ctrl-C, no need to try and read the error from
// the proxy, return an error right away.
if trace.Unwrap(err) == context.Canceled {
return nil, trace.Wrap(err)
}

// read the stderr output from the failed SSH session and append
// it to the end of our own message:
serverErrorMsg, _ := ioutil.ReadAll(proxyErr)
Expand Down