Skip to content

Commit

Permalink
Ensure the correct stderr is used for ssh sessions (#30684)
Browse files Browse the repository at this point in the history
Sessions were always copying the remote session standard error to
os.Stderr instead of the `TeleportClient.Stderr` which prevented
error messages from being seen in the web ui.

Fixes #30621
  • Loading branch information
rosstimothy committed Aug 21, 2023
1 parent 2814407 commit c14e079
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/client/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func (ns *NodeSession) allocateTerminal(ctx context.Context, termType string, s
go ns.updateTerminalSize(ctx, s)
}
go func() {
if _, err := io.Copy(os.Stderr, stderr); err != nil {
if _, err := io.Copy(ns.nodeClient.TC.Stderr, stderr); err != nil {
log.Debugf("Error reading remote STDERR: %v", err)
}
}()
Expand Down Expand Up @@ -551,7 +551,7 @@ func (ns *NodeSession) runCommand(ctx context.Context, mode types.SessionPartici
// fallback to non-interactive mode
if interactive && !ns.terminal.IsAttached() {
interactive = false
fmt.Fprintf(os.Stderr, "TTY will not be allocated on the server because stdin is not a terminal\n")
fmt.Fprintf(ns.nodeClient.TC.Stderr, "TTY will not be allocated on the server because stdin is not a terminal\n")
}

// Start a interactive session ("exec" request with a TTY).
Expand Down
11 changes: 10 additions & 1 deletion lib/web/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,15 @@ func (t *TerminalHandler) handler(ws *websocket.Conn, r *http.Request) {
t.log.Debug("Closing websocket stream")
}

type stderrWriter struct {
stream *TerminalStream
}

func (s stderrWriter) Write(b []byte) (int, error) {
s.stream.writeError(string(b))
return len(b), nil
}

// makeClient builds a *client.TeleportClient for the connection.
func (t *TerminalHandler) makeClient(ctx context.Context, stream *TerminalStream, clientAddr string) (*client.TeleportClient, error) {
ctx, span := tracing.DefaultProvider().Tracer("terminal").Start(ctx, "terminal/makeClient")
Expand All @@ -421,7 +430,7 @@ func (t *TerminalHandler) makeClient(ctx context.Context, stream *TerminalStream
clientConfig.ForwardAgent = client.ForwardAgentLocal
clientConfig.Namespace = apidefaults.Namespace
clientConfig.Stdout = stream
clientConfig.Stderr = stream
clientConfig.Stderr = stderrWriter{stream: stream}
clientConfig.Stdin = stream
clientConfig.SiteName = t.sessionData.ClusterName
if err := clientConfig.ParseProxyHost(t.proxyHostPort); err != nil {
Expand Down

0 comments on commit c14e079

Please sign in to comment.