Class: CemAcpt::Utils::SSH::Keygen

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/cem_acpt/utils/ssh.rb

Overview

Class for generating SSH keys

Constant Summary collapse

DEFAULT_TYPE =
'ed25519'
DEFAULT_PASSPHRASE =
''
DEFAULT_ROUNDS =
100
DEFAULT_BITS =
4096

Constants included from Logging

Logging::LEVEL_MAP

Instance Method Summary collapse

Methods included from Logging

current_log_config, #current_log_config, current_log_format, #current_log_format, #current_log_level, current_log_level, included, #logger, logger, new_log_config, #new_log_config, new_log_formatter, #new_log_formatter, #new_log_level, new_log_level, #new_logger, new_logger, verbose?, #verbose?

Constructor Details

#initialize(key_dir: ::File.join(ENV['HOME'], '.ssh')) ⇒ Keygen

Returns a new instance of Keygen.



19
20
21
22
# File 'lib/cem_acpt/utils/ssh.rb', line 19

def initialize(key_dir: ::File.join(ENV['HOME'], '.ssh'))
  @key_dir = key_dir
  @bin_path = find_bin_path
end

Instance Method Details

#create(key_name, **options) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/cem_acpt/utils/ssh.rb', line 31

def create(key_name, **options)
  delete(key_name) # Delete existing keys with same name
  cmd = new_keygen_cmd(key_name, **options)
  logger.debug('CemAcpt::Utils::SSH::Keygen') { "Creating SSH key with command: #{cmd}" }
  CemAcpt::Utils::Shell.run_cmd(cmd, output: logger)
  key_paths(key_name)
end

#delete(key_name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cem_acpt/utils/ssh.rb', line 39

def delete(key_name)
  priv_key = key_path(key_name)
  pub_key = key_path(key_name, public_key: true)
  if ::File.file?(priv_key)
    logger.debug('CemAcpt::Utils::SSH::Keygen') { "Deleting private key: #{priv_key}" }
    ::File.delete(priv_key)
  end
  if ::File.file?(pub_key)
    logger.debug("Deleting public key: #{pub_key}")
    ::File.delete(pub_key)
  end
end

#exist?(key_name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/cem_acpt/utils/ssh.rb', line 24

def exist?(key_name)
  key_paths(key_name, chmod: false)
  true
rescue StandardError
  false
end