fix: correctly override with ~/.ssh/config if failing to connect
continuous-integration/drone/push Build is passing Details

This commit is contained in:
decentral1se 2022-01-19 13:28:57 +01:00
parent b1b9612e01
commit 13e582349c
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
2 changed files with 20 additions and 14 deletions

View File

@ -325,7 +325,7 @@ func connect(username, host, port string, authMethod ssh.AuthMethod, timeout tim
conn, err = net.DialTimeout("tcp", hostnameAndPort, timeout) conn, err = net.DialTimeout("tcp", hostnameAndPort, timeout)
if err != nil { if err != nil {
logrus.Debugf("tcp dialing %s failed, trying via ~/.ssh/config", hostnameAndPort) 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 { if err != nil {
return nil, err 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 { if err != nil {
return &dockerSSHPkg.Spec{}, err return &dockerSSHPkg.Spec{}, err
} }
@ -472,30 +472,36 @@ func GetContextConnDetails(serverName string) (*dockerSSHPkg.Spec, error) {
} }
// GetHostConfig retrieves a ~/.ssh/config config for a host. // 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 var hostConfig HostConfig
if hostname == "" { if hostname == "" || override {
if hostname = ssh_config.Get(hostname, "Hostname"); hostname == "" { if sshHost := ssh_config.Get(hostname, "Hostname"); sshHost != "" {
logrus.Debugf("no hostname found in SSH config, assuming %s", hostname) hostname = sshHost
} }
} }
if username == "" { if username == "" || override {
if username = ssh_config.Get(hostname, "User"); username == "" { if sshUser := ssh_config.Get(hostname, "User"); sshUser != "" {
username = sshUser
} else {
systemUser, err := user.Current() systemUser, err := user.Current()
if err != nil { if err != nil {
return hostConfig, err return hostConfig, err
} }
logrus.Debugf("no username found in SSH config or passed on command-line, assuming %s", username)
username = systemUser.Username username = systemUser.Username
} }
} }
if port == "" { if port == "" || override {
if port = ssh_config.Get(hostname, "Port"); port == "" { if sshPort := ssh_config.Get(hostname, "Port"); sshPort != "" {
logrus.Debugf("no port found in SSH config or passed on command-line, assuming 22") // skip override probably correct port with dummy default value from
port = "22" // 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 hostConfig.IdentityFile = idf
} else { } else {
logrus.Debugf("no identity file found in SSH config for %s", hostname)
hostConfig.IdentityFile = "" hostConfig.IdentityFile = ""
} }

View File

@ -44,6 +44,7 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.Conne
ctxConnDetails.Host, ctxConnDetails.Host,
ctxConnDetails.User, ctxConnDetails.User,
ctxConnDetails.Port, ctxConnDetails.Port,
false,
) )
if err != nil { if err != nil {
return nil, err return nil, err