Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export account not found #183

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions pkg/polaris/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ const (
)

var (
// errAccountNotFound signals that no trace of the account was found.
errAccountNotFound accountNotFound
// ErrAccountNotFound signals that the account could not be found.
// Note that this is different from the account data being invalid or
// malformed.
ErrAccountNotFound = errors.New("account not found")
)

// Account represents a Polaris account. Implemented by UserAccount and
Expand Down Expand Up @@ -109,46 +111,46 @@ func FindAccount(credentials string, allowEnvOverride bool) (Account, error) {
if err == nil {
return serviceAccount, nil
}
if !errors.Is(err, errAccountNotFound) {
if !errors.Is(err, ErrAccountNotFound) {
return nil, fmt.Errorf("failed to load service account from default file: %s", err)
}

serviceAccount, err = ServiceAccountFromEnv()
if err == nil {
return serviceAccount, nil
}
if !errors.Is(err, errAccountNotFound) {
if !errors.Is(err, ErrAccountNotFound) {
return nil, fmt.Errorf("failed to load service account from env: %s", err)
}

return nil, errors.New("failed to load account, searched: default service account file and env")
return nil, fmt.Errorf("%w, searched: default service account file and env", ErrAccountNotFound)
}

serviceAccount, err := ServiceAccountFromText(credentials, allowEnvOverride)
if err == nil {
return serviceAccount, nil
}
if !errors.Is(err, errAccountNotFound) {
if !errors.Is(err, ErrAccountNotFound) {
return nil, fmt.Errorf("failed to load service account from text: %s", err)
}

serviceAccount, err = ServiceAccountFromFile(credentials, allowEnvOverride)
if err == nil {
return serviceAccount, nil
}
if !errors.Is(err, errAccountNotFound) {
if !errors.Is(err, ErrAccountNotFound) {
return nil, fmt.Errorf("failed to load service account from file: %s", err)
}

userAccount, err := DefaultUserAccount(credentials, allowEnvOverride)
if err == nil {
return userAccount, nil
}
if !errors.Is(err, errAccountNotFound) {
if !errors.Is(err, ErrAccountNotFound) {
return nil, fmt.Errorf("failed to load user account from default file: %s", err)
}

return nil, errors.New("failed to load account, searched: passed in credentials, default service account file and default user account file")
return nil, fmt.Errorf("%w, searched: passed in credentials, default service account file and default user account file", ErrAccountNotFound)
}

// UserAccount holds an RSC local user account configuration. Depending on how
Expand Down Expand Up @@ -259,7 +261,7 @@ func UserAccountFromEnv() (*UserAccount, error) {
account.envOverride = true

if account.Name == "" && account.Username == "" && account.Password == "" && account.URL == "" {
return nil, fmt.Errorf("%wuser account not found in env", errAccountNotFound)
return nil, fmt.Errorf("%w in env", ErrAccountNotFound)
}

if err := initUserAccount(&account); err != nil {
Expand Down Expand Up @@ -394,7 +396,7 @@ func userAccountFromFile(file, name string) (UserAccount, error) {
if err == nil {
err = fmt.Errorf("user account file is a directory")
}
return UserAccount{}, fmt.Errorf("%wfailed to access user account file: %s", errAccountNotFound, err)
return UserAccount{}, fmt.Errorf("%w in file: %s", ErrAccountNotFound, err)
}
buf, err := os.ReadFile(expFile)
if err != nil {
Expand Down Expand Up @@ -538,7 +540,7 @@ func ServiceAccountFromEnv() (*ServiceAccount, error) {
account.envOverride = true

if account.Name == "" && account.ClientID == "" && account.ClientSecret == "" && account.AccessTokenURI == "" {
return nil, fmt.Errorf("%wservice account not found in env", errAccountNotFound)
return nil, fmt.Errorf("%w in env", ErrAccountNotFound)
}

if err := initServiceAccount(&account); err != nil {
Expand Down Expand Up @@ -645,7 +647,7 @@ func serviceAccountFromFile(file string) (ServiceAccount, error) {
if err == nil {
err = fmt.Errorf("service account file is a directory")
}
return ServiceAccount{}, fmt.Errorf("%wfailed to access service account file: %s", errAccountNotFound, err)
return ServiceAccount{}, fmt.Errorf("%w in file: %s", ErrAccountNotFound, err)
}
buf, err := os.ReadFile(expFile)
if err != nil {
Expand All @@ -665,7 +667,7 @@ func serviceAccountFromFile(file string) (ServiceAccount, error) {
func serviceAccountFromString(s string) (ServiceAccount, error) {
var account ServiceAccount
if err := json.Unmarshal([]byte(s), &account); err != nil {
return ServiceAccount{}, fmt.Errorf("%wfailed to unmarshal service account text: %s", errAccountNotFound, err)
return ServiceAccount{}, fmt.Errorf("%w in text: %s", ErrAccountNotFound, err)
}

return account, nil
Expand Down Expand Up @@ -725,13 +727,6 @@ func overrideServiceAccount(account *ServiceAccount, other ServiceAccount) {
}
}

// accountNotFound is an error type that signals that an account was not found.
type accountNotFound struct{}

func (e accountNotFound) Error() string {
return ""
}

func expandPath(file string) (string, error) {
// Expand the ~ token to the user's home directory. This should never fail
// unless the shell environment is broken.
Expand Down
18 changes: 9 additions & 9 deletions pkg/polaris/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ func TestFindAccount(t *testing.T) {
errPrefix: "failed to load service account from text: invalid service account client secret",
}, {
name: "NoDefaultAndNoCredentials",
errPrefix: "failed to load account, searched: default service account file and env",
errPrefix: "account not found, searched: default service account file and env",
}, {
name: "NoDefaultAndInvalidCredentials",
text: "Content of a file not containing an RSC service account",
errPrefix: "failed to load account, searched: passed in credentials, default service account file and default user account file",
errPrefix: "account not found, searched: passed in credentials, default service account file and default user account file",
}}

tempDir := t.TempDir()
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestServiceAccountFromEnv(t *testing.T) {
},
}, {
name: "NoVars",
errPrefix: "service account not found in env",
errPrefix: "account not found in env",
}}

for _, testCase := range testCases {
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestServiceAccountFromFile(t *testing.T) {
file: toText(serviceAccount{}),
envs: map[string]string{keyServiceAccountFile: "/path/to/some/file"},
override: true,
errPrefix: "failed to access service account file",
errPrefix: "account not found in file",
}, {
name: "ClientSecretInEnvNoOverride",
file: toText(serviceAccount{noClientSecret: true}),
Expand All @@ -296,7 +296,7 @@ func TestServiceAccountFromFile(t *testing.T) {
}, {
name: "NoFileName",
noFile: true,
errPrefix: "failed to access service account file",
errPrefix: "account not found in file",
}, {
name: "NoFileContent",
errPrefix: "failed to unmarshal service account file",
Expand Down Expand Up @@ -383,11 +383,11 @@ func TestServiceAccountFromText(t *testing.T) {
errPrefix: "invalid service account access token uri",
}, {
name: "EmptyText",
errPrefix: "failed to unmarshal service account text",
errPrefix: "account not found in text",
}, {
name: "InvalidText",
text: "/path/to/some/file",
errPrefix: "failed to unmarshal service account text",
errPrefix: "account not found in text",
}}

for _, testCase := range testCases {
Expand Down Expand Up @@ -480,7 +480,7 @@ func TestUserAccountFromEnv(t *testing.T) {
},
}, {
name: "NoVars",
errPrefix: "user account not found in env",
errPrefix: "account not found in env",
}}

for _, testCase := range testCases {
Expand Down Expand Up @@ -582,7 +582,7 @@ func TestUserAccountFromFile(t *testing.T) {
}, {
name: "NoFile",
noFile: true,
errPrefix: "failed to access user account file",
errPrefix: "account not found in file",
}, {
name: "InvalidFileContent",
file: "Content of a file not containing an RSC user account",
Expand Down