From 7103ad17779c00ee82ef7174d3465f14104291bd Mon Sep 17 00:00:00 2001 From: Matt Muller <53055821+mullermp@users.noreply.github.com> Date: Mon, 14 Feb 2022 12:32:52 -0800 Subject: [PATCH] Raise an error when credentials or config path does not exist (#2662) --- gems/aws-sdk-core/CHANGELOG.md | 2 ++ .../lib/aws-sdk-core/shared_config.rb | 7 +------ .../spec/aws/shared_credentials_spec.rb | 21 +++++++++++++++---- .../credentials/mock_shared_credentials | 5 ++++- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/gems/aws-sdk-core/CHANGELOG.md b/gems/aws-sdk-core/CHANGELOG.md index 31720590eee..4a49a43bbb8 100644 --- a/gems/aws-sdk-core/CHANGELOG.md +++ b/gems/aws-sdk-core/CHANGELOG.md @@ -1,6 +1,8 @@ Unreleased Changes ------------------ +* Issue - Raise a `NoSuchProfileError` when config and credentials files don't exist. + 3.126.1 (2022-02-14) ------------------ diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/shared_config.rb b/gems/aws-sdk-core/lib/aws-sdk-core/shared_config.rb index c82a43cb8a4..6ccf633a39c 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/shared_config.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/shared_config.rb @@ -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)) @@ -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'] diff --git a/gems/aws-sdk-core/spec/aws/shared_credentials_spec.rb b/gems/aws-sdk-core/spec/aws/shared_credentials_spec.rb index 24005a27221..f05d08eea77 100644 --- a/gems/aws-sdk-core/spec/aws/shared_credentials_spec.rb +++ b/gems/aws-sdk-core/spec/aws/shared_credentials_spec.rb @@ -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 diff --git a/gems/aws-sdk-core/spec/fixtures/credentials/mock_shared_credentials b/gems/aws-sdk-core/spec/fixtures/credentials/mock_shared_credentials index e1555c3c1d4..b164395aa9d 100644 --- a/gems/aws-sdk-core/spec/fixtures/credentials/mock_shared_credentials +++ b/gems/aws-sdk-core/spec/fixtures/credentials/mock_shared_credentials @@ -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 @@ -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