Class: Net::SSH::Authentication::Agent
- Inherits:
-
Object
- Object
- Net::SSH::Authentication::Agent
- Includes:
- JRubyPageant, Loggable
- Defined in:
- lib/net/ssh/authentication/agent/socket.rb,
lib/net/ssh/authentication/agent/java_pageant.rb
Overview
This class implements an agent for JRuby + Pageant.
Written by Artūras Šlajus <[email protected]>
Defined Under Namespace
Constant Summary collapse
- SSH2_AGENT_REQUEST_VERSION =
1
- SSH2_AGENT_REQUEST_IDENTITIES =
11
- SSH2_AGENT_IDENTITIES_ANSWER =
12
- SSH2_AGENT_SIGN_REQUEST =
13
- SSH2_AGENT_SIGN_RESPONSE =
14
- SSH2_AGENT_FAILURE =
30
- SSH2_AGENT_VERSION_RESPONSE =
103
- SSH_COM_AGENT2_FAILURE =
102
- SSH_AGENT_REQUEST_RSA_IDENTITIES =
1
- SSH_AGENT_RSA_IDENTITIES_ANSWER1 =
2
- SSH_AGENT_RSA_IDENTITIES_ANSWER2 =
5
- SSH_AGENT_FAILURE =
5
Instance Attribute Summary collapse
-
#socket ⇒ Object
readonly
The underlying socket being used to communicate with the SSH agent.
Attributes included from Loggable
Class Method Summary collapse
-
.connect(logger = nil) ⇒ Object
Instantiates a new agent object, connects to a running SSH agent, negotiates the agent protocol version, and returns the agent object.
Instance Method Summary collapse
-
#close ⇒ Object
Simulate agent close.
-
#connect! ⇒ Object
Connect to the agent process using the socket factory and socket name given by the attribute writers.
-
#identities ⇒ Object
Return an array of all identities (public keys) known to the agent.
-
#initialize(logger = nil) ⇒ Agent
constructor
Creates a new Agent object, using the optional logger instance to report status.
-
#negotiate! ⇒ Object
Attempts to negotiate the SSH agent protocol version.
-
#sign(key, data) ⇒ Object
Using the agent and the given public key, sign the given data.
Methods included from Loggable
#debug, #error, #fatal, #info, #lwarn
Constructor Details
#initialize(logger = nil) ⇒ Agent
Creates a new Agent object, using the optional logger instance to report status.
54 55 56 |
# File 'lib/net/ssh/authentication/agent/socket.rb', line 54 def initialize(logger=nil) self.logger = logger end |
Instance Attribute Details
#socket ⇒ Object (readonly)
The underlying socket being used to communicate with the SSH agent.
41 42 43 |
# File 'lib/net/ssh/authentication/agent/socket.rb', line 41 def socket @socket end |
Class Method Details
.connect(logger = nil) ⇒ Object
Instantiates a new agent object, connects to a running SSH agent, negotiates the agent protocol version, and returns the agent object.
45 46 47 48 49 50 |
# File 'lib/net/ssh/authentication/agent/socket.rb', line 45 def self.connect(logger=nil) agent = new(logger) agent.connect! agent.negotiate! agent end |
Instance Method Details
#close ⇒ Object
Simulate agent close. This agent reference is no longer able to query the agent.
106 107 108 |
# File 'lib/net/ssh/authentication/agent/socket.rb', line 106 def close @socket.close end |
#connect! ⇒ Object
Connect to the agent process using the socket factory and socket name 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).
62 63 64 65 66 67 68 69 70 |
# File 'lib/net/ssh/authentication/agent/socket.rb', line 62 def connect! begin debug { "connecting to ssh-agent" } @socket = agent_socket_factory.open(ENV['SSH_AUTH_SOCK']) rescue error { "could not connect to ssh-agent" } raise AgentNotAvailable, $!. end end |
#identities ⇒ Object
Return an array of all identities (public keys) known to the agent. Each key returned is augmented with a comment
property which is set to the comment returned by the agent for that key.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/net/ssh/authentication/agent/socket.rb', line 88 def identities type, body = send_and_wait(SSH2_AGENT_REQUEST_IDENTITIES) raise AgentError, "could not get identity count" if agent_failed(type) raise AgentError, "bad authentication reply: #{type}" if type != SSH2_AGENT_IDENTITIES_ANSWER identities = [] body.read_long.times do key = Buffer.new(body.read_string).read_key key.extend(Comment) key.comment = body.read_string identities.push key end return identities end |
#negotiate! ⇒ Object
Attempts to negotiate the SSH agent protocol version. Raises an error if the version could not be negotiated successfully.
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/net/ssh/authentication/agent/socket.rb', line 74 def negotiate! # determine what type of agent we're communicating with type, body = send_and_wait(SSH2_AGENT_REQUEST_VERSION, :string, Transport::ServerVersion::PROTO_VERSION) if type == SSH2_AGENT_VERSION_RESPONSE raise NotImplementedError, "SSH2 agents are not yet supported" elsif type != SSH_AGENT_RSA_IDENTITIES_ANSWER1 && type != SSH_AGENT_RSA_IDENTITIES_ANSWER2 raise AgentError, "unknown response from agent: #{type}, #{body.to_s.inspect}" end end |
#sign(key, data) ⇒ Object
Using the agent and the given public key, sign the given data. The signature is returned in SSH2 format.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/net/ssh/authentication/agent/socket.rb', line 112 def sign(key, data) type, reply = send_and_wait(SSH2_AGENT_SIGN_REQUEST, :string, Buffer.from(:key, key), :string, data, :long, 0) if agent_failed(type) raise AgentError, "agent could not sign data with requested identity" elsif type != SSH2_AGENT_SIGN_RESPONSE raise AgentError, "bad authentication response #{type}" end return reply.read_string end |