diff --git a/lib/fog/aws/credential_fetcher.rb b/lib/fog/aws/credential_fetcher.rb index 40f4f9804..132ef92bb 100644 --- a/lib/fog/aws/credential_fetcher.rb +++ b/lib/fog/aws/credential_fetcher.rb @@ -6,6 +6,8 @@ module Fog INSTANCE_METADATA_PATH = "/latest/meta-data/iam/security-credentials/" INSTANCE_METADATA_AZ = "/latest/meta-data/placement/availability-zone/" + CONTAINER_CREDENTIALS_HOST = "http://169.254.170.2" + module ServiceMethods def fetch_credentials(options) if options[:use_iam_profile] && Fog.mocking? @@ -13,10 +15,23 @@ module Fog end if options[:use_iam_profile] begin - connection = options[:connection] || Excon.new(INSTANCE_METADATA_HOST) - role_name = connection.get(:path => INSTANCE_METADATA_PATH, :expects => 200).body - role_data = connection.get(:path => INSTANCE_METADATA_PATH+role_name, :expects => 200).body - az_data = connection.get(:path => INSTANCE_METADATA_AZ, :expects => 200).body + role_data = nil + az_data = nil + + if ENV["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] + connection = options[:connection] || Excon.new(CONTAINER_CREDENTIALS_HOST) + credential_path = options[:credential_path] || ENV["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] + role_data = connection.get(:path => credential_path, :expects => 200).body + + connection = options[:metadata_connection] || Excon.new(INSTANCE_METADATA_HOST) + az_data = connection.get(:path => INSTANCE_METADATA_AZ, :expects => 200).body + else + connection = options[:connection] || Excon.new(INSTANCE_METADATA_HOST) + role_name = connection.get(:path => INSTANCE_METADATA_PATH, :expects => 200).body + role_data = connection.get(:path => INSTANCE_METADATA_PATH+role_name, :expects => 200).body + az_data = connection.get(:path => INSTANCE_METADATA_AZ, :expects => 200).body + end + region = az_data[0..-2] # get region from az session = Fog::JSON.decode(role_data) diff --git a/tests/credentials_tests.rb b/tests/credentials_tests.rb index d2d4cea5d..e431db574 100644 --- a/tests/credentials_tests.rb +++ b/tests/credentials_tests.rb @@ -28,6 +28,19 @@ Shindo.tests('AWS | credentials', ['aws']) do :aws_credentials_expire_at => expires_at}) { Fog::Compute::AWS.fetch_credentials(:use_iam_profile => true) } end + ENV["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] = '/v1/credentials?id=task_id' + Excon.stub({:method => :get, :path => '/v1/credentials?id=task_id'}, {:status => 200, :body => Fog::JSON.encode(credentials)}) + + tests("#fetch_credentials") do + returns({:aws_access_key_id => 'dummykey', + :aws_secret_access_key => 'dummysecret', + :aws_session_token => 'dummytoken', + :region => "us-west-1", + :aws_credentials_expire_at => expires_at}) { Fog::Compute::AWS.fetch_credentials(:use_iam_profile => true) } + end + + ENV["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] = nil + compute = Fog::Compute::AWS.new(:use_iam_profile => true) tests("#refresh_credentials_if_expired") do @@ -54,6 +67,7 @@ Shindo.tests('AWS | credentials', ['aws']) do end ensure + ENV["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] = nil Excon.stubs.clear Excon.defaults[:mock] = old_mock_value Fog.unmock! if !fog_was_mocked