2014-12-30 17:25:09 -05:00
module Fog
module AWS
module CredentialFetcher
2016-06-29 18:24:53 -04:00
2014-12-30 17:25:09 -05:00
INSTANCE_METADATA_HOST = " http://169.254.169.254 "
INSTANCE_METADATA_PATH = " /latest/meta-data/iam/security-credentials/ "
2016-06-29 18:24:53 -04:00
INSTANCE_METADATA_AZ = " /latest/meta-data/placement/availability-zone/ "
2016-07-25 19:36:29 -04:00
CONTAINER_CREDENTIALS_HOST = " http://169.254.170.2 "
2014-12-30 17:25:09 -05:00
module ServiceMethods
def fetch_credentials ( options )
2016-05-11 16:31:54 -04:00
if options [ :use_iam_profile ] && Fog . mocking?
2016-05-18 14:55:36 -04:00
Fog :: Compute :: AWS :: Mock . data [ :iam_role_based_creds ]
2016-05-11 16:31:54 -04:00
end
2014-12-30 17:25:09 -05:00
if options [ :use_iam_profile ]
begin
2016-07-25 19:36:29 -04:00
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
2016-06-29 18:24:53 -04:00
region = az_data [ 0 .. - 2 ] # get region from az
2014-12-30 17:25:09 -05:00
session = Fog :: JSON . decode ( role_data )
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' ]
2016-06-29 18:24:53 -04:00
# set region by default to the one the instance is in.
credentials [ :region ] = region
2014-12-30 17:25:09 -05:00
#these indicate the metadata service is unavailable or has no profile setup
credentials
2016-07-01 09:47:14 -04:00
rescue Excon :: Error = > e
2014-12-30 17:25:09 -05:00
Fog :: Logger . warning ( " Unable to fetch credentials: #{ e . message } " )
super
end
else
super
end
end
end
module ConnectionMethods
def refresh_credentials_if_expired
refresh_credentials if credentials_expired?
end
private
def credentials_expired?
@use_iam_profile &&
( ! @aws_credentials_expire_at ||
( @aws_credentials_expire_at && Fog :: Time . now > @aws_credentials_expire_at - 15 ) ) #new credentials become available from around 5 minutes before expiration time
end
def refresh_credentials
if @use_iam_profile
new_credentials = service . fetch_credentials :use_iam_profile = > @use_iam_profile
if new_credentials . any?
setup_credentials new_credentials
return true
else
false
end
else
false
end
end
end
end
end
end