Sha256: aeac8a42e97088e3515128f14fb33e300c1b134f6285c81651d76dce0229e2ca

Contents?: true

Size: 1.8 KB

Versions: 5

Compression:

Stored size: 1.8 KB

Contents

# encoding: utf-8
module ::LogStash::Util::SubstitutionVariables

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

  # Recursive method to replace environment variable references in parameters
  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 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 : Environment entry value, default value as provided in the pattern
  # If the value does not match the pattern, the 'value' param returns as-is
  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)

      replacement = ENV.fetch(name, default)
      if replacement.nil?
        raise LogStash::ConfigurationError, "Cannot evaluate `#{placeholder}`. Environment variable `#{name}` is not set and there is no default value given."
      end
      replacement
    end
  end # def replace_placeholders
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
logstash-core-6.1.4-java lib/logstash/util/substitution_variables.rb
logstash-core-6.1.3-java lib/logstash/util/substitution_variables.rb
logstash-core-6.1.2-java lib/logstash/util/substitution_variables.rb
logstash-core-6.1.1-java lib/logstash/util/substitution_variables.rb
logstash-core-6.1.0-java lib/logstash/util/substitution_variables.rb