From bf51f6a71d6426b04d220aabf5081d5de5ac66b1 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sun, 5 Jun 2016 13:36:58 -0400 Subject: [PATCH] guests/freebsd: Insert public key in one command --- .../guests/freebsd/cap/insert_public_key.rb | 29 ++++++++++++----- .../freebsd/cap/insert_public_key_test.rb | 31 +++++++++++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 test/unit/plugins/guests/freebsd/cap/insert_public_key_test.rb diff --git a/plugins/guests/freebsd/cap/insert_public_key.rb b/plugins/guests/freebsd/cap/insert_public_key.rb index 5fc414d39..86afb0b66 100644 --- a/plugins/guests/freebsd/cap/insert_public_key.rb +++ b/plugins/guests/freebsd/cap/insert_public_key.rb @@ -1,19 +1,32 @@ -require "vagrant/util/shell_quote" +require "tempfile" module VagrantPlugins module GuestFreeBSD module Cap class InsertPublicKey def self.insert_public_key(machine, contents) - contents = Vagrant::Util::ShellQuote.escape(contents, "'") - contents = contents.gsub("\n", "\\n") + comm = machine.communicate + contents = contents.chomp - machine.communicate.tap do |comm| - comm.execute("mkdir -p ~/.ssh", shell: "sh") - comm.execute("chmod 0700 ~/.ssh", shell: "sh") - comm.execute("printf '#{contents}' >> ~/.ssh/authorized_keys", shell: "sh") - comm.execute("chmod 0600 ~/.ssh/authorized_keys", shell: "sh") + remote_path = "/tmp/vagrant-authorized-keys-#{Time.now.to_i}" + Tempfile.open("vagrant-freebsd-insert-public-key") do |f| + f.binmode + f.write(contents) + f.fsync + f.close + comm.upload(f.path, remote_path) end + + command = <<-EOH.gsub(/^ {12}/, '') + mkdir -p ~/.ssh + chmod 0700 ~/.ssh + cat '#{remote_path}' >> ~/.ssh/authorized_keys + chmod 0600 ~/.ssh/authorized_keys + + # Remove the temporary file + rm -f '#{remote_path}' + EOH + comm.execute(command, { shell: "sh" }) end end end diff --git a/test/unit/plugins/guests/freebsd/cap/insert_public_key_test.rb b/test/unit/plugins/guests/freebsd/cap/insert_public_key_test.rb new file mode 100644 index 000000000..8f4c45da8 --- /dev/null +++ b/test/unit/plugins/guests/freebsd/cap/insert_public_key_test.rb @@ -0,0 +1,31 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestFreeBSD::Cap::InsertPublicKey" do + let(:described_class) do + VagrantPlugins::GuestFreeBSD::Plugin + .components + .guest_capabilities[:freebsd] + .get(:insert_public_key) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".insert_public_key" do + it "inserts the public key" do + described_class.insert_public_key(machine, "ssh-rsa ...") + expect(comm.received_commands[0]).to match(/mkdir -p ~\/.ssh/) + expect(comm.received_commands[0]).to match(/chmod 0700 ~\/.ssh/) + expect(comm.received_commands[0]).to match(/cat '\/tmp\/vagrant-(.+)' >> ~\/.ssh\/authorized_keys/) + expect(comm.received_commands[0]).to match(/chmod 0600 ~\/.ssh\/authorized_keys/) + end + end +end