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

Strip whitespace from user input #43

Merged
merged 2 commits into from
Apr 9, 2019
Merged
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
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
JohnVonNeumann marked this conversation as resolved.
Show resolved Hide resolved
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
JohnVonNeumann marked this conversation as resolved.
Show resolved Hide resolved
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
tristanmorgan marked this conversation as resolved.
Show resolved Hide resolved
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