logstash-filter-cef 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ebf5ff9780644f0093381ec3656ca654381e5d9b
4
+ data.tar.gz: b0561ae6cdf87c42d0e0a631156607c99f15e89a
5
+ SHA512:
6
+ metadata.gz: 1ed5271e8f064de97df95576c4017337ef754089fc6ca2aee827db9515bff596b21a9dd7b657f2cdb5aa62ee2bd0175679e6ad7bcde61aa2b45513ae80a1a85e
7
+ data.tar.gz: 9f5c141c735d740c5b3808048b35c1f88e299a2173963c5bcd0536c8cae987b236d949d6ae78dab2348adc9f2f79773e458899a0bb2ab0d16685b3f4fde8ec73
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## 2.0.0
2
+ - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully,
3
+ instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895
4
+ - Dependency on logstash-core update to 2.0
5
+
data/CONTRIBUTORS ADDED
@@ -0,0 +1,11 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * Aaron Mildenstein (untergeek)
6
+ * Pier-Hugues Pellerin (ph)
7
+
8
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
9
+ Logstash, and you aren't on the list above and want to be, please let us know
10
+ and we'll make sure you're here. Contributions from folks like you are what make
11
+ open source awesome.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-filter-example
2
+ Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012–2015 Elasticsearch <http://www.elastic.co>
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/NOTICE.TXT ADDED
@@ -0,0 +1,5 @@
1
+ Elasticsearch
2
+ Copyright 2012-2015 Elasticsearch
3
+
4
+ This product includes software developed by The Apache Software
5
+ Foundation (http://www.apache.org/).
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Logstash Plugin
2
+
3
+ [![Build
4
+ Status](http://build-eu-00.elastic.co/view/LS%20Plugins/view/LS%20Filters/job/logstash-plugin-filter-cef-unit/badge/icon)](http://build-eu-00.elastic.co/view/LS%20Plugins/view/LS%20Filters/job/logstash-plugin-filter-cef-unit/)
5
+
6
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
7
+
8
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
9
+
10
+ ## Documentation
11
+
12
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).
13
+
14
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
15
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
16
+
17
+ ## Need Help?
18
+
19
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
20
+
21
+ ## Developing
22
+
23
+ ### 1. Plugin Developement and Testing
24
+
25
+ #### Code
26
+ - To get started, you'll need JRuby with the Bundler gem installed.
27
+
28
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=cef).
29
+
30
+ - Install dependencies
31
+ ```sh
32
+ bundle install
33
+ ```
34
+
35
+ #### Test
36
+
37
+ - Update your dependencies
38
+
39
+ ```sh
40
+ bundle install
41
+ ```
42
+
43
+ - Run tests
44
+
45
+ ```sh
46
+ bundle exec rspec
47
+ ```
48
+
49
+ ### 2. Running your unpublished Plugin in Logstash
50
+
51
+ #### 2.1 Run in a local Logstash clone
52
+
53
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
54
+ ```ruby
55
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
56
+ ```
57
+ - Install plugin
58
+ ```sh
59
+ bin/plugin install --no-verify
60
+ ```
61
+ - Run Logstash with your plugin
62
+ ```sh
63
+ bin/logstash -e 'filter {awesome {}}'
64
+ ```
65
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
66
+
67
+ #### 2.2 Run in an installed Logstash
68
+
69
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
70
+
71
+ - Build your plugin gem
72
+ ```sh
73
+ gem build logstash-filter-awesome.gemspec
74
+ ```
75
+ - Install the plugin from the Logstash home
76
+ ```sh
77
+ bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
78
+ ```
79
+ - Start Logstash and proceed to test the plugin
80
+
81
+ ## Contributing
82
+
83
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
84
+
85
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
86
+
87
+ It is more important to the community that you are able to contribute.
88
+
89
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,177 @@
1
+ # encoding: utf-8
2
+ require 'logstash/filters/base'
3
+ require 'logstash/namespace'
4
+
5
+ # This is a CEF parsing filter. It takes an existing field which contains CEF and
6
+ # expands it into an actual data structure within the Logstash event.
7
+ #
8
+ # Inspired from: https://github.com/logstash-plugins/logstash-codec-cef
9
+ #
10
+ # A CEF string such as:
11
+ #
12
+ # 'CEF: 0|Figgity Foo Bar Inc.|ThingyThang|1.0.0|Firewall|Something Bad Happened|Informative|foo=bar baz=ah Hellz Nah'
13
+ #
14
+ # results in the following parsed structure.
15
+ # {
16
+ # "cef_version"=>"0",
17
+ # "cef_vendor"=>"Figgity Foo Bar Inc.",
18
+ # "cef_product"=>"ThingyThang",
19
+ # "cef_device_version"=>"1.0.0",
20
+ # "cef_sigid"=>"Firewall",
21
+ # "cef_name"=>"Something Bad Happened",
22
+ # "cef_severity"=>"Informative",
23
+ # "cef_syslog"=>"CEF:",
24
+ # "cef_ext"=> {
25
+ # "foo"=>"bar",
26
+ # "baz"=>"ah Hellz Nah"
27
+ # }
28
+ # }
29
+ #
30
+ #
31
+ # By default it will place the parsed CEF in the root (top level) of the Logstash event, but this
32
+ # filter can be configured to place the CEF into any arbitrary event field, using the
33
+ # `target` configuration.
34
+ #
35
+ class LogStash::Filters::CEF < LogStash::Filters::Base
36
+ # Implementation of a Logstash codec for the ArcSight Common Event Format (CEF)
37
+ # Based on Revision 20 of Implementing ArcSight CEF, dated from June 05, 2013
38
+ # https://protect724.hp.com/servlet/JiveServlet/downloadBody/1072-102-6-4697/CommonEventFormat.pdf
39
+ config_name 'cef'
40
+
41
+ # The configuration for the CEF filter:
42
+ # [source,ruby]
43
+ # source => source_field
44
+ #
45
+ # For example, if you have CEF data in the `message` field:
46
+ # [source,ruby]
47
+ # filter {
48
+ # cef {
49
+ # source => "message"
50
+ # }
51
+ # }
52
+ #
53
+ # The above would parse the cef from the `message` field
54
+ config :source, validate: :string, required: true
55
+
56
+ # Define the target field for placing the parsed data. If this setting is
57
+ # omitted, the CEF data will be stored at the root (top level) of the event.
58
+ #
59
+ # For example, if you want the data to be put in the `doc` field:
60
+ # [source,ruby]
61
+ # filter {
62
+ # cef {
63
+ # target => "doc"
64
+ # }
65
+ # }
66
+ #
67
+ # CEF in the value of the `source` field will be expanded into a
68
+ # data structure in the `target` field.
69
+ #
70
+ # NOTE: if the `target` field already exists, it will be overwritten!
71
+ config :target, validate: :string
72
+
73
+ # Append values to the `tags` field when there has been no
74
+ # successful match
75
+ config :tag_on_failure, validate: :array, default: ['_cefparsefailure']
76
+
77
+ def register
78
+ # Nothing to do here
79
+ end # def register
80
+
81
+ def filter(event)
82
+ @logger.debug? && @logger.debug('Running CEF filter', event: event)
83
+
84
+ source = event[@source]
85
+ return unless source
86
+
87
+ begin
88
+ parsed = cef_decode(source)
89
+ rescue => e
90
+ @tag_on_failure.each { |tag| event.tag(tag) }
91
+ @logger.warn('Error parsing CEF', source: @source, raw: source, exception: e)
92
+ return
93
+ end
94
+
95
+ if @target
96
+ event[@target] = parsed
97
+ else
98
+ unless parsed.is_a?(Hash)
99
+ @tag_on_failure.each { |tag| event.tag(tag) }
100
+ @logger.warn('Parsed CEF object/hash requires a target configuration option', source: @source, raw: source)
101
+ return
102
+ end
103
+
104
+ # TODO: (colin) the timestamp initialization should be DRY'ed but exposing the similar code
105
+ # in the Event#init_timestamp method. See https://github.com/elastic/logstash/issues/4293
106
+
107
+ # a) since the parsed hash will be set in the event root, first extract any @timestamp field to properly initialized it
108
+ parsed_timestamp = parsed.delete(LogStash::Event::TIMESTAMP)
109
+ begin
110
+ timestamp = parsed_timestamp ? LogStash::Timestamp.coerce(parsed_timestamp) : nil
111
+ rescue LogStash::TimestampParserError => e
112
+ timestamp = nil
113
+ end
114
+
115
+ # b) then set all parsed fields in the event
116
+ parsed.each { |k, v| event[k] = v }
117
+ # c) finally re-inject proper @timestamp
118
+ if parsed_timestamp
119
+ if timestamp
120
+ event.timestamp = timestamp
121
+ else
122
+ event.timestamp = LogStash::Timestamp.new
123
+ @logger.warn("Unrecognized #{LogStash::Event::TIMESTAMP} value, setting current time to #{LogStash::Event::TIMESTAMP}, original in #{LogStash::Event::TIMESTAMP_FAILURE_FIELD} field", value: parsed_timestamp.inspect)
124
+ event.tag(LogStash::Event::TIMESTAMP_FAILURE_TAG)
125
+ event[LogStash::Event::TIMESTAMP_FAILURE_FIELD] = parsed_timestamp.to_s
126
+ end
127
+ end
128
+ end
129
+
130
+ # filter_matched should go in the last line of our successful code
131
+ filter_matched(event)
132
+
133
+ @logger.debug? && @logger.debug('Event after CEF filter', event: event)
134
+ end # def filter
135
+
136
+ private
137
+
138
+ # CEF deocoding logic
139
+ def cef_decode(data)
140
+ # Strip any quotations at the start and end, flex connectors seem to send this
141
+ data = data[1..-2] if data[0] == '"'
142
+ event = {}
143
+
144
+ # Split by the pipes
145
+ event['cef_version'], event['cef_vendor'], event['cef_product'], event['cef_device_version'], event['cef_sigid'], event['cef_name'], event['cef_severity'], message = data.split /(?<!\\)[\|]/
146
+
147
+ # Try and parse out the syslog header if there is one
148
+ if event['cef_version'].include? ' '
149
+ event['cef_syslog'], unused, event['cef_version'] = event['cef_version'].rpartition(' ')
150
+ end
151
+
152
+ # Get rid of the CEF bit in the version
153
+ version = event['cef_version'].sub /^CEF:/, ''
154
+ event['cef_version'] = version
155
+
156
+ # Strip any whitespace from the message
157
+ if !message.nil? && message.include?('=')
158
+ message = message.strip
159
+
160
+ # If the last KVP has no value, add an empty string, this prevents hash errors below
161
+ message += ' ' if message.end_with?('=')
162
+
163
+ # Now parse the key value pairs into it
164
+ extensions = {}
165
+ message = message.split(/ ([\w\.]+)=/)
166
+ key, value = message.shift.split('=', 2)
167
+ extensions[key] = value
168
+
169
+ Hash[*message].each { |k, v| extensions[k] = v }
170
+
171
+ # And save the new has as the extensions
172
+ event['cef_ext'] = extensions
173
+ end
174
+
175
+ return event
176
+ end
177
+ end # class LogStash::Filters::Example
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-cef'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = "This is a CEF parsing filter. It takes an existing field which contains CEF and expands it into an actual data structure within the Logstash event."
6
+ s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
7
+ s.authors = ["Domingo Kiser"]
8
+ s.email = 'domingo.kiser@gmail.com'
9
+ s.homepage = "https://github.com/dkiser/logstash-filter-cef"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core", ">= 2.0.0", "< 3.0.0"
22
+ s.add_development_dependency 'logstash-devutils'
23
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ require 'logstash/devutils/rspec/spec_helper'
4
+ require 'logstash/filters/cef'
5
+ require 'logstash/timestamp'
6
+
7
+ describe LogStash::Filters::CEF do
8
+ describe 'parse message into the event' do
9
+ config <<-CONFIG
10
+ filter {
11
+ cef {
12
+ # Parse message as CEF string
13
+ source => "message"
14
+ }
15
+ }
16
+ CONFIG
17
+
18
+ sample 'CEF: 0|Figgity Foo Bar Inc.|ThingyThang|1.0.0|Firewall|Something Bad Happened|Informative|foo=bar baz=ah Hellz Nah' do
19
+ insist { subject['cef_version'] } == '0'
20
+ insist { subject['cef_vendor'] } == 'Figgity Foo Bar Inc.'
21
+ insist { subject['cef_product'] } == 'ThingyThang'
22
+ insist { subject['cef_device_version'] } == '1.0.0'
23
+ insist { subject['cef_sigid'] } == 'Firewall'
24
+ insist { subject['cef_name'] } == 'Something Bad Happened'
25
+ insist { subject['cef_syslog'] } == "CEF:"
26
+ insist { subject['cef_severity'] } == 'Informative'
27
+ insist { subject['cef_ext']['foo'] } == 'bar'
28
+ insist { subject['cef_ext']['baz'] } == 'ah Hellz Nah'
29
+ end
30
+ end
31
+
32
+ context 'using message field source' do
33
+ subject(:filter) { LogStash::Filters::CEF.new(config) }
34
+
35
+ let(:config) { { 'source' => 'message' } }
36
+ let(:event) { LogStash::Event.new('message' => message) }
37
+
38
+ before(:each) do
39
+ filter.register
40
+ filter.filter(event)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-cef
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Domingo Kiser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - '>='
17
+ - !ruby/object:Gem::Version
18
+ version: 2.0.0
19
+ - - <
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ name: logstash-core
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.0
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ name: logstash-devutils
40
+ prerelease: false
41
+ type: :development
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program
48
+ email: domingo.kiser@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - CHANGELOG.md
54
+ - CONTRIBUTORS
55
+ - DEVELOPER.md
56
+ - Gemfile
57
+ - LICENSE
58
+ - NOTICE.TXT
59
+ - README.md
60
+ - lib/logstash/filters/cef.rb
61
+ - logstash-filter-cef.gemspec
62
+ - spec/filters/cef_spec.rb
63
+ - spec/spec_helper.rb
64
+ homepage: https://github.com/dkiser/logstash-filter-cef
65
+ licenses:
66
+ - Apache License (2.0)
67
+ metadata:
68
+ logstash_plugin: 'true'
69
+ logstash_group: filter
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.4.5
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: This is a CEF parsing filter. It takes an existing field which contains CEF and expands it into an actual data structure within the Logstash event.
90
+ test_files:
91
+ - spec/filters/cef_spec.rb
92
+ - spec/spec_helper.rb