Skip to content

Commit

Permalink
Use net helper for local ssh CommonOpts
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Jul 11, 2024
1 parent 31a72f3 commit dc05c8d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/limactl/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
} else {
scpArgs = append(scpArgs, fmt.Sprintf("scp://%s@%s:%d/%s", u.Username, inst.SSHAddress, inst.SSHLocalPort, path[1]))
}
if inst.SSHAddress != "127.0.0.1" {
if !sshutil.IsLocalhost(inst.SSHAddress) {
instAddr = inst.SSHAddress
localhostOnly = false
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/sshutil/sshutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io/fs"
"net"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -121,6 +122,14 @@ var sshInfo struct {
openSSHVersion semver.Version
}

func IsLocalhost(address string) bool {
ip := net.ParseIP(address)
if ip == nil {
return false
}
return ip.IsLoopback()
}

// CommonOpts returns ssh option key-value pairs like {"IdentityFile=/path/to/id_foo"}.
// The result may contain different values with the same key.
//
Expand Down Expand Up @@ -238,7 +247,7 @@ func SSHOpts(instDir string, useDotSSH bool, hostAddress string, forwardAgent, f
if err != nil {
return nil, err
}
opts, err := CommonOpts(useDotSSH, hostAddress == "127.0.0.1")
opts, err := CommonOpts(useDotSSH, IsLocalhost(hostAddress))
if err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/sshutil/sshutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func TestDefaultPubKeys(t *testing.T) {
}
}

func TestIsLocalhost(t *testing.T) {
assert.Equal(t, IsLocalhost("127.0.0.1"), true)
}

func TestParseOpenSSHVersion(t *testing.T) {
assert.Check(t, ParseOpenSSHVersion([]byte("OpenSSH_8.4p1 Ubuntu")).Equal(
semver.Version{Major: 8, Minor: 4, Patch: 1, PreRelease: "", Metadata: ""}))
Expand Down

0 comments on commit dc05c8d

Please sign in to comment.