Class: SecretConfig::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/secret_config/registry.rb

Overview

Centralized configuration with values stored in AWS System Manager Parameter Store

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, provider: :file, provider_args: nil) ⇒ Registry

Returns a new instance of Registry.

Raises:



10
11
12
13
14
15
16
17
# File 'lib/secret_config/registry.rb', line 10

def initialize(path: nil, provider: :file, provider_args: nil)
  @path = path || default_path
  raise(UndefinedRootError, 'Root must start with /') unless @path.start_with?('/')

  @provider = create_provider(provider, provider_args)
  @cache    = Concurrent::Map.new
  refresh!
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/secret_config/registry.rb', line 8

def path
  @path
end

#providerObject (readonly)

Returns the value of attribute provider.



7
8
9
# File 'lib/secret_config/registry.rb', line 7

def provider
  @provider
end

Instance Method Details

#[](key) ⇒ Object

Returns [String] configuration value for the supplied key, or nil when missing.



31
32
33
# File 'lib/secret_config/registry.rb', line 31

def [](key)
  cache[expand_key(key)]
end

#configuration(relative: true, filters: SecretConfig.filters) ⇒ Object

Returns [Hash] a copy of the in memory configuration data.



20
21
22
23
24
25
26
27
28
# File 'lib/secret_config/registry.rb', line 20

def configuration(relative: true, filters: SecretConfig.filters)
  h = {}
  cache.each_pair do |key, value|
    key   = relative_key(key) if relative
    value = filter_value(key, value, filters)
    decompose(key, value, h)
  end
  h
end

#fetch(key, default: nil, type: :string, encoding: nil) ⇒ Object

Returns [String] configuration value for the supplied key



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/secret_config/registry.rb', line 41

def fetch(key, default: nil, type: :string, encoding: nil)
  value = self[key]
  if value.nil?
    raise(MissingMandatoryKey, "Missing configuration value for #{path}/#{key}") if default.nil?

    value = default.respond_to?(:call) ? default.call : default
  end

  value = convert_encoding(encoding, value) if encoding
  type == :string ? value : convert_type(type, value)
end

#key?(key) ⇒ Boolean

Returns [String] configuration value for the supplied key, or nil when missing.



36
37
38
# File 'lib/secret_config/registry.rb', line 36

def key?(key)
  cache.key?(expand_key(key))
end

#refresh!Object

Refresh the in-memory cached copy of the centralized configuration information. Environment variable values will take precendence over the central store values.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/secret_config/registry.rb', line 62

def refresh!
  existing_keys = cache.keys
  updated_keys  = []
  provider.each(path) do |key, value|
    cache[key] = env_var_override(key, value)
    updated_keys << key
  end

  # Remove keys deleted from the central registry.
  (existing_keys - updated_keys).each { |key| provider.delete(key) }

  true
end

#set(key:, value:, encrypt: true) ⇒ Object

Set the value for a key in the centralized configuration store.



54
55
56
57
58
# File 'lib/secret_config/registry.rb', line 54

def set(key:, value:, encrypt: true)
  key = expand_key(key)
  provider.set(key, value, encrypt: true)
  cache[key] = value
end