diff --git a/libvirt/uri/connection_uri_test.go b/libvirt/uri/connection_uri_test.go index 966b2bf73..7fa5991d1 100644 --- a/libvirt/uri/connection_uri_test.go +++ b/libvirt/uri/connection_uri_test.go @@ -12,15 +12,17 @@ func TestURI(t *testing.T) { Driver string Transport string RemoteName string + HostName string + Port string }{ - {"xxx://servername/", "xxx", "tls", "xxx:///"}, - {"xxx+tls://servername/", "xxx", "tls", "xxx:///"}, - {"xxx+tls:///", "xxx", "tls", "xxx:///"}, - {"xxx+tcp://servername/", "xxx", "tcp", "xxx:///"}, - {"xxx+tcp:///", "xxx", "tcp", "xxx:///"}, - {"xxx+unix:///", "xxx", "unix", "xxx:///"}, - {"xxx+tls://servername/?foo=bar&name=dong:///ding", "xxx", "tls", "dong:///ding"}, - {"xxx+ssh://username@hostname:2222/path?foo=bar&bar=foo", "xxx", "ssh", "xxx:///path"}, + {"xxx://servername/", "xxx", "tls", "xxx:///", "servername", ""}, + {"xxx+tls://servername/", "xxx", "tls", "xxx:///", "servername", ""}, + {"xxx+tls:///", "xxx", "tls", "xxx:///", "", ""}, + {"xxx+tcp://servername/", "xxx", "tcp", "xxx:///", "servername", ""}, + {"xxx+tcp:///", "xxx", "tcp", "xxx:///", "", ""}, + {"xxx+unix:///", "xxx", "unix", "xxx:///", "", ""}, + {"xxx+tls://servername/?foo=bar&name=dong:///ding", "xxx", "tls", "dong:///ding", "servername", ""}, + {"xxx+ssh://username@hostname:2222/path?foo=bar&bar=foo", "xxx", "ssh", "xxx:///path", "hostname", "2222"}, } for _, fixture := range fixtures { @@ -29,5 +31,7 @@ func TestURI(t *testing.T) { assert.Equal(t, fixture.Transport, u.transport()) assert.Equal(t, fixture.Driver, u.driver()) assert.Equal(t, fixture.RemoteName, u.RemoteName()) + assert.Equal(t, fixture.HostName, u.Host) + assert.Equal(t, fixture.Port, u.Port()) } } diff --git a/libvirt/uri/ssh.go b/libvirt/uri/ssh.go index 342f4c029..0ac2345cf 100644 --- a/libvirt/uri/ssh.go +++ b/libvirt/uri/ssh.go @@ -162,11 +162,37 @@ func (u *ConnectionURI) dialHost(target string, sshcfg *ssh_config.Config, depth q := u.Query() - port := u.Port() - if port == "" { - port = defaultSSHPort + // 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") + 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 != "" { + + 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] ssh Port is overridden to: '%s'", port) + + port := defaultSSHPort + log.Printf("[DEBUG] using default port for ssh connection ('%s')", port) } hostName := target