class Fluent::Plugin::MaskingFilter

Constants

MASK_STRING

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/filter_masking.rb, line 48
def initialize
  super
  @fields_to_mask = []
  @fields_to_mask_regex = {}
  @fields_to_mask_keys = {}
  @fieldsToExcludeJSONPathsArray = []
end

Public Instance Methods

configure(conf) click to toggle source

this method only called ones (on startup time)

Calls superclass method
# File lib/fluent/plugin/filter_masking.rb, line 57
def configure(conf)
  super
  fieldsToMaskFilePath = conf['fieldsToMaskFilePath']
  fieldsToExcludeJSONPaths = conf['fieldsToExcludeJSONPaths']

  if fieldsToExcludeJSONPaths != nil && fieldsToExcludeJSONPaths.size() > 0 
    fieldsToExcludeJSONPaths.split(",").each do | field |
      # To save splits we'll save the path as an array
      splitArray = field.split(".")
      symArray = []
      splitArray.each do | pathPortion |
        symArray.push(pathPortion.to_sym)
      end
      @fieldsToExcludeJSONPathsArray.push(symArray)
    end
  end

  File.open(fieldsToMaskFilePath, "r") do |f|
    f.each_line do |line|
      value = line.to_s # make sure it's string
      value = value.gsub(/\s+/, "") # remove spaces
      value = value.gsub('\n', '') # remove line breakers

      if value.end_with? "/i"
        # case insensitive
        value = value.delete_suffix('/i')
        hashObjectRegex = Regexp.new(/(?::#{value}=>")(.*?)(?:")/mi)
        innerJSONStringRegex = Regexp.new(/(\\+)"#{value}\\+"\s*:\s*(\\+|\{).+?((?=(})|,( *|)(\s|\\+)\"(}*))|(?=}"$)|("}(?!\"|\\)))/mi)
      else
        # case sensitive
        hashObjectRegex = Regexp.new(/(?::#{value}=>")(.*?)(?:")/m)
        innerJSONStringRegex = Regexp.new(/(\\+)"#{value}\\+"\s*:\s*(\\+|\{).+?((?=(})|,( *|)(\s|\\+)\"(}*))|(?=}"$)|("}(?!\"|\\)))/m)
      end

      @fields_to_mask.push(value)

      hashObjectRegexStringReplacement = ":#{value}=>\"#{MASK_STRING}\""
      @fields_to_mask_regex[hashObjectRegex] = hashObjectRegexStringReplacement
      @fields_to_mask_keys[hashObjectRegex] = value

      innerJSONStringRegexStringReplacement = "\\1\"#{value}\\1\":\\1\"#{MASK_STRING}\\1\""
      @fields_to_mask_regex[innerJSONStringRegex] = innerJSONStringRegexStringReplacement
      @fields_to_mask_keys[innerJSONStringRegex] = value
    end
  end

  puts "black list fields:"
  puts @fields_to_mask
end
filter(tag, time, record) click to toggle source
# File lib/fluent/plugin/filter_masking.rb, line 107
def filter(tag, time, record)
  # This method implements the filtering logic for individual filters
  # It is internal to this class and called by filter_stream unless
  # the user overrides filter_stream.
  maskRecord(record)
end
maskRecord(record) click to toggle source

returns the masked record error safe method - if any error occurs the original record is returned

# File lib/fluent/plugin/filter_masking.rb, line 18
def maskRecord(record)
  maskedRecord = record
  excludedFields = []
  begin
    @fieldsToExcludeJSONPathsArray.each do | field |
      field_value = myDig(record, field)
      if field_value != nil
        excludedFields = excludedFields + field_value.split(',')
      end
    end
  rescue Exception => e
    $log.error "Failed to find mask exclude record: #{e}"
  end
  begin
    recordStr = record.to_s
    @fields_to_mask_regex.each do | fieldToMaskRegex, fieldToMaskRegexStringReplacement |
      if !(excludedFields.include? @fields_to_mask_keys[fieldToMaskRegex])
        recordStr = recordStr.gsub(fieldToMaskRegex, fieldToMaskRegexStringReplacement) 
      end
    end

    maskedRecord = strToHash(recordStr)

  rescue Exception => e
    $log.error "Failed to mask record: #{e}"
  end

  maskedRecord
end
strToHash(str) click to toggle source
# File lib/fluent/plugin/filter_masking.rb, line 12
def strToHash(str)
  eval(str)
end