From a60ea1f17090e6574c9a23f2617fa3c8cb73ab11 Mon Sep 17 00:00:00 2001 From: Tristan Morgan Date: Mon, 8 Apr 2019 17:28:20 +1000 Subject: [PATCH 1/2] Updates for Rubocop changes. --- exe/awskeyring | 4 ++-- lib/awskeyring/awsapi.rb | 6 +++--- lib/awskeyring_command.rb | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/exe/awskeyring b/exe/awskeyring index 8ee3ac6..2a3ebb7 100755 --- a/exe/awskeyring +++ b/exe/awskeyring @@ -9,7 +9,7 @@ end begin AwskeyringCommand.start -rescue Keychain::UserCancelledError => err - warn err.to_s +rescue Keychain::UserCancelledError => e + warn e.to_s exit 1 end diff --git a/lib/awskeyring/awsapi.rb b/lib/awskeyring/awsapi.rb index d3baf89..dd9b0a2 100644 --- a/lib/awskeyring/awsapi.rb +++ b/lib/awskeyring/awsapi.rb @@ -70,8 +70,8 @@ def self.get_token(params = {}) # rubocop:disable Metrics/AbcSize, Metrics/Meth duration_seconds: params[:duration] ) end - rescue Aws::STS::Errors::AccessDenied => err - warn err.to_s + rescue Aws::STS::Errors::AccessDenied => e + warn e.to_s exit 1 end @@ -185,7 +185,7 @@ def self.region # @return [String] key The aws_access_key_id # @return [String] secret The aws_secret_access_key # @return [String] account the associated account name. - def self.rotate(account:, key:, secret:, key_message:) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize + def self.rotate(account:, key:, secret:, key_message:) # rubocop:disable Metrics/MethodLength ENV['AWS_DEFAULT_REGION'] = 'us-east-1' unless region iam = Aws::IAM::Client.new(access_key_id: key, secret_access_key: secret) diff --git a/lib/awskeyring_command.rb b/lib/awskeyring_command.rb index 9665e03..1ff433a 100644 --- a/lib/awskeyring_command.rb +++ b/lib/awskeyring_command.rb @@ -105,8 +105,8 @@ def exec(account, *command) pid = Process.spawn(env_vars, command.join(' ')) Process.wait pid $CHILD_STATUS - rescue Errno::ENOENT => err - warn err.to_s + rescue Errno::ENOENT => e + warn e.to_s exit 1 end end From 99846a9a06416718c58f6dd9597950492fe8f768 Mon Sep 17 00:00:00 2001 From: Tristan Morgan Date: Tue, 9 Apr 2019 10:45:08 +1000 Subject: [PATCH 2/2] Strip whitespace on user input --- lib/awskeyring_command.rb | 2 +- spec/lib/awskeyring_more_command_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/awskeyring_command.rb b/lib/awskeyring_command.rb index 1ff433a..00324d5 100644 --- a/lib/awskeyring_command.rb +++ b/lib/awskeyring_command.rb @@ -457,7 +457,7 @@ def ask_check(existing:, message:, secure: false, optional: false, validator: ni end def ask_missing(existing:, message:, secure: false, optional: false) - existing || ask(message: message, secure: secure, optional: optional) + existing || ask(message: message, secure: secure, optional: optional).strip end def ask(message:, secure: false, optional: false) diff --git a/spec/lib/awskeyring_more_command_spec.rb b/spec/lib/awskeyring_more_command_spec.rb index 8f7764e..d398331 100644 --- a/spec/lib/awskeyring_more_command_spec.rb +++ b/spec/lib/awskeyring_more_command_spec.rb @@ -296,6 +296,30 @@ end end + context 'When we try to add an AWS account with white space' do + let(:access_key) { 'AKIA0123456789ABCDEF' } + let(:secret_access_key) { 'AbCkTEsTAAAi8ni0987ASDFwer23j14FEQW3IUJV' } + let(:mfa_arn) { 'arn:aws:iam::012345678901:mfa/readonly' } + + before do + allow(Thor::LineEditor).to receive(:readline).and_return(" #{access_key} \n") + allow(Awskeyring::Input).to receive(:read_secret).and_return(" #{secret_access_key} \t") + allow(Awskeyring).to receive(:account_not_exists).with('test').and_return('test') + allow(Awskeyring).to receive(:add_account).and_return(nil) + allow(Awskeyring::Awsapi).to receive(:verify_cred) + .and_return(true) + end + + it 'tries to add an account with whitespace' do + expect(Awskeyring::Awsapi).to receive(:verify_cred) + expect(Awskeyring).to_not receive(:update_account) + expect(Awskeyring).to receive(:add_account) + expect do + AwskeyringCommand.start(['add', 'test', '-m', mfa_arn]) + end.to output("# Added account test\n").to_stdout + end + end + context 'When we try to add a Role' do let(:role_arn) { 'arn:aws:iam::012345678901:role/readonly' } let(:bad_role_arn) { 'arn:azure:iamnot::ABCD45678901:Administrators' }