From 0b03502bf2e16f1744eb6a164b3073edb4628601 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 27 Aug 2010 00:05:03 -0700 Subject: [PATCH] Converted final error_and_exit in ssh.rb to exceptions --- lib/vagrant/errors.rb | 5 +++++ lib/vagrant/ssh.rb | 6 ++---- templates/locales/en.yml | 7 +++++++ templates/strings.yml | 7 ------- test/vagrant/ssh_test.rb | 10 +++------- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 04e1d97fa..6030a405c 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -61,6 +61,11 @@ module Vagrant error_key(:ssh_authentication_failed) end + class SSHKeyBadPermissions < VagrantError + status_code(12) + error_key(:ssh_key_bad_permissions) + end + class SSHUnavailableWindows < VagrantError status_code(10) error_key(:ssh_unavailable_windows) diff --git a/lib/vagrant/ssh.rb b/lib/vagrant/ssh.rb index 6220faee9..2e07e43d5 100644 --- a/lib/vagrant/ssh.rb +++ b/lib/vagrant/ssh.rb @@ -7,8 +7,6 @@ module Vagrant # replace the process with SSH itself, run a specific set of commands, # upload files, or even check if a host is up. class SSH - include Vagrant::Util - # Reference back up to the environment which this SSH object belongs # to attr_accessor :env @@ -120,12 +118,12 @@ module Vagrant env.ui.info "Permissions on private key incorrect, fixing..." File.chmod(0600, key_path) - error_and_exit(:ssh_bad_permissions, :key_path => key_path) if file_perms(key_path) != "600" + raise Errors::SSHKeyBadPermissions.new(:key_path => key_path) if file_perms(key_path) != "600" end rescue Errno::EPERM # This shouldn't happen since we verify we own the file, but just # in case. - error_and_exit(:ssh_bad_permissions, :key_path => key_path) + raise Errors::SSHKeyBadPermissions.new(:key_path => key_path) end # Returns the file permissions of a given file. This is fairly unix specific diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 3b46bb846..313729feb 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -16,6 +16,13 @@ en: keypair for the SSH user not being properly set on the guest VM. Please verify that the guest VM is setup with the proper public key, and that the private key path for Vagrant is setup propery as well. + ssh_key_bad_permissions: |- + The private key to connect to this box via SSH has invalid permissions + set on it. The permissions of the private key should be set to 0600, otherwise SSH will + ignore the key. Vagrant tried to do this automatically for you but failed. Please set the + permissions on the following file to 0600 and then try running this command again: + + <%= key_path %> ssh_unavailable_windows: |- `vagrant ssh` isn't available on the Windows platform. The vagrant.ppk file for use with Putty is available at: diff --git a/templates/strings.yml b/templates/strings.yml index a2660a57e..3cc1c74f7 100644 --- a/templates/strings.yml +++ b/templates/strings.yml @@ -141,13 +141,6 @@ Provisioners must be an instance of Vagrant::Provisioners::Base :provisioner_unknown_type: |- Unknown provisioner type: <%= provisioner %> -:ssh_bad_permissions: |- - The private key to connect to this box via SSH has invalid permissions - set on it. The permissions of the private key should be set to 0600, otherwise SSH will - ignore the key. Vagrant tried to do this automatically for you but failed. Please set the - permissions on the following file to 0600 and then try running this command again: - - <%= key_path %> :ssh_bad_exit_status: |- The following SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed! diff --git a/test/vagrant/ssh_test.rb b/test/vagrant/ssh_test.rb index ff5085833..592ed20e5 100644 --- a/test/vagrant/ssh_test.rb +++ b/test/vagrant/ssh_test.rb @@ -25,7 +25,6 @@ class SshTest < Test::Unit::TestCase setup do mock_ssh @ssh.stubs(:check_key_permissions) - @ssh.stubs(:error_and_exit) Kernel.stubs(:exec) Vagrant::Util::Platform.stubs(:leopard?).returns(false) @@ -285,8 +284,7 @@ class SshTest < Test::Unit::TestCase @ssh.expects(:file_perms).returns("900").in_sequence(perm_sequence) File.expects(:chmod).with(0600, @key_path).once.in_sequence(perm_sequence) @ssh.expects(:file_perms).returns("600").in_sequence(perm_sequence) - @ssh.expects(:error_and_exit).never - @ssh.check_key_permissions(@key_path) + assert_nothing_raised { @ssh.check_key_permissions(@key_path) } end should "error and exit if the resulting chmod doesn't work" do @@ -294,15 +292,13 @@ class SshTest < Test::Unit::TestCase @ssh.expects(:file_perms).returns("900").in_sequence(perm_sequence) File.expects(:chmod).with(0600, @key_path).once.in_sequence(perm_sequence) @ssh.expects(:file_perms).returns("900").in_sequence(perm_sequence) - @ssh.expects(:error_and_exit).once.with(:ssh_bad_permissions, :key_path => @key_path).in_sequence(perm_sequence) - @ssh.check_key_permissions(@key_path) + assert_raises(Vagrant::Errors::SSHKeyBadPermissions) { @ssh.check_key_permissions(@key_path) } end should "error and exit if a bad file perm is raised" do @ssh.expects(:file_perms).with(@key_path).returns("900") File.expects(:chmod).raises(Errno::EPERM) - @ssh.expects(:error_and_exit).once.with(:ssh_bad_permissions, :key_path => @key_path) - @ssh.check_key_permissions(@key_path) + assert_raises(Vagrant::Errors::SSHKeyBadPermissions) { @ssh.check_key_permissions(@key_path) } end end