Class: Fluent::AnonymizerOutput

Inherits:
Output
  • Object
show all
Includes:
HandleTagNameMixin, SetTagKeyMixin
Defined in:
lib/fluent/plugin/out_anonymizer.rb

Constant Summary collapse

HASH_ALGORITHM =
%w(md5 sha1 sha256 sha384 sha512 ipv4_mask)
DIGEST =
{
  "md5" => Proc.new { Digest::MD5 },
  "sha1" => Proc.new { Digest::SHA1 },
  "sha256" => Proc.new { Digest::SHA256 },
  "sha384" => Proc.new { Digest::SHA384 },
  "sha512" => Proc.new { Digest::SHA512 }
}

Instance Method Summary collapse

Constructor Details

#initializeAnonymizerOutput

Returns a new instance of AnonymizerOutput.



21
22
23
24
25
# File 'lib/fluent/plugin/out_anonymizer.rb', line 21

def initialize
  require 'digest/sha2'
  require 'ipaddr'
  super
end

Instance Method Details

#anonymize(message, algorithm, salt) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/fluent/plugin/out_anonymizer.rb', line 69

def anonymize(message, algorithm, salt)
  case algorithm
  when 'md5','sha1','sha256','sha384','sha512'
    DIGEST[algorithm].call.hexdigest(salt + message.to_s)
  when 'ipv4_mask'
    IPAddr.new(message).mask(@ipv4_mask_subnet).to_s
  else
    $log.warn "anonymizer: unknown algorithm #{algorithm} has called."
  end
end

#configure(conf) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fluent/plugin/out_anonymizer.rb', line 27

def configure(conf)
  super

  @hash_keys = Hash.new
  conf.keys.select{|k| k =~ /_keys$/}.each do |key|
    hash_algorithm_name = key.sub('_keys','')
    raise Fluent::ConfigError, "anonymizer: unsupported key #{hash_algorithm_name}" unless HASH_ALGORITHM.include?(hash_algorithm_name)
    conf[key].gsub(' ', '').split(',').each do |record_key|
      @hash_keys.store(record_key, hash_algorithm_name)
    end
  end

  if @hash_keys.count < 1
    raise Fluent::ConfigError, "anonymizer: missing hash keys setting."
  end

  if ( !@remove_tag_prefix && !@remove_tag_suffix && !@add_tag_prefix && !@add_tag_suffix )
    raise Fluent::ConfigError, "anonymizer: missing remove_tag_prefix, remove_tag_suffix, add_tag_prefix or add_tag_suffix."
  end
end

#emit(tag, es, chain) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fluent/plugin/out_anonymizer.rb', line 48

def emit(tag, es, chain)
  es.each do |time, record|
    @hash_keys.each do |hash_key, hash_algorithm|
      next unless record.include?(hash_key)
      record[hash_key] = filter_anonymize_record(record[hash_key], hash_algorithm)
    end
    filter_record(tag, time, record)
    Fluent::Engine.emit(tag, time, record)
  end
  chain.next
end

#filter_anonymize_record(data, hash_algorithm) ⇒ Object



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

def filter_anonymize_record(data, hash_algorithm)
  if data.is_a?(Array)
    data = data.collect { |v| anonymize(v, hash_algorithm, @hash_salt) }
  else
    data = anonymize(data, hash_algorithm, @hash_salt)
  end
  data
end