Skip to content

Commit

Permalink
Added ability to specify agent socket
Browse files Browse the repository at this point in the history
  • Loading branch information
alongoldboim committed Apr 27, 2016
1 parent e0ca4a9 commit 3466837
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/net/ssh.rb
Expand Up @@ -71,7 +71,7 @@ module SSH
: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
]

# The standard means of starting a new SSH connection. When used with a
Expand Down Expand Up @@ -195,6 +195,8 @@ module SSH
# 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 => specify a specific socket that belongs to an agent
#
# If +user+ parameter is nil it defaults to USER from ssh_config, or
# local username
def self.start(host, user=nil, options={}, &block)
Expand Down
9 changes: 5 additions & 4 deletions lib/net/ssh/authentication/agent/socket.rb
Expand Up @@ -42,9 +42,9 @@ module Comment

# 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 = nil)
agent = new(logger)
agent.connect!
agent.connect!(agent_socket)
agent.negotiate!
agent
end
Expand All @@ -59,10 +59,11 @@ def initialize(logger=nil)
# 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 = nil)
begin
debug { "connecting to ssh-agent" }
@socket = agent_socket_factory.open(ENV['SSH_AUTH_SOCK'])
byebug
@socket = agent_socket_factory.open(agent_socket.nil? ? ENV['SSH_AUTH_SOCK'] : agent_socket)
rescue
error { "could not connect to ssh-agent" }
raise AgentNotAvailable, $!.message
Expand Down
2 changes: 1 addition & 1 deletion lib/net/ssh/authentication/key_manager.rb
Expand Up @@ -176,7 +176,7 @@ def use_agent=(use_agent)
# 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])
rescue AgentNotAvailable
@use_agent = false
nil
Expand Down
2 changes: 1 addition & 1 deletion lib/net/ssh/service/forward.rb
Expand Up @@ -357,7 +357,7 @@ def auth_agent_channel(session, channel, packet)
channel[:invisible] = true

begin
agent = Authentication::Agent.connect(logger)
agent = Authentication::Agent.connect(logger, self.session.options[:agent_socket])
if (agent.socket.is_a? ::IO)
prepare_client(agent.socket, channel, :agent)
else
Expand Down

0 comments on commit 3466837

Please sign in to comment.