Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silence verify_host_key warning from net-ssh #430

Merged
merged 3 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions lib/train/transports/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ def connection_options(opts)
non_interactive: opts[:non_interactive],
transport_options: opts,
}
# disable host key verification. The hash key to use
# disable host key verification. The hash key and value to use
# depends on the version of net-ssh in use.
connection_options[verify_host_key_option] = opts[:verify_host_key] || false
connection_options[verify_host_key_option] = verify_host_key_value(opts[:verify_host_key])

connection_options
end
Expand All @@ -193,6 +193,35 @@ def verify_host_key_option
current_net_ssh >= new_option_version ? :verify_host_key : :paranoid
end

# Likewise, version <5 accepted false; 5+ requires :never or will
# issue a deprecation warning. This method allows a lot of common
# things through.
def verify_host_key_value(given)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some, but stubbing the const used to detect the net-ssh version (which affects the mapping, here) proved elusive. So, the tests check whatever net-ssh version is currently being used, which is not ideal.

current_net_ssh = Net::SSH::Version::CURRENT
new_value_version = Net::SSH::Version[5, 0, 0]
if current_net_ssh >= new_value_version
# 5.0+ style
{
# It's not a boolean anymore.
'true' => :always,
'false' => :never,
true => :always,
false => :never,
# May be correct value, but strings from JSON config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing the nil case here, since it's possible for someone to specify the option w/ nil value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks!

'always' => :always,
'never' => :never,
nil => :never,
}.fetch(given, given)
else
# up to 4.2 style
{
'true' => true,
'false' => false,
nil => false,
}.fetch(given, given)
end
end

# Creates a new SSH Connection instance and save it for potential future
# reuse.
#
Expand Down
1 change: 1 addition & 0 deletions test/unit/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'minitest/autorun'
require 'minitest/spec'
require 'mocha/minitest'
require 'mocha/setup'
require 'byebug'

Expand Down
38 changes: 37 additions & 1 deletion test/unit/transports/ssh_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8

require 'helper'
require_relative '../helper'
require 'train/transports/ssh'

describe 'ssh transport' do
Expand Down Expand Up @@ -62,6 +62,42 @@
connection_options[:verify_host_key].must_equal true
end
end

describe "various values are mapped appropriately for verify_host_key" do
# This would be better:
# Net::SSH::Version.stub_const(:CURRENT, Net::SSH::Version[5,0,1])
current_version = Net::SSH::Version::CURRENT
threshold_version = Net::SSH::Version[5, 0, 0]
if current_version < threshold_version
it "maps correctly when net-ssh < 5.0" do
{
'true' => true,
'false' => false,
nil => false,
}.each do |given, expected|
opts = { :verify_host_key => given }
seen_opts = ssh.send(:connection_options, opts)
seen_opts[:verify_host_key].must_equal expected
end
end
else
it "maps correctly when net-ssh > 5.0" do
{
'true' => :always,
'false' => :never,
true => :always,
false => :never,
'always' => :always,
'never' => :never,
nil => :never,
}.each do |given, expected|
opts = { :verify_host_key => given }
seen_opts = ssh.send(:connection_options, opts)
seen_opts[:verify_host_key].must_equal expected
end
end
end
end
end

describe 'ssh options' do
Expand Down