Skip to content

Commit

Permalink
Fix inheritance for all fields (fix #54)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Sep 9, 2015
1 parent 2c71892 commit 99677a6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ Get a released version on: https://github.com/moul/advanced-ssh-config/releases

### master (unreleased)

* No entry
* Fix: inheritance was not working for non assh-related fields ([#54](https://github.com/moul/advanced-ssh-config/issues/54))

[Full commits list](https://github.com/moul/advanced-ssh-config/compare/v2.0.0...master)

Expand Down
53 changes: 30 additions & 23 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *Config) JsonString() ([]byte, error) {
return output, err
}

// computeHost applies defaults, inherits hosts and configure internal fields
// computeHost returns a copy of the host with applied defaults, resolved inheritances and configured internal fields
func computeHost(host *Host, config *Config, name string, fullCompute bool) (*Host, error) {
var computedHost Host
if host != nil {
Expand All @@ -47,30 +47,33 @@ func computeHost(host *Host, config *Config, name string, fullCompute bool) (*Ho
// self is already inherited
computedHost.inherited[name] = true

// Inheritance
// FIXME: allow deeper inheritance:
// currently not resolving inherited hosts
// we should resolve all inherited hosts and pass the
// currently resolved hosts to avoid computing an host twice
for _, name := range host.Inherits {
_, found := computedHost.inherited[name]
if found {
Logger.Debugf("Detected circular loop inheritance, skiping...")
continue
}
computedHost.inherited[name] = true

target, err := config.getHostByPath(name, false, false)
if err != nil {
Logger.Warnf("Cannot inherits from %q: %v", name, err)
continue
}
computedHost.ApplyDefaults(target)
}

// fullCompute applies config.Defaults
// config.Defaults should be applied when proxying
// but should not when exporting .ssh/config file
if fullCompute {
// apply defaults based on "Host *"
computedHost.ApplyDefaults(&config.Defaults)

// Inheritance
// FIXME: allow deeper inheritance:
// currently not resolving inherited hosts
// we should resolve all inherited hosts and pass the
// currently resolved hosts to avoid computing an host twice
for _, name := range host.Inherits {
_, found := computedHost.inherited[name]
if found {
Logger.Debugf("Detected circular loop inheritance, skiping...")
continue
}
computedHost.inherited[name] = true

target, err := config.getHostByPath(name, false, false)
if err != nil {
Logger.Warnf("Cannot inherits from %q: %v", name, err)
continue
}
computedHost.ApplyDefaults(target)
}
}

return &computedHost, nil
Expand Down Expand Up @@ -266,7 +269,11 @@ func (c *Config) WriteSshConfigTo(w io.Writer) error {
fmt.Fprintln(w, "# host-based configuration")
for _, name := range c.sortedNames() {
host := c.Hosts[name]
host.WriteSshConfigTo(w)
computedHost, err := computeHost(&host, c, name, false)
if err != nil {
return err
}
computedHost.WriteSshConfigTo(w)
fmt.Fprintln(w)
}

Expand Down
12 changes: 10 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,9 @@ func TestConfig_GetHost(t *testing.T) {
"toto": true,
})
So(host.ProxyCommand, ShouldEqual, "nc -v 4242")
So(host.User, ShouldEqual, "root")
So(host.User, ShouldEqual, "moul")
So(host.Gateways, ShouldResemble, []string{"titi", "direct", "1.2.3.4"})
So(host.PasswordAuthentication, ShouldEqual, "")
So(host.PasswordAuthentication, ShouldEqual, "yes")

host, err = config.GetHost("tutu")
So(err, ShouldBeNil)
Expand Down Expand Up @@ -562,6 +562,11 @@ Host *.ddd
Host empty
Host tata
HostName 1.2.3.4
PasswordAuthentication yes
Port 22
User moul
# ProxyCommand nc -v 4242
Host titi
HostName tata
Expand All @@ -577,6 +582,9 @@ Host toto
Host toutou
Host tutu
HostName 1.2.3.4
PasswordAuthentication yes
Port 22
# global configuration
Host *
Expand Down

0 comments on commit 99677a6

Please sign in to comment.