Skip to content

Commit

Permalink
Merge pull request #620 from stanhu/sh-regional-sts
Browse files Browse the repository at this point in the history
 Add support for regional STS endpoints
  • Loading branch information
geemus authored Aug 20, 2021
2 parents d58fbe6 + 1eccc96 commit 4c3c55b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/fog/aws/credential_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ module CredentialFetcher

CONTAINER_CREDENTIALS_HOST = "http://169.254.170.2"

STS_GLOBAL_ENDPOINT = "https://sts.amazonaws.com"

module ServiceMethods
def fetch_credentials(options)
if options[:use_iam_profile] && Fog.mocking?
Expand All @@ -23,7 +21,7 @@ def fetch_credentials(options)
if options[:use_iam_profile]
begin
role_data = nil
region = options[:region]
region = options[:region] || ENV["AWS_DEFAULT_REGION"]

if ENV["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"]
connection = options[:connection] || Excon.new(CONTAINER_CREDENTIALS_HOST)
Expand All @@ -44,7 +42,15 @@ def fetch_credentials(options)
:WebIdentityToken => File.read(options[:aws_web_identity_token_file] || ENV.fetch("AWS_WEB_IDENTITY_TOKEN_FILE")),
:Version => "2011-06-15",
}
connection = options[:connection] || Excon.new(STS_GLOBAL_ENDPOINT, :query => params)

sts_endpoint =
if ENV["AWS_STS_REGIONAL_ENDPOINTS"] == "regional" && region
"https://sts.#{region}.amazonaws.com"
else
"https://sts.amazonaws.com"
end

connection = options[:connection] || Excon.new(sts_endpoint, :query => params)
document = Nokogiri::XML(connection.get(:idempotent => true, :expects => 200).body)

session = {
Expand All @@ -65,18 +71,19 @@ def fetch_credentials(options)
role_name = connection.get(:path => INSTANCE_METADATA_PATH, :idempotent => true, :expects => 200, :headers => token_header).body
role_data = connection.get(:path => INSTANCE_METADATA_PATH+role_name, :idempotent => true, :expects => 200, :headers => token_header).body
session = Fog::JSON.decode(role_data)

region ||= connection.get(:path => INSTANCE_METADATA_AZ, :idempotent => true, :expects => 200, :headers => token_header).body[0..-2]
end

credentials = {}
credentials[:aws_access_key_id] = session['AccessKeyId']
credentials[:aws_secret_access_key] = session['SecretAccessKey']
credentials[:aws_session_token] = session['Token']
credentials[:aws_credentials_expire_at] = Time.xmlschema session['Expiration']

# set region by default to the one the instance is in.
credentials[:region] = region
credentials[:sts_endpoint] = sts_endpoint if sts_endpoint
#these indicate the metadata service is unavailable or has no profile setup
credentials
rescue Excon::Error => e
Expand Down
41 changes: 41 additions & 0 deletions tests/credentials_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
aws_secret_access_key: 'dummysecret',
aws_session_token: 'dummytoken',
region: 'us-west-1',
sts_endpoint: "https://sts.amazonaws.com",
aws_credentials_expire_at: expires_at
) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true) }
end
Expand All @@ -95,10 +96,50 @@
aws_secret_access_key: 'dummysecret',
aws_session_token: 'dummytoken',
region: 'us-west-1',
sts_endpoint: "https://sts.amazonaws.com",
aws_credentials_expire_at: expires_at
) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true, region: 'us-west-1') }
end

ENV["AWS_STS_REGIONAL_ENDPOINTS"] = "regional"

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

tests('#fetch_credentials with regional STS endpoint') 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, region: 'us-west-1') }
end

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

tests('#fetch_credentials with regional STS endpoint with 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_STS_REGIONAL_ENDPOINTS"] = nil
ENV["AWS_DEFAULT_REGION"] = nil
ENV['AWS_WEB_IDENTITY_TOKEN_FILE'] = nil

compute = Fog::AWS::Compute.new(use_iam_profile: true)
Expand Down

0 comments on commit 4c3c55b

Please sign in to comment.