Skip to content

Commit

Permalink
Do not resolve variables in hostnames twice (fix #103)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Feb 3, 2016
1 parent 21cb898 commit c403cf4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ Get a released version on: https://github.com/moul/advanced-ssh-config/releases
* Avoid exiting when an included file contains errors ([#95](https://github.com/moul/advanced-ssh-config/issues/95))
* Anonymize paths in `assh info`
* Support of `assh proxy --dry-run` option
* Fix: do not resolve variables in hostnames twice ([#103](https://github.com/moul/advanced-ssh-config/issues/103))

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

Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
)

func cmdProxy(c *cli.Context) {
Logger.Debugf("assh args: %s", c.Args())

if len(c.Args()) < 1 {
Logger.Fatalf("assh: \"proxy\" requires 1 argument. See 'assh proxy --help'.")
}
Expand Down
12 changes: 11 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,17 @@ func computeHost(host *Host, config *Config, name string, fullCompute bool) (*Ho
// expands variables in host
// i.e: %h.some.zone -> {name}.some.zone
hostname := strings.Replace(computedHost.HostName, "%h", "%n", -1)
computedHost.HostName = computedHost.ExpandString(hostname)

// ssh resolve '%h' in hostnames
// -> we bypass the string expansion if the input matches
// an already resolved hostname
// See https://github.com/moul/advanced-ssh-config/issues/103
pattern := strings.Replace(hostname, "%n", "*", -1)
if match, _ := path.Match(pattern, computedHost.inputName); match {
computedHost.HostName = computedHost.inputName
} else {
computedHost.HostName = computedHost.ExpandString(hostname)
}
}

return computedHost, nil
Expand Down
22 changes: 19 additions & 3 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ hosts:
"*.kkk":
HostName: "%h.kkkkk"
"lll-*":
HostName: "%h.lll"
nnn:
Inherits:
- mmm
Expand Down Expand Up @@ -198,7 +201,7 @@ func TestConfig_LoadConfig(t *testing.T) {
config := New()
err := config.LoadConfig(strings.NewReader(yamlConfig))
So(err, ShouldBeNil)
So(len(config.Hosts), ShouldEqual, 12)
So(len(config.Hosts), ShouldEqual, 13)
So(config.Hosts["aaa"].HostName, ShouldEqual, "1.2.3.4")
So(config.Hosts["aaa"].Port, ShouldEqual, "")
So(config.Hosts["aaa"].User, ShouldEqual, "")
Expand Down Expand Up @@ -357,6 +360,9 @@ func TestConfig_JsonSring(t *testing.T) {
"jjj": {
"HostName": "%h.jjjjj"
},
"lll-*": {
"HostName": "%h.lll"
},
"nnn": {
"User": "nnnn",
"Inherits": [
Expand Down Expand Up @@ -422,6 +428,16 @@ func TestComputeHost(t *testing.T) {
So(err, ShouldBeNil)
So(computed.HostName, ShouldEqual, "test.kkk.kkkkk")
})
Convey("Do not expand variables twice", func() {
host := config.Hosts["lll-*"]
computed, err := computeHost(&host, config, "lll-42", true)
So(err, ShouldBeNil)
So(computed.HostName, ShouldEqual, "lll-42.lll")

computed, err = computeHost(&host, config, "lll-43.lll", true)
So(err, ShouldBeNil)
So(computed.HostName, ShouldEqual, "lll-43.lll")
})
Convey("Expand variables using environment", func() {
host := config.Hosts["bbb"]
So(host.HostName, ShouldEqual, "$ENV_VAR_HOSTNAME")
Expand Down Expand Up @@ -563,7 +579,7 @@ func TestConfig_LoadFiles(t *testing.T) {
So(err, ShouldBeNil)
So(config.includedFiles[file.Name()], ShouldEqual, true)
So(len(config.includedFiles), ShouldEqual, 1)
So(len(config.Hosts), ShouldEqual, 12)
So(len(config.Hosts), ShouldEqual, 13)
So(config.Hosts["aaa"].HostName, ShouldEqual, "1.2.3.4")
So(config.Hosts["aaa"].Port, ShouldEqual, "")
So(config.Hosts["aaa"].User, ShouldEqual, "")
Expand Down Expand Up @@ -591,7 +607,7 @@ func TestConfig_LoadFiles(t *testing.T) {
So(err, ShouldBeNil)
So(config.includedFiles[file.Name()], ShouldEqual, true)
So(len(config.includedFiles), ShouldEqual, 1)
So(len(config.Hosts), ShouldEqual, 12)
So(len(config.Hosts), ShouldEqual, 13)
So(config.Hosts["aaa"].HostName, ShouldEqual, "1.2.3.4")
So(config.Hosts["aaa"].Port, ShouldEqual, "")
So(config.Hosts["aaa"].User, ShouldEqual, "")
Expand Down

0 comments on commit c403cf4

Please sign in to comment.