|
|
|
|
@ -6,6 +6,8 @@ import (
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net"
|
|
|
|
|
"os"
|
|
|
|
|
"os/user"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
packerssh "github.com/hashicorp/packer/communicator/ssh"
|
|
|
|
|
@ -13,7 +15,6 @@ import (
|
|
|
|
|
helperssh "github.com/hashicorp/packer/helper/ssh"
|
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
|
"github.com/masterzen/winrm"
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
|
"golang.org/x/crypto/ssh/agent"
|
|
|
|
|
)
|
|
|
|
|
@ -73,10 +74,16 @@ func (c *Config) ReadSSHPrivateKeyFile() ([]byte, error) {
|
|
|
|
|
var privateKey []byte
|
|
|
|
|
|
|
|
|
|
if c.SSHPrivateKeyFile != "" {
|
|
|
|
|
keyPath, err := homedir.Expand(c.SSHPrivateKeyFile)
|
|
|
|
|
u, err := user.Current()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return privateKey, fmt.Errorf("Error expanding path for SSH private key: %s", err)
|
|
|
|
|
return []byte{}, fmt.Errorf("Error trying to determine the current user for the SSH private key: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if u.HomeDir == "" {
|
|
|
|
|
return []byte{}, fmt.Errorf("Error locating home directory for the SSH private key")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keyPath := filepath.Join(u.HomeDir, c.SSHPrivateKeyFile)
|
|
|
|
|
privateKey, err = ioutil.ReadFile(keyPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return privateKey, fmt.Errorf("Error on reading SSH private key: %s", err)
|
|
|
|
|
@ -260,8 +267,19 @@ func (c *Config) prepareSSH(ctx *interpolate.Context) []error {
|
|
|
|
|
errs = append(errs, errors.New("An ssh_username must be specified\n Note: some builders used to default ssh_username to \"root\"."))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Figure out the current user's home directory
|
|
|
|
|
u, err := user.Current()
|
|
|
|
|
if err != nil {
|
|
|
|
|
errs = append(errs, fmt.Errorf("Unable to determine the current user from : %s", err))
|
|
|
|
|
} else if u.HomeDir == "" {
|
|
|
|
|
// If the home directory is empty, then set `err` so that later a failure will happen
|
|
|
|
|
err = fmt.Errorf("Unable to determine the home directory for the current user.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.SSHPrivateKeyFile != "" {
|
|
|
|
|
path, err := homedir.Expand(c.SSHPrivateKeyFile)
|
|
|
|
|
path := filepath.Join(u.HomeDir, c.SSHPrivateKeyFile)
|
|
|
|
|
|
|
|
|
|
// The `err` variable here comes from an empty home directory
|
|
|
|
|
if err != nil {
|
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
|
"ssh_private_key_file is invalid: %s", err))
|
|
|
|
|
@ -279,7 +297,9 @@ func (c *Config) prepareSSH(ctx *interpolate.Context) []error {
|
|
|
|
|
errs = append(errs, errors.New(
|
|
|
|
|
"ssh_bastion_password or ssh_bastion_private_key_file must be specified"))
|
|
|
|
|
} else if c.SSHBastionPrivateKeyFile != "" {
|
|
|
|
|
path, err := homedir.Expand(c.SSHBastionPrivateKeyFile)
|
|
|
|
|
path := filepath.Join(u.HomeDir, c.SSHBastionPrivateKeyFile)
|
|
|
|
|
|
|
|
|
|
// The `err` variable here comes from an empty home directory
|
|
|
|
|
if err != nil {
|
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
|
"ssh_bastion_private_key_file is invalid: %s", err))
|
|
|
|
|
|