Skip to content

Commit 8d34a75

Browse files
Merge pull request #232 from jaredbeck/net-ssh_verify_host_key
Extract private methods from Fog::SSH::Real constructor
2 parents 5b5d176 + 0ba54cd commit 8d34a75

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ New contributors are always welcome, when it doubt please ask questions. We stri
88
* Offer feedback on open [pull requests](https://github.com/fog/fog-core/pulls).
99
* Review open [issues](https://github.com/fog/fog-core/issues) for things to help on.
1010
* [Create an issue](https://github.com/fog/fog-core/issues/new) to start a discussion on additions or features.
11-
* Fork the project, add your changes and tests to cover them in a topic branch.
11+
* Fork the project
12+
* Setup
13+
* bundle install
14+
* bundle exec rake
15+
* Add your changes and tests to cover them in a topic branch.
1216
* Commit your changes and rebase against `fog/fog-core` to ensure everything is up to date.
1317
* [Submit a pull request](https://github.com/fog/fog-core/compare/)
1418

lib/fog/core/ssh.rb

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,10 @@ def run(commands, &_blk)
3434

3535
class Real
3636
def initialize(address, username, options)
37-
begin
38-
require "net/ssh"
39-
rescue LoadError
40-
Fog::Logger.warning("'net/ssh' missing, please install and try again.")
41-
exit(1)
42-
end
43-
44-
key_manager = Net::SSH::Authentication::KeyManager.new(nil, options)
45-
46-
unless options[:key_data] || options[:keys] || options[:password] || key_manager.agent
47-
raise ArgumentError, ":key_data, :keys, :password or a loaded ssh-agent is required to initialize SSH"
48-
end
49-
50-
options[:timeout] ||= 30
51-
if options[:key_data] || options[:keys]
52-
options[:keys_only] = true
53-
# Explicitly set these so net-ssh doesn"t add the default keys
54-
# as seen at https://github.com/net-ssh/net-ssh/blob/master/lib/net/ssh/authentication/session.rb#L131-146
55-
options[:keys] = [] unless options[:keys]
56-
options[:key_data] = [] unless options[:key_data]
57-
end
58-
37+
assert_net_ssh_loaded
5938
@address = address
6039
@username = username
61-
@options = { :paranoid => false, :verify_host_key => false }.merge(options)
62-
@options.delete(:paranoid) if Net::SSH::VALID_OPTIONS.include? :verify_host_key
40+
@options = build_options(options)
6341
end
6442

6543
def run(commands, &blk)
@@ -107,6 +85,50 @@ def run(commands, &blk)
10785
end
10886
results
10987
end
88+
89+
private
90+
91+
def assert_net_ssh_loaded
92+
begin
93+
require "net/ssh"
94+
rescue LoadError
95+
Fog::Logger.warning("'net/ssh' missing, please install and try again.")
96+
exit(1)
97+
end
98+
end
99+
100+
# Modifies the given `options` hash AND returns a new hash.
101+
# TODO: Probably shouldn't modify the given hash.
102+
def build_options(options)
103+
validate_options(options)
104+
merge_default_options_into(options)
105+
end
106+
107+
def merge_default_options_into(options)
108+
options[:timeout] ||= 30
109+
if options[:key_data] || options[:keys]
110+
options[:keys_only] = true
111+
# Explicitly set these so net-ssh doesn"t add the default keys
112+
# as seen at https://github.com/net-ssh/net-ssh/blob/master/lib/net/ssh/authentication/session.rb#L131-146
113+
options[:keys] = [] unless options[:keys]
114+
options[:key_data] = [] unless options[:key_data]
115+
end
116+
117+
# net-ssh has deprecated :paranoid in favor of :verify_host_key
118+
# https://github.com/net-ssh/net-ssh/pull/524
119+
opts = { :paranoid => false, :verify_host_key => false }.merge(options)
120+
if Net::SSH::VALID_OPTIONS.include? :verify_host_key
121+
otps.delete(:paranoid)
122+
end
123+
opts
124+
end
125+
126+
def validate_options(options)
127+
key_manager = Net::SSH::Authentication::KeyManager.new(nil, options)
128+
unless options[:key_data] || options[:keys] || options[:password] || key_manager.agent
129+
raise ArgumentError, ":key_data, :keys, :password or a loaded ssh-agent is required to initialize SSH"
130+
end
131+
end
110132
end
111133

112134
class Result

0 commit comments

Comments
 (0)