Sha256: d7397e0affcd05703e3a628affafc3922dcf8bd217945ed03ab1f8918a8cfeb1

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require 'lenjador/preprocessors/json_pointer_trie'
require 'lenjador/preprocessors/strategies/mask'
require 'lenjador/preprocessors/strategies/prune'

class Lenjador
  module Preprocessors
    class Whitelist
      DEFAULT_WHITELIST = %w[/id /message /correlation_id /queue /trace_id /span_id].freeze
      MASK_SYMBOL = '*'
      MASKED_VALUE = MASK_SYMBOL * 5

      PRUNE_ACTION_NAMES = %w[prune exclude].freeze

      class InvalidPointerFormatException < RuntimeError
      end

      def initialize(config = {})
        trie = build_trie(config)

        @strategy = if PRUNE_ACTION_NAMES.include?(config[:action].to_s)
                      Strategies::Prune.new(trie)
                    else
                      Strategies::Mask.new(trie)
                    end
      end

      def process(data)
        @strategy.process(data)
      end

      private

      def validate_pointer(pointer)
        raise InvalidPointerFormatException, 'Pointer should not contain trailing slash' if pointer.slice(-1) == '/'
      end

      def decode(pointer)
        pointer
          .gsub('~1', '/')
          .gsub('~0', '~')
      end

      def build_trie(config)
        pointers = (config[:pointers] || []) + DEFAULT_WHITELIST

        pointers.reduce(JSONPointerTrie.new(**config)) do |trie, pointer|
          validate_pointer(pointer)

          trie.insert(decode(pointer))
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lenjador-2.3.1 lib/lenjador/preprocessors/whitelist.rb
lenjador-2.3.0 lib/lenjador/preprocessors/whitelist.rb
lenjador-2.2.2 lib/lenjador/preprocessors/whitelist.rb
lenjador-2.2.1 lib/lenjador/preprocessors/whitelist.rb