Module: LogStash::Util::SubstitutionVariables

Includes:
Loggable
Included in:
Config::Mixin, Config::Mixin::DSL, Settings
Defined in:
lib/logstash/util/substitution_variables.rb

Constant Summary collapse

SUBSTITUTION_PLACEHOLDER_REGEX =
/\${(?<name>[a-zA-Z_.][a-zA-Z0-9_.]*)(:(?<default>[^}]*))?}/

Instance Method Summary collapse

Instance Method Details

#deep_replace(value) ⇒ Object

Recursive method to replace substitution variable references in parameters



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/logstash/util/substitution_variables.rb', line 17

def deep_replace(value)
  if value.is_a?(Hash)
    value.each do |valueHashKey, valueHashValue|
      value[valueHashKey.to_s] = deep_replace(valueHashValue)
    end
  else
    if value.is_a?(Array)
      value.each_index do | valueArrayIndex|
        value[valueArrayIndex] = deep_replace(value[valueArrayIndex])
      end
    else
      return replace_placeholders(value)
    end
  end
end

#replace_placeholders(value) ⇒ Object

Replace all substitution variable references in the ‘value’ param and returns the substituted value, or the original value if a substitution can not be made Process following patterns : $VAR, $VAR:defaultValue If value matches the pattern, returns the following precedence : Secret store value, Environment entry value, default value as provided in the pattern If the value does not match the pattern, the ‘value’ param returns as-is



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/logstash/util/substitution_variables.rb', line 37

def replace_placeholders(value)
  return value unless value.is_a?(String)

  value.gsub(SUBSTITUTION_PLACEHOLDER_REGEX) do |placeholder|
    # Note: Ruby docs claim[1] Regexp.last_match is thread-local and scoped to
    # the call, so this should be thread-safe.
    #
    # [1] http://ruby-doc.org/core-2.1.1/Regexp.html#method-c-last_match
    name = Regexp.last_match(:name)
    default = Regexp.last_match(:default)
    logger.debug("Replacing `#{placeholder}` with actual value")

    #check the secret store if it exists
    secret_store = SECRET_STORE.instance
    replacement = secret_store.nil? ? nil : secret_store.retrieveSecret(SecretStoreExt.getStoreId(name))
    #check the environment
    replacement = ENV.fetch(name, default) if replacement.nil?
    if replacement.nil?
      raise LogStash::ConfigurationError, "Cannot evaluate `#{placeholder}`. Replacement variable `#{name}` is not defined in a Logstash secret store " +
          "or as an Environment entry and there is no default value given."
    end
    replacement.to_s
  end
end