Class: Aspera::Secret::Store::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/secret/store/base.rb

Overview

Base class of stores of secrets, created by Factory from parameters: management by label, and read by reference (Secret::Resolver). For references, errors are raised with the reason only, Resolver adds the reference.

Constant Summary collapse

CONTENT_KEYS =
%i[label username password url description].freeze
PARAMETERS =

Parameters of store: keyword arguments of new. Secrets to access the store: the value, or @file:<path> (Secret::Bootstrap), when the type supports it.

[].freeze
REFERENCE_OPTIONS =

Options accepted in references

[].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.arguments(params) ⇒ Hash

Returns Keyword arguments of new.

Parameters:

  • params (Hash) —

    Parameters of store, checked, Symbol keys

Returns:

  • (Hash) —

    Keyword arguments of new

Raises:

  • (Error) —

    Invalid parameter



37
38
39
# File 'lib/aspera/secret/store/base.rb', line 37

def arguments(params)
  params
end

.check_parameters(params) ⇒ Object

Checks the parameters of store

Parameters:

  • params (Hash) —

    Parameters, Symbol keys

Raises:

  • (Error) —

    Unsupported parameter



29
30
31
32
# File 'lib/aspera/secret/store/base.rb', line 29

def check_parameters(params)
  unsupported = params.keys - self::PARAMETERS
  raise Error, "unsupported keys: #{unsupported.join(', ')}" unless unsupported.empty?
end

.error_reason(_error) ⇒ String?

Returns Reason of failure without secret, nil if the error is not specific to the type.

Parameters:

  • _error (StandardError) —

    Error raised by the store

Returns:

  • (String, nil) —

    Reason of failure without secret, nil if the error is not specific to the type



43
44
45
# File 'lib/aspera/secret/store/base.rb', line 43

def error_reason(_error)
  nil
end

Instance Method Details

#assert_new_label(label) ⇒ Object

Labels are unique: set does not overwrite an existing secret



65
66
67
# File 'lib/aspera/secret/store/base.rb', line 65

def assert_new_label(label)
  Aspera.assert(get(label: label, exception: false).nil?, type: Error) { "secret #{label} already exist, delete first" }
end

#check_reference(ref) ⇒ Object

Checks before access: options of reference, paths allowed by configuration

Parameters:

Raises:

  • (Error) —

    Reference not allowed



72
73
74
75
# File 'lib/aspera/secret/store/base.rb', line 72

def check_reference(ref)
  unsupported = ref.options.keys - self.class::REFERENCE_OPTIONS
  raise Error, "unsupported options: #{unsupported.join(', ')}" unless unsupported.empty?
end

#ids ⇒ Object

Returns a lightweight list: one Hash per secret with :label (and :id if the backend has one). Override in subclasses that can provide this cheaply without fetching full item details.



50
51
52
# File 'lib/aspera/secret/store/base.rb', line 50

def ids
  all.map { |s| s.slice(:id, :label).compact }
end

#list_folder(path, options = {}) ⇒ Array<Hash>

Secrets of the store that references can read, without their values: labels, no folder

Parameters:

  • path (String) —

    Folder: empty, labels have no folder

  • options (Hash<String,String>) (defaults to: {}) —

    Options of references

Returns:

  • (Array<Hash>) —

    name (label), folder (false), by name

Raises:

  • (Error) —

    Unsupported option, or a folder



92
93
94
95
96
# File 'lib/aspera/secret/store/base.rb', line 92

def list_folder(path, options = {})
  check_list_options(options)
  raise Error, "labels have no folder: #{path}" unless path.empty?
  ids.map { |item| {name: item[:label].to_s, folder: false} }.sort_by { |entry| entry[:name] }
end

#read_fields(ref) ⇒ Hash

Field names of a secret by reference, without values: the path is the label

Parameters:

  • ref (Reference) —

    Reference, its field is not used

Returns:

  • (Hash) —

    fields (Array<String>, fields that a reference can read), version (nil), updated_time (nil)

Raises:



102
103
104
105
106
# File 'lib/aspera/secret/store/base.rb', line 102

def read_fields(ref)
  entry = get(label: ref.path, exception: false)
  raise NotFound, "no label: #{ref.path}" if entry.nil?
  {fields: field_names(entry.reject { |name, _| name.to_s.eql?('label') }), version: nil, updated_time: nil}
end

#read_reference(ref) ⇒ Hash

Read a secret by reference: the path is the label, the field is password by default

Parameters:

Returns:

  • (Hash) —

    value (String), version (nil if store has no version), updated_time (Time or nil)

Raises:

  • (NotFound) —

    No secret for the label, or no such field



81
82
83
84
85
# File 'lib/aspera/secret/store/base.rb', line 81

def read_reference(ref)
  entry = get(label: ref.path, exception: false)
  raise NotFound, "no label: #{ref.path}" if entry.nil?
  {value: field_value(entry, ref, default: DEFAULT_FIELD), version: nil, updated_time: nil}
end

#validate_set(options) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/aspera/secret/store/base.rb', line 54

def validate_set(options)
  Aspera.assert_type(options, Hash, type: Error) { 'options' }
  unsupported = options.keys - CONTENT_KEYS
  Aspera.assert(unsupported.empty?, type: Error) { "unsupported options: #{unsupported}, use #{CONTENT_KEYS.join(', ')}" }
  options.each_pair do |k, v|
    Aspera.assert_type(v, String, type: Error) { k.to_s }
  end
  Aspera.assert(options.key?(:label), 'label is required', type: Error)
end

#write_reference(_ref, _fields) ⇒ Hash

Write a new secret by reference. Subclasses that support writing must override this method.

Parameters:

Returns:

  • (Hash) —

    version (Integer or nil), updated_time (Time or nil)

Raises:

  • (Error) —

    Always — writing is not supported by this store type



114
115
116
# File 'lib/aspera/secret/store/base.rb', line 114

def write_reference(_ref, _fields)
  raise Error, 'writing not supported by store type'
end