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

Bugfix: ssh port override for #1116 #1117

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
36 changes: 31 additions & 5 deletions libvirt/uri/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down 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