Class: SSH::Key::Signer

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/ssh/key/signer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#add_key_file, #add_key_from_host, #add_public_key_data

Constructor Details

#initializeSigner

Returns a new instance of Signer.



17
18
19
20
21
22
23
# File 'lib/ssh/key/signer.rb', line 17

def initialize
  @agent = Net::SSH::Authentication::Agent.new
  @use_agent = true
  @logger = Logger.new(STDERR)
  @logger.level = Logger::WARN
  @keys = []
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



12
13
14
# File 'lib/ssh/key/signer.rb', line 12

def 
  @account
end

#loggerObject

Returns the value of attribute logger.



14
15
16
# File 'lib/ssh/key/signer.rb', line 14

def logger
  @logger
end

#sshd_config_fileObject

Returns the value of attribute sshd_config_file.



13
14
15
# File 'lib/ssh/key/signer.rb', line 13

def sshd_config_file
  @sshd_config_file
end

#use_agentObject

Returns the value of attribute use_agent.



15
16
17
# File 'lib/ssh/key/signer.rb', line 15

def use_agent
  @use_agent
end

Instance Method Details

#add_private_key_file(path, passphrase = nil) ⇒ Object

Add a private key to this Signer from a file (like “.ssh/id_rsa”)

  • path - the string path to the key

  • passphrase - the passphrase for this key, omit if no passphrase.



89
90
91
# File 'lib/ssh/key/signer.rb', line 89

def add_private_key_file(path, passphrase=nil)
  @keys << Net::SSH::KeyFactory.load_private_key(path, passphrase)
end

#ensure_connectedObject

def initialize



25
26
27
28
29
30
31
# File 'lib/ssh/key/signer.rb', line 25

def ensure_connected
  begin
    @agent.connect! if !@agent.socket
  rescue Net::SSH::Authentication::AgentNotAvailable => e
    @use_agent = false
  end
end

#sign(string) ⇒ Object

Signs a string with all available ssh keys

  • string - the value to sign

Returns an array of SSH::Key::Signature objects

‘identity’ on each object is an openssl key instance of one of these typs:

  • OpenSSL::PKey::RSA

  • OpenSSL::PKey::DSA

  • OpenSSL::PKey::DH

Net::SSH monkeypatches the above classes to add additional methods, so just be aware.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ssh/key/signer.rb', line 46

def sign(string)
  identities = signing_identities 
  signatures = []
  identities.each do |identity|
    if identity.private?
      # FYI: OpenSSL::PKey::RSA#ssh_type and #ssh_do_sign are monkeypatched
      # by Net::SSH
      signature = SSH::Key::Signature.new
      signature.type = identity.ssh_type
      signature.signature = identity.ssh_do_sign(string)
    else
      # Only public signing identities come from our agent.
      signature = SSH::Key::Signature.from_string(@agent.sign(identity, string))
    end
    signature.identity = identity
    signatures << signature
  end
  return signatures
end

#signing_identitiesObject

Get a list of all identities we can sign with. This will pull from your ssh-agent if enabled.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ssh/key/signer.rb', line 68

def signing_identities
  identities = []
  if @use_agent
    ensure_connected
    begin
      @agent.identities.each { |id| identities << id }
    rescue => e
      @logger.warn("Error talking to agent while asking for message signing. Disabling agent (Error: #{e})")
      @use_agent = false
    end
  end

  if @keys
    @keys.each { |id| identities << id }
  end
  return identities
end