Skip to content

Commit

Permalink
Added support for proxy_command
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Becker <[email protected]>
  • Loading branch information
cbeckr committed Dec 10, 2017
1 parent 6472542 commit ee33440
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/train/transports/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def connection_options(opts)
keys: opts[:key_files],
password: opts[:password],
forward_agent: opts[:forward_agent],
proxy_command: opts[:proxy_command],
transport_options: opts,
}

Expand Down
6 changes: 6 additions & 0 deletions lib/train/transports/ssh_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def login_command
args += %w{ -o IdentitiesOnly=yes } if options[:keys]
args += %W( -o LogLevel=#{level} )
args += %W( -o ForwardAgent=#{fwd_agent} ) if options.key?(:forward_agent)
args += %W( -o ProxyCommand='#{options[:proxy_command]}' ) unless options[:proxy_command].nil?
Array(options[:keys]).each do |ssh_key|
args += %W( -i #{ssh_key} )
end
Expand Down Expand Up @@ -144,6 +145,11 @@ def uri
# @api private
def establish_connection(opts)
logger.debug("[SSH] opening connection to #{self}")
if @options[:proxy_command]
require 'net/ssh/proxy/command'
@options[:proxy] = Net::SSH::Proxy::Command.new(@options[:proxy_command])
@options.delete(:proxy_command)
end
Net::SSH.start(@hostname, @username, @options.clone.delete_if { |_key, value| value.nil? })
rescue *RESCUE_EXCEPTIONS_ON_ESTABLISH => e
if (opts[:retries] -= 1) <= 0
Expand Down
16 changes: 16 additions & 0 deletions test/unit/transports/ssh_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
host: rand.to_s,
password: rand.to_s,
key_files: rand.to_s,
proxy_command: 'ssh [email protected] -W %h:%p',
}}
let(:cls_agent) { cls.new({ host: rand.to_s }) }

Expand Down Expand Up @@ -95,6 +96,7 @@
"-o", "IdentitiesOnly=yes",
"-o", "LogLevel=VERBOSE",
"-o", "ForwardAgent=no",
"-o", "ProxyCommand='ssh [email protected] -W %h:%p'",
"-i", conf[:key_files],
"-p", "22",
"root@#{conf[:host]}",
Expand All @@ -120,6 +122,19 @@
cls_agent.stubs(:ssh_known_identities).returns({:some => 'rsa_key'})
cls_agent.connection
end

it 'sets up a proxy when ssh proxy command is specified' do
mock = MiniTest::Mock.new
mock.expect(:call, true) do |hostname, username, options|
options[:proxy].kind_of?(Net::SSH::Proxy::Command) &&
'ssh [email protected] -W %h:%p' == options[:proxy].command_line_template
end
connection.stubs(:run_command)
Net::SSH.stub(:start, mock) do
connection.wait_until_ready
end
mock.verify
end
end

describe 'converting connection to string for logging' do
Expand Down Expand Up @@ -154,6 +169,7 @@
it 'wont connect if it is not possible' do
conf[:host] = 'localhost'
conf[:port] = 1
conf.delete :proxy_command
conn = cls.new(conf).connection
proc { conn.run_command('uname') }.must_raise Train::Transports::SSHFailed
end
Expand Down

0 comments on commit ee33440

Please sign in to comment.