Skip to content

Commit 17e2087

Browse files
oneirocosmvirtuald
andauthored
Update IdentityFile Defaults to Match Documentation (#4)
Credit to @virtuald for the original implementation of this fix. * Add support for retrieving all IdentityFile directives via DefaultAll * fix: set IdentityFile defaults to match man page The existing default IdentityFile list was incomplete and out of order. This updates it to match the defaults listed here: https://man7.org/linux/man-pages/man5/ssh_config.5.html --------- Co-authored-by: Dustin Spicuzza <[email protected]>
1 parent cba6b6a commit 17e2087

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

config.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ var _ = version
4949

5050
type configFinder func() string
5151

52+
type config interface {
53+
getinternal(alias, key string) string
54+
}
55+
56+
var _ config = &UserSettings{}
57+
var _ config = &Config{}
58+
5259
// UserSettings checks ~/.ssh and /etc/ssh for configuration files. The config
5360
// files are parsed and cached the first time Get() or GetStrict() is called.
5461
type UserSettings struct {
@@ -189,6 +196,10 @@ func (u *UserSettings) Get(alias, key string) string {
189196
return val
190197
}
191198

199+
func (u *UserSettings) getinternal(alias, key string) string {
200+
return u.Get(alias, key)
201+
}
202+
192203
// GetAll retrieves zero or more directives for key for the given alias. GetAll
193204
// returns nil if no value was found, or if IgnoreErrors is false and we could
194205
// not parse the configuration file. Use GetStrict to disambiguate the latter
@@ -259,11 +270,7 @@ func (u *UserSettings) GetAllStrict(alias, key string) ([]string, error) {
259270
if err2 != nil || val2 != nil {
260271
return val2, err2
261272
}
262-
// TODO: IdentityFile has multiple default values that we should return.
263-
if def := Default(key); def != "" {
264-
return []string{def}, nil
265-
}
266-
return []string{}, nil
273+
return DefaultAll(key, alias, u), nil
267274
}
268275

269276
// ConfigFinder will invoke f to try to find a ssh config file in a custom
@@ -425,6 +432,11 @@ func (c *Config) Get(alias, key string) (string, error) {
425432
return "", nil
426433
}
427434

435+
func (c *Config) getinternal(alias, key string) string {
436+
v, _ := c.Get(alias, key)
437+
return v
438+
}
439+
428440
// GetAll returns all values in the configuration that match the alias and
429441
// contains key, or nil if none are present.
430442
func (c *Config) GetAll(alias, key string) ([]string, error) {

config_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ func TestGetIdentities(t *testing.T) {
111111
t.Errorf("expected nil err, got %v", err)
112112
}
113113
if len(val) != len(defaultProtocol2Identities) {
114-
// TODO: return the right values here.
115-
log.Printf("expected defaults, got %v", val)
114+
t.Errorf("expected defaults, got %v", val)
116115
} else {
117116
for i, v := range defaultProtocol2Identities {
118117
if val[i] != v {

validators.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@ func Default(keyword string) string {
1515
return defaults[strings.ToLower(keyword)]
1616
}
1717

18+
// DefaultAll returns the default value for the given keyword, but as a slice. If
19+
// there is no default for the keyword, nil is returned.
20+
//
21+
// Some multi-valued settings have different defaults based on other settings, so
22+
// you must provide the host alias and a config to retrieve a setting from
23+
func DefaultAll(keyword string, alias string, cfg config) []string {
24+
if strings.ToLower(keyword) == "identityfile" && cfg.getinternal(alias, "Protocol") == "2" {
25+
def := make([]string, len(defaultProtocol2Identities))
26+
copy(def, defaultProtocol2Identities)
27+
return def
28+
}
29+
30+
def := Default(keyword)
31+
if def != "" {
32+
return []string{def}
33+
}
34+
35+
return nil
36+
}
37+
1838
// Arguments where the value must be "yes" or "no" and *only* yes or no.
1939
var yesnos = map[string]bool{
2040
strings.ToLower("BatchMode"): true,
@@ -163,10 +183,12 @@ var defaults = map[string]string{
163183

164184
// these identities are used for SSH protocol 2
165185
var defaultProtocol2Identities = []string{
166-
"~/.ssh/id_dsa",
186+
"~/.ssh/id_rsa",
167187
"~/.ssh/id_ecdsa",
188+
"~/.ssh/id_ecdsa_sk",
168189
"~/.ssh/id_ed25519",
169-
"~/.ssh/id_rsa",
190+
"~/.ssh/id_ed25519_sk",
191+
"~/.ssh/id_dsa",
170192
}
171193

172194
// these directives support multiple items that can be collected

0 commit comments

Comments
 (0)