From 838fc24d3e1f282e100017fdde86eee5e093aa56 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Mon, 29 Jun 2026 13:20:45 +0200 Subject: [PATCH] machine: simplify ssh debug output logging When the command output is logged we do not need an extra pipe and scanner to read the output line by line, we can just log it directly via a writer. Now this of course does loose the line by line parsing of the scanner but IMO this is better as it is simpler and less code to worry about. Signed-off-by: Paul Holzinger --- pkg/machine/ssh.go | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/pkg/machine/ssh.go b/pkg/machine/ssh.go index 7f4988a403..4466b20733 100644 --- a/pkg/machine/ssh.go +++ b/pkg/machine/ssh.go @@ -1,7 +1,6 @@ package machine import ( - "bufio" "fmt" "io" "os" @@ -61,6 +60,13 @@ func LocalhostSSHCopy(username, identityPath string, sshPort int, srcPath, destP return cmd.Run() } +type sshDebugLogger struct{} + +func (w *sshDebugLogger) Write(p []byte) (int, error) { + logrus.Debugf("ssh output: %s", string(p)) + return len(p), nil +} + func localhostBuiltinSSH(username, identityPath, name string, sshPort int, inputArgs []string, passOutput bool, stdin io.Reader) error { config, err := createLocalhostConfig(username, identityPath) // WARNING: This MUST NOT be generalized to allow communication over untrusted networks. if err != nil { @@ -86,40 +92,14 @@ func localhostBuiltinSSH(username, identityPath, name string, sshPort int, input session.Stdout = os.Stdout session.Stderr = os.Stderr } else if logrus.IsLevelEnabled(logrus.DebugLevel) { - return runSessionWithDebug(session, cmd) + logger := &sshDebugLogger{} + session.Stdout = logger + session.Stderr = logger } return session.Run(cmd) } -func runSessionWithDebug(session *ssh.Session, cmd string) error { - outPipe, err := session.StdoutPipe() - if err != nil { - return err - } - errPipe, err := session.StderrPipe() - if err != nil { - return err - } - logOuput := func(pipe io.Reader, done chan struct{}) { - scanner := bufio.NewScanner(pipe) - for scanner.Scan() { - logrus.Debugf("ssh output: %s", scanner.Text()) - } - done <- struct{}{} - } - if err := session.Start(cmd); err != nil { - return err - } - completed := make(chan struct{}, 2) - go logOuput(outPipe, completed) - go logOuput(errPipe, completed) - <-completed - <-completed - - return session.Wait() -} - // createLocalhostConfig returns a *ssh.ClientConfig for authenticating a user using a private key // // WARNING: This MUST NOT be used to communicate over untrusted networks.