Skip to content

Commit 88724be

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

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
@@ -85,7 +85,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
8585
} else {
8686
scpArgs = append(scpArgs, fmt.Sprintf("scp://%s@%s:%d/%s", *inst.Config.User.Name, inst.SSHAddress, inst.SSHLocalPort, path[1]))
8787
}
88-
if inst.SSHAddress != "127.0.0.1" {
88+
if !sshutil.IsLocalhost(inst.SSHAddress) {
8989
localhostOnly = false
9090
}
9191
instances[instName] = inst

pkg/sshutil/sshutil.go

Lines changed: 10 additions & 1 deletion
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"
@@ -121,6 +122,14 @@ var sshInfo struct {
121122
openSSHVersion semver.Version
122123
}
123124

125+
func IsLocalhost(address string) bool {
126+
ip := net.ParseIP(address)
127+
if ip == nil {
128+
return false
129+
}
130+
return ip.IsLoopback()
131+
}
132+
124133
// CommonOpts returns ssh option key-value pairs like {"IdentityFile=/path/to/id_foo"}.
125134
// The result may contain different values with the same key.
126135
//
@@ -234,7 +243,7 @@ func SSHOpts(instDir, username string, useDotSSH bool, hostAddress string, forwa
234243
if len(controlSock) >= osutil.UnixPathMax {
235244
return nil, fmt.Errorf("socket path %q is too long: >= UNIX_PATH_MAX=%d", controlSock, osutil.UnixPathMax)
236245
}
237-
opts, err := CommonOpts(useDotSSH, hostAddress == "127.0.0.1")
246+
opts, err := CommonOpts(useDotSSH, IsLocalhost(hostAddress))
238247
if err != nil {
239248
return nil, err
240249
}

pkg/sshutil/sshutil_test.go

Lines changed: 4 additions & 0 deletions
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)