Skip to content

Commit 0cf341b

Browse files
committed
Respect the SSH environment variable if any
Signed-off-by: Anders F Björklund <[email protected]>
1 parent d7b8b8a commit 0cf341b

File tree

7 files changed

+21
-16
lines changed

7 files changed

+21
-16
lines changed

cmd/limactl/copy.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ func copyAction(cmd *cobra.Command, args []string) error {
7474
if recursive {
7575
scpFlags = append(scpFlags, "-r")
7676
}
77-
legacySSH := sshutil.DetectOpenSSHVersion().LessThan(*semver.New("8.0.0"))
77+
// this assumes that ssh and scp come from the same place, but scp has no -V
78+
legacySSH := sshutil.DetectOpenSSHVersion("ssh").LessThan(*semver.New("8.0.0"))
7879
for _, arg := range args {
7980
path := strings.Split(arg, ":")
8081
switch len(path) {
@@ -115,14 +116,14 @@ func copyAction(cmd *cobra.Command, args []string) error {
115116
// arguments such as ControlPath. This is preferred as we can multiplex
116117
// sessions without re-authenticating (MaxSessions permitting).
117118
for _, inst := range instances {
118-
sshOpts, err = sshutil.SSHOpts(inst.Dir, *inst.Config.User.Name, false, false, false, false)
119+
sshOpts, err = sshutil.SSHOpts("ssh", inst.Dir, *inst.Config.User.Name, false, false, false, false)
119120
if err != nil {
120121
return err
121122
}
122123
}
123124
} else {
124125
// Copying among multiple hosts; we can't pass in host-specific options.
125-
sshOpts, err = sshutil.CommonOpts(false)
126+
sshOpts, err = sshutil.CommonOpts("ssh", false)
126127
if err != nil {
127128
return err
128129
}

cmd/limactl/shell.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
167167
}
168168

169169
sshOpts, err := sshutil.SSHOpts(
170+
arg0,
170171
inst.Dir,
171172
*inst.Config.User.Name,
172173
*inst.Config.SSH.LoadDotSSHPubKeys,
@@ -188,7 +189,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
188189
logLevel := "ERROR"
189190
// For versions older than OpenSSH 8.9p, LogLevel=QUIET was needed to
190191
// avoid the "Shared connection to 127.0.0.1 closed." message with -t.
191-
olderSSH := sshutil.DetectOpenSSHVersion().LessThan(*semver.New("8.9.0"))
192+
olderSSH := sshutil.DetectOpenSSHVersion(arg0).LessThan(*semver.New("8.9.0"))
192193
if olderSSH {
193194
logLevel = "QUIET"
194195
}

cmd/limactl/show-ssh.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
8989
logrus.Warnf("`limactl show-ssh` is deprecated. Instead, use `ssh -F %s %s`.",
9090
filepath.Join(inst.Dir, filenames.SSHConfig), inst.Hostname)
9191
opts, err := sshutil.SSHOpts(
92+
"ssh",
9293
inst.Dir,
9394
*inst.Config.User.Name,
9495
*inst.Config.SSH.LoadDotSSHPubKeys,
@@ -100,7 +101,7 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
100101
}
101102
opts = append(opts, "Hostname=127.0.0.1")
102103
opts = append(opts, fmt.Sprintf("Port=%d", inst.SSHLocalPort))
103-
return sshutil.Format(w, instName, format, opts)
104+
return sshutil.Format(w, "ssh", instName, format, opts)
104105
}
105106

106107
func showSSHBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {

cmd/limactl/tunnel.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func tunnelAction(cmd *cobra.Command, args []string) error {
106106
}
107107

108108
sshOpts, err := sshutil.SSHOpts(
109+
arg0,
109110
inst.Dir,
110111
*inst.Config.User.Name,
111112
*inst.Config.SSH.LoadDotSSHPubKeys,

pkg/hostagent/hostagent.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
143143
}
144144

145145
sshOpts, err := sshutil.SSHOpts(
146+
"ssh",
146147
inst.Dir,
147148
*inst.Config.User.Name,
148149
*inst.Config.SSH.LoadDotSSHPubKeys,
@@ -152,7 +153,7 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
152153
if err != nil {
153154
return nil, err
154155
}
155-
if err = writeSSHConfigFile(inst.Name, inst.Dir, inst.SSHAddress, sshLocalPort, sshOpts); err != nil {
156+
if err = writeSSHConfigFile("ssh", inst.Name, inst.Dir, inst.SSHAddress, sshLocalPort, sshOpts); err != nil {
156157
return nil, err
157158
}
158159
sshConfig := &ssh.SSHConfig{
@@ -220,7 +221,7 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
220221
return a, nil
221222
}
222223

223-
func writeSSHConfigFile(instName, instDir, instSSHAddress string, sshLocalPort int, sshOpts []string) error {
224+
func writeSSHConfigFile(sshPath, instName, instDir, instSSHAddress string, sshLocalPort int, sshOpts []string) error {
224225
if instDir == "" {
225226
return fmt.Errorf("directory is unknown for the instance %q", instName)
226227
}
@@ -231,7 +232,7 @@ func writeSSHConfigFile(instName, instDir, instSSHAddress string, sshLocalPort i
231232
`); err != nil {
232233
return err
233234
}
234-
if err := sshutil.Format(&b, instName, sshutil.FormatConfig,
235+
if err := sshutil.Format(&b, sshPath, instName, sshutil.FormatConfig,
235236
append(sshOpts,
236237
fmt.Sprintf("Hostname=%s", instSSHAddress),
237238
fmt.Sprintf("Port=%d", sshLocalPort),

pkg/sshutil/format.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ func quoteOption(o string) string {
5858
}
5959

6060
// Format formats the ssh options.
61-
func Format(w io.Writer, instName string, format FormatT, opts []string) error {
61+
func Format(w io.Writer, sshPath, instName string, format FormatT, opts []string) error {
6262
fakeHostname := identifierutil.HostnameFromInstName(instName) // TODO: support customization
6363
switch format {
6464
case FormatCmd:
65-
args := []string{"ssh"}
65+
args := []string{sshPath}
6666
for _, o := range opts {
6767
args = append(args, "-o", quoteOption(o))
6868
}

pkg/sshutil/sshutil.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ var sshInfo struct {
127127
//
128128
// The result always contains the IdentityFile option.
129129
// The result never contains the Port option.
130-
func CommonOpts(useDotSSH bool) ([]string, error) {
130+
func CommonOpts(sshPath string, useDotSSH bool) ([]string, error) {
131131
configDir, err := dirnames.LimaConfigDir()
132132
if err != nil {
133133
return nil, err
@@ -195,7 +195,7 @@ func CommonOpts(useDotSSH bool) ([]string, error) {
195195

196196
sshInfo.Do(func() {
197197
sshInfo.aesAccelerated = detectAESAcceleration()
198-
sshInfo.openSSHVersion = DetectOpenSSHVersion()
198+
sshInfo.openSSHVersion = DetectOpenSSHVersion(sshPath)
199199
})
200200

201201
// Only OpenSSH version 8.1 and later support adding ciphers to the front of the default set
@@ -224,12 +224,12 @@ func CommonOpts(useDotSSH bool) ([]string, error) {
224224
}
225225

226226
// SSHOpts adds the following options to CommonOptions: User, ControlMaster, ControlPath, ControlPersist.
227-
func SSHOpts(instDir, username string, useDotSSH, forwardAgent, forwardX11, forwardX11Trusted bool) ([]string, error) {
227+
func SSHOpts(sshPath, instDir, username string, useDotSSH, forwardAgent, forwardX11, forwardX11Trusted bool) ([]string, error) {
228228
controlSock := filepath.Join(instDir, filenames.SSHSock)
229229
if len(controlSock) >= osutil.UnixPathMax {
230230
return nil, fmt.Errorf("socket path %q is too long: >= UNIX_PATH_MAX=%d", controlSock, osutil.UnixPathMax)
231231
}
232-
opts, err := CommonOpts(useDotSSH)
232+
opts, err := CommonOpts(sshPath, useDotSSH)
233233
if err != nil {
234234
return nil, err
235235
}
@@ -288,13 +288,13 @@ type sshExecutable struct {
288288
// sshVersion caches the parsed version of each ssh executable, if it is needed again.
289289
var sshVersions = map[sshExecutable]*semver.Version{}
290290

291-
func DetectOpenSSHVersion() semver.Version {
291+
func DetectOpenSSHVersion(ssh string) semver.Version {
292292
var (
293293
v semver.Version
294294
exe sshExecutable
295295
stderr bytes.Buffer
296296
)
297-
path, err := exec.LookPath("ssh")
297+
path, err := exec.LookPath(ssh)
298298
if err != nil {
299299
logrus.Warnf("failed to find ssh executable: %v", err)
300300
} else {

0 commit comments

Comments
 (0)