From 17a05759af211a9c46119fb75f3f7d6ce22f2b65 Mon Sep 17 00:00:00 2001 From: Shai Rosenfeld Date: Wed, 29 Jun 2016 15:24:53 -0700 Subject: [PATCH] add default region to use_iam_profile --- lib/fog/aws/credential_fetcher.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/fog/aws/credential_fetcher.rb b/lib/fog/aws/credential_fetcher.rb index 874b18baa..de56ed2c8 100644 --- a/lib/fog/aws/credential_fetcher.rb +++ b/lib/fog/aws/credential_fetcher.rb @@ -1,8 +1,11 @@ module Fog module AWS module CredentialFetcher + INSTANCE_METADATA_HOST = "http://169.254.169.254" INSTANCE_METADATA_PATH = "/latest/meta-data/iam/security-credentials/" + INSTANCE_METADATA_AZ = "/latest/meta-data/placement/availability-zone/" + module ServiceMethods def fetch_credentials(options) if options[:use_iam_profile] && Fog.mocking? @@ -13,6 +16,8 @@ module Fog 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 + region = az_data[0..-2] # get region from az session = Fog::JSON.decode(role_data) credentials = {} @@ -20,6 +25,9 @@ module Fog 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 #these indicate the metadata service is unavailable or has no profile setup credentials rescue Excon::Errors::Error => e