config: expand tilde (~) in AWS_CONFIG_FILE and AWS_SHARED_CREDENTIALS_FILE env vars#3331
config: expand tilde (~) in AWS_CONFIG_FILE and AWS_SHARED_CREDENTIALS_FILE env vars#3331LinguineCode wants to merge 1 commit intoaws:mainfrom
Conversation
…S_FILE env vars The Go os package does not perform shell-style tilde expansion, so when AWS_CONFIG_FILE or AWS_SHARED_CREDENTIALS_FILE is set to a path like ~/.aws/config, the SDK passes the literal string to file open calls, which fails. This is inconsistent with botocore (Python SDK), which calls os.path.expanduser() on these paths, and with the AWS SDKs and Tools Reference Guide which specifies that ~ followed by / at the start of a file path should resolve to the user's home directory. This change adds an ExpandHomePath helper to the shareddefaults package and applies it when reading these two environment variables in NewEnvConfig(). Reference: https://docs.aws.amazon.com/sdkref/latest/guide/file-location.html
|
Hey folks -- quick note of context on this PR. I've been a systems architect for 30 years and an exclusive AWS shop for the last 15. Not "multi-cloud with a preference" -- I mean I have never taken a gig on another cloud provider. AWS is my platform. I've been a proponent and unashamed fanboy since the days when people still asked "but what if Amazon shuts it down?" In all that time, I've been a consumer of open source -- building my career and my clients' businesses on the shoulders of work that people contributed for free. This is my first PR to an open source project, ever. And I'm glad it's to an AWS repo. I ran into this bug the hard way: hours of debugging why I've spent 30 years benefiting from open source without ever contributing back. The fact that I can now go from "this is broken and I'm frustrated" to "here's a tested fix with an issue and a PR" in a single sitting -- that genuinely moves me. The barrier to entry just got a whole lot lower, and I hope this is the first of many. Looking forward to feedback and happy to iterate on anything. |
Description
Fixes #3330
When
AWS_CONFIG_FILEorAWS_SHARED_CREDENTIALS_FILEis set to a path starting with~(e.g.~/.aws/config), the SDK passes the literal string to file open calls without expanding the tilde to the user's home directory. This is inconsistent with botocore and with the AWS SDKs and Tools Reference Guide, which specifies that~followed by/at the start of a file path should resolve to the home directory.Changes
internal/shareddefaults/shared_config.go: AddedExpandHomePath()helper that expands a leading~/toUserHomeDir(). This function is placed alongside the existingUserHomeDir()it depends on.config/env_config.go: Wrapped the twoos.Getenv()calls forAWS_CONFIG_FILEandAWS_SHARED_CREDENTIALS_FILEwithshareddefaults.ExpandHomePath().Testing
internal/shareddefaults/expand_home_test.gowith 8 test cases covering: empty string, absolute paths, relative paths, bare~,~/path, nested paths, tilde mid-path (no expansion), and~username(no expansion).config/env_config_test.goverifying end-to-end tilde expansion whenAWS_CONFIG_FILEandAWS_SHARED_CREDENTIALS_FILEare set with~/prefixes.All existing tests continue to pass.