From ab431fd1586872c38e4efa90da38e9af260b837b Mon Sep 17 00:00:00 2001 From: Memet Bilgin Date: Wed, 23 Oct 2024 09:44:03 -0300 Subject: [PATCH 1/5] add per connection Port override check --- libvirt/uri/ssh.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libvirt/uri/ssh.go b/libvirt/uri/ssh.go index 342f4c029..4e83041d8 100644 --- a/libvirt/uri/ssh.go +++ b/libvirt/uri/ssh.go @@ -165,6 +165,13 @@ func (u *ConnectionURI) dialHost(target string, sshcfg *ssh_config.Config, depth port := u.Port() if port == "" { port = defaultSSHPort + if sshcfg != nil { + configuredPort, err := sshcfg.Get(target, "Port") + if err == nil && configuredPort != "" { + port = configuredPort + } + } + } else { log.Printf("[DEBUG] ssh Port is overridden to: '%s'", port) } From b8da8da5d8efc0c712b171c31b4188cc17e0effc Mon Sep 17 00:00:00 2001 From: Memet Bilgin Date: Wed, 23 Oct 2024 09:46:51 -0300 Subject: [PATCH 2/5] add log output to track what was done --- libvirt/uri/ssh.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libvirt/uri/ssh.go b/libvirt/uri/ssh.go index 4e83041d8..3fd1ef0e0 100644 --- a/libvirt/uri/ssh.go +++ b/libvirt/uri/ssh.go @@ -169,12 +169,14 @@ func (u *ConnectionURI) dialHost(target string, sshcfg *ssh_config.Config, depth configuredPort, err := sshcfg.Get(target, "Port") if err == nil && configuredPort != "" { port = configuredPort + log.Printf("[DEBUG] using ssh port from ssh_config: '%s'", port) } } } else { - log.Printf("[DEBUG] ssh Port is overridden to: '%s'", port) + log.Printf("[DEBUG] using ssh port from querystring: '%s'", port) } + log.Printf("[DEBUG] port for ssh connection is: '%s'", port) hostName := target if sshcfg != nil { From 31bd40ac7c8cf09bb099a2706488b04795167bc6 Mon Sep 17 00:00:00 2001 From: Memet Bilgin Date: Mon, 28 Oct 2024 02:44:39 -0300 Subject: [PATCH 3/5] update port configuration precedence to something more sensisble --- libvirt/uri/ssh.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/libvirt/uri/ssh.go b/libvirt/uri/ssh.go index 3fd1ef0e0..0c2bce355 100644 --- a/libvirt/uri/ssh.go +++ b/libvirt/uri/ssh.go @@ -162,21 +162,26 @@ func (u *ConnectionURI) dialHost(target string, sshcfg *ssh_config.Config, depth q := u.Query() - port := u.Port() - if port == "" { - port = defaultSSHPort - if sshcfg != nil { - configuredPort, err := sshcfg.Get(target, "Port") - if err == nil && configuredPort != "" { - port = configuredPort - log.Printf("[DEBUG] using ssh port from ssh_config: '%s'", port) - } - } + // port override order of precedence (starting with highest): + // 1. specific stanza entry in ssh_config for this target (this includes default global entries in ssh config) + // 2. port specified in connection string + // 3. defaultSSHPort + port := "" + + if sshcfg != nil && (configuredPort, err := sshcfg.Get(target, "Port")); err == nil && configuredPort != "" { + port = configuredPort + log.Printf("[DEBUG] using ssh port from ssh_config: '%s'", port) + + } else if u.Port() != "" { + + port = u.Port() + log.Printf("[DEBUG] using connection string port ('%s')", port) } else { - log.Printf("[DEBUG] using ssh port from querystring: '%s'", port) + + port := defaultSSHPort + log.Printf("[DEBUG] using default port for ssh connection ('%s')", port) } - log.Printf("[DEBUG] port for ssh connection is: '%s'", port) hostName := target if sshcfg != nil { From 17a977d9b123ae3de4979d625cee9c6f01ec437e Mon Sep 17 00:00:00 2001 From: Memet Bilgin Date: Tue, 29 Oct 2024 06:51:12 -0300 Subject: [PATCH 4/5] remove code-golf'ing and make it more readable --- libvirt/uri/ssh.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/libvirt/uri/ssh.go b/libvirt/uri/ssh.go index 0c2bce355..0ac2345cf 100644 --- a/libvirt/uri/ssh.go +++ b/libvirt/uri/ssh.go @@ -168,9 +168,21 @@ func (u *ConnectionURI) dialHost(target string, sshcfg *ssh_config.Config, depth // 3. defaultSSHPort port := "" - if sshcfg != nil && (configuredPort, err := sshcfg.Get(target, "Port")); err == nil && configuredPort != "" { + if sshcfg != nil { + configuredPort, err := sshcfg.Get(target, "Port") + if err != nil { + log.Printf("[WARN] error reading Port attribute from ssh_config for target '%v'", target) + } else { + port = configuredPort + + if port == "" { + log.Printf("[DEBUG] port for target '%v' in ssh_config is empty", target) + } + } + } + + if port != "" { - port = configuredPort log.Printf("[DEBUG] using ssh port from ssh_config: '%s'", port) } else if u.Port() != "" { From a5fc04c8aac1b5774b10b1ac035bae2db2599252 Mon Sep 17 00:00:00 2001 From: Memet Bilgin Date: Tue, 29 Oct 2024 10:39:52 -0300 Subject: [PATCH 5/5] as per net/url, the .Host member can contain the portname this will throw off the ssh_config lookups as the target will be incorrectly given the server:port string instead of simply server --- libvirt/uri/ssh.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libvirt/uri/ssh.go b/libvirt/uri/ssh.go index 0ac2345cf..d1f22ba06 100644 --- a/libvirt/uri/ssh.go +++ b/libvirt/uri/ssh.go @@ -132,7 +132,7 @@ func (u *ConnectionURI) dialSSH() (net.Conn, error) { } // configuration loaded, build tunnel - sshClient, err := u.dialHost(u.Host, sshcfg, 0) + sshClient, err := u.dialHost(u.Hostname(), sshcfg, 0) if err != nil { return nil, err }