Example of ~/.aws/config
[profile account1]
region = us-east-1
[profile account2]
role_arn = arn:aws:iam::XXXXX:role/STSRole
source_profile = account1
mfa_serial = arn:aws:iam::XXXXX:mfa/adminuser
Example of ~/.aws/credentials
[account1]
aws_access_key_id = XXX
aws_secret_access_key = XXX
Expected behavior:
Aws::AssumeRoleCredentials uses credentials of the IAM user in account1, in order to assume STS role in account2.
What Actually happens:
Aws::AssumeRoleCredentials uses the default credentials to assume the STS role in account2. This fails due to lack of access.
Either Aws::SharedCredentials does not respect the source_profile directive, or Aws:AssumeRoleCredentials does not use the SharedCredentials object to obtain it's access key (possibly both).
So this issue might include a hidden request for AssumeRoleCredentials to support passing it an instance of SharedCredentials, instead of passing it the role_arn manually. Currently, I am parsing the ~/.aws/config file manually and extract role_arn from there.
Sample Ruby snippet:
require 'aws-sdk'
require 'inifile'
def aws_init(profile = 'default')
# conform to https://github.com/aws/aws-sdk-ruby/blob/master/CHANGELOG.md#240-2016-07-19
ENV['AWS_SDK_LOAD_CONFIG'] = 'true'
# string for looking up aws_config, account for default profile
configSearch = profile == 'default' ? profile : "profile #{profile}"
# load .aws/config from $HOME
configFile = IniFile.load(ENV['HOME'] + '/.aws/config')
# load profile settings for --profile
@configData = configFile[configSearch]
# Create new Credentials object from profile
credentials = Aws::SharedCredentials.new(profile_name: profile)
# if ~/.aws/config has the role_arn specified, assume STS role
if !@configData['role_arn'].nil?
role_credentials = Aws::AssumeRoleCredentials.new(
role_arn: @configData['role_arn'],
role_session_name: profile
)
ENV['AWS_ACCESS_KEY_ID'] = role_credentials.credentials.access_key_id
ENV['AWS_SECRET_ACCESS_KEY'] = role_credentials.credentials.secret_access_key
ENV['AWS_SECURITY_TOKEN'] = role_credentials.credentials.session_token
else
# if no role_arn specifed in AWS_CONFIG, provide current user creds
ENV['AWS_ACCESS_KEY_ID'] = credentials.credentials.access_key_id
ENV['AWS_SECRET_ACCESS_KEY'] = credentials.credentials.secret_access_key
ENV['AWS_SECURITY_TOKEN'] = credentials.credentials.session_token
end
end
aws_init('account2')
Example of ~/.aws/config
[profile account1]
region = us-east-1
[profile account2]
role_arn = arn:aws:iam::XXXXX:role/STSRole
source_profile = account1
mfa_serial = arn:aws:iam::XXXXX:mfa/adminuser
Example of ~/.aws/credentials
[account1]
aws_access_key_id = XXX
aws_secret_access_key = XXX
Expected behavior:
Aws::AssumeRoleCredentialsuses credentials of the IAM user in account1, in order to assume STS role in account2.What Actually happens:
Aws::AssumeRoleCredentialsuses the default credentials to assume the STS role in account2. This fails due to lack of access.Either
Aws::SharedCredentialsdoes not respect the source_profile directive, or Aws:AssumeRoleCredentials does not use theSharedCredentialsobject to obtain it's access key (possibly both).So this issue might include a hidden request for
AssumeRoleCredentialsto support passing it an instance ofSharedCredentials, instead of passing it the role_arn manually. Currently, I am parsing the ~/.aws/config file manually and extract role_arn from there.Sample Ruby snippet: