summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiklós Fazekas <mfazekas@szemafor.com>2016-04-28 11:14:28 +0200
committerMiklós Fazekas <mfazekas@szemafor.com>2016-04-28 11:14:28 +0200
commit0d7c2459e1c53faa8b178a76898dd2ebd0229f3d (patch)
tree2b0403207b313bda9a0a523f07bd355ec0ddc963
parente0ca4a97439357307210cf46ab7376de0d9affbd (diff)
parent9007b5d6cf626356c19d124073c0c726fc61cae6 (diff)
downloadnet-ssh-0d7c2459e1c53faa8b178a76898dd2ebd0229f3d.tar.gz
Merge pull request #360 from alongoldboim/add_socket_option
Added ability to specify agent socket
-rw-r--r--lib/net/ssh.rb5
-rw-r--r--lib/net/ssh/authentication/agent/java_pageant.rb2
-rw-r--r--lib/net/ssh/authentication/agent/socket.rb10
-rw-r--r--lib/net/ssh/authentication/key_manager.rb2
-rw-r--r--lib/net/ssh/service/forward.rb2
-rw-r--r--test/authentication/test_agent.rb10
6 files changed, 21 insertions, 10 deletions
diff --git a/lib/net/ssh.rb b/lib/net/ssh.rb
index 27e5847..d98f43a 100644
--- a/lib/net/ssh.rb
+++ b/lib/net/ssh.rb
@@ -71,7 +71,7 @@ module Net
:known_hosts, :global_known_hosts_file, :user_known_hosts_file, :host_key_alias,
:host_name, :user, :properties, :passphrase, :keys_only, :max_pkt_size,
:max_win_size, :send_env, :use_agent, :number_of_password_prompts,
- :append_supported_algorithms, :non_interactive, :password_prompt
+ :append_supported_algorithms, :non_interactive, :password_prompt, :agent_socket_factory
]
# The standard means of starting a new SSH connection. When used with a
@@ -195,6 +195,9 @@ module Net
# to prefer failing a password/etc auth methods vs asking for password
# * :password_prompt => a custom prompt object with ask method. See Net::SSH::Prompt
#
+ # * :agent_socket_factory => enables the user to pass a lambda/block that will serve as the socket factory
+ # Net::SSH::start(user,host,agent_socket_factory: ->{ UNIXSocket.open('/foo/bar') })
+ # example: ->{ UNIXSocket.open('/foo/bar')}
# If +user+ parameter is nil it defaults to USER from ssh_config, or
# local username
def self.start(host, user=nil, options={}, &block)
diff --git a/lib/net/ssh/authentication/agent/java_pageant.rb b/lib/net/ssh/authentication/agent/java_pageant.rb
index ec3f635..d0d81d1 100644
--- a/lib/net/ssh/authentication/agent/java_pageant.rb
+++ b/lib/net/ssh/authentication/agent/java_pageant.rb
@@ -19,7 +19,7 @@ module Net; module SSH; module Authentication
# Instantiates a new agent object, connects to a running SSH agent,
# negotiates the agent protocol version, and returns the agent object.
- def self.connect(logger=nil)
+ def self.connect(logger=nil, agent_socket_factory)
agent = new(logger)
agent.connect!
agent
diff --git a/lib/net/ssh/authentication/agent/socket.rb b/lib/net/ssh/authentication/agent/socket.rb
index 7ca6d92..6466c13 100644
--- a/lib/net/ssh/authentication/agent/socket.rb
+++ b/lib/net/ssh/authentication/agent/socket.rb
@@ -42,9 +42,9 @@ module Net; module SSH; module Authentication
# Instantiates a new agent object, connects to a running SSH agent,
# negotiates the agent protocol version, and returns the agent object.
- def self.connect(logger=nil)
+ def self.connect(logger=nil, agent_socket_factory = nil)
agent = new(logger)
- agent.connect!
+ agent.connect!(agent_socket_factory)
agent.negotiate!
agent
end
@@ -59,10 +59,10 @@ module Net; module SSH; module Authentication
# given by the attribute writers. If the agent on the other end of the
# socket reports that it is an SSH2-compatible agent, this will fail
# (it only supports the ssh-agent distributed by OpenSSH).
- def connect!
+ def connect!(agent_socket_factory = nil)
begin
debug { "connecting to ssh-agent" }
- @socket = agent_socket_factory.open(ENV['SSH_AUTH_SOCK'])
+ @socket = agent_socket_factory.nil? ? socket_class.open(ENV['SSH_AUTH_SOCK']) : agent_socket_factory.call
rescue
error { "could not connect to ssh-agent" }
raise AgentNotAvailable, $!.message
@@ -132,7 +132,7 @@ module Net; module SSH; module Authentication
private
# Returns the agent socket factory to use.
- def agent_socket_factory
+ def socket_class
if Net::SSH::Authentication::PLATFORM == :win32
Pageant::Socket
else
diff --git a/lib/net/ssh/authentication/key_manager.rb b/lib/net/ssh/authentication/key_manager.rb
index 0309a5e..b5ab8c4 100644
--- a/lib/net/ssh/authentication/key_manager.rb
+++ b/lib/net/ssh/authentication/key_manager.rb
@@ -176,7 +176,7 @@ module Net
# or if the agent is otherwise not available.
def agent
return unless use_agent?
- @agent ||= Agent.connect(logger)
+ @agent ||= Agent.connect(logger, options[:agent_socket_factory])
rescue AgentNotAvailable
@use_agent = false
nil
diff --git a/lib/net/ssh/service/forward.rb b/lib/net/ssh/service/forward.rb
index 541cbb1..2e65d73 100644
--- a/lib/net/ssh/service/forward.rb
+++ b/lib/net/ssh/service/forward.rb
@@ -357,7 +357,7 @@ module Net; module SSH; module Service
channel[:invisible] = true
begin
- agent = Authentication::Agent.connect(logger)
+ agent = Authentication::Agent.connect(logger, session.options[:agent_socket_factory])
if (agent.socket.is_a? ::IO)
prepare_client(agent.socket, channel, :agent)
else
diff --git a/test/authentication/test_agent.rb b/test/authentication/test_agent.rb
index ba7962e..33bf1aa 100644
--- a/test/authentication/test_agent.rb
+++ b/test/authentication/test_agent.rb
@@ -32,6 +32,11 @@ module Authentication
agent(false).connect!
end
+ def test_connect_should_use_agent_socket_factory_instead_of_factory
+ assert_equal agent.connect!, socket
+ assert_equal agent.connect!(agent_socket_factory), "/foo/bar.sock"
+ end
+
def test_connect_should_raise_error_if_connection_could_not_be_established
factory.expects(:open).raises(SocketError)
assert_raises(Net::SSH::Authentication::AgentNotAvailable) { agent(false).connect! }
@@ -213,12 +218,15 @@ module Authentication
def agent(auto=:connect)
@agent ||= begin
agent = Net::SSH::Authentication::Agent.new
- agent.stubs(:agent_socket_factory).returns(factory)
+ agent.stubs(:socket_class).returns(factory)
agent.connect! if auto == :connect
agent
end
end
+ def agent_socket_factory
+ @agent_socket_factory ||= ->{"/foo/bar.sock"}
+ end
end
end