Class: Aspera::Cli::VaultManager

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/cli/vault_manager.rb

Overview

Secrets of the vault, shared in Context#vault: read by @vault:, managed by config vault, written by presets. Option vault: named stores (stores), the name of the default store (default) and the cache of references (cache_ttl), see Secret::Resolver.

  • <label>.<field>: secret of the default store, unlocked with option vault_password, managed by label
  • //<store>/<path>[?<options>][#<field>]: reference to a secret of a store, read only

Values @file:<path> in these options (e.g. token) are read by Secret::Bootstrap, not by ExtendedValue: they are checked here as extended value @file:, so that option security applies (e.g. in MCP server mode).

Constant Summary collapse

REFERENCE_PREFIX =

Beginning of a reference after @vault:, secret:// without secret:

'//'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, main_folder) ⇒ VaultManager

Returns a new instance of VaultManager.

Parameters:

  • options (Parser) —

    Options vault and vault_password

  • main_folder (String) —

    Folder of relative paths of vault files



55
56
57
58
59
60
61
62
63
# File 'lib/aspera/cli/vault_manager.rb', line 55

def initialize(options, main_folder)
  @options = options
  @main_folder = main_folder
  @resolver = nil
  # Name of the default store, `nil` if none
  @default = nil
  # Name and type of stores, without parameters (may contain secrets)
  @stores = []
end

Class Method Details

.convert_option(value) ⇒ Hash

Value of option vault when set (convert of option): the parameters of one store (deprecated) are a store named after its type, which is the default store.

Parameters:

  • value (Hash) —

    Value of option vault

Returns:

  • (Hash) —

    Value with stores



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/aspera/cli/vault_manager.rb', line 40

def convert_option(value)
  value = value.transform_keys(&:to_s)
  params = value.except(*OPTION_KEYS)
  return value if params.empty?
  params['type'] ||= 'file'
  name = params['type'].to_s
  Log.log.warn do
    "option vault: parameters of one store are #{Deprecation.new(last: '4.28.0', message: "set a store in #{STORES_KEY}, and its name in #{DEFAULT_KEY}: {\"#{DEFAULT_KEY}\":\"#{name}\",\"#{STORES_KEY}\":{\"#{name}\":{...}}}")}"
  end
  value.slice(*OPTION_KEYS).merge(DEFAULT_KEY => name, STORES_KEY => (value[STORES_KEY] || {}).merge(name => params))
end

Instance Method Details

#fields(label) ⇒ Array<String>

Field names of a secret of the default store, without values

Parameters:

  • label (String) —

    Label of secret

Returns:

  • (Array<String>) —

    Names of fields that a reference can read



106
107
108
109
110
# File 'lib/aspera/cli/vault_manager.rb', line 106

def fields(label)
  store(mandatory: true)
  reference = Secret.reference_text(store: @default, path: label)
  resolver(mandatory: true).fields(reference)[:fields]
end

#metadata(name) ⇒ Hash

Checks that a reference can be read, without its value

Parameters:

  • name (String) —

    //<store>/<path>[?<options>][#<field>]

Returns:

  • (Hash) —

    store, version, updated_time



98
99
100
101
# File 'lib/aspera/cli/vault_manager.rb', line 98

def (name)
  Aspera.assert(name.start_with?(REFERENCE_PREFIX), type: BadArgument) { "expecting a reference: @vault:#{REFERENCE_PREFIX}<store>/<path>[?<options>][#<field>]" }
  resolver(mandatory: true).(reference_text(name))
end

#store(mandatory: false) ⇒ Secret::Store::Base?

Default store, opened when first used.

Parameters:

  • mandatory (Boolean) (defaults to: false) —

    Raise if not configured

Returns:



75
76
77
78
79
# File 'lib/aspera/cli/vault_manager.rb', line 75

def store(mandatory: false)
  return if resolver(mandatory: mandatory).nil?
  Aspera.assert(!mandatory || !@default.nil?, type: BadArgument) { "option vault: no default store: set #{DEFAULT_KEY}" }
  @resolver.store(@default) unless @default.nil?
end

#stores ⇒ Array<Hash>

Stores of option vault, without their parameters

Returns:

  • (Array<Hash>) —

    name, type, default of each store, empty if not configured



67
68
69
70
# File 'lib/aspera/cli/vault_manager.rb', line 67

def stores
  resolver
  @stores
end

#value(name) ⇒ String

Value of @vault:, declared as read from the configuration: a secret of the vault can be sent only to a URL of the configuration, see Security#check_credential_host

Parameters:

  • name (String) —

    <label>.<field> or //<store>/<path>[?<options>][#<field>]

Returns:

  • (String) —

    Value of secret



84
85
86
87
88
89
90
91
92
93
# File 'lib/aspera/cli/vault_manager.rb', line 84

def value(name)
  return Security.instance.add_configured(resolver(mandatory: true).resolve(reference_text(name))) if name.start_with?(REFERENCE_PREFIX)
  # Label may contain dots (e.g. preset name)
  label, _, param = name.rpartition('.')
  Aspera.assert(!label.empty? && !param.empty?, type: BadArgument) { 'vault name shall match <name>.<param>' }
  info = store(mandatory: true).get(label: label)
  value = info[param.to_sym]
  raise "no such entry value: #{param}" if value.nil?
  Security.instance.add_configured(value)
end