Class: Aspera::Secret::Store::Base
- Inherits:
-
Object
- Object
- Aspera::Secret::Store::Base
- 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.
Direct Known Subclasses
EncryptedHash, HashicorpVault, Keeper, MacosSystem, OnePasswordApi, OnePasswordCli, SecretService
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
-
.arguments(params) ⇒ Hash
Keyword arguments of
new. -
.check_parameters(params) ⇒ Object
Checks the parameters of store.
-
.error_reason(_error) ⇒ String?
Reason of failure without secret,
nilif the error is not specific to the type.
Instance Method Summary collapse
-
#assert_new_label(label) ⇒ Object
Labels are unique:
setdoes not overwrite an existing secret. -
#check_reference(ref) ⇒ Object
Checks before access: options of reference, paths allowed by configuration.
-
#ids ⇒ Object
Returns a lightweight list: one Hash per secret with :label (and :id if the backend has one).
-
#list_folder(path, options = {}) ⇒ Array<Hash>
Secrets of the store that references can read, without their values: labels, no folder.
-
#read_fields(ref) ⇒ Hash
Field names of a secret by reference, without values: the path is the label.
-
#read_reference(ref) ⇒ Hash
Read a secret by reference: the path is the label, the field is
passwordby default. - #validate_set(options) ⇒ Object
-
#write_reference(_ref, _fields) ⇒ Hash
Write a new secret by reference.
Class Method Details
.arguments(params) ⇒ Hash
Returns Keyword arguments of new.
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
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.
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
72 73 74 75 |
# File 'lib/aspera/secret/store/base.rb', line 72 def check_reference(ref) unsupported = ref..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
92 93 94 95 96 |
# File 'lib/aspera/secret/store/base.rb', line 92 def list_folder(path, = {}) () 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
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
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() Aspera.assert_type(, Hash, type: Error) { 'options' } unsupported = .keys - CONTENT_KEYS Aspera.assert(unsupported.empty?, type: Error) { "unsupported options: #{unsupported}, use #{CONTENT_KEYS.join(', ')}" } .each_pair do |k, v| Aspera.assert_type(v, String, type: Error) { k.to_s } end Aspera.assert(.key?(:label), 'label is required', type: Error) end |