From 13e582349cdd02be00b8e344161feed7fa1970a0 Mon Sep 17 00:00:00 2001 From: cellarspoon Date: Wed, 19 Jan 2022 13:28:57 +0100 Subject: [PATCH] fix: correctly override with ~/.ssh/config if failing to connect --- pkg/ssh/ssh.go | 33 +++++++++++++++----------- pkg/upstream/commandconn/connection.go | 1 + 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go index 49ceb24f..d9ad4147 100644 --- a/pkg/ssh/ssh.go +++ b/pkg/ssh/ssh.go @@ -325,7 +325,7 @@ func connect(username, host, port string, authMethod ssh.AuthMethod, timeout tim conn, err = net.DialTimeout("tcp", hostnameAndPort, timeout) if err != nil { logrus.Debugf("tcp dialing %s failed, trying via ~/.ssh/config", hostnameAndPort) - hostConfig, err := GetHostConfig(host, username, port) + hostConfig, err := GetHostConfig(host, username, port, true) if err != nil { return nil, err } @@ -452,7 +452,7 @@ func GetContextConnDetails(serverName string) (*dockerSSHPkg.Spec, error) { } } - hostConfig, err := GetHostConfig(serverName, "", "") + hostConfig, err := GetHostConfig(serverName, "", "", false) if err != nil { return &dockerSSHPkg.Spec{}, err } @@ -472,30 +472,36 @@ func GetContextConnDetails(serverName string) (*dockerSSHPkg.Spec, error) { } // GetHostConfig retrieves a ~/.ssh/config config for a host. -func GetHostConfig(hostname, username, port string) (HostConfig, error) { +func GetHostConfig(hostname, username, port string, override bool) (HostConfig, error) { var hostConfig HostConfig - if hostname == "" { - if hostname = ssh_config.Get(hostname, "Hostname"); hostname == "" { - logrus.Debugf("no hostname found in SSH config, assuming %s", hostname) + if hostname == "" || override { + if sshHost := ssh_config.Get(hostname, "Hostname"); sshHost != "" { + hostname = sshHost } } - if username == "" { - if username = ssh_config.Get(hostname, "User"); username == "" { + if username == "" || override { + if sshUser := ssh_config.Get(hostname, "User"); sshUser != "" { + username = sshUser + } else { systemUser, err := user.Current() if err != nil { return hostConfig, err } - logrus.Debugf("no username found in SSH config or passed on command-line, assuming %s", username) username = systemUser.Username } } - if port == "" { - if port = ssh_config.Get(hostname, "Port"); port == "" { - logrus.Debugf("no port found in SSH config or passed on command-line, assuming 22") - port = "22" + if port == "" || override { + if sshPort := ssh_config.Get(hostname, "Port"); sshPort != "" { + // skip override probably correct port with dummy default value from + // ssh_config which is 22. only when the original port number is empty + // should we try this default. this might not cover all cases + // unfortunately. + if port != "" && sshPort != "22" { + port = sshPort + } } } @@ -507,7 +513,6 @@ func GetHostConfig(hostname, username, port string) (HostConfig, error) { } hostConfig.IdentityFile = idf } else { - logrus.Debugf("no identity file found in SSH config for %s", hostname) hostConfig.IdentityFile = "" } diff --git a/pkg/upstream/commandconn/connection.go b/pkg/upstream/commandconn/connection.go index 2554248e..94ce8905 100644 --- a/pkg/upstream/commandconn/connection.go +++ b/pkg/upstream/commandconn/connection.go @@ -44,6 +44,7 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.Conne ctxConnDetails.Host, ctxConnDetails.User, ctxConnDetails.Port, + false, ) if err != nil { return nil, err