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(root:, provider:) ⇒ Registry

Returns a new instance of Registry.



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

def initialize(root:, provider:)
  # TODO: Validate root starts with /, etc
  @root     = root
  @provider = provider
  refresh!
end

Instance Attribute Details

#providerObject (readonly)

Returns the value of attribute provider.



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

def provider
  @provider
end

#rootObject

Returns the value of attribute root.



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

def root
  @root
end

Instance Method Details

#[](key) ⇒ Object

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



24
25
26
# File 'lib/secret_config/registry.rb', line 24

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

#configurationObject

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



17
18
19
20
21
# File 'lib/secret_config/registry.rb', line 17

def configuration
  h = {}
  registry.each_pair { |k, v| decompose(k, v, h) }
  h
end

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

Returns [String] configuration value for the supplied key



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/secret_config/registry.rb', line 34

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

    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)


29
30
31
# File 'lib/secret_config/registry.rb', line 29

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

#refresh!Object



50
51
52
53
54
# File 'lib/secret_config/registry.rb', line 50

def refresh!
  h = {}
  provider.each(root) { |k, v| h[k] = v }
  @registry = h
end

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



46
47
48
# File 'lib/secret_config/registry.rb', line 46

def set(key:, value:, encrypt: true)
  SSM.new(key_id: key_id).set(expand_key(key), value, encrypt: encrypt)
end