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.

This commit is contained in:
Ali Rizvi-Santiago 2018-12-02 18:06:14 -06:00
parent a4a42001f5
commit e04986659a
2 changed files with 18 additions and 4 deletions

View File

@ -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)

View File

@ -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))