Class: Aspera::Secret::Resolver

Inherits:
Object
  • Object
show all
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, with type (see Store::Factory)
    • writable: true: allow write to 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

Constructor Details

#initialize(config) ⇒ Resolver

Returns a new instance of Resolver.

Parameters:

  • config (Hash) —

    Configuration, keys String or Symbol

Raises:

  • (Error) —

    Invalid configuration



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

Parameters:

Raises:

  • (Error) —

    Invalid reference, not allowed, or not readable



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

Parameters:

  • text (String) —

    Reference, its field is not used

Returns:

  • (Hash) —

    store, fields (Array<String>), version (nil if store has no version), updated_time (Time or nil)

Raises:

  • (Error) —

    Invalid reference, not allowed, or not readable



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

Parameters:

  • store_name (String, nil) (defaults to: nil) —

    Name of store, nil: all stores



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

Parameters:

  • name (String) —

    Name of store

  • path (String) (defaults to: '') —

    Folder, empty for a store without folders (labels)

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

    Options of references that select the folder (mount of type vault)

Returns:

  • (Array<Hash>) —

    name (last segment of path, or label), folder (true for a folder), by name

Raises:

  • (Error) —

    Unknown store, folder not allowed, listing not supported by the type, or store not readable



112
113
114
115
116
117
# File 'lib/aspera/secret/resolver.rb', line 112

def list(name, path = '', options = {})
  found = store(name)
  found.list_folder(path, options)
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

Parameters:

Returns:

  • (Hash) —

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

Raises:

  • (Error) —

    Invalid reference, not allowed, or not readable



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.

Parameters:

Returns:

  • (String) —

    Value of secret, from cache if not expired

Raises:

  • (Error) —

    Invalid reference, not allowed, or not readable



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.

Parameters:

  • name (String) —

    Name of store

Returns:

  • (Store::Base) —

    Store, created when first used, e.g. to manage its secrets by label

Raises:

  • (Error) —

    Unknown store, or invalid parameter



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.

Returns:

  • (Array<String>) —

    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.

Parameters:

  • name (String) —

    Name of store

Returns:

  • (Boolean) —

    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

Parameters:

  • text (String) —

    Reference (without field: the field names are given in fields)

  • fields (Hash<String,String>) —

    Field name → value

Returns:

  • (Hash) —

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

Raises:

  • (Error) —

    Store not writable, reference has a field, unknown store, not allowed, or write failed



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