moved to key based ssh

This commit is contained in:
John Bender 2010-03-12 01:19:45 -08:00
parent 9ba0647b64
commit 00eecf5bdd
10 changed files with 75 additions and 40 deletions

View File

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

27
keys/vagrant Normal file
View File

@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzI
w+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoP
kcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2
hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NO
Td0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcW
yLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQIBIwKCAQEA4iqWPJXtzZA68mKd
ELs4jJsdyky+ewdZeNds5tjcnHU5zUYE25K+ffJED9qUWICcLZDc81TGWjHyAqD1
Bw7XpgUwFgeUJwUlzQurAv+/ySnxiwuaGJfhFM1CaQHzfXphgVml+fZUvnJUTvzf
TK2Lg6EdbUE9TarUlBf/xPfuEhMSlIE5keb/Zz3/LUlRg8yDqz5w+QWVJ4utnKnK
iqwZN0mwpwU7YSyJhlT4YV1F3n4YjLswM5wJs2oqm0jssQu/BT0tyEXNDYBLEF4A
sClaWuSJ2kjq7KhrrYXzagqhnSei9ODYFShJu8UWVec3Ihb5ZXlzO6vdNQ1J9Xsf
4m+2ywKBgQD6qFxx/Rv9CNN96l/4rb14HKirC2o/or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BckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== johnbender@vagrant.local

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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