Class: Net::SSH::Authentication::KeyManager

### Home Classes Methods

### In Files

- lib/net/ssh/authentication/key_manager.rb

### Parent

Object

### Methods

- ::new
- #add
- #add_key_data
- #agent
- #clear!
- #each_identity
- #finish
- #sign
- #use_agent=
- #use_agent?

### Included Modules

- Net::SSH::Loggable

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Net
- Net::SSH
- Net::SSH::Authentication
- Net::SSH::Authentication::Agent
- Net::SSH::Authentication::Agent::Comment
- Net::SSH::Authentication::AgentError
- Net::SSH::Authentication::AgentNotAvailable
- Net::SSH::Authentication::Constants
- Net::SSH::Authentication::DisallowedMethod
- Net::SSH::Authentication::KeyManager
- Net::SSH::Authentication::KeyManagerError
- Net::SSH::Authentication::Methods
- Net::SSH::Authentication::Methods::Abstract
- Net::SSH::Authentication::Methods::Hostbased
- Net::SSH::Authentication::Methods::KeyboardInteractive
- Net::SSH::Authentication::Methods::None
- Net::SSH::Authentication::Methods::Password
- Net::SSH::Authentication::Methods::Publickey
- Net::SSH::Authentication::Session
- Net::SSH::AuthenticationFailed
- Net::SSH::Buffer
- Net::SSH::ChannelOpenFailed
- Net::SSH::ChannelRequestFailed
- Net::SSH::Compat
- Net::SSH::Config
- Net::SSH::Connection
- Net::SSH::Connection::Channel
- Net::SSH::Connection::Constants
- Net::SSH::Connection::Session
- Net::SSH::Connection::Session::NilChannel
- Net::SSH::Connection::Term
- Net::SSH::ConnectionTimeout
- Net::SSH::Disconnect
- Net::SSH::Exception
- Net::SSH::HostKeyError
- Net::SSH::HostKeyMismatch
- Net::SSH::HostKeyUnknown
- Net::SSH::KeyFactory
- Net::SSH::KnownHosts
- Net::SSH::Loggable
- Net::SSH::Packet
- Net::SSH::PromptMethods
- Net::SSH::PromptMethods::Clear
- Net::SSH::PromptMethods::Highline
- Net::SSH::PromptMethods::Termios
- Net::SSH::Proxy
- Net::SSH::Proxy::Command
- Net::SSH::Proxy::ConnectError
- Net::SSH::Proxy::Error
- Net::SSH::Proxy::HTTP
- Net::SSH::Proxy::SOCKS4
- Net::SSH::Proxy::SOCKS5
- Net::SSH::Proxy::UnauthorizedError
- Net::SSH::Test
- Net::SSH::Test::Channel
- Net::SSH::Test::Extensions
- Net::SSH::Test::Extensions::BufferedIo
- Net::SSH::Test::Extensions::Channel
- Net::SSH::Test::Extensions::IO
- Net::SSH::Test::Extensions::IO::ClassMethods
- Net::SSH::Test::Extensions::PacketStream
- Net::SSH::Test::Kex
- Net::SSH::Test::LocalPacket
- Net::SSH::Test::Packet
- Net::SSH::Test::RemotePacket
- Net::SSH::Test::Script
- Net::SSH::Test::Socket
- Net::SSH::Transport
- Net::SSH::Transport::Algorithms
- Net::SSH::Transport::CTR
- Net::SSH::Transport::CipherFactory
- Net::SSH::Transport::Constants
- Net::SSH::Transport::HMAC
- Net::SSH::Transport::HMAC::Abstract
- Net::SSH::Transport::HMAC::MD5
- Net::SSH::Transport::HMAC::MD5_96
- Net::SSH::Transport::HMAC::None
- Net::SSH::Transport::HMAC::RIPEMD160
- Net::SSH::Transport::HMAC::SHA1
- Net::SSH::Transport::HMAC::SHA1_96
- Net::SSH::Transport::HMAC::SHA2_256
- Net::SSH::Transport::HMAC::SHA2_256_96
- Net::SSH::Transport::HMAC::SHA2_512
- Net::SSH::Transport::HMAC::SHA2_512_96
- Net::SSH::Transport::IdentityCipher
- Net::SSH::Transport::Kex
- Net::SSH::Transport::Kex::DiffieHellmanGroup14SHA1
- Net::SSH::Transport::Kex::DiffieHellmanGroup1SHA1
- Net::SSH::Transport::Kex::DiffieHellmanGroupExchangeSHA1
- Net::SSH::Transport::Kex::DiffieHellmanGroupExchangeSHA256
- Net::SSH::Transport::Kex::EcdhSHA2NistP256
- Net::SSH::Transport::Kex::EcdhSHA2NistP384
- Net::SSH::Transport::Kex::EcdhSHA2NistP521
- Net::SSH::Transport::KeyExpander
- Net::SSH::Transport::PacketStream
- Net::SSH::Transport::ServerVersion
- Net::SSH::Transport::Session
- Net::SSH::Transport::State
- Net::SSH::Verifiers
- Net::SSH::Verifiers::Lenient
- Net::SSH::Verifiers::Null
- Net::SSH::Verifiers::Secure
- Net::SSH::Verifiers::Strict
- Net::SSH::Version
- OpenSSL
- OpenSSL::BN
- OpenSSL::PKey
- OpenSSL::PKey::DH
- OpenSSL::PKey::DSA
- OpenSSL::PKey::EC
- OpenSSL::PKey::OpenSSL
- OpenSSL::PKey::OpenSSL::PKey
- OpenSSL::PKey::OpenSSL::PKey::OpenSSL
- OpenSSL::PKey::OpenSSL::PKey::OpenSSL::PKey
- OpenSSL::PKey::OpenSSL::PKey::OpenSSL::PKey::ECError
- OpenSSL::PKey::PKey
- OpenSSL::PKey::RSA
- String

No matching classes.

# Net::SSH::Authentication::KeyManager

This class encapsulates all operations done by clients on a user's private keys. In practice, the client should never need a reference to a private key; instead, they grab a list of "identities" (public keys) that are available from the KeyManager, and then use the KeyManager to do various private key operations using those identities.

The KeyManager also uses the Agent class to encapsulate the ssh-agent. Thus, from a client's perspective it is completely hidden whether an identity comes from the ssh-agent or from a file on disk.

### Attributes

key_data[R]

The list of user key data that will be examined

key_files[R]

The list of user key files that will be examined

known_identities[R]

The map of loaded identities

options[R]

The map of options that were passed to the key-manager

### Public Class Methods

new(logger, options={}) click to toggle source

Create a new KeyManager. By default, the manager will use the ssh-agent (if it is running).

    # File lib/net/ssh/authentication/key_manager.rb, line 41
    def initialize(logger, options={})
      self.logger = logger
      @key_files = []
      @key_data = []
      @use_agent = true
      @known_identities = {}
      @agent = nil
      @options = options
    end

### Public Instance Methods

add(key_file) click to toggle source

Add the given key_file to the list of key files that will be used.

    # File lib/net/ssh/authentication/key_manager.rb, line 63
    def add(key_file)
      key_files.push(File.expand_path(key_file)).uniq!
      self
    end

add_key_data(key_data_) click to toggle source

Add the given key_file to the list of keys that will be used.

    # File lib/net/ssh/authentication/key_manager.rb, line 69
    def add_key_data(key_data_)
      key_data.push(key_data_).uniq!
      self
    end

agent() click to toggle source

Returns an Agent instance to use for communicating with an SSH agent process. Returns nil if use of an SSH agent has been disabled, or if the agent is otherwise not available.

    # File lib/net/ssh/authentication/key_manager.rb, line 176
    def agent
      return unless use_agent?
      @agent ||= Agent.connect(logger)
    rescue AgentNotAvailable
      @use_agent = false
      nil
    end

clear!() click to toggle source

Clear all knowledge of any loaded user keys. This also clears the list of default identity files that are to be loaded, thus making it appropriate to use if a client wishes to NOT use the default identity files.

    # File lib/net/ssh/authentication/key_manager.rb, line 55
    def clear!
      key_files.clear
      key_data.clear
      known_identities.clear
      self
    end

each_identity() click to toggle source

Iterates over all available identities (public keys) known to this manager. As it finds one, it will then yield it to the caller. The origin of the identities may be from files on disk or from an ssh-agent. Note that identities from an ssh-agent are always listed first in the array, with other identities coming after.

If key manager was created with :keys_only option, any identity from ssh-agent will be ignored unless it present in key_files or key_data.

    # File lib/net/ssh/authentication/key_manager.rb, line 97
    def each_identity
      prepared_identities = prepare_identities_from_files + prepare_identities_from_data

      user_identities = load_identities(prepared_identities, false)

      if agent
        agent.identities.each do |key|
          corresponding_user_identity = user_identities.detect { |identity|
            identity[:public_key] && identity[:public_key].to_pem == key.to_pem
          }
          user_identities.delete(corresponding_user_identity) if corresponding_user_identity

          if !options[:keys_only] || corresponding_user_identity
            known_identities[key] = { :from => :agent }
            yield key
          end
        end
      end

      user_identities = load_identities(user_identities, true)

      user_identities.each do |identity|
        key = identity.delete(:public_key)
        known_identities[key] = identity
        yield key
      end

      self
    end

finish() click to toggle source

This is used as a hint to the KeyManager indicating that the agent connection is no longer needed. Any other open resources may be closed at this time.

Calling this does NOT indicate that the KeyManager will no longer be used. Identities may still be requested and operations done on loaded identities, in which case, the agent will be automatically reconnected. This method simply allows the client connection to be closed when it will not be used in the immediate future.

    # File lib/net/ssh/authentication/key_manager.rb, line 83
    def finish
      @agent.close if @agent
      @agent = nil
    end

sign(identity, data) click to toggle source

Sign the given data, using the corresponding private key of the given identity. If the identity was originally obtained from an ssh-agent, then the ssh-agent will be used to sign the data, otherwise the private key for the identity will be loaded from disk (if it hasn't been loaded already) and will then be used to sign the data.

Regardless of the identity's origin or who does the signing, this will always return the signature in an SSH2-specified "signature blob" format.

    # File lib/net/ssh/authentication/key_manager.rb, line 136
    def sign(identity, data)
      info = known_identities[identity] or raise KeyManagerError, "the given identity is unknown to the key manager"

      if info[:key].nil? && info[:from] == :file
        begin
          info[:key] = KeyFactory.load_private_key(info[:file], options[:passphrase], true)
        rescue Exception, OpenSSL::OpenSSLError => e
          raise KeyManagerError, "the given identity is known, but the private key could not be loaded: #{e.class} (#{e.message})"
        end
      end

      if info[:key]
        return Net::SSH::Buffer.from(:string, identity.ssh_type,
          :string, info[:key].ssh_do_sign(data.to_s)).to_s
      end

      if info[:from] == :agent
        raise KeyManagerError, "the agent is no longer available" unless agent
        return agent.sign(identity, data.to_s)
      end

      raise KeyManagerError, "[BUG] can't determine identity origin (#{info.inspect})"
    end

use_agent=(use_agent) click to toggle source

Toggles whether the ssh-agent will be used or not. If true, an attempt will be made to use the ssh-agent. If false, any existing connection to an agent is closed and the agent will not be used.

    # File lib/net/ssh/authentication/key_manager.rb, line 168
    def use_agent=(use_agent)
      finish if !use_agent
      @use_agent = use_agent
    end

use_agent?() click to toggle source

Identifies whether the ssh-agent will be used or not.

    # File lib/net/ssh/authentication/key_manager.rb, line 161
    def use_agent?
      @use_agent
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.