class ElasticAPM::Transport::Filters::SecretsFilter

@api private

Constants

FILTERED
KEY_FILTERS
VALUE_FILTERS

Public Class Methods

new(config) click to toggle source
# File lib/elastic_apm/transport/filters/secrets_filter.rb, line 26
def initialize(config)
  @config = config
  @key_filters = KEY_FILTERS + config.custom_key_filters
end

Public Instance Methods

call(payload) click to toggle source
# File lib/elastic_apm/transport/filters/secrets_filter.rb, line 31
def call(payload)
  strip_from! payload.dig(:transaction, :context, :request, :headers)
  strip_from! payload.dig(:transaction, :context, :request, :env)
  strip_from! payload.dig(:transaction, :context, :request, :cookies)
  strip_from! payload.dig(:transaction, :context, :response, :headers)
  strip_from! payload.dig(:error, :context, :request, :headers)
  strip_from! payload.dig(:error, :context, :response, :headers)
  strip_from! payload.dig(:transaction, :context, :request, :body)

  payload
end
filter_key?(key) click to toggle source

rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity

# File lib/elastic_apm/transport/filters/secrets_filter.rb, line 64
def filter_key?(key)
  @key_filters.any? { |regex| key.match regex }
end
filter_value?(value) click to toggle source
# File lib/elastic_apm/transport/filters/secrets_filter.rb, line 68
def filter_value?(value)
  VALUE_FILTERS.any? { |regex| value.match regex }
end
strip_from!(obj) click to toggle source

rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity

# File lib/elastic_apm/transport/filters/secrets_filter.rb, line 44
def strip_from!(obj)
  return unless obj && obj.is_a?(Hash)

  obj.each do |k, v|
    if filter_key?(k)
      next obj[k] = FILTERED
    end

    case v
    when Hash
      strip_from!(v)
    when String
      if filter_value?(v)
        obj[k] = FILTERED
      end
    end
  end
end