From 8ffe0b4013607567c6414b8d6317856f72424450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Thu, 10 Aug 2023 17:20:02 +0200 Subject: [PATCH 1/2] Don't delete local socket when reverse forward MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When we are forwarding a local socket, we should not delete it. Doing so would interrupt the local service, if delete succeeds. Signed-off-by: Anders F Björklund --- pkg/hostagent/hostagent.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/pkg/hostagent/hostagent.go b/pkg/hostagent/hostagent.go index e17b280b6a7..57a6e3abbd6 100644 --- a/pkg/hostagent/hostagent.go +++ b/pkg/hostagent/hostagent.go @@ -597,9 +597,9 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, logrus.Infof("Forwarding %q (host) to %q (guest)", local, remote) } else { logrus.Infof("Forwarding %q (guest) to %q (host)", remote, local) - } - if err := os.RemoveAll(local); err != nil { - logrus.WithError(err).Warnf("Failed to clean up %q (host) before setting up forwarding", local) + if err := os.RemoveAll(local); err != nil { + logrus.WithError(err).Warnf("Failed to clean up %q (host) before setting up forwarding", local) + } } if err := os.MkdirAll(filepath.Dir(local), 0750); err != nil { return fmt.Errorf("can't create directory for local socket %q: %w", local, err) @@ -609,12 +609,12 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, logrus.Infof("Stopping forwarding %q (host) to %q (guest)", local, remote) } else { logrus.Infof("Stopping forwarding %q (guest) to %q (host)", remote, local) + defer func() { + if err := os.RemoveAll(local); err != nil { + logrus.WithError(err).Warnf("Failed to clean up %q (host) after stopping forwarding", local) + } + }() } - defer func() { - if err := os.RemoveAll(local); err != nil { - logrus.WithError(err).Warnf("Failed to clean up %q (host) after stopping forwarding", local) - } - }() default: panic(fmt.Errorf("invalid verb %q", verb)) } @@ -622,9 +622,11 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, cmd := exec.CommandContext(ctx, sshConfig.Binary(), args...) if out, err := cmd.Output(); err != nil { if verb == verbForward && strings.HasPrefix(local, "/") { - logrus.WithError(err).Warnf("Failed to set up forward from %q (guest) to %q (host)", remote, local) - if removeErr := os.RemoveAll(local); err != nil { - logrus.WithError(removeErr).Warnf("Failed to clean up %q (host) after forwarding failed", local) + if !reverse { + logrus.WithError(err).Warnf("Failed to set up forward from %q (guest) to %q (host)", remote, local) + if removeErr := os.RemoveAll(local); err != nil { + logrus.WithError(removeErr).Warnf("Failed to clean up %q (host) after forwarding failed", local) + } } } return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err) From 05cff79b8a1d7eddf8833494b4bfc81721f9fcaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Thu, 10 Aug 2023 17:59:20 +0200 Subject: [PATCH 2/2] Do remove remote socket when reverse forward MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders F Björklund --- pkg/hostagent/hostagent.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/pkg/hostagent/hostagent.go b/pkg/hostagent/hostagent.go index 57a6e3abbd6..9296ff48792 100644 --- a/pkg/hostagent/hostagent.go +++ b/pkg/hostagent/hostagent.go @@ -568,6 +568,21 @@ const ( verbCancel = "cancel" ) +func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command ...string) error { + args := sshConfig.Args() + args = append(args, + "-p", strconv.Itoa(port), + "127.0.0.1", + "--", + ) + args = append(args, command...) + cmd := exec.CommandContext(ctx, sshConfig.Binary(), args...) + if out, err := cmd.Output(); err != nil { + return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err) + } + return nil +} + func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote string, verb string, reverse bool) error { args := sshConfig.Args() args = append(args, @@ -595,6 +610,9 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, case verbForward: if reverse { logrus.Infof("Forwarding %q (host) to %q (guest)", local, remote) + if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil { + logrus.WithError(err).Warnf("Failed to clean up %q (guest) before setting up forwarding", remote) + } } else { logrus.Infof("Forwarding %q (guest) to %q (host)", remote, local) if err := os.RemoveAll(local); err != nil { @@ -607,6 +625,9 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, case verbCancel: if reverse { logrus.Infof("Stopping forwarding %q (host) to %q (guest)", local, remote) + if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil { + logrus.WithError(err).Warnf("Failed to clean up %q (guest) after stopping forwarding", remote) + } } else { logrus.Infof("Stopping forwarding %q (guest) to %q (host)", remote, local) defer func() { @@ -622,7 +643,12 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, cmd := exec.CommandContext(ctx, sshConfig.Binary(), args...) if out, err := cmd.Output(); err != nil { if verb == verbForward && strings.HasPrefix(local, "/") { - if !reverse { + if reverse { + logrus.WithError(err).Warnf("Failed to set up forward from %q (host) to %q (guest)", local, remote) + if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil { + logrus.WithError(err).Warnf("Failed to clean up %q (guest) after forwarding failed", remote) + } + } else { logrus.WithError(err).Warnf("Failed to set up forward from %q (guest) to %q (host)", remote, local) if removeErr := os.RemoveAll(local); err != nil { logrus.WithError(removeErr).Warnf("Failed to clean up %q (host) after forwarding failed", local)