Module: Duse::KeyHelper

Included in:
CLI::AccountUpdate, CLI::Login, CLI::Register, CLI::SecretAdd, CLI::SecretGet, CLI::SecretUpdate
Defined in:
lib/duse/cli/key_helper.rb

Instance Method Summary collapse

Instance Method Details

#choose_key(options = { allow_generate: true }) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/duse/cli/key_helper.rb', line 3

def choose_key(options = { allow_generate: true })
  key = nil
  generate_option = 'Generate a new one'
  choose_myself_option = 'Let me choose it myself'
  choices = possible_ssh_keys
  choices << generate_option if options[:allow_generate]
  choices << choose_myself_option
  terminal.choose do |ssh_keys|
    ssh_keys.prompt = 'Which private ssh-key do you want to use?'
    ssh_keys.choices *choices do |choice|
      key = generate_key if choice == generate_option
      key = choose_private_key_file if choice == choose_myself_option
      key ||= OpenSSL::PKey::RSA.new File.read choice
    end
  end
  key
end

#choose_private_key_fileObject



55
56
57
58
59
60
61
62
# File 'lib/duse/cli/key_helper.rb', line 55

def choose_private_key_file
  private_key_file = nil
  loop do
    private_key_file = terminal.ask('Private key file: ') { |q| q.default = File.join ssh_dir, 'id_rsa' }
    break if valid_ssh_private_key? private_key_file
  end
  OpenSSL::PKey::RSA.new File.read private_key_file
end

#ensure_matching_keys_for(user) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/duse/cli/key_helper.rb', line 21

def ensure_matching_keys_for(user)
  key_pair = {
    public: user.public_key,
    private: private_key_for(user)
  }
  loop do
    break if matching_keys? key_pair
    warn 'Your private key does not match the public key, please select a new one.'
    key_pair[:private] = choose_key(allow_generate: false)
  end
  Duse::CLIConfig.new.save_private_key_for user, key_pair[:private].to_pem
end

#generate_keyObject



71
72
73
# File 'lib/duse/cli/key_helper.rb', line 71

def generate_key
  OpenSSL::PKey::RSA.generate 4096
end

#home_dirObject



79
80
81
# File 'lib/duse/cli/key_helper.rb', line 79

def home_dir
  File.expand_path '~'
end

#matching_keys?(key_pair) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/duse/cli/key_helper.rb', line 44

def matching_keys?(key_pair)
  public_key = key_pair[:public]
  private_key = key_pair[:private]
  public_key.params['n'] == private_key.params['n']
end

#possible_ssh_keysObject



50
51
52
53
# File 'lib/duse/cli/key_helper.rb', line 50

def possible_ssh_keys
  ssh_keys = Dir.glob File.join(ssh_dir, '*')
  ssh_keys.keep_if &method(:valid_ssh_private_key?)
end

#private_key_for(user) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/duse/cli/key_helper.rb', line 34

def private_key_for(user)
  Duse::CLIConfig.new.private_key_for(user)
rescue PrivateKeyMissing
  warn 'No private key found, please select one.'
  choose_key(allow_generate: false)
rescue OpenSSL::PKey::RSAError
  warn 'The private key file does not contain a valid private key, please select a new one.'
  choose_key(allow_generate: false)
end

#ssh_dirObject



75
76
77
# File 'lib/duse/cli/key_helper.rb', line 75

def ssh_dir
  File.join home_dir, '.ssh'
end

#valid_ssh_private_key?(private_key_file) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
# File 'lib/duse/cli/key_helper.rb', line 64

def valid_ssh_private_key?(private_key_file)
  rsa_key = OpenSSL::PKey::RSA.new File.read private_key_file
  rsa_key.private?
rescue
  false
end