Skip to content

Commit

Permalink
Only append the UserKnownHostsFile ssh flag when required (#4674)
Browse files Browse the repository at this point in the history
Don't set a default value for the UserKnownHostsFile flag.
Only append `-o UserKnownHostsFile` to the ssh command if it
has been specified by the user or vault ssh has set it based on another
flag (such as flagHostKeyMountPoint)

Fixes #4672
  • Loading branch information
Crazybus authored and jefferai committed Jun 1, 2018
1 parent 75aa8df commit 7090348
Showing 1 changed file with 43 additions and 21 deletions.
64 changes: 43 additions & 21 deletions command/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (c *SSHCommand) Flags() *FlagSets {
f.StringVar(&StringVar{
Name: "user-known-hosts-file",
Target: &c.flagUserKnownHostsFile,
Default: "~/.ssh/known_hosts",
Default: "",
EnvVar: "VAULT_SSH_USER_KNOWN_HOSTS_FILE",
Completion: complete.PredictFiles("*"),
Usage: "Value to use for the SSH configuration option " +
Expand Down Expand Up @@ -451,10 +451,21 @@ func (c *SSHCommand) handleTypeCA(username, hostname, ip string, sshArgs []strin
args := append([]string{
"-i", c.flagPrivateKeyPath,
"-i", signedPublicKeyPath,
"-o UserKnownHostsFile=" + userKnownHostsFile,
"-o StrictHostKeyChecking=" + strictHostKeyChecking,
username + "@" + hostname,
}, sshArgs...)
})

if userKnownHostsFile != "" {
args = append(args,
"-o UserKnownHostsFile="+userKnownHostsFile,
)
}

args = append(args,
username+"@"+hostname,
)

// Add extra user defined ssh arguments
args = append(args, sshArgs...)

cmd := exec.Command("ssh", args...)
cmd.Stdin = os.Stdin
Expand Down Expand Up @@ -503,36 +514,47 @@ func (c *SSHCommand) handleTypeOTP(username, hostname string, ip string, sshArgs
// it is then, use it to automate typing in OTP to the prompt. Unfortunately,
// it was not possible to automate it without a third-party application, with
// only the Go libraries. Feel free to try and remove this dependency.
args := make([]string, 0)
env := os.Environ()
sshCmd := "ssh"

sshpassPath, err := exec.LookPath("sshpass")
if err != nil {
// No sshpass available so using normal ssh client
c.UI.Warn(wrapAtLength(
"Vault could not locate \"sshpass\". The OTP code for the session is " +
"displayed below. Enter this code in the SSH password prompt. If you " +
"install sshpass, Vault can automatically perform this step for you."))
c.UI.Output("OTP for the session is: " + cred.Key)

args := append([]string{
"-o UserKnownHostsFile=" + c.flagUserKnownHostsFile,
"-o StrictHostKeyChecking=" + c.flagStrictHostKeyChecking,
"-p", cred.Port,
username + "@" + hostname,
}, sshArgs...)
cmd = exec.Command("ssh", args...)
} else {
args := append([]string{
// sshpass is available so lets use it instead
sshCmd = sshpassPath
args = append(args,
"-e", // Read password for SSHPASS environment variable
"ssh",
"-o UserKnownHostsFile=" + c.flagUserKnownHostsFile,
"-o StrictHostKeyChecking=" + c.flagStrictHostKeyChecking,
"-p", cred.Port,
username + "@" + hostname,
}, sshArgs...)
cmd = exec.Command(sshpassPath, args...)
env := os.Environ()
)
env = append(env, fmt.Sprintf("SSHPASS=%s", string(cred.Key)))
cmd.Env = env
}

// Only harcode the knownhostsfile path if it has been set
if c.flagUserKnownHostsFile != "" {
args = append(args,
"-o UserKnownHostsFile="+c.flagUserKnownHostsFile,
)
}

args = append(args,
"-o StrictHostKeyChecking="+c.flagStrictHostKeyChecking,
"-p", cred.Port,
username+"@"+hostname,
)

// Add the rest of the ssh args appended by the user
args = append(args, sshArgs...)

cmd = exec.Command(sshCmd, args...)
cmd.Env = env

cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down

0 comments on commit 7090348

Please sign in to comment.