Skip to content

Commit

Permalink
Prefer AWS_REGION over AWS_DEFAULT_REGION in credential fetching (#715)
Browse files Browse the repository at this point in the history
As https://docs.aws.amazon.com/sdkref/latest/guide/feature-region.html#feature-region-sdk-compat
describes, the AWS CLI v2 uses AWS_REGION before AWS_DEFAULT_VERSION.
  • Loading branch information
stanhu committed Jun 27, 2024
1 parent 05a11b5 commit 53505a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/fog/aws/credential_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def fetch_credentials(options)
if options[:use_iam_profile]
begin
role_data = nil
region = options[:region] || ENV["AWS_DEFAULT_REGION"]
region = options[:region] || ENV["AWS_REGION"] || ENV["AWS_DEFAULT_REGION"]

if ENV["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"]
connection = options[:connection] || Excon.new(CONTAINER_CREDENTIALS_HOST)
Expand Down
18 changes: 17 additions & 1 deletion tests/credentials_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,23 @@
end

ENV["AWS_DEFAULT_REGION"] = "us-west-1"
ENV["AWS_REGION"] = nil

tests('#fetch_credentials with regional STS endpoint with region in env') do
tests('#fetch_credentials with regional STS endpoint with AWS_DEFAULT_REGION in env') do
returns(
aws_access_key_id: 'dummykey',
aws_secret_access_key: 'dummysecret',
aws_session_token: 'dummytoken',
region: 'us-west-1',
sts_endpoint: "https://sts.us-west-1.amazonaws.com",
aws_credentials_expire_at: expires_at
) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true) }
end

ENV["AWS_DEFAULT_REGION"] = "us-west-2"
ENV["AWS_REGION"] = "us-west-1"

tests('#fetch_credentials with regional STS endpoint with AWS_REGION in env') do
returns(
aws_access_key_id: 'dummykey',
aws_secret_access_key: 'dummysecret',
Expand All @@ -165,6 +180,7 @@

ENV["AWS_STS_REGIONAL_ENDPOINTS"] = nil
ENV["AWS_DEFAULT_REGION"] = nil
ENV["AWS_REGION"] = nil
ENV['AWS_WEB_IDENTITY_TOKEN_FILE'] = nil

storage = Fog::Storage.new(
Expand Down

0 comments on commit 53505a0

Please sign in to comment.