Class: SecretConfig::Registry
- Inherits:
-
Object
- Object
- SecretConfig::Registry
- Defined in:
- lib/secret_config/registry.rb
Overview
Centralized configuration with values stored in AWS System Manager Parameter Store
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns [String] configuration value for the supplied key, or nil when missing.
-
#[]=(key, value) ⇒ Object
Set the value for a key in the centralized configuration store.
-
#configuration(relative: true, filters: SecretConfig.filters) ⇒ Object
Returns [Hash] a copy of the in memory configuration data.
-
#delete(key) ⇒ Object
Delete a key from the centralized configuration store.
-
#fetch(key, default: nil, type: :string, encoding: nil) ⇒ Object
Returns [String] configuration value for the supplied key.
-
#initialize(path: nil, provider: nil, provider_args: nil) ⇒ Registry
constructor
A new instance of Registry.
-
#key?(key) ⇒ Boolean
Returns [String] configuration value for the supplied key, or nil when missing.
-
#refresh! ⇒ Object
Refresh the in-memory cached copy of the centralized configuration information.
-
#set(key, value, encrypt: true) ⇒ Object
Set the value for a key in the centralized configuration store.
Constructor Details
#initialize(path: nil, provider: nil, provider_args: nil) ⇒ Registry
Returns a new instance of Registry.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/secret_config/registry.rb', line 10 def initialize(path: nil, provider: nil, provider_args: nil) @path = default_path(path) raise(UndefinedRootError, 'Root must start with /') unless @path.start_with?('/') resolved_provider = default_provider(provider) provider_args = nil if resolved_provider != provider @provider = create_provider(resolved_provider, provider_args) @cache = Concurrent::Map.new refresh! end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
8 9 10 |
# File 'lib/secret_config/registry.rb', line 8 def path @path end |
#provider ⇒ Object (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.
34 35 36 |
# File 'lib/secret_config/registry.rb', line 34 def [](key) cache[(key)] end |
#[]=(key, value) ⇒ Object
Set the value for a key in the centralized configuration store.
57 58 59 |
# File 'lib/secret_config/registry.rb', line 57 def []=(key, value) set(key, value) end |
#configuration(relative: true, filters: SecretConfig.filters) ⇒ Object
Returns [Hash] a copy of the in memory configuration data.
23 24 25 26 27 28 29 30 31 |
# File 'lib/secret_config/registry.rb', line 23 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 |
#delete(key) ⇒ Object
Delete a key from the centralized configuration store.
69 70 71 72 73 |
# File 'lib/secret_config/registry.rb', line 69 def delete(key) key = (key) provider.delete(key) cache.delete(key) end |
#fetch(key, default: nil, type: :string, encoding: nil) ⇒ Object
Returns [String] configuration value for the supplied key
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/secret_config/registry.rb', line 44 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.
39 40 41 |
# File 'lib/secret_config/registry.rb', line 39 def key?(key) cache.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.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/secret_config/registry.rb', line 77 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.
62 63 64 65 66 |
# File 'lib/secret_config/registry.rb', line 62 def set(key, value, encrypt: true) key = (key) provider.set(key, value, encrypt: encrypt) cache[key] = value end |