Module: Aspera::Cli::VaultManager

Included in:
Plugins::Config
Defined in:
lib/aspera/cli/vault_manager.rb

Overview

Mixin providing vault/keychain functionality to Plugin::Config. Depends on options and context.main_folder being available in the including class.

Instance Method Summary collapse

Instance Method Details

#action_vault_create(info:) ⇒ Object



19
20
21
22
# File 'lib/aspera/cli/vault_manager.rb', line 19

def action_vault_create(info:, **)
  vault_required.set(info.symbolize_keys)
  Result::Status.new('Secret added')
end

#action_vault_delete(label:, id: nil) ⇒ Object



32
33
34
35
36
37
# File 'lib/aspera/cli/vault_manager.rb', line 32

def action_vault_delete(label:, id: nil, **)
  v = vault_required
  kwargs = id && v.method(:delete).parameters.any? { |_t, n| n == :id } ? {id: id} : {}
  v.delete(label: label, **kwargs)
  Result::Status.new("Secret deleted: #{label}")
end

#action_vault_import(secrets:) ⇒ Object

Import secrets from a JSON array; skips entries missing :label



25
26
27
28
29
30
# File 'lib/aspera/cli/vault_manager.rb', line 25

def action_vault_import(secrets:, **)
  bulk_result(secrets, command: :import, id_result: 'label') do |entry|
    vault_required.set(entry.symbolize_keys)
    {'label' => entry['label'] || entry[:label]}
  end
end

#action_vault_password(new_password:) ⇒ Object



39
40
41
42
43
# File 'lib/aspera/cli/vault_manager.rb', line 39

def action_vault_password(new_password:, **)
  Aspera.assert(vault_required.respond_to?(:change_password), 'Vault does not support password change')
  vault_required.change_password(new_password)
  Result::Status.new('Vault password updated')
end

#action_vault_show(label:, id: nil) ⇒ Object



13
14
15
16
17
# File 'lib/aspera/cli/vault_manager.rb', line 13

def action_vault_show(label:, id: nil, **)
  v = vault_required
  kwargs = id && v.method(:get).parameters.any? { |_t, n| n == :id } ? {id: id} : {}
  Result::SingleObject.new(v.get(label: label, **kwargs))
end

#vaultKeychain::Base?

Returns vault instance, lazily created from options, or nil if not configured.

Returns:

  • (Keychain::Base, nil)

    vault instance, lazily created from options, or nil if not configured



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aspera/cli/vault_manager.rb', line 62

def vault
  return @vault_instance if @vault_instance_initialized
  @vault_instance_initialized = true
  info = options.get_option(:vault, mandatory: false)
  return @vault_instance = nil if info.nil? || (info.is_a?(Hash) && info.empty?)
  info = info.symbolize_keys
  info[:type] ||= 'file'
  require 'aspera/keychain/factory'
  @vault_instance = Keychain::Factory.create(
    info,
    Info::CMD_NAME,
    context.main_folder,
    options.get_option(:vault_password)
  )
end

#vault_requiredKeychain::Base

Returns vault instance, raises if not configured.

Returns:



46
47
48
49
# File 'lib/aspera/cli/vault_manager.rb', line 46

def vault_required
  Aspera.assert(!vault.nil?, type: Cli::BadArgument) { 'Missing mandatory option: vault' }
  vault
end

#vault_value(name) ⇒ String

Returns value from vault matching ..

Returns:

  • (String)

    value from vault matching .



52
53
54
55
56
57
58
59
# File 'lib/aspera/cli/vault_manager.rb', line 52

def vault_value(name)
  m = name.split('.')
  Aspera.assert(m.length.eql?(2), type: BadArgument) { 'vault name shall match <name>.<param>' }
  info = vault_required.get(label: m[0])
  value = info[m[1].to_sym]
  raise "no such entry value: #{m[1]}" if value.nil?
  return value
end