Class: Fluent::Plugin::MaskingFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_masking.rb

Constant Summary collapse

MASK_STRING =
"*******"

Instance Method Summary collapse

Constructor Details

#initializeMaskingFilter

Returns a new instance of MaskingFilter.



36
37
38
39
# File 'lib/fluent/plugin/filter_masking.rb', line 36

def initialize
  super
  @fields_to_mask = []
end

Instance Method Details

#configure(conf) ⇒ Object

this method only called ones (on startup time)



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fluent/plugin/filter_masking.rb', line 42

def configure(conf)
  super
  fieldsToMaskFilePath = conf['fieldsToMaskFilePath']

  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

      @fields_to_mask.push(value)
    end
  end

  puts "black list fields:"
  puts @fields_to_mask
end

#filter(tag, time, record) ⇒ Object



61
62
63
64
65
66
# File 'lib/fluent/plugin/filter_masking.rb', line 61

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) ⇒ Object

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fluent/plugin/filter_masking.rb', line 17

def maskRecord(record)
  maskedRecord = record

  begin
    recordStr = record.to_s

    @fields_to_mask.each do | fieldToMask |
      recordStr = recordStr.gsub(/"#{fieldToMask}"=>.+?((?=,\s\")|(?=}{1}$))/m, "\"#{fieldToMask}\"=>\"#{MASK_STRING}\"")
    end

    maskedRecord = strToHash(recordStr)

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

  maskedRecord
end

#strToHash(str) ⇒ Object



10
11
12
# File 'lib/fluent/plugin/filter_masking.rb', line 10

def strToHash(str)
  eval(str)
end