Class: Aspera::Keychain::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/keychain/factory.rb

Overview

Factory to create vault/keychain instances

Constant Summary collapse

LIST =
%i[file system vault 1password].freeze

Class Method Summary collapse

Class Method Details

.create(info, name, folder, password) ⇒ Object

Create a vault instance

Parameters:

  • info (Hash) —

    vault options

  • name (String) —

    name of the vault

  • folder (String) —

    folder to store the vault (if needed)

  • password (String) —

    password to open the vault



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aspera/keychain/factory.rb', line 14

def create(info, name, folder, password)
  Aspera.assert_hash_all(info, Symbol, String) { 'vault info shall have only string values' }
  info = info.symbolize_keys
  vault_type = info.delete(:type)
  Aspera.assert_values(vault_type, LIST.map(&:to_s)) { 'vault.type' }
  case vault_type
  when 'file'
    info[:file] = info.delete(:name) || 'vault.bin' if info.key?(:name)
    info[:file] = File.join(folder, info[:file]) unless File.absolute_path?(info[:file])
    Aspera.assert(!password.nil?, 'please provide password')
    info[:password] = password
    # this module requires compilation, so it is optional
    require 'aspera/keychain/encrypted_hash'
    Keychain::EncryptedHash.new(**info)
  when 'system'
    case Environment.instance.os
    when Environment::OS_MACOS
      info[:name] ||= name
      Keychain::MacosSystem.new(**info)
    else
      raise Error, 'not implemented for this OS'
    end
  when 'vault'
    require 'aspera/keychain/hashicorp_vault'
    info[:token] ||= password
    Keychain::HashicorpVault.new(**info)
  when '1password'
    source = info.delete(:source) || 'api'
    Aspera.assert_values(source, %w[api cli]) { 'vault.source' }
    case source
    when 'api'
      require 'aspera/keychain/one_password_api'
      info[:token] ||= password
      Keychain::OnePasswordApi.new(**info)
    when 'cli'
      require 'aspera/keychain/one_password_cli'
      Keychain::OnePasswordCli.new(**info)
    end
  else Aspera.error_unexpected_value(vault_type)
  end
end