Skip to content

config: tilde (~) not expanded in AWS_CONFIG_FILE and AWS_SHARED_CREDENTIALS_FILE environment variables #3330

@LinguineCode

Description

@LinguineCode

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:

  1. botocore (Python SDK), which calls os.path.expanduser() on these paths before opening them
  2. 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 fail

This 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.go line 343: cfg.SharedCredentialsFile
  • config/env_config.go line 344: cfg.SharedConfigFile

I have a PR ready with the fix and tests.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    queuedThis issues is on the AWS team's backlog

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions