Skip to content

Commit 44cc362

Browse files
committed
pkg/hostagent: Update all ssh execution to support SSH address other than "127.0.0.1"
Split from #4175 Fixes #4240 affected functions: - Copy to host - Reverse SSHFS - SSH port forwarding Signed-off-by: Norio Nomura <[email protected]> pkg/hostagent: Add `HostAgent.sshAddressPort()` Signed-off-by: Norio Nomura <[email protected]>
1 parent 280f3b9 commit 44cc362

File tree

6 files changed

+64
-49
lines changed

6 files changed

+64
-49
lines changed

pkg/hostagent/hostagent.go

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
244244
instName: instName,
245245
instSSHAddress: inst.SSHAddress,
246246
sshConfig: sshConfig,
247-
portForwarder: newPortForwarder(sshConfig, sshLocalPort, rules, ignoreTCP, inst.VMType),
248247
grpcPortForwarder: portfwd.NewPortForwarder(rules, ignoreTCP, ignoreUDP),
249248
driver: limaDriver,
250249
signalCh: signalCh,
@@ -254,6 +253,7 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
254253
guestAgentAliveCh: make(chan struct{}),
255254
showProgress: o.showProgress,
256255
}
256+
a.portForwarder = newPortForwarder(sshConfig, a.sshAddressPort, rules, ignoreTCP, inst.VMType)
257257
return a, nil
258258
}
259259

@@ -483,6 +483,12 @@ func (a *HostAgent) Info(_ context.Context) (*hostagentapi.Info, error) {
483483
return info, nil
484484
}
485485

486+
func (a *HostAgent) sshAddressPort() (sshAddress string, sshPort int) {
487+
sshAddress = a.instSSHAddress
488+
sshPort = a.sshLocalPort
489+
return sshAddress, sshPort
490+
}
491+
486492
func (a *HostAgent) startHostAgentRoutines(ctx context.Context) error {
487493
if *a.instConfig.Plain {
488494
msg := "Running in plain mode. Mounts, dynamic port forwarding, containerd, etc. will be ignored. Guest agent will not be running."
@@ -589,7 +595,8 @@ sudo chown -R "${USER}" /run/host-services`
589595
}
590596
// Copy all config files _after_ the requirements are done
591597
for _, rule := range a.instConfig.CopyToHost {
592-
if err := copyToHost(ctx, a.sshConfig, a.sshLocalPort, rule.HostFile, rule.GuestFile); err != nil {
598+
sshAddress, sshPort := a.sshAddressPort()
599+
if err := copyToHost(ctx, a.sshConfig, sshAddress, sshPort, rule.HostFile, rule.GuestFile); err != nil {
593600
errs = append(errs, err)
594601
}
595602
}
@@ -636,10 +643,11 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
636643
// Setup all socket forwards and defer their teardown
637644
if !(a.driver.Info().Features.SkipSocketForwarding) {
638645
logrus.Debugf("Forwarding unix sockets")
646+
sshAddress, sshPort := a.sshAddressPort()
639647
for _, rule := range a.instConfig.PortForwards {
640648
if rule.GuestSocket != "" {
641649
local := hostAddress(rule, &guestagentapi.IPPort{})
642-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
650+
_ = forwardSSH(ctx, a.sshConfig, sshAddress, sshPort, local, rule.GuestSocket, verbForward, rule.Reverse)
643651
}
644652
}
645653
}
@@ -650,17 +658,18 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
650658
a.cleanUp(func() error {
651659
logrus.Debugf("Stop forwarding unix sockets")
652660
var errs []error
661+
sshAddress, sshPort := a.sshAddressPort()
653662
for _, rule := range a.instConfig.PortForwards {
654663
if rule.GuestSocket != "" {
655664
local := hostAddress(rule, &guestagentapi.IPPort{})
656665
// using ctx.Background() because ctx has already been cancelled
657-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
666+
if err := forwardSSH(context.Background(), a.sshConfig, sshAddress, sshPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
658667
errs = append(errs, err)
659668
}
660669
}
661670
}
662671
if a.driver.ForwardGuestAgent() {
663-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbCancel, false); err != nil {
672+
if err := forwardSSH(context.Background(), a.sshConfig, sshAddress, sshPort, localUnix, remoteUnix, verbCancel, false); err != nil {
664673
errs = append(errs, err)
665674
}
666675
}
@@ -671,7 +680,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
671680
if a.instConfig.MountInotify != nil && *a.instConfig.MountInotify {
672681
if a.client == nil || !isGuestAgentSocketAccessible(ctx, a.client) {
673682
if a.driver.ForwardGuestAgent() {
674-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
683+
sshAddress, sshPort := a.sshAddressPort()
684+
_ = forwardSSH(ctx, a.sshConfig, sshAddress, sshPort, localUnix, remoteUnix, verbForward, false)
675685
}
676686
}
677687
err := a.startInotify(ctx)
@@ -687,7 +697,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
687697
for {
688698
if a.client == nil || !isGuestAgentSocketAccessible(ctx, a.client) {
689699
if a.driver.ForwardGuestAgent() {
690-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
700+
sshAddress, sshPort := a.sshAddressPort()
701+
_ = forwardSSH(ctx, a.sshConfig, sshAddress, sshPort, localUnix, remoteUnix, verbForward, false)
691702
}
692703
}
693704
client, err := a.getOrCreateClient(ctx)
@@ -711,6 +722,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
711722
}
712723

713724
func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPortForwards []limatype.PortForward) {
725+
sshAddress, sshPort := a.sshAddressPort()
714726
for _, rule := range staticPortForwards {
715727
if rule.GuestSocket == "" {
716728
guest := &guestagentapi.IPPort{
@@ -721,7 +733,7 @@ func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPor
721733
local, remote := a.portForwarder.forwardingAddresses(guest)
722734
if local != "" {
723735
logrus.Infof("Setting up static TCP forwarding from %s to %s", remote, local)
724-
if err := forwardTCP(ctx, a.sshConfig, a.sshLocalPort, local, remote, verbForward); err != nil {
736+
if err := forwardTCP(ctx, a.sshConfig, sshAddress, sshPort, local, remote, verbForward); err != nil {
725737
logrus.WithError(err).Warnf("failed to set up static TCP forwarding %s -> %s", remote, local)
726738
}
727739
}
@@ -832,11 +844,11 @@ const (
832844
verbCancel = "cancel"
833845
)
834846

835-
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command ...string) error {
847+
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, command ...string) error {
836848
args := sshConfig.Args()
837849
args = append(args,
838-
"-p", strconv.Itoa(port),
839-
"127.0.0.1",
850+
"-p", strconv.Itoa(sshPort),
851+
sshAddress,
840852
"--",
841853
)
842854
args = append(args, command...)
@@ -847,7 +859,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
847859
return nil
848860
}
849861

850-
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string, reverse bool) error {
862+
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote, verb string, reverse bool) error {
851863
args := sshConfig.Args()
852864
args = append(args,
853865
"-T",
@@ -865,16 +877,16 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
865877
args = append(args,
866878
"-N",
867879
"-f",
868-
"-p", strconv.Itoa(port),
869-
"127.0.0.1",
880+
"-p", strconv.Itoa(sshPort),
881+
sshAddress,
870882
"--",
871883
)
872884
if strings.HasPrefix(local, "/") {
873885
switch verb {
874886
case verbForward:
875887
if reverse {
876888
logrus.Infof("Forwarding %q (host) to %q (guest)", local, remote)
877-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
889+
if err := executeSSH(ctx, sshConfig, sshAddress, sshPort, "rm", "-f", remote); err != nil {
878890
logrus.WithError(err).Warnf("Failed to clean up %q (guest) before setting up forwarding", remote)
879891
}
880892
} else {
@@ -889,7 +901,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
889901
case verbCancel:
890902
if reverse {
891903
logrus.Infof("Stopping forwarding %q (host) to %q (guest)", local, remote)
892-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
904+
if err := executeSSH(ctx, sshConfig, sshAddress, sshPort, "rm", "-f", remote); err != nil {
893905
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after stopping forwarding", remote)
894906
}
895907
} else {
@@ -910,7 +922,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
910922
if verb == verbForward && strings.HasPrefix(local, "/") {
911923
if reverse {
912924
logrus.WithError(err).Warnf("Failed to set up forward from %q (host) to %q (guest)", local, remote)
913-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
925+
if err := executeSSH(ctx, sshConfig, sshAddress, sshPort, "rm", "-f", remote); err != nil {
914926
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after forwarding failed", remote)
915927
}
916928
} else {
@@ -944,10 +956,11 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
944956
Active: true,
945957
})
946958

959+
sshAddress, sshPort := a.sshAddressPort()
947960
args := a.sshConfig.Args()
948961
args = append(args,
949-
"-p", strconv.Itoa(a.sshLocalPort),
950-
"127.0.0.1",
962+
"-p", strconv.Itoa(sshPort),
963+
sshAddress,
951964
"sh", "-c",
952965
`"if command -v systemctl >/dev/null 2>&1 && systemctl is-enabled -q cloud-init-main.service; then
953966
sudo journalctl -u cloud-init-main.service -b -S @0 -o cat -f
@@ -1032,8 +1045,8 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
10321045

10331046
finalArgs := a.sshConfig.Args()
10341047
finalArgs = append(finalArgs,
1035-
"-p", strconv.Itoa(a.sshLocalPort),
1036-
"127.0.0.1",
1048+
"-p", strconv.Itoa(sshPort),
1049+
sshAddress,
10371050
"sudo", "tail", "-n", "20", "/var/log/cloud-init-output.log",
10381051
)
10391052

@@ -1073,11 +1086,11 @@ func isDeactivatedCloudInitMainService(line string) bool {
10731086
return strings.HasPrefix(line, "cloud-init-main.service: consumed")
10741087
}
10751088

1076-
func copyToHost(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote string) error {
1089+
func copyToHost(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote string) error {
10771090
args := sshConfig.Args()
10781091
args = append(args,
1079-
"-p", strconv.Itoa(port),
1080-
"127.0.0.1",
1092+
"-p", strconv.Itoa(sshPort),
1093+
sshAddress,
10811094
"--",
10821095
)
10831096
args = append(args,

pkg/hostagent/mount.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ func (a *HostAgent) setupMount(ctx context.Context, m limatype.Mount) (*mount, e
6161
}
6262
}
6363

64+
sshAddress, sshPort := a.sshAddressPort()
6465
rsf := &reversesshfs.ReverseSSHFS{
6566
Driver: *m.SSHFS.SFTPDriver,
6667
SSHConfig: a.sshConfig,
6768
LocalPath: resolvedLocation,
68-
Host: "127.0.0.1",
69-
Port: a.sshLocalPort,
69+
Host: sshAddress,
70+
Port: sshPort,
7071
RemotePath: *m.MountPoint,
7172
Readonly: !(*m.Writable),
7273
SSHFSAdditionalArgs: []string{"-o", sshfsOptions},

pkg/hostagent/port.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ import (
1616
)
1717

1818
type portForwarder struct {
19-
sshConfig *ssh.SSHConfig
20-
sshHostPort int
21-
rules []limatype.PortForward
22-
ignore bool
23-
vmType limatype.VMType
19+
sshConfig *ssh.SSHConfig
20+
sshAddressPort func() (string, int)
21+
rules []limatype.PortForward
22+
ignore bool
23+
vmType limatype.VMType
2424
}
2525

2626
const sshGuestPort = 22
2727

2828
var IPv4loopback1 = limayaml.IPv4loopback1
2929

30-
func newPortForwarder(sshConfig *ssh.SSHConfig, sshHostPort int, rules []limatype.PortForward, ignore bool, vmType limatype.VMType) *portForwarder {
30+
func newPortForwarder(sshConfig *ssh.SSHConfig, sshAddressPort func() (string, int), rules []limatype.PortForward, ignore bool, vmType limatype.VMType) *portForwarder {
3131
return &portForwarder{
32-
sshConfig: sshConfig,
33-
sshHostPort: sshHostPort,
34-
rules: rules,
35-
ignore: ignore,
36-
vmType: vmType,
32+
sshConfig: sshConfig,
33+
sshAddressPort: sshAddressPort,
34+
rules: rules,
35+
ignore: ignore,
36+
vmType: vmType,
3737
}
3838
}
3939

@@ -87,6 +87,7 @@ func (pf *portForwarder) forwardingAddresses(guest *api.IPPort) (hostAddr, guest
8787
}
8888

8989
func (pf *portForwarder) OnEvent(ctx context.Context, ev *api.Event) {
90+
sshAddress, sshPort := pf.sshAddressPort()
9091
for _, f := range ev.RemovedLocalPorts {
9192
if f.Protocol != "tcp" {
9293
continue
@@ -96,7 +97,7 @@ func (pf *portForwarder) OnEvent(ctx context.Context, ev *api.Event) {
9697
continue
9798
}
9899
logrus.Infof("Stopping forwarding TCP from %s to %s", remote, local)
99-
if err := forwardTCP(ctx, pf.sshConfig, pf.sshHostPort, local, remote, verbCancel); err != nil {
100+
if err := forwardTCP(ctx, pf.sshConfig, sshAddress, sshPort, local, remote, verbCancel); err != nil {
100101
logrus.WithError(err).Warnf("failed to stop forwarding tcp port %d", f.Port)
101102
}
102103
}
@@ -112,7 +113,7 @@ func (pf *portForwarder) OnEvent(ctx context.Context, ev *api.Event) {
112113
continue
113114
}
114115
logrus.Infof("Forwarding TCP from %s to %s", remote, local)
115-
if err := forwardTCP(ctx, pf.sshConfig, pf.sshHostPort, local, remote, verbForward); err != nil {
116+
if err := forwardTCP(ctx, pf.sshConfig, sshAddress, sshPort, local, remote, verbForward); err != nil {
116117
logrus.WithError(err).Warnf("failed to set up forwarding tcp port %d (negligible if already forwarded)", f.Port)
117118
}
118119
}

pkg/hostagent/port_darwin.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
)
2121

2222
// forwardTCP is not thread-safe.
23-
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string) error {
23+
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote, verb string) error {
2424
if strings.HasPrefix(local, "/") {
25-
return forwardSSH(ctx, sshConfig, port, local, remote, verb, false)
25+
return forwardSSH(ctx, sshConfig, sshAddress, sshPort, local, remote, verb, false)
2626
}
2727
localIPStr, localPortStr, err := net.SplitHostPort(local)
2828
if err != nil {
@@ -35,7 +35,7 @@ func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
3535
}
3636

3737
if !localIP.Equal(IPv4loopback1) || localPort >= 1024 {
38-
return forwardSSH(ctx, sshConfig, port, local, remote, verb, false)
38+
return forwardSSH(ctx, sshConfig, sshAddress, sshPort, local, remote, verb, false)
3939
}
4040

4141
// on macOS, listening on 127.0.0.1:80 requires root while 0.0.0.0:80 does not require root.
@@ -50,7 +50,7 @@ func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
5050
localUnix := plf.unixAddr.Name
5151
_ = plf.Close()
5252
delete(pseudoLoopbackForwarders, local)
53-
if err := forwardSSH(ctx, sshConfig, port, localUnix, remote, verb, false); err != nil {
53+
if err := forwardSSH(ctx, sshConfig, sshAddress, sshPort, localUnix, remote, verb, false); err != nil {
5454
return err
5555
}
5656
} else {
@@ -65,12 +65,12 @@ func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
6565
}
6666
localUnix := filepath.Join(localUnixDir, "sock")
6767
logrus.Debugf("forwarding %q to %q", localUnix, remote)
68-
if err := forwardSSH(ctx, sshConfig, port, localUnix, remote, verb, false); err != nil {
68+
if err := forwardSSH(ctx, sshConfig, sshAddress, sshPort, localUnix, remote, verb, false); err != nil {
6969
return err
7070
}
7171
plf, err := newPseudoLoopbackForwarder(localPort, localUnix)
7272
if err != nil {
73-
if cancelErr := forwardSSH(ctx, sshConfig, port, localUnix, remote, verbCancel, false); cancelErr != nil {
73+
if cancelErr := forwardSSH(ctx, sshConfig, sshAddress, sshPort, localUnix, remote, verbCancel, false); cancelErr != nil {
7474
logrus.WithError(cancelErr).Warnf("failed to cancel forwarding %q to %q", localUnix, remote)
7575
}
7676
return err

pkg/hostagent/port_others.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ import (
1111
"github.com/lima-vm/sshocker/pkg/ssh"
1212
)
1313

14-
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string) error {
15-
return forwardSSH(ctx, sshConfig, port, local, remote, verb, false)
14+
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote, verb string) error {
15+
return forwardSSH(ctx, sshConfig, sshAddress, sshPort, local, remote, verb, false)
1616
}

pkg/hostagent/port_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ import (
99
"github.com/lima-vm/sshocker/pkg/ssh"
1010
)
1111

12-
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string) error {
13-
return forwardSSH(ctx, sshConfig, port, local, remote, verb, false)
12+
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote, verb string) error {
13+
return forwardSSH(ctx, sshConfig, sshAddress, sshPort, local, remote, verb, false)
1414
}

0 commit comments

Comments
 (0)