Class: LogStash::Filters::Cipher

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/cipher.rb

Overview

This filter parses a source and apply a cipher or decipher before storing it in the target.

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/logstash/filters/cipher.rb', line 93

def filter(event)
  return unless filter?(event)


  #If decrypt or encrypt fails, we keep it it intact.
  begin
    #@logger.debug("Event to filter", :event => event)
    data = event[@source]
    if @mode == "decrypt"
      data =  Base64.decode64(data) if @base64 == true
    end
    result = @cipher.update(data) + @cipher.final
    if @mode == "encrypt"
      data =  Base64.encode64(data) if @base64 == true
    end
  rescue => e
    @logger.warn("Exception catch on cipher filter", :event => event, :error => e)
  else
    event[@target]= result
    #Is it necessary to add 'if !result.nil?' ? exception have been already catched.
    #In doubt, I keep it.
    filter_matched(event) if !result.nil?
    #Too much bad result can be a problem, reinit cipher prevent this.
    init_cipher
  end
end

#init_cipherObject

def filter



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
# File 'lib/logstash/filters/cipher.rb', line 120

def init_cipher

  @cipher = OpenSSL::Cipher.new(@algorithm)
  if @mode == "encrypt"
    @cipher.encrypt
  elsif @mode == "decrypt"
    @cipher.decrypt
  else
    @logger.error("Invalid cipher mode. Valid values are \"encrypt\" or \"decrypt\"", :mode => @mode)
    raise "Bad configuration, aborting."
  end

  if @key.length != @key_size
    @logger.debug("key length is " + @key.length.to_s + ", padding it to " + @key_size.to_s + " with '" + @key_pad.to_s + "'")
    @key = @key[0,@key_size].ljust(@key_size,@key_pad)
  end

  @cipher.key = @key

  @cipher.iv = @iv if @iv

  @cipher.padding = @cipher_padding if @cipher_padding

  @logger.debug("Cipher initialisation done", :mode => @mode, :key => @key, :iv => @iv, :cipher_padding => @cipher_padding)
end

#registerObject



87
88
89
90
# File 'lib/logstash/filters/cipher.rb', line 87

def register
  require 'base64' if @base64
  init_cipher
end