From e04986659af93fbed426fd9342e4e66841eabcdc Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Sun, 2 Dec 2018 18:06:14 -0600 Subject: [PATCH] Changed the logic for when to actually prefix a path with a u.HomeDir so that it's only done when a non-absolute path is specified. --- builder/oracle/oci/config.go | 6 +++++- helper/communicator/config.go | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/builder/oracle/oci/config.go b/builder/oracle/oci/config.go index 948b2fc23..85de3b2cc 100644 --- a/builder/oracle/oci/config.go +++ b/builder/oracle/oci/config.go @@ -103,7 +103,11 @@ func NewConfig(raws ...interface{}) (*Config, error) { return nil, fmt.Errorf("Unable to determine the home directory for the current user.") } - path := filepath.Join(u.HomeDir, c.KeyFile) + // If c.KeyFile is not an absolute path, then it's relative so prefix it relative to the home directory + path := c.KeyFile + if !filepath.IsAbs(c.KeyFile) { + path = filepath.Join(u.HomeDir, c.KeyFile) + } // Read API signing key keyContent, err = ioutil.ReadFile(path) diff --git a/helper/communicator/config.go b/helper/communicator/config.go index 117602d33..0bd54a134 100644 --- a/helper/communicator/config.go +++ b/helper/communicator/config.go @@ -83,7 +83,12 @@ func (c *Config) ReadSSHPrivateKeyFile() ([]byte, error) { return []byte{}, fmt.Errorf("Error locating home directory for the SSH private key") } - keyPath := filepath.Join(u.HomeDir, c.SSHPrivateKeyFile) + // If c.SSHPrivateKeyFile is not an absolute path, then it's relative and should be prefixed with u.HomeDir + keyPath := c.SSHPrivateKeyFile + if !filepath.IsAbs(c.SSHPrivateKeyFile) { + 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) @@ -277,9 +282,14 @@ func (c *Config) prepareSSH(ctx *interpolate.Context) []error { } if c.SSHPrivateKeyFile != "" { - path := filepath.Join(u.HomeDir, c.SSHPrivateKeyFile) + path := c.SSHPrivateKeyFile - // The `err` variable here comes from an empty home directory + // If c.SSHPrivateKeyFile is not an absolute path, then it's relative and should be prefixed with u.HomeDir + if err == nil && !filepath.IsAbs(c.SSHPrivateKeyFile) { + path = filepath.Join(u.HomeDir, c.SSHPrivateKeyFile) + } + + // The `err` variable here comes from the empty home directory above if err != nil { errs = append(errs, fmt.Errorf( "ssh_private_key_file is invalid: %s", err))