Class: Fluent::Plugin::AnonymizerFilter

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

Constant Summary collapse

MASK_METHODS =
{
  md5:    ->(opts){ ->(v,salt){ OpenSSL::Digest.new("md5").update(salt).update(v.to_s).hexdigest } },
  sha1:   ->(opts){ ->(v,salt){ OpenSSL::Digest.new("sha1").update(salt).update(v.to_s).hexdigest } },
  sha256: ->(opts){ ->(v,salt){ OpenSSL::Digest.new("sha256").update(salt).update(v.to_s).hexdigest } },
  sha384: ->(opts){ ->(v,salt){ OpenSSL::Digest.new("sha384").update(salt).update(v.to_s).hexdigest } },
  sha512: ->(opts){ ->(v,salt){ OpenSSL::Digest.new("sha512").update(salt).update(v.to_s).hexdigest } },
  uri_path: ->(opts){ ->(v,salt){
      begin
        uri = URI.parse(v)
        if uri.absolute?
          uri.path = '/'
          uri.user = uri.password = uri.query = uri.fragment = nil
        end
        uri.to_s
      rescue
        v
      end
    } },
  network: ->(opts){ ->(v,salt){
      begin
        addr = IPAddr.new(v)
        if addr.ipv4? && opts.ipv4_mask_bits
          addr.mask(opts.ipv4_mask_bits).to_s
        elsif addr.ipv6? && opts.ipv6_mask_bits
          addr.mask(opts.ipv6_mask_bits).to_s
        else
          addr.to_s
        end
      rescue
        v
      end
    } },
}

Instance Method Summary collapse

Constructor Details

#initializeAnonymizerFilter

Returns a new instance of AnonymizerFilter.



99
100
101
102
103
104
# File 'lib/fluent/plugin/filter_anonymizer.rb', line 99

def initialize
  super
  @salt_list = []
  @salt_map = {}
  @conversions = []
end

Instance Method Details

#configure(conf) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fluent/plugin/filter_anonymizer.rb', line 106

def configure(conf)
  super

  salt_missing = false

  @salt_list << @salt
  @salt_list += @salts if @salts

  @masks = []
  @mask_config_list.each do |c|
    unless c.salt || @salt_list.size > 0
      salt_missing = true
    end

    conv = MASK_METHODS[c.method].call(c)
    [c.key || nil, *c.keys].compact.each do |key|
      @masks << masker_for_key(conv, key, c)
    end
    [c.key_chain || nil, *c.key_chains].compact.each do |key_chain|
      @masks << masker_for_key_chain(conv, key_chain.split('.'), c)
    end
    @masks << masker_for_key_pattern(conv, c.key_pattern, c) if c.key_pattern
    @masks << masker_for_value_pattern(conv, c.value_pattern, c) if c.value_pattern
    @masks << masker_for_value_in_subnet(conv, c.value_in_subnet, c) if c.value_in_subnet
  end

  # obsolete option handling
  [[@md5_keys,:md5],[@sha1_keys,:sha1],[@sha256_keys,:sha256],[@sha384_keys,:sha384],[@sha512_keys,:sha512]].each do |param,m|
    next unless param
    @salt_list << (@hash_salt || '') if @salt_list.empty? # to suppress ConfigError for salt missing
    conf = OpenStruct.new
    conf.salt = @hash_salt || ''
    conf.mask_array_elements = true
    conv = MASK_METHODS[m].call(conf)
    param.split(',').map(&:strip).each do |key|
      if key.include?('.')
        @masks << masker_for_key_chain(conv, key.split('.'), conf)
      else
        @masks << masker_for_key(conv, key, conf)
      end
    end
  end
  if @ipaddr_mask_keys
    @salt_list << (@hash_salt || '') if @salt_list.empty? # to suppress ConfigError for salt missing
    conf = OpenStruct.new
    conf.salt = @hash_salt || ''
    conf.mask_array_elements = true
    conf.ipv4_mask_bits = @ipv4_mask_subnet
    conf.ipv6_mask_bits = @ipv6_mask_subnet
    conv = MASK_METHODS[:network].call(conf)
    @ipaddr_mask_keys.split(',').map(&:strip).each do |key|
      if key.include?('.')
        @masks << masker_for_key_chain(conv, key.split('.'), conf)
      else
        @masks << masker_for_key(conv, key, conf)
      end
    end
  end

  if @masks.size < 1
    raise Fluent::ConfigError, "no anonymizing operations configured"
  end
  if salt_missing
    raise Fluent::ConfigError, "salt (or salts) required, but missing"
  end
end

#filter(tag, time, record) ⇒ Object



173
174
175
# File 'lib/fluent/plugin/filter_anonymizer.rb', line 173

def filter(tag, time, record)
  record.update(@masks.reduce(record){|r,mask| mask.call(r)})
end

#mask_value(value, for_each) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/fluent/plugin/filter_anonymizer.rb', line 188

def mask_value(value, for_each)
  if for_each && value.is_a?(Array)
    value.map{|v|
      yield v
    }
  else
    yield value
  end
end

#masker_for_key(conv, key, opts) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/fluent/plugin/filter_anonymizer.rb', line 198

def masker_for_key(conv, key, opts)
  for_each = opts.mask_array_elements
  salt = opts.salt || salt_determine(key)
  if for_each
    ->(record){
      begin
        if record.has_key?(key)
          record[key] = mask_value(record[key], for_each){|v| conv.call(v, salt) }
        end
      rescue => e
        log.error "unexpected error while masking value", error_class: e.class, error: e.message
      end
      record
    }
  else
    ->(record){
      begin
        if record.has_key?(key)
          record[key] = conv.call(record[key], salt)
        end
      rescue => e
        log.error "unexpected error while masking value", error_class: e.class, error: e.message
      end
      record
    }
  end
end

#masker_for_key_chain(conv, key_chain, opts) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/fluent/plugin/filter_anonymizer.rb', line 226

def masker_for_key_chain(conv, key_chain, opts)
  for_each = opts.mask_array_elements
  heading = key_chain[0..-2]
  container_fetcher = ->(record){ heading.reduce(record){|r,c| r && r.has_key?(c) ? r[c] : nil } }
  tailing = key_chain[-1]
  ->(record){
    begin
      container = container_fetcher.call(record)
      if container && container.has_key?(tailing)
        container[tailing] = mask_value(container[tailing], for_each){|v| conv.call(v, opts.salt || salt_determine(tailing)) }
      end
    rescue => e
      log.error "unexpected error while masking value", error_class: e.class, error: e.message
    end
    record
  }
end

#masker_for_key_pattern(conv, pattern, opts) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/fluent/plugin/filter_anonymizer.rb', line 244

def masker_for_key_pattern(conv, pattern, opts)
  for_each = opts.mask_array_elements
  regexp = Regexp.new(pattern)
  ->(record){
    begin
      record.each_pair do |key, value|
        next unless (regexp =~ key.to_s rescue nil)
        record[key] = mask_value(record[key], for_each){|v| conv.call(v, opts.salt || salt_determine(key)) }
      end
    rescue => e
      log.error "unexpected error while masking value", error_class: e.class, error: e.message
    end
    record
  }
end

#masker_for_value_in_subnet(conv, network_str, opts) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/fluent/plugin/filter_anonymizer.rb', line 275

def masker_for_value_in_subnet(conv, network_str, opts)
  network = IPAddr.new(network_str)
  ->(record){
    begin
      record.each_pair do |key, value|
        next unless (network.include?(value) rescue nil)
        record[key] = conv.call(value, opts.salt || salt_determine(key))
      end
    rescue => e
      log.error "unexpected error while masking value", error_class: e.class, error: e.message
    end
    record
  }
end

#masker_for_value_pattern(conv, pattern, opts) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/fluent/plugin/filter_anonymizer.rb', line 260

def masker_for_value_pattern(conv, pattern, opts)
  regexp = Regexp.new(pattern)
  ->(record){
    begin
      record.each_pair do |key, value|
        next unless (regexp =~ value.to_s rescue nil)
        record[key] = conv.call(value, opts.salt || salt_determine(key))
      end
    rescue => e
      log.error "unexpected error while masking value", error_class: e.class, error: e.message
    end
    record
  }
end

#salt_determine(key) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/fluent/plugin/filter_anonymizer.rb', line 177

def salt_determine(key)
  return @salt_map[key] if @salt_map.has_key?(key)
  keystr = key.to_s
  if keystr.empty?
    @salt_map[key] = @salt_list[0]
  else
    @salt_map[key] = @salt_list[(keystr[0].ord + keystr[-1].ord) % @salt_list.size]
  end
  @salt_map[key]
end