Skip to content

Commit 6a3ede0

Browse files
committed
Use net helper for local ssh CommonOpts
Signed-off-by: Anders F Björklund <[email protected]>
1 parent eb11503 commit 6a3ede0

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

cmd/limactl/copy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
116116
} else {
117117
scpArgs = append(scpArgs, fmt.Sprintf("scp://%s@%s:%d/%s", *inst.Config.User.Name, inst.SSHAddress, inst.SSHLocalPort, path[1]))
118118
}
119-
if inst.SSHAddress != "127.0.0.1" {
119+
if !sshutil.IsLocalhost(inst.SSHAddress) {
120120
localhostOnly = false
121121
}
122122
instances[instName] = inst

pkg/sshutil/sshutil.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"errors"
1111
"fmt"
1212
"io/fs"
13+
"net"
1314
"os"
1415
"os/exec"
1516
"path/filepath"
@@ -162,6 +163,14 @@ var sshInfo struct {
162163
openSSHVersion semver.Version
163164
}
164165

166+
func IsLocalhost(address string) bool {
167+
ip := net.ParseIP(address)
168+
if ip == nil {
169+
return false
170+
}
171+
return ip.IsLoopback()
172+
}
173+
165174
// CommonOpts returns ssh option key-value pairs like {"IdentityFile=/path/to/id_foo"}.
166175
// The result may contain different values with the same key.
167176
//
@@ -285,7 +294,7 @@ func SSHOpts(sshPath, instDir, username string, useDotSSH bool, hostAddress stri
285294
if len(controlSock) >= osutil.UnixPathMax {
286295
return nil, fmt.Errorf("socket path %q is too long: >= UNIX_PATH_MAX=%d", controlSock, osutil.UnixPathMax)
287296
}
288-
opts, err := CommonOpts(sshPath, useDotSSH, hostAddress == "127.0.0.1")
297+
opts, err := CommonOpts(sshPath, useDotSSH, IsLocalhost(hostAddress))
289298
if err != nil {
290299
return nil, err
291300
}

pkg/sshutil/sshutil_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ func TestDefaultPubKeys(t *testing.T) {
1818
}
1919
}
2020

21+
func TestIsLocalhost(t *testing.T) {
22+
assert.Equal(t, IsLocalhost("127.0.0.1"), true)
23+
}
24+
2125
func TestParseOpenSSHVersion(t *testing.T) {
2226
assert.Check(t, ParseOpenSSHVersion([]byte("OpenSSH_8.4p1 Ubuntu")).Equal(
2327
semver.Version{Major: 8, Minor: 4, Patch: 1, PreRelease: "", Metadata: ""}))

0 commit comments

Comments
 (0)