From 00eecf5bdda678014f333a69724725a131e25198 Mon Sep 17 00:00:00 2001 From: John Bender Date: Fri, 12 Mar 2010 01:19:45 -0800 Subject: [PATCH] moved to key based ssh --- config/default.rb | 1 + keys/vagrant | 27 +++++++++++++++++++++++++++ keys/vagrant.pub | 1 + lib/vagrant/config.rb | 5 +++++ lib/vagrant/ssh.rb | 15 ++++++++------- script/vagrant-ssh-expect.sh | 23 ----------------------- test/test_helper.rb | 1 + test/vagrant/config_test.rb | 7 +++++++ test/vagrant/ssh_test.rb | 31 +++++++++++++++++++++++-------- vagrant.gemspec | 4 ++-- 10 files changed, 75 insertions(+), 40 deletions(-) create mode 100644 keys/vagrant create mode 100644 keys/vagrant.pub delete mode 100755 script/vagrant-ssh-expect.sh diff --git a/config/default.rb b/config/default.rb index dc24ca140..de8336d65 100644 --- a/config/default.rb +++ b/config/default.rb @@ -10,6 +10,7 @@ Vagrant::Config.run do |config| config.ssh.forwarded_port_key = "ssh" config.ssh.max_tries = 10 config.ssh.timeout = 30 + config.ssh.private_key_path = File.join(File.dirname(__FILE__), '..', 'keys', 'vagrant') config.vm.box_ovf = "box.ovf" config.vm.base_mac = "0800279C2E42" diff --git a/keys/vagrant b/keys/vagrant new file mode 100644 index 000000000..7d6a08390 --- /dev/null +++ b/keys/vagrant @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzI +w+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoP +kcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2 +hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NO +Td0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcW +yLbIbEgE98OHlnV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xT2axM0/W/CRknmGaJ +kda/AoGANWrLCz708y7VYgAtW2Uf1DPOIYMdvo6fxIB5i9ZfISgcJ/bbCUkFrhoH ++vq/5CIWxCPp0f85R4qxxQ5ihxJ0YDQT9Jpx4TMss4PSavPaBH3RXow5Ohe+bYoQ +NE5OgEXk2wVfZczCZpigBKbKZHNYcelXtTt/nP3rsCuGcM4h53s= +-----END RSA PRIVATE KEY----- diff --git a/keys/vagrant.pub b/keys/vagrant.pub new file mode 100644 index 000000000..f027f9c32 --- /dev/null +++ b/keys/vagrant.pub @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== johnbender@vagrant.local diff --git a/lib/vagrant/config.rb b/lib/vagrant/config.rb index afeaa78e4..88cf11b23 100644 --- a/lib/vagrant/config.rb +++ b/lib/vagrant/config.rb @@ -64,6 +64,11 @@ module Vagrant attr_accessor :forwarded_port_key attr_accessor :max_tries attr_accessor :timeout + attr_accessor :private_key_path + + def private_key_path + File.expand_path(@private_key_path) + end end class VMConfig < Base diff --git a/lib/vagrant/ssh.rb b/lib/vagrant/ssh.rb index 9da69b3f7..b068ad2d0 100644 --- a/lib/vagrant/ssh.rb +++ b/lib/vagrant/ssh.rb @@ -1,19 +1,20 @@ module Vagrant class SSH - SCRIPT = File.join(File.dirname(__FILE__), '..', '..', 'script', 'vagrant-ssh-expect.sh') - class << self def connect(opts={}) options = {} - [:host, :password, :username].each do |param| + [:host, :username, :private_key_path].each do |param| options[param] = opts[param] || Vagrant.config.ssh.send(param) end - Kernel.exec "#{SCRIPT} #{options[:username]} #{options[:password]} #{options[:host]} #{port(opts)}".strip + Kernel.exec "ssh -p #{port(opts)} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i #{options[:private_key_path]} #{options[:username]}@#{options[:host]}".strip end - def execute - Net::SSH.start(Vagrant.config.ssh.host, Vagrant.config[:ssh][:username], :port => port, :password => Vagrant.config[:ssh][:password]) do |ssh| + def execute(opts={}) + Net::SSH.start(Vagrant.config.ssh.host, + Vagrant.config[:ssh][:username], + opts.merge( :port => port, + :keys => [Vagrant.config.ssh.private_key_path])) do |ssh| yield ssh end end @@ -29,7 +30,7 @@ module Vagrant check_thread = Thread.new do begin Thread.current[:result] = false - Net::SSH.start(Vagrant.config.ssh.host, Vagrant.config.ssh.username, :port => port, :password => Vagrant.config.ssh.password, :timeout => Vagrant.config.ssh.timeout) do |ssh| + execute(:timeout => Vagrant.config.ssh.timeout) do |ssh| Thread.current[:result] = true end rescue Errno::ECONNREFUSED, Net::SSH::Disconnect diff --git a/script/vagrant-ssh-expect.sh b/script/vagrant-ssh-expect.sh deleted file mode 100755 index 280bd8efc..000000000 --- a/script/vagrant-ssh-expect.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/expect - -set uname [lrange $argv 0 0] -set password [lrange $argv 1 1] -set host [lrange $argv 2 2] -set port [lrange $argv 3 3] -set timeout 30 - -if { $port != "" } { - set port_option "-p $port" -} else { - set port_option "" -} - -spawn ssh $port_option -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $uname@$host - -expect "*password: " { - send "$password\r" -} timeout { - send_user "Error connecting" -} - -interact diff --git a/test/test_helper.rb b/test/test_helper.rb index 17dc61e40..07049ccfc 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -31,6 +31,7 @@ class Test::Unit::TestCase config.ssh.forwarded_port_key = "ssh" config.ssh.max_tries = 10 config.ssh.timeout = 10 + config.ssh.private_key_path = '~/foo' config.vm.box = "foo" config.vm.box_ovf = "box.ovf" diff --git a/test/vagrant/config_test.rb b/test/vagrant/config_test.rb index 547e07cb3..6e9807fed 100644 --- a/test/vagrant/config_test.rb +++ b/test/vagrant/config_test.rb @@ -1,6 +1,13 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper') class ConfigTest < Test::Unit::TestCase + context "the ssh config" do + should "expand any path when requesting the value" do + File.stubs(:expand_path).with(Vagrant.config.ssh[:private_key_path]).returns('success') + assert Vagrant.config.ssh.private_key_path, 'success' + end + end + context "adding configures" do should "forward the method to the Top class" do key = mock("key") diff --git a/test/vagrant/ssh_test.rb b/test/vagrant/ssh_test.rb index aafe07344..3e991ffc2 100644 --- a/test/vagrant/ssh_test.rb +++ b/test/vagrant/ssh_test.rb @@ -6,21 +6,31 @@ class SshTest < Test::Unit::TestCase end context "connecting to SSH" do - setup do - @script = Vagrant::SSH::SCRIPT - end - test "should call exec with defaults when no options are supplied" do ssh = Vagrant.config.ssh - Kernel.expects(:exec).with("#{@script} #{ssh[:username]} #{ssh[:password]} #{ssh[:host]} #{Vagrant::SSH.port}") + ssh_exec_expect(Vagrant::SSH.port, + Vagrant.config.ssh.private_key_path, + Vagrant.config.ssh.username, + Vagrant.config.ssh.host) Vagrant::SSH.connect end test "should call exec with supplied params" do - args = {:username => 'bar', :password => 'baz', :host => 'bak', :port => 'bag'} - Kernel.expects(:exec).with("#{@script} #{args[:username]} #{args[:password]} #{args[:host]} #{args[:port]}") + args = {:username => 'bar', :private_key_path => 'baz', :host => 'bak', :port => 'bag'} + ssh_exec_expect(args[:port], args[:private_key_path], args[:username], args[:host]) Vagrant::SSH.connect(args) end + + def ssh_exec_expect(port, key_path, uname, host) + Kernel.expects(:exec).with() do |arg| + assert arg =~ /^ssh/ + assert arg =~ /-p #{port}/ + assert arg =~ /-i #{key_path}/ + assert arg =~ /#{uname}@#{host}/ + # TODO options not tested for as they may be removed, they may be removed + true + end + end end context "executing ssh commands" do @@ -29,7 +39,7 @@ class SshTest < Test::Unit::TestCase assert_equal Vagrant.config.ssh.host, host assert_equal Vagrant.config.ssh.username, username assert_equal Vagrant::SSH.port, opts[:port] - assert_equal Vagrant.config.ssh.password, opts[:password] + assert_equal [Vagrant.config.ssh.private_key_path], opts[:keys] true end Vagrant::SSH.execute @@ -89,6 +99,11 @@ class SshTest < Test::Unit::TestCase assert !Vagrant::SSH.up? } end + + should "specifity the timeout as an option to execute" do + Vagrant::SSH.expects(:execute).with(:timeout => Vagrant.config.ssh.timeout).yields(true) + assert Vagrant::SSH.up? + end end context "getting the ssh port" do diff --git a/vagrant.gemspec b/vagrant.gemspec index 0d132cec1..f2a57c678 100644 --- a/vagrant.gemspec +++ b/vagrant.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Mitchell Hashimoto", "John Bender"] - s.date = %q{2010-03-09} + s.date = %q{2010-03-11} s.description = %q{Vagrant is a tool for building and distributing virtualized development environments.} s.email = ["mitchell.hashimoto@gmail.com", "john.m.bender@gmail.com"] s.executables = ["vagrant", "vagrant-box", "vagrant-down", "vagrant-halt", "vagrant-init", "vagrant-package", "vagrant-reload", "vagrant-resume", "vagrant-ssh", "vagrant-suspend", "vagrant-up"] @@ -112,7 +112,7 @@ Gem::Specification.new do |s| s.homepage = %q{http://github.com/mitchellh/vagrant} s.rdoc_options = ["--charset=UTF-8"] s.require_paths = ["lib"] - s.rubygems_version = %q{1.3.6} + s.rubygems_version = %q{1.3.5} s.summary = %q{Vagrant is a tool for building and distributing virtualized development environments.} s.test_files = [ "test/test_helper.rb",