Class: Aspera::Secret::Resolver
- Inherits:
-
Object
- Object
- Aspera::Secret::Resolver
- Defined in:
- lib/aspera/secret/resolver.rb
Overview
Reads the values of references from named stores, with a cache.
Configuration (e.g. from a YAML file):
cache_ttl: time in seconds a value is kept in memory (default: 60, 0: no cache)-
stores: name of store → parameters of store, withtype(seeStore::Factory)writable: true: allowwriteto this store (default: false)
The type and the names of parameters are checked by new, a store is created when first used:
a store not used (e.g. its secret is not available) is not an error.
Bootstrap secrets of stores (password of type file, token and secret_id of type vault): the value, or @file:<path> (Bootstrap).
Error messages give the reference, never a value.
Constant Summary collapse
- DEFAULT_CACHE_TTL =
60
Instance Method Summary collapse
-
#check(text) ⇒ Object
Checks that a reference is valid and reachable, without reading its value.
-
#fields(text) ⇒ Hash
Field names of a secret, without values, e.g.
-
#initialize(config) ⇒ Resolver
constructor
A new instance of Resolver.
-
#invalidate(store_name = nil) ⇒ Object
Remove values from cache, e.g.
-
#list(name, path = '', options = {}) ⇒ Array<Hash>
Secrets and folders of a store that references can read, without reading the secrets, e.g.
-
#metadata(text) ⇒ Hash
Checks that a secret can be read, without its value.
-
#resolve(text) ⇒ String
Value of secret, from cache if not expired.
-
#store(name) ⇒ Store::Base
Store, created when first used, e.g.
-
#store_names ⇒ Array<String>
Names of stores.
-
#writable?(name) ⇒ Boolean
trueif the store is declared writable in the configuration. -
#write(text, fields) ⇒ Hash
Write a new secret to a writable store.
Constructor Details
#initialize(config) ⇒ Resolver
Returns a new instance of Resolver.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/aspera/secret/resolver.rb', line 32 def initialize(config) raise Error, 'secret references configuration: expecting a Hash' unless config.is_a?(Hash) config = config.transform_keys(&:to_s) unsupported = config.keys - CONFIG_KEYS raise Error, "secret references configuration: unsupported keys: #{unsupported.join(', ')}" unless unsupported.empty? @cache_ttl = config.fetch('cache_ttl', DEFAULT_CACHE_TTL) raise Error, 'secret references configuration: cache_ttl: expecting a number >= 0' unless @cache_ttl.is_a?(Numeric) && !@cache_ttl.negative? stores = config.fetch('stores', nil) || {} raise Error, 'secret references configuration: stores: expecting a Hash' unless stores.is_a?(Hash) # Name of store → {params: ..., writable: bool}, checked @store_configs = stores.to_h { |name, params| [name.to_s, check_store(name.to_s, params)] }.freeze # Name of store → store instance, created when first used @stores = {} # Key of reference → [value, monotonic time of expiry] @cache = {} @lock = Mutex.new end |
Instance Method Details
#check(text) ⇒ Object
Checks that a reference is valid and reachable, without reading its value
72 73 74 75 |
# File 'lib/aspera/secret/resolver.rb', line 72 def check(text) ref = Secret.parse_reference(text) access(ref) { nil } end |
#fields(text) ⇒ Hash
Field names of a secret, without values, e.g. to choose the field of a reference
101 102 103 104 |
# File 'lib/aspera/secret/resolver.rb', line 101 def fields(text) ref = Secret.parse_reference(text) access(ref) { |store| {store: ref.store}.merge(store.read_fields(ref)) } end |
#invalidate(store_name = nil) ⇒ Object
Remove values from cache, e.g. after a rotation
137 138 139 140 141 142 |
# File 'lib/aspera/secret/resolver.rb', line 137 def invalidate(store_name = nil) @lock.synchronize do store_name.nil? ? @cache.clear : @cache.delete_if { |_, entry| entry[:store].eql?(store_name) } end nil end |
#list(name, path = '', options = {}) ⇒ Array<Hash>
Secrets and folders of a store that references can read, without reading the secrets, e.g. to build references
112 113 114 115 116 117 |
# File 'lib/aspera/secret/resolver.rb', line 112 def list(name, path = '', = {}) found = store(name) found.list_folder(path, ) rescue StandardError => e raise error_class(e), "secret store #{name}#{", folder #{path}" unless path.empty?}: #{reason(e, found)}" end |
#metadata(text) ⇒ Hash
Checks that a secret can be read, without its value
92 93 94 95 |
# File 'lib/aspera/secret/resolver.rb', line 92 def (text) ref = Secret.parse_reference(text) access(ref) { |store| {store: ref.store}.merge(store.read_reference(ref).except(:value)) } end |
#resolve(text) ⇒ String
Returns Value of secret, from cache if not expired.
80 81 82 83 84 85 86 |
# File 'lib/aspera/secret/resolver.rb', line 80 def resolve(text) ref = Secret.parse_reference(text) access(ref) do |store| key = ref.to_h.except(:text) cached(key) || cache(key, ref.store, store.read_reference(ref)[:value]) end end |
#store(name) ⇒ Store::Base
Returns Store, created when first used, e.g. to manage its secrets by label.
64 65 66 67 |
# File 'lib/aspera/secret/resolver.rb', line 64 def store(name) config = @store_configs.fetch(name) { raise Error, "unknown store #{name}" } @lock.synchronize { @stores[name] ||= create_store(name, config[:params]) } end |
#store_names ⇒ Array<String>
Returns Names of stores.
51 52 53 |
# File 'lib/aspera/secret/resolver.rb', line 51 def store_names @store_configs.keys end |
#writable?(name) ⇒ Boolean
Returns true if the store is declared writable in the configuration.
57 58 59 |
# File 'lib/aspera/secret/resolver.rb', line 57 def writable?(name) @store_configs.fetch(name) { raise Error, "unknown store #{name}" }[:writable] end |
#write(text, fields) ⇒ Hash
Write a new secret to a writable store
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/aspera/secret/resolver.rb', line 124 def write(text, fields) ref = Secret.parse_reference(text) raise Error, "secret reference #{ref.text}: field not allowed when writing (use field names in fields)" unless ref.field.nil? raise Error, "secret reference #{ref.text}: store #{ref.store} is not writable" unless writable?(ref.store) result = access(ref) { |store| store.write_reference(ref, fields) } # Invalidate cache for this reference so it is readable at once key = ref.to_h.except(:text) @lock.synchronize { @cache.delete(key) } result end |