Class: SSH::Key::Signer
- Inherits:
-
Object
- Object
- SSH::Key::Signer
- Includes:
- Helper
- Defined in:
- lib/ssh/key/signer.rb
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#sshd_config_file ⇒ Object
Returns the value of attribute sshd_config_file.
-
#use_agent ⇒ Object
Returns the value of attribute use_agent.
Instance Method Summary collapse
-
#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.
-
#ensure_connected ⇒ Object
def initialize.
-
#initialize ⇒ Signer
constructor
A new instance of Signer.
-
#sign(string) ⇒ Object
Signs a string with all available ssh keys.
-
#signing_identities ⇒ Object
Get a list of all identities we can sign with.
Methods included from Helper
#add_key_file, #add_key_from_host, #add_public_key_data
Constructor Details
#initialize ⇒ Signer
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
#account ⇒ Object
Returns the value of attribute account.
12 13 14 |
# File 'lib/ssh/key/signer.rb', line 12 def account @account end |
#logger ⇒ Object
Returns the value of attribute logger.
14 15 16 |
# File 'lib/ssh/key/signer.rb', line 14 def logger @logger end |
#sshd_config_file ⇒ Object
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_agent ⇒ Object
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_connected ⇒ Object
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_identities ⇒ Object
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 |