Class: SSH::Ident::AgentManager
- Inherits:
-
Object
- Object
- SSH::Ident::AgentManager
- Defined in:
- lib/ssh/ident/agent_manager.rb
Overview
ssh agent manager
Instance Method Summary collapse
- #agent_file ⇒ Object
- #agent_loaded_keys ⇒ Object
- #agent_running? ⇒ Boolean
- #find_identity ⇒ Object
- #find_keys ⇒ Object
-
#initialize(options = {}) ⇒ AgentManager
constructor
A new instance of AgentManager.
- #key_fingerprint(key_file) ⇒ Object
- #load_needed_keys ⇒ Object
- #start_agent ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ AgentManager
Returns a new instance of AgentManager.
10 11 12 13 14 15 16 17 |
# File 'lib/ssh/ident/agent_manager.rb', line 10 def initialize( = {}) @config = Config.new @identity = [:identity] @identity ||= find_identity @agent_file = agent_file start_agent unless agent_running? load_needed_keys end |
Instance Method Details
#agent_file ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/ssh/ident/agent_manager.rb', line 19 def agent_file agent_path = @config.get('DIR_AGENTS') unless File.directory?(agent_path) FileUtils.mkdir_p agent_path FileUtils.chmod 0700, agent_path end File.join(agent_path, "agent-#{@identity}-#{Socket.gethostname}") end |
#agent_loaded_keys ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/ssh/ident/agent_manager.rb', line 73 def agent_loaded_keys fingerprints = [] result = ShellCmd.new(command: 'ssh-add -l', agent_file: @agent_file, output: true).run return fingerprints if result.nil? result.split("\n").each do |line| fingerprints << line.split(' ')[1] end fingerprints end |
#agent_running? ⇒ Boolean
28 29 30 31 32 33 |
# File 'lib/ssh/ident/agent_manager.rb', line 28 def agent_running? return false unless File.exist?(@agent_file) result = ShellCmd.new(command: 'ssh-add -l', agent_file: @agent_file).run return false if result.nil? true end |
#find_identity ⇒ Object
35 36 37 38 39 |
# File 'lib/ssh/ident/agent_manager.rb', line 35 def find_identity env_indentity = ENV['SSH_IDENTITY'] return env_indentity if env_indentity && !env_indentity.empty? @config.get('DEFAULT_IDENTITY') end |
#find_keys ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ssh/ident/agent_manager.rb', line 41 def find_keys @identity ||= find_identity search_dir = [] search_dir << File.join(File.(@config.get('DIR_IDENTITIES')), @identity) if ENV['USER'] && @identity.eql?(ENV['USER']) search_dir << File.("#{ENV['HOME']}/.ssh") end keys = {} search_dir.each do |search| Dir.glob("#{search}/*") do |key_file| key = File.basename(key_file, '.*') keys[key] ||= {} if key_file.end_with?('.pem') ShellCmd.new(command: "ssh-keygen -yf #{key_file} > #{key_file}.pub", output: true) unless File.exist?("#{key_file}.pub") keys[key][:private] = key_file keys[key][:public] = "#{key_file}.pub" elsif key_file.end_with?('.pub') keys[key][:public] = key_file else keys[key][:private] = key_file end end end keys.select { |_k, v| v.key?(:private) && v.key?(:public) } end |
#key_fingerprint(key_file) ⇒ Object
67 68 69 70 71 |
# File 'lib/ssh/ident/agent_manager.rb', line 67 def key_fingerprint(key_file) output = ShellCmd.new(command: "ssh-keygen -l -f #{key_file}").run return nil unless output output.split(' ')[1] end |
#load_needed_keys ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ssh/ident/agent_manager.rb', line 83 def load_needed_keys loaded_keys = agent_loaded_keys keys_to_add = [] find_keys.each do |_key, prop| unless loaded_keys.include?(key_fingerprint(prop[:public])) keys_to_add << prop[:private] end end if keys_to_add.size > 0 ShellCmd.new(command: "ssh-add #{@config.get('SSH_ADD_OPTIONS')} #{keys_to_add.join(' ')}", agent_file: @agent_file).run end end |