Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only append the UserKnownHostsFile ssh flag when required #4674

Merged
merged 1 commit into from
Jun 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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