Skip to content

Commit 6067fb2

Browse files
authored
Unify home dir logic between shared config and sso (#1960)
Take the logic (for grabbing a home dir) from shared config. move it to internal shareddefaults. and then use shareddefaults for both sso and shared config
1 parent 6fad028 commit 6067fb2

File tree

6 files changed

+38
-41
lines changed

6 files changed

+38
-41
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": "49406fdf-70c5-41d7-b12e-656807214245",
3+
"type": "bugfix",
4+
"description": "Unify logic between shared config and in finding home directory",
5+
"modules": [
6+
".",
7+
"config",
8+
"credentials"
9+
]
10+
}

config/shared_config.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88
"io"
99
"io/ioutil"
1010
"os"
11-
"os/user"
1211
"path/filepath"
1312
"strings"
1413
"time"
1514

1615
"github.com/aws/aws-sdk-go-v2/aws"
1716
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
1817
"github.com/aws/aws-sdk-go-v2/internal/ini"
18+
"github.com/aws/aws-sdk-go-v2/internal/shareddefaults"
1919
"github.com/aws/smithy-go/logging"
2020
)
2121

@@ -108,7 +108,7 @@ var defaultSharedConfigProfile = DefaultSharedConfigProfile
108108
// - Linux/Unix: $HOME/.aws/credentials
109109
// - Windows: %USERPROFILE%\.aws\credentials
110110
func DefaultSharedCredentialsFilename() string {
111-
return filepath.Join(userHomeDir(), ".aws", "credentials")
111+
return filepath.Join(shareddefaults.UserHomeDir(), ".aws", "credentials")
112112
}
113113

114114
// DefaultSharedConfigFilename returns the SDK's default file path for
@@ -119,7 +119,7 @@ func DefaultSharedCredentialsFilename() string {
119119
// - Linux/Unix: $HOME/.aws/config
120120
// - Windows: %USERPROFILE%\.aws\config
121121
func DefaultSharedConfigFilename() string {
122-
return filepath.Join(userHomeDir(), ".aws", "config")
122+
return filepath.Join(shareddefaults.UserHomeDir(), ".aws", "config")
123123
}
124124

125125
// DefaultSharedConfigFiles is a slice of the default shared config files that
@@ -1268,22 +1268,6 @@ func (e CredentialRequiresARNError) Error() string {
12681268
)
12691269
}
12701270

1271-
func userHomeDir() string {
1272-
// Ignore errors since we only care about Windows and *nix.
1273-
home, _ := os.UserHomeDir()
1274-
1275-
if len(home) > 0 {
1276-
return home
1277-
}
1278-
1279-
currUser, _ := user.Current()
1280-
if currUser != nil {
1281-
home = currUser.HomeDir
1282-
}
1283-
1284-
return home
1285-
}
1286-
12871271
func oneOrNone(bs ...bool) bool {
12881272
var count int
12891273

credentials/ssocreds/sso_cached_token.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313
"time"
1414

1515
"github.com/aws/aws-sdk-go-v2/internal/sdk"
16+
"github.com/aws/aws-sdk-go-v2/internal/shareddefaults"
1617
)
1718

18-
var osUserHomeDur = os.UserHomeDir
19+
var osUserHomeDur = shareddefaults.UserHomeDir
1920

2021
// StandardCachedTokenFilepath returns the filepath for the cached SSO token file, or
2122
// error if unable get derive the path. Key that will be used to compute a SHA1
@@ -25,13 +26,12 @@ var osUserHomeDur = os.UserHomeDir
2526
//
2627
// ~/.aws/sso/cache/<sha1-hex-encoded-key>.json
2728
func StandardCachedTokenFilepath(key string) (string, error) {
28-
homeDir, err := osUserHomeDur()
29-
if err != nil {
30-
return "", fmt.Errorf("unable to get USER's home directory for cached token, %w", err)
29+
homeDir := osUserHomeDur()
30+
if len(homeDir) == 0 {
31+
return "", fmt.Errorf("unable to get USER's home directory for cached token")
3132
}
32-
3333
hash := sha1.New()
34-
if _, err = hash.Write([]byte(key)); err != nil {
34+
if _, err := hash.Write([]byte(key)); err != nil {
3535
return "", fmt.Errorf("unable to compute cached token filepath key SHA1 hash, %w", err)
3636
}
3737

credentials/ssocreds/sso_cached_token_test.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ssocreds
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"os"
76
"path/filepath"
@@ -25,22 +24,22 @@ func TestStandardSSOCacheTokenFilepath(t *testing.T) {
2524

2625
cases := map[string]struct {
2726
key string
28-
osUserHomeDir func() (string, error)
27+
osUserHomeDir func() string
2928
expectFilename string
3029
expectErr string
3130
}{
3231
"success": {
3332
key: "https://example.awsapps.com/start",
34-
osUserHomeDir: func() (string, error) {
35-
return os.TempDir(), nil
33+
osUserHomeDir: func() string {
34+
return os.TempDir()
3635
},
3736
expectFilename: filepath.Join(os.TempDir(), ".aws", "sso", "cache",
3837
"e8be5486177c5b5392bd9aa76563515b29358e6e.json"),
3938
},
4039
"failure": {
4140
key: "https://example.awsapps.com/start",
42-
osUserHomeDir: func() (string, error) {
43-
return "", fmt.Errorf("some error")
41+
osUserHomeDir: func() string {
42+
return ""
4443
},
4544
expectErr: "some error",
4645
},
@@ -55,9 +54,6 @@ func TestStandardSSOCacheTokenFilepath(t *testing.T) {
5554
if err == nil {
5655
t.Fatalf("expect error, got none")
5756
}
58-
if e, a := c.expectErr, err.Error(); !strings.Contains(a, e) {
59-
t.Fatalf("expect %v error in %v", e, a)
60-
}
6157
return
6258
}
6359
if err != nil {

credentials/ssocreds/sso_credentials_provider_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func TestProvider(t *testing.T) {
6161
osUserHomeDur = origHomeDir
6262
}()
6363

64-
osUserHomeDur = func() (string, error) {
65-
return "testdata", nil
64+
osUserHomeDur = func() string {
65+
return "testdata"
6666
}
6767

6868
restoreTime := sdk.TestingUseReferenceTime(time.Date(2021, 01, 19, 19, 50, 0, 0, time.UTC))

internal/shareddefaults/shared_config.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package shareddefaults
22

33
import (
44
"os"
5+
"os/user"
56
"path/filepath"
6-
"runtime"
77
)
88

99
// SharedCredentialsFilename returns the SDK's default file path
@@ -31,10 +31,17 @@ func SharedConfigFilename() string {
3131
// UserHomeDir returns the home directory for the user the process is
3232
// running under.
3333
func UserHomeDir() string {
34-
if runtime.GOOS == "windows" { // Windows
35-
return os.Getenv("USERPROFILE")
34+
// Ignore errors since we only care about Windows and *nix.
35+
home, _ := os.UserHomeDir()
36+
37+
if len(home) > 0 {
38+
return home
39+
}
40+
41+
currUser, _ := user.Current()
42+
if currUser != nil {
43+
home = currUser.HomeDir
3644
}
3745

38-
// *nix
39-
return os.Getenv("HOME")
46+
return home
4047
}

0 commit comments

Comments
 (0)