Skip to content

Commit

Permalink
Support relative paths for the credentials file (#130)
Browse files Browse the repository at this point in the history
Expansion of environment variables is also supported, $var or ${var}
in the path will be replaced with the values of the current
environment variables.
  • Loading branch information
johan3141592 committed Oct 27, 2023
1 parent f3f8187 commit e98f28c
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions pkg/polaris/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,19 @@ func UserAccountFromEnv() (*UserAccount, error) {
// userAccountFromFile returns a UserAccount from the specified file with the
// given name.
func userAccountFromFile(file, name string) (UserAccount, error) {
if strings.HasPrefix(file, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return UserAccount{}, fmt.Errorf("failed to get home dir: %v", err)
}
file = filepath.Join(home, strings.TrimPrefix(file, "~/"))
expFile, err := expandPath(file)
if err != nil {
return UserAccount{}, fmt.Errorf("failed to expand file path: %s", err)
}

buf, err := os.ReadFile(file)
buf, err := os.ReadFile(expFile)
if err != nil {
return UserAccount{}, fmt.Errorf("failed to read user account file: %v", err)
return UserAccount{}, fmt.Errorf("failed to read user account file: %s", err)
}

var accounts map[string]UserAccount
if err := json.Unmarshal(buf, &accounts); err != nil {
return UserAccount{}, fmt.Errorf("failed to unmarshal user account file: %v", err)
return UserAccount{}, fmt.Errorf("failed to unmarshal user account file: %s", err)
}

account, ok := accounts[name]
Expand Down Expand Up @@ -254,22 +251,19 @@ func ServiceAccountFromEnv() (*ServiceAccount, error) {

// serviceAccountFromFile returns a ServiceAccount from the specified file.
func serviceAccountFromFile(file string) (ServiceAccount, error) {
if strings.HasPrefix(file, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return ServiceAccount{}, fmt.Errorf("failed to get home dir: %v", err)
}
file = filepath.Join(home, strings.TrimPrefix(file, "~/"))
expFile, err := expandPath(file)
if err != nil {
return ServiceAccount{}, fmt.Errorf("failed to expand file path: %s", err)
}

buf, err := os.ReadFile(file)
buf, err := os.ReadFile(expFile)
if err != nil {
return ServiceAccount{}, fmt.Errorf("failed to read service account file: %v", err)
return ServiceAccount{}, fmt.Errorf("failed to read service account file: %s", err)
}

var account ServiceAccount
if err := json.Unmarshal(buf, &account); err != nil {
return ServiceAccount{}, fmt.Errorf("failed to unmarshal service account file: %v", err)
return ServiceAccount{}, fmt.Errorf("failed to unmarshal service account file: %s", err)
}

return account, nil
Expand Down Expand Up @@ -353,3 +347,23 @@ func ServiceAccountFromFile(file string, allowEnvOverride bool) (*ServiceAccount
func DefaultServiceAccount(allowEnvOverride bool) (*ServiceAccount, error) {
return ServiceAccountFromFile(DefaultServiceAccountFile, allowEnvOverride)
}

func expandPath(file string) (string, error) {
// Expand the ~ token to the user's home directory.
if homeToken := fmt.Sprintf("~%c", filepath.Separator); strings.HasPrefix(file, homeToken) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
file = filepath.Join(home, strings.TrimPrefix(file, homeToken))
}

// Expand environment variables and make sure that the path is absolute.
var err error
file, err = filepath.Abs(os.ExpandEnv(file))
if err != nil {
return "", err
}

return file, nil
}

0 comments on commit e98f28c

Please sign in to comment.