-
Notifications
You must be signed in to change notification settings - Fork 769
config: tilde (~) not expanded in AWS_CONFIG_FILE and AWS_SHARED_CREDENTIALS_FILE environment variables #3330
Description
Describe the bug
When AWS_CONFIG_FILE or AWS_SHARED_CREDENTIALS_FILE is set to a path starting with ~ (e.g. ~/.aws/config), the Go SDK does not expand the tilde to the user's home directory. The literal string ~/.aws/config is passed to file open calls, which fails because no such path exists.
This is inconsistent with:
- botocore (Python SDK), which calls
os.path.expanduser()on these paths before opening them - The AWS SDKs and Tools Reference Guide, which specifies that
~followed by/(or the platform path separator) at the start of a shared config/credentials file path should resolve to the user's home directory
Expected Behavior
AWS_CONFIG_FILE=~/.aws/config should resolve to $HOME/.aws/config (e.g. /home/user/.aws/config) before the SDK attempts to open the file, consistent with other AWS SDKs and the shared configuration specification.
Current Behavior
The SDK reads the raw environment variable value via os.Getenv() in config/env_config.go and stores it without any path expansion. When it later attempts to open ~/.aws/config as a literal file path, it fails.
Reproduction Steps
export AWS_CONFIG_FILE=~/.aws/config
export AWS_PROFILE=my-profile
# Any Go program using aws-sdk-go-v2 to load config will failThis commonly affects users of direnv who set AWS_CONFIG_FILE in .envrc files, and any environment where these variables are set with ~ rather than the expanded $HOME.
Solution
The SDK's internal/shareddefaults package already has a UserHomeDir() function used to construct default paths. A small ExpandHomePath() helper can be added alongside it and applied to the two os.Getenv() calls in NewEnvConfig():
config/env_config.goline 343:cfg.SharedCredentialsFileconfig/env_config.goline 344:cfg.SharedConfigFile
I have a PR ready with the fix and tests.
References
- AWS SDKs and Tools Reference Guide - Shared config and credentials files: Location
- botocore
raw_config_parse()- callsexpanduser()before opening config files