Class: Prefab::ConfigValueUnwrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/prefab/config_value_unwrapper.rb

Constant Summary collapse

LOG =
Prefab::InternalLogger.new(self)
CONFIDENTIAL_PREFIX =
"*****"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_value, resolver, weighted_value_index = nil) ⇒ ConfigValueUnwrapper

Returns a new instance of ConfigValueUnwrapper.



9
10
11
12
13
# File 'lib/prefab/config_value_unwrapper.rb', line 9

def initialize(config_value, resolver, weighted_value_index = nil)
  @config_value = config_value
  @resolver = resolver
  @weighted_value_index = weighted_value_index
end

Instance Attribute Details

#weighted_value_indexObject (readonly)

Returns the value of attribute weighted_value_index.



7
8
9
# File 'lib/prefab/config_value_unwrapper.rb', line 7

def weighted_value_index
  @weighted_value_index
end

Class Method Details

.coerce_into_type(value_string, config, env_var_name) ⇒ Object

Don’t allow env vars to resolve to a value_type other than the config’s value_type



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/prefab/config_value_unwrapper.rb', line 93

def self.coerce_into_type(value_string, config, env_var_name)
  case config.value_type
  when :INT then Integer(value_string)
  when :DOUBLE then Float(value_string)
  when :STRING then String(value_string)
  when :STRING_LIST then
    maybe_string_list = YAML.load(value_string)
    case maybe_string_list
    when Array
      maybe_string_list
    else
      raise raise Prefab::Errors::EnvVarParseError.new(value_string, config, env_var_name)
    end
  when :BOOL then
    maybe_bool = YAML.load(value_string)
    case maybe_bool
    when TrueClass, FalseClass
      maybe_bool
    else
      raise Prefab::Errors::EnvVarParseError.new(value_string, config, env_var_name)
    end
  when :NOT_SET_VALUE_TYPE
    YAML.load(value_string)
  else
    raise Prefab::Errors::EnvVarParseError.new(value_string, config, env_var_name)
  end
rescue ArgumentError
  raise Prefab::Errors::EnvVarParseError.new(value_string, config, env_var_name)
end

.deepest_value(config_value, config, context, resolver) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/prefab/config_value_unwrapper.rb', line 65

def self.deepest_value(config_value, config, context, resolver)
  if config_value&.type == :weighted_values
    value, index = Prefab::WeightedValueResolver.new(
      config_value.weighted_values.weighted_values,
      config.key,
      context.get(config_value.weighted_values.hash_by_property_name)
    ).resolve

    new(deepest_value(value.value, config, context, resolver).raw_config_value, resolver, index)

  elsif config_value&.type == :provided
    if :ENV_VAR == config_value.provided.source
      raw = ENV[config_value.provided.lookup]
      if raw.nil?
        raise Prefab::Errors::MissingEnvVarError.new("Missing environment variable #{config_value.provided.lookup}")
      else
        coerced = coerce_into_type(raw, config, config_value.provided.lookup)
        new(Prefab::ConfigValueWrapper.wrap(coerced, confidential: config_value.confidential), resolver)
      end
    else
      raise "Unknown Provided Source #{config_value.provided.source}"
    end
  else
    new(config_value, resolver)
  end
end

Instance Method Details

#raw_config_valueObject



28
29
30
# File 'lib/prefab/config_value_unwrapper.rb', line 28

def raw_config_value
  @config_value
end

#reportable_valueObject



24
25
26
# File 'lib/prefab/config_value_unwrapper.rb', line 24

def reportable_value
  Prefab::ConfigValueUnwrapper.new(reportable_wrapped_value, @resolver, @weighted_value_index).unwrap
end

#reportable_wrapped_valueObject



15
16
17
18
19
20
21
22
# File 'lib/prefab/config_value_unwrapper.rb', line 15

def reportable_wrapped_value
  if @config_value.confidential || @config_value.decrypt_with&.length&.positive?
    # Unique hash for differentiation
    Prefab::ConfigValueWrapper.wrap("#{CONFIDENTIAL_PREFIX}#{Digest::MD5.hexdigest(unwrap)[0, 5]}")
  else
    @config_value
  end
end

#unwrap(raw_json: false) ⇒ Object

this will return the actual value of confidential, use reportable_value unless you need it



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/prefab/config_value_unwrapper.rb', line 33

def unwrap(raw_json: false)
  raw = case @config_value.type
        when :int, :string, :double, :bool, :log_level
          @config_value.public_send(@config_value.type)
        when :string_list
          @config_value.string_list.values
        when :duration
          Prefab::Duration.new(@config_value.duration.definition)
        when :json
          if raw_json
            @config_value.json.json
          else
            JSON.parse(@config_value.json.json, symbolize_names: @resolver.symbolize_json_names?)
          end
        else
          LOG.error "Unknown type: #{@config_value.type}"
          raise "Unknown type: #{@config_value.type}"
        end
  if @config_value.has_decrypt_with?
    decryption_key = @resolver.get(@config_value.decrypt_with)&.unwrapped_value
    if decryption_key.nil?
      LOG.warn "No value for decryption key #{@config_value.decrypt_with} found."
      return ""
    else
      unencrypted = Prefab::Encryption.new(decryption_key).decrypt(raw)
      return unencrypted
    end
  end

  raw
end