~ubuntu-branches/ubuntu/vivid/ruby-net-ssh/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/net/ssh/authentication/agent/java_pageant.rb

  • Committer: Package Import Robot
  • Author(s): Paul van Tilburg
  • Date: 2012-05-24 20:55:35 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120524205535-af0bligbndbhj1se
Tags: 1:2.5.1-1
* New upstream release.
* debian/control:
  + Added myself to uploaders.
  + Bumped standards version to 3.9.3; no changes required.
  + Added a depend on ruby-test-unit; the one shipped with ruby1.8 is too
    old to run the tests.
  + Set the priority of the lib*-ruby transitional packages to extra.
* debian/copyright: converted to the Debian copyright format version 1.0.
* debian/patches:
  + Added patch disable-config-tests.patch to disable config tests for
    which the input config does not actually exist.
  + Added patch fix-test-inheritance.patch to fix some tests that use
    inheritance but forget to require the parent test file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'jruby_pageant'
 
2
 
 
3
module Net; module SSH; module Authentication
 
4
 
 
5
  # This class implements an agent for JRuby + Pageant.
 
6
  #
 
7
  # Written by Artūras Šlajus <arturas.slajus@gmail.com>
 
8
  class Agent
 
9
    include Loggable
 
10
    include JRubyPageant
 
11
 
 
12
    # A simple module for extending keys, to allow blobs and comments to be
 
13
    # specified for them.
 
14
    module Key
 
15
      # :blob is used by OpenSSL::PKey::RSA#to_blob
 
16
      attr_accessor :java_blob
 
17
      attr_accessor :comment
 
18
    end
 
19
 
 
20
    # Instantiates a new agent object, connects to a running SSH agent,
 
21
    # negotiates the agent protocol version, and returns the agent object.
 
22
    def self.connect(logger=nil)
 
23
      agent = new(logger)
 
24
      agent.connect!
 
25
      agent
 
26
    end
 
27
 
 
28
    # Creates a new Agent object, using the optional logger instance to
 
29
    # report status.
 
30
    def initialize(logger=nil)
 
31
      self.logger = logger
 
32
    end
 
33
 
 
34
    # Connect to the agent process using the socket factory and socket name
 
35
    # given by the attribute writers. If the agent on the other end of the
 
36
    # socket reports that it is an SSH2-compatible agent, this will fail
 
37
    # (it only supports the ssh-agent distributed by OpenSSH).
 
38
    def connect!
 
39
      debug { "connecting to Pageant ssh-agent (via java connector)" }
 
40
      @agent_proxy = JRubyPageant.create
 
41
      unless @agent_proxy.is_running
 
42
        raise AgentNotAvailable, "Pageant is not running!"
 
43
      end
 
44
      debug { "connection to Pageant ssh-agent (via java connector) succeeded" }
 
45
    rescue AgentProxyException => e
 
46
      error { "could not connect to Pageant ssh-agent (via java connector)" }
 
47
      raise AgentNotAvailable, e.message, e.backtrace
 
48
    end
 
49
 
 
50
    # Return an array of all identities (public keys) known to the agent.
 
51
    # Each key returned is augmented with a +comment+ property which is set
 
52
    # to the comment returned by the agent for that key.
 
53
    def identities
 
54
      debug { "getting identities from Pageant" }
 
55
      @agent_proxy.get_identities.map do |identity|
 
56
        blob = identity.get_blob
 
57
        key = Buffer.new(String.from_java_bytes(blob)).read_key
 
58
        key.extend(Key)
 
59
        key.java_blob = blob
 
60
        key.comment = String.from_java_bytes(identity.get_comment)
 
61
        key
 
62
      end
 
63
    rescue AgentProxyException => e
 
64
      raise AgentError, "Cannot get identities: #{e.message}", e.backtrace
 
65
    end
 
66
 
 
67
    # Simulate agent close. This agent reference is no longer able to
 
68
    # query the agent.
 
69
    def close
 
70
      @agent_proxy = nil
 
71
    end
 
72
 
 
73
    # Using the agent and the given public key, sign the given data. The
 
74
    # signature is returned in SSH2 format.
 
75
    def sign(key, data)
 
76
      signed = @agent_proxy.sign(key.java_blob, data.to_java_bytes)
 
77
      String.from_java_bytes(signed)
 
78
    rescue AgentProxyException => e
 
79
      raise AgentError,
 
80
        "agent could not sign data with requested identity: #{e.message}",
 
81
        e.backtrace
 
82
    end
 
83
  end
 
84
 
 
85
end; end; end