Skip to content

Commit

Permalink
only read ssh_config for non machine connections
Browse files Browse the repository at this point in the history
For machine we know we have all the info we need so there is no reason
to read and parse another file.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Nov 14, 2024
1 parent cbb2820 commit 71f1f52
Showing 1 changed file with 55 additions and 50 deletions.
105 changes: 55 additions & 50 deletions pkg/bindings/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,68 +162,73 @@ func sshClient(_url *url.URL, uri string, identity string, machine bool) (Connec
return connection, err
}
}
// ssh_config
alias := _url.Hostname()
cfg := ssh_config.DefaultUserSettings
cfg.IgnoreErrors = true
found := false

if userinfo == nil {
if val := cfg.Get(alias, "User"); val != "" {
userinfo = url.User(val)
found = true

// only parse ssh_config when we are not connecting to a machine
// For machine connections we always have the full URL in the
// system connection so reading the file is just unnecessary.
if !machine {
alias := _url.Hostname()
cfg := ssh_config.DefaultUserSettings
cfg.IgnoreErrors = true
found := false

if userinfo == nil {
if val := cfg.Get(alias, "User"); val != "" {
userinfo = url.User(val)
found = true
}
}
}
// not in url or ssh_config so default to current user
if userinfo == nil {
u, err := user.Current()
if err != nil {
return connection, fmt.Errorf("current user could not be determined: %w", err)
// not in url or ssh_config so default to current user
if userinfo == nil {
u, err := user.Current()
if err != nil {
return connection, fmt.Errorf("current user could not be determined: %w", err)
}
userinfo = url.User(u.Username)
}
userinfo = url.User(u.Username)
}

if val := cfg.Get(alias, "Hostname"); val != "" {
uri = val
found = true
}
if val := cfg.Get(alias, "Hostname"); val != "" {
uri = val
found = true
}

if port == 0 {
if val := cfg.Get(alias, "Port"); val != "" {
if val != ssh_config.Default("Port") {
port, err = strconv.Atoi(val)
if err != nil {
return connection, fmt.Errorf("port is not an int: %s: %w", val, err)
if port == 0 {
if val := cfg.Get(alias, "Port"); val != "" {
if val != ssh_config.Default("Port") {
port, err = strconv.Atoi(val)
if err != nil {
return connection, fmt.Errorf("port is not an int: %s: %w", val, err)
}
found = true
}
found = true
}
}
}
// not in ssh config or url so use default 22 port
if port == 0 {
port = 22
}
// not in ssh config or url so use default 22 port
if port == 0 {
port = 22
}

if identity == "" {
if val := cfg.Get(alias, "IdentityFile"); val != "" {
identity = strings.Trim(val, "\"")
if strings.HasPrefix(identity, "~/") {
homedir, err := os.UserHomeDir()
if err != nil {
return connection, fmt.Errorf("failed to find home dir: %w", err)
if identity == "" {
if val := cfg.Get(alias, "IdentityFile"); val != "" {
identity = strings.Trim(val, "\"")
if strings.HasPrefix(identity, "~/") {
homedir, err := os.UserHomeDir()
if err != nil {
return connection, fmt.Errorf("failed to find home dir: %w", err)
}
identity = filepath.Join(homedir, identity[2:])
}
identity = filepath.Join(homedir, identity[2:])
found = true
}
found = true
}
}

if found {
logrus.Debugf("ssh_config alias found: %s", alias)
logrus.Debugf(" User: %s", userinfo.Username())
logrus.Debugf(" Hostname: %s", uri)
logrus.Debugf(" Port: %d", port)
logrus.Debugf(" IdentityFile: %q", identity)
if found {
logrus.Debugf("ssh_config alias found: %s", alias)
logrus.Debugf(" User: %s", userinfo.Username())
logrus.Debugf(" Hostname: %s", uri)
logrus.Debugf(" Port: %d", port)
logrus.Debugf(" IdentityFile: %q", identity)
}
}
conn, err := ssh.Dial(&ssh.ConnectionDialOptions{
Host: uri,
Expand Down

0 comments on commit 71f1f52

Please sign in to comment.