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: nil, provider_args: nil) ⇒ Registry

Returns a new instance of Registry.

Raises:



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

#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.



34
35
36
# File 'lib/secret_config/registry.rb', line 34

def [](key)
  cache[expand_key(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 = expand_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.

Returns:

  • (Boolean)


39
40
41
# File 'lib/secret_config/registry.rb', line 39

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.



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 = expand_key(key)
  provider.set(key, value, encrypt: encrypt)
  cache[key] = value
end