From 6d2a0ab0dfcbdc90284848da30bdfee5d5286005 Mon Sep 17 00:00:00 2001 From: Mikhail Ushanov Date: Thu, 1 Nov 2018 00:47:50 +0300 Subject: [PATCH] communicator/ssh: expand user path for private key Signed-off-by: Mikhail Ushanov --- helper/communicator/config.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/helper/communicator/config.go b/helper/communicator/config.go index b7041eb40..bf481a636 100644 --- a/helper/communicator/config.go +++ b/helper/communicator/config.go @@ -13,6 +13,7 @@ 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" ) @@ -109,12 +110,11 @@ func (c *Config) SSHConfigFunc() func(multistep.StateBag) (*ssh.ClientConfig, er var privateKeys [][]byte if c.SSHPrivateKeyFile != "" { - // key based auth - bytes, err := ioutil.ReadFile(c.SSHPrivateKeyFile) + privateKey, err := c.ReadSSHPrivateKeyFile() if err != nil { - return nil, fmt.Errorf("Error setting up SSH config: %s", err) + return nil, err } - privateKeys = append(privateKeys, bytes) + privateKeys = append(privateKeys, privateKey) } // aws,alicloud,cloudstack,digitalOcean,oneAndOne,openstack,oracle & profitbricks key @@ -260,10 +260,14 @@ func (c *Config) prepareSSH(ctx *interpolate.Context) []error { } if c.SSHPrivateKeyFile != "" { - if _, err := os.Stat(c.SSHPrivateKeyFile); err != nil { + path, err := homedir.Expand(c.SSHPrivateKeyFile) + if err != nil { + errs = append(errs, fmt.Errorf( + "ssh_private_key_file is invalid: %s", err)) + } else if _, err := os.Stat(path); err != nil { errs = append(errs, fmt.Errorf( "ssh_private_key_file is invalid: %s", err)) - } else if _, err := helperssh.FileSigner(c.SSHPrivateKeyFile); err != nil { + } else if _, err := helperssh.FileSigner(path); err != nil { errs = append(errs, fmt.Errorf( "ssh_private_key_file is invalid: %s", err)) }