Skip to content

Commit

Permalink
Don't delete local socket when reverse forward
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
afbjorklund committed Aug 10, 2023
1 parent 8fbc88a commit 44fac1d
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions pkg/hostagent/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -609,22 +609,24 @@ 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))
}
}
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)
Expand Down

0 comments on commit 44fac1d

Please sign in to comment.