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

Expands ~ and environment variables in file paths #118

Merged
merged 1 commit into from
Feb 16, 2022
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
28 changes: 27 additions & 1 deletion aws_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/hashicorp/aws-sdk-go-base/v2/internal/constants"
"github.com/hashicorp/aws-sdk-go-base/v2/internal/endpoints"
"github.com/hashicorp/aws-sdk-go-base/v2/internal/httpclient"
"github.com/hashicorp/go-multierror"
"github.com/mitchellh/go-homedir"
)

func GetAwsConfig(ctx context.Context, c *Config) (aws.Config, error) {
Expand Down Expand Up @@ -170,9 +172,13 @@ func commonLoadOptions(c *Config) ([]func(*config.LoadOptions) error, error) {
}

if len(c.SharedConfigFiles) > 0 {
configFiles, err := expandFilePaths(c.SharedConfigFiles)
if err != nil {
return nil, fmt.Errorf("error expanding shared config files: %w", err)
}
loadOptions = append(
loadOptions,
config.WithSharedConfigFiles(c.SharedConfigFiles),
config.WithSharedConfigFiles(configFiles),
)
}

Expand Down Expand Up @@ -216,3 +222,23 @@ func commonLoadOptions(c *Config) ([]func(*config.LoadOptions) error, error) {

return loadOptions, nil
}

func expandFilePaths(in []string) ([]string, error) {
var errs *multierror.Error
result := make([]string, 0, len(in))
for _, v := range in {
p, err := expandFilePath(v)
if err != nil {
errs = multierror.Append(errs, err)
continue
}
result = append(result, p)
}
return result, errs.ErrorOrNil()
}

func expandFilePath(in string) (s string, err error) {
e := os.ExpandEnv(in)
s, err = homedir.Expand(e)
return
}
58 changes: 58 additions & 0 deletions aws_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,61 @@ func (r *withNoDelay) RetryDelay(attempt int, err error) (time.Duration, error)

return 0 * time.Second, nil
}

func TestExpandFilePath(t *testing.T) {
testcases := map[string]struct {
path string
expected string
envvars map[string]string
}{
"filename": {
path: "file",
expected: "file",
},
"file in current dir": {
path: "./file",
expected: "./file",
},
"file with tilde": {
path: "~/file",
expected: "/my/home/dir/file",
envvars: map[string]string{
"HOME": "/my/home/dir",
},
},
"file with envvar": {
path: "$HOME/file",
expected: "/home/dir/file",
envvars: map[string]string{
"HOME": "/home/dir",
},
},
"full file in envvar": {
path: "$CONF_FILE",
expected: "/path/to/conf/file",
envvars: map[string]string{
"CONF_FILE": "/path/to/conf/file",
},
},
}

for name, testcase := range testcases {
t.Run(name, func(t *testing.T) {
oldEnv := servicemocks.StashEnv()
defer servicemocks.PopEnv(oldEnv)

for k, v := range testcase.envvars {
os.Setenv(k, v)
}

a, err := expandFilePath(testcase.path)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if a != testcase.expected {
t.Errorf("expected expansion to %q, got %q", testcase.expected, a)
}
})
}
}
6 changes: 5 additions & 1 deletion credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ func getCredentialsProvider(ctx context.Context, c *Config) (aws.CredentialsProv
)
}
if len(c.SharedCredentialsFiles) > 0 {
credsFiles, err := expandFilePaths(c.SharedCredentialsFiles)
if err != nil {
return nil, fmt.Errorf("error expanding shared credentials files: %w", err)
}
loadOptions = append(
loadOptions,
config.WithSharedCredentialsFiles(c.SharedCredentialsFiles),
config.WithSharedCredentialsFiles(credsFiles),
)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/aws/smithy-go v1.10.0
github.com/google/go-cmp v0.5.7
github.com/hashicorp/go-multierror v1.1.1
github.com/mitchellh/go-homedir v1.1.0
)

go 1.16
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down
2 changes: 2 additions & 0 deletions v2/awsv1shim/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down