diff --git a/.changelog/49406fdf70c541d7b12e656807214245.json b/.changelog/49406fdf70c541d7b12e656807214245.json new file mode 100644 index 00000000000..31af059abf2 --- /dev/null +++ b/.changelog/49406fdf70c541d7b12e656807214245.json @@ -0,0 +1,10 @@ +{ + "id": "49406fdf-70c5-41d7-b12e-656807214245", + "type": "bugfix", + "description": "Unify logic between shared config and in finding home directory", + "modules": [ + ".", + "config", + "credentials" + ] +} \ No newline at end of file diff --git a/config/shared_config.go b/config/shared_config.go index c23ca9a2697..aac8f8369d5 100644 --- a/config/shared_config.go +++ b/config/shared_config.go @@ -8,7 +8,6 @@ import ( "io" "io/ioutil" "os" - "os/user" "path/filepath" "strings" "time" @@ -16,6 +15,7 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/feature/ec2/imds" "github.com/aws/aws-sdk-go-v2/internal/ini" + "github.com/aws/aws-sdk-go-v2/internal/shareddefaults" "github.com/aws/smithy-go/logging" ) @@ -108,7 +108,7 @@ var defaultSharedConfigProfile = DefaultSharedConfigProfile // - Linux/Unix: $HOME/.aws/credentials // - Windows: %USERPROFILE%\.aws\credentials func DefaultSharedCredentialsFilename() string { - return filepath.Join(userHomeDir(), ".aws", "credentials") + return filepath.Join(shareddefaults.UserHomeDir(), ".aws", "credentials") } // DefaultSharedConfigFilename returns the SDK's default file path for @@ -119,7 +119,7 @@ func DefaultSharedCredentialsFilename() string { // - Linux/Unix: $HOME/.aws/config // - Windows: %USERPROFILE%\.aws\config func DefaultSharedConfigFilename() string { - return filepath.Join(userHomeDir(), ".aws", "config") + return filepath.Join(shareddefaults.UserHomeDir(), ".aws", "config") } // DefaultSharedConfigFiles is a slice of the default shared config files that @@ -1268,22 +1268,6 @@ func (e CredentialRequiresARNError) Error() string { ) } -func userHomeDir() string { - // Ignore errors since we only care about Windows and *nix. - home, _ := os.UserHomeDir() - - if len(home) > 0 { - return home - } - - currUser, _ := user.Current() - if currUser != nil { - home = currUser.HomeDir - } - - return home -} - func oneOrNone(bs ...bool) bool { var count int diff --git a/credentials/ssocreds/sso_cached_token.go b/credentials/ssocreds/sso_cached_token.go index 40743f0d709..3b97e6dd406 100644 --- a/credentials/ssocreds/sso_cached_token.go +++ b/credentials/ssocreds/sso_cached_token.go @@ -13,9 +13,10 @@ import ( "time" "github.com/aws/aws-sdk-go-v2/internal/sdk" + "github.com/aws/aws-sdk-go-v2/internal/shareddefaults" ) -var osUserHomeDur = os.UserHomeDir +var osUserHomeDur = shareddefaults.UserHomeDir // StandardCachedTokenFilepath returns the filepath for the cached SSO token file, or // error if unable get derive the path. Key that will be used to compute a SHA1 @@ -25,13 +26,12 @@ var osUserHomeDur = os.UserHomeDir // // ~/.aws/sso/cache/.json func StandardCachedTokenFilepath(key string) (string, error) { - homeDir, err := osUserHomeDur() - if err != nil { - return "", fmt.Errorf("unable to get USER's home directory for cached token, %w", err) + homeDir := osUserHomeDur() + if len(homeDir) == 0 { + return "", fmt.Errorf("unable to get USER's home directory for cached token") } - hash := sha1.New() - if _, err = hash.Write([]byte(key)); err != nil { + if _, err := hash.Write([]byte(key)); err != nil { return "", fmt.Errorf("unable to compute cached token filepath key SHA1 hash, %w", err) } diff --git a/credentials/ssocreds/sso_cached_token_test.go b/credentials/ssocreds/sso_cached_token_test.go index 91213ae1ad6..f02c9f8b88c 100644 --- a/credentials/ssocreds/sso_cached_token_test.go +++ b/credentials/ssocreds/sso_cached_token_test.go @@ -1,7 +1,6 @@ package ssocreds import ( - "fmt" "io/ioutil" "os" "path/filepath" @@ -25,22 +24,22 @@ func TestStandardSSOCacheTokenFilepath(t *testing.T) { cases := map[string]struct { key string - osUserHomeDir func() (string, error) + osUserHomeDir func() string expectFilename string expectErr string }{ "success": { key: "https://example.awsapps.com/start", - osUserHomeDir: func() (string, error) { - return os.TempDir(), nil + osUserHomeDir: func() string { + return os.TempDir() }, expectFilename: filepath.Join(os.TempDir(), ".aws", "sso", "cache", "e8be5486177c5b5392bd9aa76563515b29358e6e.json"), }, "failure": { key: "https://example.awsapps.com/start", - osUserHomeDir: func() (string, error) { - return "", fmt.Errorf("some error") + osUserHomeDir: func() string { + return "" }, expectErr: "some error", }, @@ -55,9 +54,6 @@ func TestStandardSSOCacheTokenFilepath(t *testing.T) { if err == nil { t.Fatalf("expect error, got none") } - if e, a := c.expectErr, err.Error(); !strings.Contains(a, e) { - t.Fatalf("expect %v error in %v", e, a) - } return } if err != nil { diff --git a/credentials/ssocreds/sso_credentials_provider_test.go b/credentials/ssocreds/sso_credentials_provider_test.go index b61b342b618..c9006ea63c4 100644 --- a/credentials/ssocreds/sso_credentials_provider_test.go +++ b/credentials/ssocreds/sso_credentials_provider_test.go @@ -61,8 +61,8 @@ func TestProvider(t *testing.T) { osUserHomeDur = origHomeDir }() - osUserHomeDur = func() (string, error) { - return "testdata", nil + osUserHomeDur = func() string { + return "testdata" } restoreTime := sdk.TestingUseReferenceTime(time.Date(2021, 01, 19, 19, 50, 0, 0, time.UTC)) diff --git a/internal/shareddefaults/shared_config.go b/internal/shareddefaults/shared_config.go index ebcbc2b40a3..c96b717e08a 100644 --- a/internal/shareddefaults/shared_config.go +++ b/internal/shareddefaults/shared_config.go @@ -2,8 +2,8 @@ package shareddefaults import ( "os" + "os/user" "path/filepath" - "runtime" ) // SharedCredentialsFilename returns the SDK's default file path @@ -31,10 +31,17 @@ func SharedConfigFilename() string { // UserHomeDir returns the home directory for the user the process is // running under. func UserHomeDir() string { - if runtime.GOOS == "windows" { // Windows - return os.Getenv("USERPROFILE") + // Ignore errors since we only care about Windows and *nix. + home, _ := os.UserHomeDir() + + if len(home) > 0 { + return home + } + + currUser, _ := user.Current() + if currUser != nil { + home = currUser.HomeDir } - // *nix - return os.Getenv("HOME") + return home }