ssh_config icon indicating copy to clipboard operation
ssh_config copied to clipboard

Add support for retrieving all IdentityFile directives

Open virtuald opened this issue 4 years ago • 7 comments
trafficstars

Moving #22 to be from a different branch.

Additionally, I'm going to publish a re-targeted fork of this repo until you merge this, because it's annoying to add replace directives to my modules.

virtuald avatar Mar 22 '21 16:03 virtuald

I finally have time to look at this today. I'm sorry for the delay. I would like to try to merge something by the end of the day.

  • Can we think of a better name than IsMultifileDirective ? Maybe SupportsMultiple?
  • The API for DefaultAll feels a little too convoluted. Could we either special case DefaultIdentityFiles(), or only return the results you'd get for Protocol 2, or some other change?

How are you currently using DefaultAll? I don't see any examples for it currently.

kevinburke avatar Mar 27 '21 19:03 kevinburke

OK, I merged 124166206d4a0bb6b065541711b51fda944e6ea1, which doesn't have DefaultAll and renames IsMultifileDirective to SupportsMultiple. Still open to ideas/merging future changes on both of those.

kevinburke avatar Mar 27 '21 20:03 kevinburke

I've only been using DefaultAll only for IdentityFile, so making it a special case is fine for my usage. The reason that DefaultAll has the weird getter function is because the default value changes based on the config that you're currently parsing -- though I can't imagine anyone cares much about SSH protocol 1 anymore?

There's a wrapper struct of course...

func (s *Settings) GetAllStrict(alias, key string) ([]string, error) {
	val, err := s.cfg.GetAll(alias, key)
	if val != nil || err != nil {
		return val, err
	}
	return ssh_config.DefaultAll(key, alias, func(a, k string) string {
		v, _ := s.cfg.Get(a, k)
		return v
	}), nil
}

And here's how I call that:

	files, err := s.GetAllStrict(hostAlias, "IdentityFile")

virtuald avatar Mar 27 '21 21:03 virtuald

If no one cares about ssh protocol 1 then let's just ignore it or return the default for ssh protocol 2

kevinburke avatar Mar 27 '21 21:03 kevinburke

An alternative would be accepting a Config object as the third argument to DefaultAll instead of a function? Then it could remain correct even in the presence of SSH protocol 1 for some reason.

virtuald avatar Mar 27 '21 21:03 virtuald

Rebased and made the change to use a config object. Unfortunately, UserSettings and Config don't have the same function signatures, so I had to add an internal method to make them both compatible -- which is likely why I chose a function originally.

virtuald avatar Mar 28 '21 16:03 virtuald

@kevinburke are you open to merging this or something similar that accomplishes the same?

I came across this while debugging some very surprising behavior where connections kept trying to use ~/.ssh/identity instead of the usual suspects (id_rsa, etc.).

blampe avatar Jul 26 '23 22:07 blampe