Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for source_profile and role_arn #998

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions aws-sdk-core/lib/aws-sdk-core/shared_credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ def initialize(options = {})
attr_reader :profile_name

# @return [Credentials]
attr_reader :credentials
def credentials
if @credentials.respond_to?(:credentials)
@credentials.credentials
else
@credentials
end
end

# @api private
def inspect
Expand Down Expand Up @@ -67,18 +73,34 @@ def default_path

def load_from_path
profile = load_profile
@credentials = Credentials.new(
credentials = Credentials.new(
profile['aws_access_key_id'],
profile['aws_secret_access_key'],
profile['aws_session_token']
)
@credentials = if profile['role_arn']
AssumeRoleCredentials.new(
role_arn: profile['role_arn'],
role_session_name: profile_name,
credentials: credentials
)
else credentials
end
end

def load_profile
if profile = profiles[profile_name]
profile = load_named_profile(profile_name)
if profile['source_profile']
profile = load_named_profile(profile['source_profile']).merge(profile)
end
profile
end

def load_named_profile(name)
if profile = profiles[name]
profile
else
msg = "Profile `#{profile_name}' not found in #{path}"
msg = "Profile `#{name}' not found in #{path}"
raise Errors::NoSuchProfileError, msg
end
end
Expand Down
8 changes: 8 additions & 0 deletions aws-sdk-core/spec/aws/shared_credentials_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ module Aws
expect(creds.session_token).to eq('TOKEN_1')
end

it 'supports fetching a source_profile' do
stub_const('ENV', { 'AWS_PROFILE' => 'childprofile' })
creds = SharedCredentials.new(path:mock_credential_file).credentials
expect(creds.access_key_id).to eq('ACCESS_KEY_1')
expect(creds.secret_access_key).to eq('SECRET_KEY_1')
expect(creds.session_token).to eq('TOKEN_3')
end

it 'raises when a profile does not exist' do
msg = /^Profile `bazprofile' not found in .+mock_shared_credentials/
expect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ aws_session_token = TOKEN_1
aws_access_key_id = ACCESS_KEY_2
aws_secret_access_key = SECRET_KEY_2
aws_session_token = TOKEN_2

[childprofile]
source_profile = fooprofile
aws_session_token = TOKEN_3