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, interpolate: true) ⇒ Registry

Returns a new instance of Registry.

Raises:



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

def initialize(path: nil, provider: nil, provider_args: nil, interpolate: true)
  @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
  @interpolate = interpolate
  refresh!
end

Instance Attribute Details

#interpolateObject (readonly)

Returns the value of attribute interpolate.



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

def interpolate
  @interpolate
end

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



39
40
41
42
43
44
45
46
# File 'lib/secret_config/registry.rb', line 39

def [](key)
  value = cache[key]
  if value.nil? && SecretConfig.check_env_var?
    value      = env_var_override(key, value)
    cache[key] = value unless value.nil?
  end
  value.nil? ? nil : value.to_s
end

#[]=(key, value) ⇒ Object

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



72
73
74
# File 'lib/secret_config/registry.rb', line 72

def []=(key, value)
  set(key, value)
end

#configuration(path: nil, filters: SecretConfig.filters) ⇒ Object

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

Supply the relative path to start from so that only keys and values in that path will be returned.



27
28
29
30
31
32
33
34
35
36
# File 'lib/secret_config/registry.rb', line 27

def configuration(path: nil, filters: SecretConfig.filters)
  h = {}
  cache.each_pair do |key, value|
    next if path && !key.start_with?(path)

    value = filter_value(key, value, filters)
    Utils.decompose(key, value, h)
  end
  h
end

#delete(key) ⇒ Object

Delete a key from the centralized configuration store.



84
85
86
87
88
# File 'lib/secret_config/registry.rb', line 84

def delete(key)
  full_key = expand_key(key)
  provider.delete(full_key)
  cache.delete(key)
end

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

Returns [String] configuration value for the supplied key Convert the string value into an array of values by supplying a ‘separator`.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/secret_config/registry.rb', line 55

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

    value = block_given? ? yield : default
  end

  value = convert_encoding(encoding, value) if encoding

  return convert_type(type, value) unless separator
  return value if value.is_a?(Array)

  value.to_s.split(separator).collect { |element| convert_type(type, element.strip) }
end

#key?(key) ⇒ Boolean

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

Returns:

  • (Boolean)


49
50
51
# File 'lib/secret_config/registry.rb', line 49

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 precedence over the central store values.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/secret_config/registry.rb', line 92

def refresh!
  existing_keys = cache.keys
  updated_keys  = []
  fetch_path(path).each_pair 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) ⇒ Object

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



77
78
79
80
81
# File 'lib/secret_config/registry.rb', line 77

def set(key, value)
  full_key = expand_key(key)
  provider.set(full_key, value)
  cache[key] = value
end