Sha256: 88c15f8f7805691e25811d35d0491b6043429f652c03d8a8d1795c42090cf124

Contents?: true

Size: 1.95 KB

Versions: 48

Compression:

Stored size: 1.95 KB

Contents

require "tempfile"

require "vagrant/util/shell_quote"

module VagrantPlugins
  module GuestLinux
    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-linux-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(/^ */, "")
            mkdir -p ~/.ssh
            chmod 0700 ~/.ssh
            cat '#{remote_path}' >> ~/.ssh/authorized_keys && chmod 0600 ~/.ssh/authorized_keys
            result=$?
            rm -f '#{remote_path}'
            exit $result
          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-linux-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(/^ */, "")
            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 && chmod 0600 ~/.ssh/authorized_keys
              result=$?
            fi
            rm -f '#{remote_path}'
            exit $result
          EOH
        end
      end
    end
  end
end

Version data entries

48 entries across 44 versions & 5 rubygems

Version Path
vagrant-unbundled-2.3.6.0 plugins/guests/linux/cap/public_key.rb
tamtam-vagrant-reload-1.2.1 vendor/cache/vagrant-2092df529ae7/plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.3.3.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.3.2.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.19.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.18.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.16.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.14.0 plugins/guests/linux/cap/public_key.rb
vagrant-aws-mkubenka-0.7.2.pre.24 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-22795b161bf6/plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.10.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.9.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.8.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.7.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.6.2 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.6.1 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.6.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.5.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.4.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.3.0 plugins/guests/linux/cap/public_key.rb
vagrant-unbundled-2.2.2.0 plugins/guests/linux/cap/public_key.rb