Skip to content

Commit 96c1c24

Browse files
authored
Bugfix: ssh port override for #1116 (#1117)
* add per connection Port override check * add log output to track what was done * update port configuration precedence to something more sensisble * remove code-golf'ing and make it more readable * 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
1 parent abaefa6 commit 96c1c24

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

libvirt/uri/ssh.go

+31-5
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (u *ConnectionURI) dialSSH() (net.Conn, error) {
132132
}
133133

134134
// configuration loaded, build tunnel
135-
sshClient, err := u.dialHost(u.Host, sshcfg, 0)
135+
sshClient, err := u.dialHost(u.Hostname(), sshcfg, 0)
136136
if err != nil {
137137
return nil, err
138138
}
@@ -162,11 +162,37 @@ func (u *ConnectionURI) dialHost(target string, sshcfg *ssh_config.Config, depth
162162

163163
q := u.Query()
164164

165-
port := u.Port()
166-
if port == "" {
167-
port = defaultSSHPort
165+
// port override order of precedence (starting with highest):
166+
// 1. specific stanza entry in ssh_config for this target (this includes default global entries in ssh config)
167+
// 2. port specified in connection string
168+
// 3. defaultSSHPort
169+
port := ""
170+
171+
if sshcfg != nil {
172+
configuredPort, err := sshcfg.Get(target, "Port")
173+
if err != nil {
174+
log.Printf("[WARN] error reading Port attribute from ssh_config for target '%v'", target)
175+
} else {
176+
port = configuredPort
177+
178+
if port == "" {
179+
log.Printf("[DEBUG] port for target '%v' in ssh_config is empty", target)
180+
}
181+
}
182+
}
183+
184+
if port != "" {
185+
186+
log.Printf("[DEBUG] using ssh port from ssh_config: '%s'", port)
187+
188+
} else if u.Port() != "" {
189+
190+
port = u.Port()
191+
log.Printf("[DEBUG] using connection string port ('%s')", port)
168192
} else {
169-
log.Printf("[DEBUG] ssh Port is overridden to: '%s'", port)
193+
194+
port := defaultSSHPort
195+
log.Printf("[DEBUG] using default port for ssh connection ('%s')", port)
170196
}
171197

172198
hostName := target

0 commit comments

Comments
 (0)