Class Net::SSH::Authentication::KeyManager
In: lib/net/ssh/authentication/key_manager.rb
lib/net/ssh/authentication/key_manager.rb
Parent: Object

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.

Methods

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

Included Modules

Loggable Loggable

Attributes

key_data  [R]  The list of user key data that will be examined
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
key_files  [R]  The list of user key files that will be examined
known_identities  [R]  The map of loaded identities
known_identities  [R]  The map of loaded identities
options  [R]  The map of options that were passed to the key-manager
options  [R]  The map of options that were passed to the key-manager

Public Class methods

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

[Source]

    # File lib/net/ssh/authentication/key_manager.rb, line 41
41:         def initialize(logger, options={})
42:           self.logger = logger
43:           @key_files = []
44:           @key_data = []
45:           @use_agent = true
46:           @known_identities = {}
47:           @agent = nil
48:           @options = options
49:         end

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

[Source]

    # File lib/net/ssh/authentication/key_manager.rb, line 41
41:         def initialize(logger, options={})
42:           self.logger = logger
43:           @key_files = []
44:           @key_data = []
45:           @use_agent = true
46:           @known_identities = {}
47:           @agent = nil
48:           @options = options
49:         end

Public Instance methods

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

[Source]

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

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

[Source]

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

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

[Source]

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

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

[Source]

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

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.

[Source]

     # File lib/net/ssh/authentication/key_manager.rb, line 182
182:         def agent
183:           return unless use_agent?
184:           @agent ||= Agent.connect(logger)
185:         rescue AgentNotAvailable
186:           @use_agent = false
187:           nil
188:         end

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.

[Source]

     # File lib/net/ssh/authentication/key_manager.rb, line 182
182:         def agent
183:           return unless use_agent?
184:           @agent ||= Agent.connect(logger)
185:         rescue AgentNotAvailable
186:           @use_agent = false
187:           nil
188:         end

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.

[Source]

    # File lib/net/ssh/authentication/key_manager.rb, line 55
55:         def clear!
56:           key_files.clear
57:           key_data.clear
58:           known_identities.clear
59:           self
60:         end

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.

[Source]

    # File lib/net/ssh/authentication/key_manager.rb, line 55
55:         def clear!
56:           key_files.clear
57:           key_data.clear
58:           known_identities.clear
59:           self
60:         end

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.

[Source]

     # File lib/net/ssh/authentication/key_manager.rb, line 93
 93:         def each_identity
 94:           if agent
 95:             agent.identities.each do |key|
 96:               known_identities[key] = { :from => :agent }
 97:               yield key
 98:             end
 99:           end
100:           
101:           key_files.each do |file|
102:             public_key_file = file + ".pub"
103:             if File.readable?(public_key_file)
104:               begin
105:                 key = KeyFactory.load_public_key(public_key_file)
106:                 known_identities[key] = { :from => :file, :file => file }
107:                 yield key
108:               rescue Exception => e
109:                 error { "could not load public key file `#{public_key_file}': #{e.class} (#{e.message})" }
110:               end
111:             elsif File.readable?(file)
112:               begin
113:                 private_key = KeyFactory.load_private_key(file, options[:passphrase])
114:                 key = private_key.send(:public_key)
115:                 known_identities[key] = { :from => :file, :file => file, :key => private_key }
116:                 yield key
117:               rescue Exception => e
118:                 error { "could not load private key file `#{file}': #{e.class} (#{e.message})" }
119:               end
120:             end
121:           end
122: 
123:           key_data.each do |data|
124:             private_key = KeyFactory.load_data_private_key(data)
125:             key = private_key.send(:public_key)
126:             known_identities[key] = { :from => :key_data, :data => data, :key => private_key }
127:             yield key
128:           end
129: 
130:           self
131:         end

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.

[Source]

     # File lib/net/ssh/authentication/key_manager.rb, line 93
 93:         def each_identity
 94:           if agent
 95:             agent.identities.each do |key|
 96:               known_identities[key] = { :from => :agent }
 97:               yield key
 98:             end
 99:           end
100:           
101:           key_files.each do |file|
102:             public_key_file = file + ".pub"
103:             if File.readable?(public_key_file)
104:               begin
105:                 key = KeyFactory.load_public_key(public_key_file)
106:                 known_identities[key] = { :from => :file, :file => file }
107:                 yield key
108:               rescue Exception => e
109:                 error { "could not load public key file `#{public_key_file}': #{e.class} (#{e.message})" }
110:               end
111:             elsif File.readable?(file)
112:               begin
113:                 private_key = KeyFactory.load_private_key(file, options[:passphrase])
114:                 key = private_key.send(:public_key)
115:                 known_identities[key] = { :from => :file, :file => file, :key => private_key }
116:                 yield key
117:               rescue Exception => e
118:                 error { "could not load private key file `#{file}': #{e.class} (#{e.message})" }
119:               end
120:             end
121:           end
122: 
123:           key_data.each do |data|
124:             private_key = KeyFactory.load_data_private_key(data)
125:             key = private_key.send(:public_key)
126:             known_identities[key] = { :from => :key_data, :data => data, :key => private_key }
127:             yield key
128:           end
129: 
130:           self
131:         end

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.

[Source]

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

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.

[Source]

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

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.

[Source]

     # File lib/net/ssh/authentication/key_manager.rb, line 142
142:         def sign(identity, data)
143:           info = known_identities[identity] or raise KeyManagerError, "the given identity is unknown to the key manager"
144: 
145:           if info[:key].nil? && info[:from] == :file
146:             begin
147:               info[:key] = KeyFactory.load_private_key(info[:file], options[:passphrase])
148:             rescue Exception => e 
149:               raise KeyManagerError, "the given identity is known, but the private key could not be loaded: #{e.class} (#{e.message})"
150:             end
151:           end
152: 
153:           if info[:key]
154:             return Net::SSH::Buffer.from(:string, identity.ssh_type,
155:               :string, info[:key].ssh_do_sign(data.to_s)).to_s
156:           end
157: 
158:           if info[:from] == :agent
159:             raise KeyManagerError, "the agent is no longer available" unless agent
160:             return agent.sign(identity, data.to_s)
161:           end
162: 
163:           raise KeyManagerError, "[BUG] can't determine identity origin (#{info.inspect})"
164:         end

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.

[Source]

     # File lib/net/ssh/authentication/key_manager.rb, line 142
142:         def sign(identity, data)
143:           info = known_identities[identity] or raise KeyManagerError, "the given identity is unknown to the key manager"
144: 
145:           if info[:key].nil? && info[:from] == :file
146:             begin
147:               info[:key] = KeyFactory.load_private_key(info[:file], options[:passphrase])
148:             rescue Exception => e 
149:               raise KeyManagerError, "the given identity is known, but the private key could not be loaded: #{e.class} (#{e.message})"
150:             end
151:           end
152: 
153:           if info[:key]
154:             return Net::SSH::Buffer.from(:string, identity.ssh_type,
155:               :string, info[:key].ssh_do_sign(data.to_s)).to_s
156:           end
157: 
158:           if info[:from] == :agent
159:             raise KeyManagerError, "the agent is no longer available" unless agent
160:             return agent.sign(identity, data.to_s)
161:           end
162: 
163:           raise KeyManagerError, "[BUG] can't determine identity origin (#{info.inspect})"
164:         end

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.

[Source]

     # File lib/net/ssh/authentication/key_manager.rb, line 174
174:         def use_agent=(use_agent)
175:           finish if !use_agent
176:           @use_agent = use_agent
177:         end

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.

[Source]

     # File lib/net/ssh/authentication/key_manager.rb, line 174
174:         def use_agent=(use_agent)
175:           finish if !use_agent
176:           @use_agent = use_agent
177:         end

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

[Source]

     # File lib/net/ssh/authentication/key_manager.rb, line 167
167:         def use_agent?
168:           @use_agent
169:         end

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

[Source]

     # File lib/net/ssh/authentication/key_manager.rb, line 167
167:         def use_agent?
168:           @use_agent
169:         end

[Validate]