Skip to content

Commit 1413cb9

Browse files
committed
Use net helper for local ssh CommonOpts
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
1 parent c0fa862 commit 1413cb9

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

cmd/limactl/copy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
100100
} else {
101101
scpArgs = append(scpArgs, fmt.Sprintf("scp://%s@%s:%d/%s", *inst.Config.User.Name, inst.SSHAddress, inst.SSHLocalPort, path[1]))
102102
}
103-
if inst.SSHAddress != "127.0.0.1" {
103+
if !sshutil.IsLocalhost(inst.SSHAddress) {
104104
localhostOnly = false
105105
}
106106
instances[instName] = inst

pkg/sshutil/sshutil.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"io/fs"
10+
"net"
1011
"os"
1112
"os/exec"
1213
"path/filepath"
@@ -152,6 +153,14 @@ var sshInfo struct {
152153
openSSHVersion semver.Version
153154
}
154155

156+
func IsLocalhost(address string) bool {
157+
ip := net.ParseIP(address)
158+
if ip == nil {
159+
return false
160+
}
161+
return ip.IsLoopback()
162+
}
163+
155164
// CommonOpts returns ssh option key-value pairs like {"IdentityFile=/path/to/id_foo"}.
156165
// The result may contain different values with the same key.
157166
//
@@ -265,7 +274,7 @@ func SSHOpts(sshPath, instDir, username string, useDotSSH bool, hostAddress stri
265274
if len(controlSock) >= osutil.UnixPathMax {
266275
return nil, fmt.Errorf("socket path %q is too long: >= UNIX_PATH_MAX=%d", controlSock, osutil.UnixPathMax)
267276
}
268-
opts, err := CommonOpts(sshPath, useDotSSH, hostAddress == "127.0.0.1")
277+
opts, err := CommonOpts(sshPath, useDotSSH, IsLocalhost(hostAddress))
269278
if err != nil {
270279
return nil, err
271280
}

pkg/sshutil/sshutil_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ func TestDefaultPubKeys(t *testing.T) {
1515
}
1616
}
1717

18+
func TestIsLocalhost(t *testing.T) {
19+
assert.Equal(t, IsLocalhost("127.0.0.1"), true)
20+
}
21+
1822
func TestParseOpenSSHVersion(t *testing.T) {
1923
assert.Check(t, ParseOpenSSHVersion([]byte("OpenSSH_8.4p1 Ubuntu")).Equal(
2024
semver.Version{Major: 8, Minor: 4, Patch: 1, PreRelease: "", Metadata: ""}))

0 commit comments

Comments
 (0)