From 91c7a99df9911b5a717f3bf2a7f186adaa7b067c Mon Sep 17 00:00:00 2001
From: Pavel Safonov
Date: Fri, 10 Jan 2025 08:36:36 +0300
Subject: [PATCH] fix: get ssh username from ~/.ssh/config, if not specified in
uri
---
libvirt/uri/ssh.go | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/libvirt/uri/ssh.go b/libvirt/uri/ssh.go
index 342f4c029..b8b93cf2d 100644
--- a/libvirt/uri/ssh.go
+++ b/libvirt/uri/ssh.go
@@ -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 {
@@ -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")