On SSH authentication failure, give a helpful error message outlining what may have gone wrong.

This commit is contained in:
Mitchell Hashimoto 2010-03-15 00:50:23 -07:00
parent 5aeee61e83
commit de54433496
2 changed files with 29 additions and 5 deletions

View File

@ -1,5 +1,7 @@
module Vagrant module Vagrant
class SSH class SSH
include Vagrant::Util
class << self class << self
def connect(opts={}) def connect(opts={})
options = {} options = {}
@ -11,9 +13,9 @@ module Vagrant
end end
def execute(opts={}) def execute(opts={})
Net::SSH.start(Vagrant.config.ssh.host, Net::SSH.start(Vagrant.config.ssh.host,
Vagrant.config[:ssh][:username], Vagrant.config[:ssh][:username],
opts.merge( :port => port, opts.merge( :port => port,
:keys => [Vagrant.config.ssh.private_key_path])) do |ssh| :keys => [Vagrant.config.ssh.private_key_path])) do |ssh|
yield ssh yield ssh
end end
@ -40,6 +42,22 @@ module Vagrant
check_thread.join(Vagrant.config.ssh.timeout) check_thread.join(Vagrant.config.ssh.timeout)
return check_thread[:result] return check_thread[:result]
rescue Net::SSH::AuthenticationFailed
error_and_exit(<<-msg)
SSH authentication failed! While this could be due to a variety of reasons,
the two most common are: private key path is incorrect or you're using a box
which was built for Vagrant 0.1.x.
Vagrant 0.2.x dropped support for password-based authentication. If you're
tring to `vagrant up` a box which does not support Vagrant's private/public
keypair, then this error will be raised. To resolve this, read the guide
on converting base boxes from password-based to keypairs here:
http://vagrantup.com/docs/converting_password_to_key_ssh.html
If the box was built for 0.2.x and contains a custom public key, perhaps
the path to the private key is incorrect. Check your `config.ssh.private_key_path`.
msg
end end
def port(opts={}) def port(opts={})

View File

@ -8,8 +8,8 @@ class SshTest < Test::Unit::TestCase
context "connecting to SSH" do context "connecting to SSH" do
test "should call exec with defaults when no options are supplied" do test "should call exec with defaults when no options are supplied" do
ssh = Vagrant.config.ssh ssh = Vagrant.config.ssh
ssh_exec_expect(Vagrant::SSH.port, ssh_exec_expect(Vagrant::SSH.port,
Vagrant.config.ssh.private_key_path, Vagrant.config.ssh.private_key_path,
Vagrant.config.ssh.username, Vagrant.config.ssh.username,
Vagrant.config.ssh.host) Vagrant.config.ssh.host)
Vagrant::SSH.connect Vagrant::SSH.connect
@ -104,6 +104,12 @@ class SshTest < Test::Unit::TestCase
Vagrant::SSH.expects(:execute).with(:timeout => Vagrant.config.ssh.timeout).yields(true) Vagrant::SSH.expects(:execute).with(:timeout => Vagrant.config.ssh.timeout).yields(true)
assert Vagrant::SSH.up? assert Vagrant::SSH.up?
end end
should "error and exit if a Net::SSH::AuthenticationFailed is raised" do
Vagrant::SSH.expects(:execute).raises(Net::SSH::AuthenticationFailed)
Vagrant::SSH.expects(:error_and_exit).once
Vagrant::SSH.up?
end
end end
context "getting the ssh port" do context "getting the ssh port" do