Skip to content

Commit

Permalink
fix: get ssh username from ~/.ssh/config, if not specified in uri
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Safonov committed Jan 10, 2025
1 parent 1cd90b7 commit 91c7a99
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions libvirt/uri/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (u *ConnectionURI) parseAuthMethods(target string, sshcfg *ssh_config.Confi
// construct the whole ssh connection, which can consist of multiple hops if using proxy jumps,
// the ssh configuration file is loaded once and passed along to each host connection.
func (u *ConnectionURI) dialSSH() (net.Conn, error) {
var sshcfg* ssh_config.Config = nil
var sshcfg *ssh_config.Config = nil

sshConfigFile, err := os.Open(os.ExpandEnv(defaultSSHConfigFile))
if err != nil {
Expand Down Expand Up @@ -277,13 +277,14 @@ func (u *ConnectionURI) dialHost(target string, sshcfg *ssh_config.Config, depth
// cfg.User value defaults to u.User.Username()
if sshcfg != nil {
sshu, err := sshcfg.Get(target, "User")
if err != nil {
log.Printf("[DEBUG] ssh user for target '%v' is overridden to '%v'", target, sshu)
if err == nil && sshu != "" && cfg.User == "" {
// get username from ~/.ssh/config
// if uri doesn't contains username
log.Printf("[DEBUG] ssh user for target '%v' is set to '%v'", target, sshu)
cfg.User = sshu
}
}


cfg.Auth = u.parseAuthMethods(target, sshcfg)
if len(cfg.Auth) < 1 {
return nil, fmt.Errorf("could not configure SSH authentication methods")
Expand Down

0 comments on commit 91c7a99

Please sign in to comment.