summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan McLellan <btm@opscode.com>2012-11-01 17:28:49 -0700
committerBryan McLellan <btm@opscode.com>2012-11-13 09:09:06 -0800
commitf4755ea7229361c87fb51b11c18a73ca40f1d9f5 (patch)
tree7858760c57cffeb541bb722bdc1e63fda232a368
parentc09abac8d868063a2e4f884f7073ea21b16af102 (diff)
downloadchef-f4755ea7229361c87fb51b11c18a73ca40f1d9f5.tar.gz
CHEF-3577: Allow ssh config port and user to be used
Net::SSH::Multi takes a user argument which we pass, so we need to read the user from the ssh_config before passing the user and use it if applicable. We also were sending port 22 even if the user hadn't specified a port, due to a mixlib-cli default. This would override any port set in an ssh config. It isn't necessary to send a port, Net::SSH will default to 22 on its own.
-rw-r--r--chef/lib/chef/knife/bootstrap.rb1
-rw-r--r--chef/lib/chef/knife/ssh.rb11
-rw-r--r--chef/spec/unit/knife/ssh_spec.rb17
3 files changed, 24 insertions, 5 deletions
diff --git a/chef/lib/chef/knife/bootstrap.rb b/chef/lib/chef/knife/bootstrap.rb
index a8e9201c26..a4c117e28f 100644
--- a/chef/lib/chef/knife/bootstrap.rb
+++ b/chef/lib/chef/knife/bootstrap.rb
@@ -51,7 +51,6 @@ class Chef
:short => "-p PORT",
:long => "--ssh-port PORT",
:description => "The ssh port",
- :default => "22",
:proc => Proc.new { |key| Chef::Config[:knife][:ssh_port] = key }
option :ssh_gateway,
diff --git a/chef/lib/chef/knife/ssh.rb b/chef/lib/chef/knife/ssh.rb
index 26031b14e4..c34bbe14ec 100644
--- a/chef/lib/chef/knife/ssh.rb
+++ b/chef/lib/chef/knife/ssh.rb
@@ -72,7 +72,6 @@ class Chef
:short => "-p PORT",
:long => "--ssh-port PORT",
:description => "The ssh port",
- :default => "22",
:proc => Proc.new { |key| Chef::Config[:knife][:ssh_port] = key }
option :ssh_gateway,
@@ -166,13 +165,17 @@ class Chef
list.each do |item|
Chef::Log.debug("Adding #{item}")
-
- hostspec = config[:ssh_user] ? "#{config[:ssh_user]}@#{item}" : item
session_opts = {}
+
+ ssh_config = Net::SSH.configuration_for(item)
+
+ # Chef::Config[:knife][:ssh_user] is parsed in #configure_user and written to config[:ssh_user]
+ user = config[:ssh_user] || ssh_config[:user]
+ hostspec = user ? "#{user}@#{item}" : item
session_opts[:keys] = File.expand_path(config[:identity_file]) if config[:identity_file]
session_opts[:keys_only] = true if config[:identity_file]
session_opts[:password] = config[:ssh_password] if config[:ssh_password]
- session_opts[:port] = Chef::Config[:knife][:ssh_port] || config[:ssh_port]
+ session_opts[:port] = config[:ssh_port] || Chef::Config[:knife][:ssh_port] || ssh_config[:port]
session_opts[:logger] = Chef::Log.logger if Chef::Log.level == :debug
if !config[:host_key_verify]
diff --git a/chef/spec/unit/knife/ssh_spec.rb b/chef/spec/unit/knife/ssh_spec.rb
index 4ac12117ae..cec3d40e65 100644
--- a/chef/spec/unit/knife/ssh_spec.rb
+++ b/chef/spec/unit/knife/ssh_spec.rb
@@ -180,5 +180,22 @@ describe Chef::Knife::Ssh do
end
end
+ describe "#session_from_list" do
+ before :each do
+ @knife.instance_variable_set(:@longest, 0)
+ ssh_config = {:timeout => 50, :user => "locutus", :port => 23 }
+ Net::SSH.stub!(:configuration_for).with('the.b.org').and_return(ssh_config)
+ end
+
+ it "uses the port from an ssh config file" do
+ @knife.session_from_list(['the.b.org'])
+ @knife.session.servers[0].port.should == 23
+ end
+
+ it "uses the user from an ssh config file" do
+ @knife.session_from_list(['the.b.org'])
+ @knife.session.servers[0].user.should == "locutus"
+ end
+ end
end