guests/darwin: Upload public key instead of trying to shellescape

This commit is contained in:
Seth Vargo 2016-05-31 01:11:40 -04:00
parent dc883aa46f
commit 499e4afba8
No known key found for this signature in database
GPG Key ID: 905A90C2949E8787
1 changed files with 19 additions and 7 deletions

View File

@ -1,19 +1,31 @@
require "vagrant/util/shell_quote"
require "tempfile"
module VagrantPlugins
module GuestDarwin
module Cap
class InsertPublicKey
def self.insert_public_key(machine, contents)
comm = machine.communicate
contents = contents.chomp
contents = Vagrant::Util::ShellQuote.escape(contents, "'")
machine.communicate.tap do |comm|
comm.execute("mkdir -p ~/.ssh")
comm.execute("chmod 0700 ~/.ssh")
comm.execute("printf '#{contents}\\n' >> ~/.ssh/authorized_keys")
comm.execute("chmod 0600 ~/.ssh/authorized_keys")
remote_path = "/tmp/vagrant-authorized-keys-#{Time.now.to_i}"
Tempfile.open("vagrant-darwin-insert-public-key") do |f|
f.binmode
f.write(contents)
f.fsync
f.close
comm.upload(f.path, remote_path)
end
comm.execute <<-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
end
end
end