Skip to content

Commit 71cf7da

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

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
@@ -91,7 +91,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
9191
} else {
9292
scpArgs = append(scpArgs, fmt.Sprintf("scp://%s@%s:%d/%s", u.Username, inst.SSHAddress, inst.SSHLocalPort, path[1]))
9393
}
94-
if inst.SSHAddress != "127.0.0.1" {
94+
if !sshutil.IsLocalhost(inst.SSHAddress) {
9595
instAddr = inst.SSHAddress
9696
localhostOnly = false
9797
}

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"
@@ -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
//
@@ -238,7 +247,7 @@ func SSHOpts(instDir string, useDotSSH bool, hostAddress string, forwardAgent, f
238247
if err != nil {
239248
return nil, err
240249
}
241-
opts, err := CommonOpts(useDotSSH, hostAddress == "127.0.0.1")
250+
opts, err := CommonOpts(useDotSSH, IsLocalhost(hostAddress))
242251
if err != nil {
243252
return nil, err
244253
}

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)