mirror of
https://github.com/fog/fog-aws.git
synced 2022-11-09 13:50:52 -05:00
Merge pull request #620 from stanhu/sh-regional-sts
Add support for regional STS endpoints
This commit is contained in:
commit
4c3c55b32a
2 changed files with 55 additions and 7 deletions
|
@ -13,8 +13,6 @@ module Fog
|
|||
|
||||
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?
|
||||
|
@ -23,7 +21,7 @@ module Fog
|
|||
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)
|
||||
|
@ -44,7 +42,15 @@ module Fog
|
|||
: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 = {
|
||||
|
@ -65,18 +71,19 @@ module Fog
|
|||
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
|
||||
|
|
|
@ -83,6 +83,7 @@ Shindo.tests('AWS | credentials', ['aws']) do
|
|||
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
|
||||
|
@ -95,10 +96,50 @@ Shindo.tests('AWS | credentials', ['aws']) do
|
|||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue