Skip to content

Commit

Permalink
Merge pull request #43 from vibrato/update-rubocop
Browse files Browse the repository at this point in the history
Strip whitespace from user input
  • Loading branch information
tristanmorgan authored Apr 9, 2019
2 parents e2fdaf3 + 99846a9 commit 1470bad
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
4 changes: 2 additions & 2 deletions exe/awskeyring
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions lib/awskeyring/awsapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions lib/awskeyring_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions spec/lib/awskeyring_more_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down

0 comments on commit 1470bad

Please sign in to comment.