Skip to content

Commit

Permalink
fix: always create empty directory under controlpath (#394)
Browse files Browse the repository at this point in the history
Fix #393.

Also remove gateway name from `%h` realce in `expandSSHTokens`. Only
add the gateway name in the path here has no effect to raw `ssh`
command, because the raw `ssh` command will only replace the `%h` with
the remote hostname.
  • Loading branch information
wd committed Feb 17, 2021
1 parent e8f8068 commit 7a2307e
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions pkg/commands/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func computeHost(dest string, portOverride int, conf *config.Config) (*config.Ho
return host, nil
}

func expandSSHTokens(tokenized string, host *config.Host, gateway *config.Host) string {
func expandSSHTokens(tokenized string, host *config.Host) string {
result := tokenized

// OpenSSH Token Cheatsheet (stolen directly from the man pages)
Expand Down Expand Up @@ -191,7 +191,7 @@ func expandSSHTokens(tokenized string, host *config.Host, gateway *config.Host)

result = strings.Replace(result, "%%", "%", -1)
result = strings.Replace(result, "%C", "%l%h%p%r", -1)
result = strings.Replace(result, "%h", path.Join(host.Name(), gateway.Name()), -1)
result = strings.Replace(result, "%h", host.Name(), -1)
result = strings.Replace(result, "%i", strconv.Itoa(os.Geteuid()), -1)
result = strings.Replace(result, "%p", host.Port, -1)

Expand All @@ -214,20 +214,19 @@ func expandSSHTokens(tokenized string, host *config.Host, gateway *config.Host)
return result
}

func prepareHostControlPath(host, gateway *config.Host) error {
if !config.BoolVal(host.ControlMasterMkdir) && ("none" == host.ControlPath || "" == host.ControlPath) {
func prepareHostControlPath(host *config.Host) error {
if !config.BoolVal(host.ControlMasterMkdir) || ("none" == host.ControlPath || "" == host.ControlPath) {
return nil
}

controlPath := expandSSHTokens(host.ControlPath, host, gateway)
controlPath := expandSSHTokens(host.ControlPath, host)
controlPathDir := path.Dir(controlPath)
logger().Debug("Creating control path", zap.String("path", controlPathDir))
return os.MkdirAll(controlPathDir, 0700)
}

func proxy(host *config.Host, conf *config.Config, dryRun bool) error {
emptygw := config.Host{}
if err := prepareHostControlPath(host.Clone(), emptygw.Clone()); err != nil {
if err := prepareHostControlPath(host.Clone()); err != nil {
return errors.Wrap(err, "failed to prepare host control-path")
}

Expand All @@ -247,7 +246,7 @@ func proxy(host *config.Host, conf *config.Config, dryRun bool) error {
hostCopy := host.Clone()
gatewayHost := conf.GetGatewaySafe(gateway)

if err := prepareHostControlPath(hostCopy, gatewayHost); err != nil {
if err := prepareHostControlPath(hostCopy); err != nil {
return errors.Wrap(err, "failed to prepare host control-path")
}

Expand Down

0 comments on commit 7a2307e

Please sign in to comment.