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

Fix for invalid format of auto generated keypair file name #626

Merged
merged 1 commit into from
Dec 23, 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
2 changes: 1 addition & 1 deletion lib/chef/knife/ec2_server_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ def create_volume_tags(hashed_volume_tags)
end

def create_key_pair
key_name = "#{config[:connection_user]}-#{SecureRandom.hex(10)}"
key_name = "#{config[:connection_user].delete(".\\")}-#{SecureRandom.hex(10)}"
key_pair = ec2_connection.create_key_pair({
key_name: key_name,
})
Expand Down
41 changes: 32 additions & 9 deletions spec/unit/ec2_server_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,22 @@
end
end

shared_examples "create keypair" do
let(:file_path) { "/root/.chef/#{keypair.key_name}.pem" }
let(:file_like_object) { double(path: file_path) }
before do
Chef::Config[:knife].delete(:ssh_key_name)
allow(File).to receive(:open).with(file_path, "w+").and_return(file_like_object)
end

it "calls create_key_pair method and Generate new keypair" do
expect(ec2_connection).to receive(:create_key_pair).and_return(keypair)
knife_ec2_create.plugin_validate_options!
expect(Chef::Config[:knife][:ssh_key_name]).to eq(keypair.key_name)
expect(Chef::Config[:knife][:ssh_identity_file]).to eq(file_path)
end
end

describe "S3 secret test cases" do
before do
Chef::Config[:knife][:s3_secret] =
Expand Down Expand Up @@ -627,21 +643,28 @@
key_name: "ubuntu-07f692a0d8d1fcc4a086"
)
end
let(:file_path) { "/root/.chef/#{keypair.key_name}.pem" }
let(:file_like_object) { double(path: file_path) }

before do
knife_ec2_create.config[:connection_user] = "ubuntu"
Chef::Config[:knife].delete(:ssh_key_name)
allow(File).to receive(:open).with(file_path, "w+").and_return(file_like_object)
end

it "should be call create_key_pair method and Generate new keypair" do
expect(ec2_connection).to receive(:create_key_pair).and_return(keypair)
knife_ec2_create.plugin_validate_options!
expect(Chef::Config[:knife][:ssh_key_name]).to eq(keypair.key_name)
expect(Chef::Config[:knife][:ssh_identity_file]).to eq(file_path)
it_behaves_like "create keypair"
end

context "when user name is passed with system domain (.\\) and ssh_key_name option is not provided on the CLI" do
let(:keypair) do
Aws::EC2::Types::KeyPair.new(
key_fingerprint: "a9:00:ec:1d:bd:80:ae:00",
key_material: "test private key",
key_name: "a_local_user-07f692a0d8d1fcc4a086"
)
end

before do
knife_ec2_create.config[:connection_user] = ".\\a_local_user"
end

it_behaves_like "create keypair"
end

context "when ssh_key_name option is passed on the CLI" do
Expand Down