Module: Elzar::SshKeyLocator

Defined in:
lib/elzar/ssh_key_locator.rb

Class Method Summary collapse

Class Method Details

.default_key_file_pathsObject



5
6
7
8
9
# File 'lib/elzar/ssh_key_locator.rb', line 5

def default_key_file_paths
  %w[id_dsa.pub id_ecdsa.pub id_rsa.pub].map do |base_filename|
    File.expand_path("~/.ssh/#{base_filename}")
  end
end

.find_agent_keysObject



24
25
26
27
# File 'lib/elzar/ssh_key_locator.rb', line 24

def find_agent_keys
  keys = split_keys(`ssh-add -L`)
  $?.success? ? keys : []
end

.find_keys(candidate_key_file_paths = default_key_file_paths) ⇒ Object



11
12
13
14
# File 'lib/elzar/ssh_key_locator.rb', line 11

def find_keys(candidate_key_file_paths = default_key_file_paths)
  local_keys = find_local_keys(candidate_key_file_paths)
  local_keys.empty? ? find_agent_keys : local_keys
end

.find_local_keys(candidate_key_file_paths = default_key_file_paths) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/elzar/ssh_key_locator.rb', line 16

def find_local_keys(candidate_key_file_paths = default_key_file_paths)
  first_existing_file = candidate_key_file_paths.find { |p| File.exist?(p) }
  return [] unless first_existing_file

  file_content = File.read(first_existing_file)
  split_keys(file_content)
end