Skip to content

Commit

Permalink
Raise an error when credentials or config path does not exist (#2662)
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp authored Feb 14, 2022
1 parent a8aea9d commit 7103ad1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 2 additions & 0 deletions gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Raise a `NoSuchProfileError` when config and credentials files don't exist.

3.126.1 (2022-02-14)
------------------

Expand Down
7 changes: 1 addition & 6 deletions gems/aws-sdk-core/lib/aws-sdk-core/shared_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def config_enabled?
# or `nil` if no valid credentials were found.
def credentials(opts = {})
p = opts[:profile] || @profile_name
validate_profile_exists(p) if credentials_present?
validate_profile_exists(p)
if (credentials = credentials_from_shared(p, opts))
credentials
elsif (credentials = credentials_from_config(p, opts))
Expand Down Expand Up @@ -195,11 +195,6 @@ def get_config_value(key, opts)
value
end

def credentials_present?
(@parsed_credentials && !@parsed_credentials.empty?) ||
(@parsed_config && !@parsed_config.empty?)
end

def assume_role_from_profile(cfg, profile, opts, chain_config)
if cfg && prof_cfg = cfg[profile]
opts[:source_profile] ||= prof_cfg['source_profile']
Expand Down
21 changes: 17 additions & 4 deletions gems/aws-sdk-core/spec/aws/shared_credentials_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,36 @@ module Aws
expect(creds.session_token).to eq('TOKEN_1')
end

it 'raises when a path does not exist' do
msg = /^Profile `doesnotexist' not found in \/no\/file\/here/
expect {
SharedCredentials.new(
path: '/no/file/here',
profile_name: 'doesnotexist'
)
}.to raise_error(Errors::NoSuchProfileError, msg)
end

it 'raises when a profile does not exist' do
msg = /^Profile `bazprofile' not found in .+mock_shared_credentials/
msg = /^Profile `doesnotexist' not found in .+mock_shared_credentials/
expect {
SharedCredentials.new(
path: mock_credential_file,
profile_name: 'bazprofile'
profile_name: 'doesnotexist'
)
}.to raise_error(Errors::NoSuchProfileError, msg)
end

it 'is set when credentails is valid' do
it 'is set when credentials is valid' do
creds = SharedCredentials.new(path:mock_credential_file)
expect(creds.set?).to eq(true)
end

it 'is not set when key_id or access_key is missing' do
creds = SharedCredentials.new(path:'/no/file/here')
creds = SharedCredentials.new(
path: mock_credential_file,
profile_name: 'no_creds'
)
expect(creds.set?).to eq(false)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[default]
aws_access_key_id = ACCESS_KEY_0
aws_access_key_id = ACCESS_KEY_0
aws_secret_access_key = SECRET_KEY_0
aws_session_token = TOKEN_0

Expand All @@ -25,3 +25,6 @@ role_arn = arn:aws:iam:123456789012:role/bar

[incomplete_cred]
aws_secret_access_key = SECRET_KEY_INC_CRED

[no_creds]
necssary_for = valid_profile_syntax

0 comments on commit 7103ad1

Please sign in to comment.