Merge pull request #7481 from mitchellh/sethvargo/bsd_keys

guests/bsd: Centralize logic for public key management
This commit is contained in:
Seth Vargo 2016-06-18 13:53:37 -04:00 committed by GitHub
commit f357d0d018
11 changed files with 72 additions and 142 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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