Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection port parsing #1125

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions libvirt/uri/connection_uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
}
}
34 changes: 30 additions & 4 deletions libvirt/uri/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading