@@ -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