From 079acb42cdd775fc0eeb1fc83039432ad07c36b5 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 17 Jun 2016 21:36:28 -0400 Subject: [PATCH] guests/bsd: Centralize logic for public key management --- plugins/guests/bsd/cap/insert_public_key.rb | 36 ---------- plugins/guests/bsd/cap/public_key.rb | 65 +++++++++++++++++++ plugins/guests/bsd/plugin.rb | 9 ++- .../guests/darwin/cap/remove_public_key.rb | 21 ------ plugins/guests/darwin/plugin.rb | 5 -- .../guests/freebsd/cap/remove_public_key.rb | 21 ------ plugins/guests/freebsd/plugin.rb | 5 -- .../guests/netbsd/cap/remove_public_key.rb | 21 ------ plugins/guests/netbsd/plugin.rb | 5 -- .../guests/openbsd/cap/remove_public_key.rb | 21 ------ plugins/guests/openbsd/plugin.rb | 5 -- 11 files changed, 72 insertions(+), 142 deletions(-) delete mode 100644 plugins/guests/bsd/cap/insert_public_key.rb create mode 100644 plugins/guests/bsd/cap/public_key.rb delete mode 100644 plugins/guests/darwin/cap/remove_public_key.rb delete mode 100644 plugins/guests/freebsd/cap/remove_public_key.rb delete mode 100644 plugins/guests/netbsd/cap/remove_public_key.rb delete mode 100644 plugins/guests/openbsd/cap/remove_public_key.rb diff --git a/plugins/guests/bsd/cap/insert_public_key.rb b/plugins/guests/bsd/cap/insert_public_key.rb deleted file mode 100644 index 714904b35..000000000 --- a/plugins/guests/bsd/cap/insert_public_key.rb +++ /dev/null @@ -1,36 +0,0 @@ -require "tempfile" - -module VagrantPlugins - module GuestBSD - module Cap - class InsertPublicKey - def self.insert_public_key(machine, contents) - comm = machine.communicate - contents = contents.strip << "\n" - - remote_path = "/tmp/vagrant-authorized-keys-#{Time.now.to_i}" - Tempfile.open("vagrant-bsd-insert-public-key") do |f| - f.binmode - f.write(contents) - f.fsync - f.close - comm.upload(f.path, remote_path) - end - - # Use execute (not sudo) because we want to execute this as the SSH - # user (which is "vagrant" by default). - comm.execute <<-EOH.gsub(/^ {12}/, '') - set -e - 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 - end - end - end - end -end diff --git a/plugins/guests/bsd/cap/public_key.rb b/plugins/guests/bsd/cap/public_key.rb new file mode 100644 index 000000000..957df1c58 --- /dev/null +++ b/plugins/guests/bsd/cap/public_key.rb @@ -0,0 +1,65 @@ +require "tempfile" + +require "vagrant/util/shell_quote" + +module VagrantPlugins + module GuestBSD + module Cap + class PublicKey + def self.insert_public_key(machine, contents) + comm = machine.communicate + contents = contents.strip << "\n" + + remote_path = "/tmp/vagrant-insert-pubkey-#{Time.now.to_i}" + Tempfile.open("vagrant-bsd-insert-public-key") do |f| + f.binmode + f.write(contents) + f.fsync + f.close + comm.upload(f.path, remote_path) + end + + # Use execute (not sudo) because we want to execute this as the SSH + # user (which is "vagrant" by default). + comm.execute <<-EOH.gsub(/^ {12}/, "") + set -e + + mkdir -p ~/.ssh + chmod 0700 ~/.ssh + cat '#{remote_path}' >> ~/.ssh/authorized_keys + chmod 0600 ~/.ssh/authorized_keys + + rm -f '#{remote_path}' + EOH + end + + def self.remove_public_key(machine, contents) + comm = machine.communicate + contents = contents.strip << "\n" + + remote_path = "/tmp/vagrant-remove-pubkey-#{Time.now.to_i}" + Tempfile.open("vagrant-bsd-remove-public-key") do |f| + f.binmode + f.write(contents) + f.fsync + f.close + comm.upload(f.path, remote_path) + end + + # Use execute (not sudo) because we want to execute this as the SSH + # user (which is "vagrant" by default). + comm.execute <<-EOH.sub(/^ {12}/, "") + set -e + + if test -f ~/.ssh/authorized_keys; then + grep -v -x -f '#{remote_path}' ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.tmp + mv ~/.ssh/authorized_keys.tmp ~/.ssh/authorized_keys + fi + + rm -f '#{remote_path}' + EOH + end + end + end + end +end diff --git a/plugins/guests/bsd/plugin.rb b/plugins/guests/bsd/plugin.rb index 2da50eaf7..6d8e245a8 100644 --- a/plugins/guests/bsd/plugin.rb +++ b/plugins/guests/bsd/plugin.rb @@ -12,14 +12,19 @@ module VagrantPlugins end guest_capability(:bsd, :insert_public_key) do - require_relative "cap/insert_public_key" - Cap::InsertPublicKey + require_relative "cap/public_key" + Cap::PublicKey end guest_capability(:bsd, :mount_nfs_folder) do require_relative "cap/nfs" Cap::NFS end + + guest_capability(:bsd, :remove_public_key) do + require_relative "cap/public_key" + Cap::PublicKey + end end end end diff --git a/plugins/guests/darwin/cap/remove_public_key.rb b/plugins/guests/darwin/cap/remove_public_key.rb deleted file mode 100644 index 1e0fe415a..000000000 --- a/plugins/guests/darwin/cap/remove_public_key.rb +++ /dev/null @@ -1,21 +0,0 @@ -require "vagrant/util/shell_quote" - -module VagrantPlugins - module GuestDarwin - module Cap - class RemovePublicKey - def self.remove_public_key(machine, contents) - contents = contents.chomp - contents = Vagrant::Util::ShellQuote.escape(contents, "'") - - machine.communicate.tap do |comm| - if comm.test("test -f ~/.ssh/authorized_keys") - comm.execute( - "sed -i '' '/^.*#{contents}.*$/d' ~/.ssh/authorized_keys") - end - end - end - end - end - end -end diff --git a/plugins/guests/darwin/plugin.rb b/plugins/guests/darwin/plugin.rb index c3c264bd2..8cadc9f31 100644 --- a/plugins/guests/darwin/plugin.rb +++ b/plugins/guests/darwin/plugin.rb @@ -41,11 +41,6 @@ module VagrantPlugins Cap::MountVmwareSharedFolder end - guest_capability(:darwin, :remove_public_key) do - require_relative "cap/remove_public_key" - Cap::RemovePublicKey - end - guest_capability(:darwin, :rsync_installed) do require_relative "cap/rsync" Cap::RSync diff --git a/plugins/guests/freebsd/cap/remove_public_key.rb b/plugins/guests/freebsd/cap/remove_public_key.rb deleted file mode 100644 index 8d5526ca4..000000000 --- a/plugins/guests/freebsd/cap/remove_public_key.rb +++ /dev/null @@ -1,21 +0,0 @@ -require "vagrant/util/shell_quote" - -module VagrantPlugins - module GuestFreeBSD - module Cap - class RemovePublicKey - def self.remove_public_key(machine, contents) - contents = contents.chomp - contents = Vagrant::Util::ShellQuote.escape(contents, "'") - - machine.communicate.tap do |comm| - if comm.test("test -f ~/.ssh/authorized_keys") - comm.execute( - "sed -i .bak '/^.*#{contents}.*$/d' ~/.ssh/authorized_keys") - end - end - end - end - end - end -end diff --git a/plugins/guests/freebsd/plugin.rb b/plugins/guests/freebsd/plugin.rb index 000c67021..129b9bd3c 100644 --- a/plugins/guests/freebsd/plugin.rb +++ b/plugins/guests/freebsd/plugin.rb @@ -26,11 +26,6 @@ module VagrantPlugins Cap::Halt end - guest_capability(:freebsd, :remove_public_key) do - require_relative "cap/remove_public_key" - Cap::RemovePublicKey - end - guest_capability(:freebsd, :rsync_install) do require_relative "cap/rsync" Cap::RSync diff --git a/plugins/guests/netbsd/cap/remove_public_key.rb b/plugins/guests/netbsd/cap/remove_public_key.rb deleted file mode 100644 index d25e97679..000000000 --- a/plugins/guests/netbsd/cap/remove_public_key.rb +++ /dev/null @@ -1,21 +0,0 @@ -require "vagrant/util/shell_quote" - -module VagrantPlugins - module GuestNetBSD - module Cap - class RemovePublicKey - def self.remove_public_key(machine, contents) - contents = contents.chomp - contents = Vagrant::Util::ShellQuote.escape(contents, "'") - - machine.communicate.tap do |comm| - if comm.test("test -f ~/.ssh/authorized_keys") - comm.execute( - "sed -i '/^.*#{contents}.*$/d' ~/.ssh/authorized_keys") - end - end - end - end - end - end -end diff --git a/plugins/guests/netbsd/plugin.rb b/plugins/guests/netbsd/plugin.rb index e29e713f2..904a1a3b5 100644 --- a/plugins/guests/netbsd/plugin.rb +++ b/plugins/guests/netbsd/plugin.rb @@ -26,11 +26,6 @@ module VagrantPlugins Cap::Halt end - guest_capability(:netbsd, :remove_public_key) do - require_relative "cap/remove_public_key" - Cap::RemovePublicKey - end - guest_capability(:netbsd, :rsync_install) do require_relative "cap/rsync" Cap::RSync diff --git a/plugins/guests/openbsd/cap/remove_public_key.rb b/plugins/guests/openbsd/cap/remove_public_key.rb deleted file mode 100644 index 94cfce03b..000000000 --- a/plugins/guests/openbsd/cap/remove_public_key.rb +++ /dev/null @@ -1,21 +0,0 @@ -require "vagrant/util/shell_quote" - -module VagrantPlugins - module GuestOpenBSD - module Cap - class RemovePublicKey - def self.remove_public_key(machine, contents) - contents = contents.chomp - contents = Vagrant::Util::ShellQuote.escape(contents, "'") - - machine.communicate.tap do |comm| - if comm.test("test -f ~/.ssh/authorized_keys") - comm.execute( - "sed -i '/^.*#{contents}.*$/d' ~/.ssh/authorized_keys") - end - end - end - end - end - end -end diff --git a/plugins/guests/openbsd/plugin.rb b/plugins/guests/openbsd/plugin.rb index 2b4683755..821c99195 100644 --- a/plugins/guests/openbsd/plugin.rb +++ b/plugins/guests/openbsd/plugin.rb @@ -26,11 +26,6 @@ module VagrantPlugins Cap::Halt end - guest_capability(:openbsd, :remove_public_key) do - require_relative "cap/remove_public_key" - Cap::RemovePublicKey - end - guest_capability(:openbsd, :rsync_install) do require_relative "cap/rsync" Cap::RSync