logstash-filter-sanitize_mac 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 939dc138695bc3f6396f42d17d6ab48cc60f5a2e
4
+ data.tar.gz: c1dba234a312e4d04c24871723bc0ba2488df343
5
+ SHA512:
6
+ metadata.gz: 14aefd60009065b2543a6a20bf77b04bf14223dec9d6c393530f92da398bb81e1a230192fa5abf312099563e74445b1147a70e2e55ea26c8684952090b6c28c5
7
+ data.tar.gz: 9a5ea392249735181dd5c95f88b429cb53b71db63cadf8f4276c03f0858d9540605f48db164a3b6b16652b15ab137f76532fa0b46380f133a640dc464d8d76a6
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin from logstash v1 (was at https://github.com/mcnewton/elk/tree/master/logstash-filters)
@@ -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
+ * - Matthew Newton
6
+ * - Enno Gröper
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.
@@ -0,0 +1,2 @@
1
+ # logstash-filter-sanitize_mac
2
+ Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
@@ -0,0 +1,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
+
5
+ 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.
6
+
7
+ ## Documentation
8
+
9
+ 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/).
10
+
11
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
12
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
17
+
18
+ ## Developing
19
+
20
+ ### 1. Plugin Developement and Testing
21
+
22
+ #### Code
23
+ - To get started, you'll need JRuby with the Bundler gem installed.
24
+
25
+ - 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=example).
26
+
27
+ - Install dependencies
28
+ ```sh
29
+ bundle install
30
+ ```
31
+
32
+ #### Test
33
+
34
+ - Update your dependencies
35
+
36
+ ```sh
37
+ bundle install
38
+ ```
39
+
40
+ - Run tests
41
+
42
+ ```sh
43
+ bundle exec rspec
44
+ ```
45
+
46
+ ### 2. Running your unpublished Plugin in Logstash
47
+
48
+ #### 2.1 Run in a local Logstash clone
49
+
50
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
51
+ ```ruby
52
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
53
+ ```
54
+ - Install plugin
55
+ ```sh
56
+ bin/logstash-plugin install --no-verify
57
+ ```
58
+ - Run Logstash with your plugin
59
+ ```sh
60
+ bin/logstash -e 'filter {awesome {}}'
61
+ ```
62
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
63
+
64
+ #### 2.2 Run in an installed Logstash
65
+
66
+ 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:
67
+
68
+ - Build your plugin gem
69
+ ```sh
70
+ gem build logstash-filter-awesome.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
75
+ ```
76
+ - Start Logstash and proceed to test the plugin
77
+
78
+ ## Contributing
79
+
80
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
81
+
82
+ 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.
83
+
84
+ It is more important to the community that you are able to contribute.
85
+
86
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,129 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+
5
+ # Sanitize MAC addresses
6
+ #
7
+ # Many logs contain MAC addresses, but they can arrive in several different
8
+ # formats, such as colon-delimited (00:11:22:aa:bb:cc), hyphen-delimited, Cisco
9
+ # format (aabb.ccdd.1122), or just plain hex (001122334455). They can also be
10
+ # uppercase, lowercase or mixed. This is especially problematic in logs such as
11
+ # from a RADIUS server where clients are not all under local control and can
12
+ # contain many different formats. This filter makes it easy to convert MAC
13
+ # addresses into the same pattern to make searching easier.
14
+ #
15
+ # Examples:
16
+ #
17
+ # # Create new field "client_mac_sanitized" with copy of "client_mac" field
18
+ # # in lowercase colon-delimited format.
19
+ # filter {
20
+ # sanitize_mac {
21
+ # match => { "client_mac" => "client_mac_sanitized" }
22
+ # separator => ":"
23
+ # fixcase => "lower"
24
+ # }
25
+ # }
26
+ #
27
+ # # Replace "client_mac" and "server_mac" fields with versions in uppercase
28
+ # # Cisco format.
29
+ # filter {
30
+ # sanitize_mac {
31
+ # match => { "client_mac" => "client_mac"
32
+ # "server_mac" => "server_mac" }
33
+ # separator => "."
34
+ # fixcase => "upper"
35
+ # }
36
+ # }
37
+
38
+ class LogStash::Filters::SanitizeMac < LogStash::Filters::Base
39
+ config_name "sanitize_mac"
40
+
41
+ # Hash of fields to process; key is input field, value is output field.
42
+ # Input and output field may be the same, in which case the value of the field
43
+ # is replaced assuming the data looks like a MAC address and can be sanitized.
44
+ config :match, :validate => :hash, :required => true
45
+
46
+ # MAC address separator for rewritten address; can be any of
47
+ # ":", "-", "." or "".
48
+ config :separator, :validate => :string, :default => "-"
49
+
50
+ # Fix case of MAC address. "lower", "upper" or "" to just leave it alone.
51
+ config :fixcase, :validate => :string, :default => ""
52
+
53
+ public
54
+ def register
55
+ if [":", "-", ".", ""].index(@separator).nil?
56
+ @logger.error("Invalid sanitize_mac configuration. 'separator' must be one of ':', '-', '.' or blank.")
57
+ raise "Bad configuration, aborting."
58
+ end
59
+
60
+ if ["lower", "upper", ""].index(@fixcase).nil?
61
+ @logger.error("Invalid sanitize_mac configuration. 'fixcase' must be one of 'lower', 'upper', blank.")
62
+ raise "Bad configuration, aborting."
63
+ end
64
+
65
+ if @match.nil?
66
+ @logger.error("Invalid sanitize_mac configuration. 'match' must be defined.")
67
+ raise "Bad configuration, aborting."
68
+ end
69
+ end # def register
70
+
71
+ public
72
+ def filter(event)
73
+ return unless filter?(event)
74
+
75
+ @match.keys.each do |field|
76
+ next if event.get(field).nil?
77
+
78
+ # Work out what format the incoming MAC address is in. As well as
79
+ # well-formed addresses, this has to cope with things like missing
80
+ # leading-zeroes, and ensuring that something using mixed delimiters
81
+ # does not parse.
82
+
83
+ # looks colon-delimited?
84
+ if event.get(field) =~ /^(?:[0-9a-f]{1,2}:){5}[0-9a-f]{1,2}$/i
85
+ octets = event.get(field).split(":")
86
+ mac = octets.map { |o| (o.length == 1 ? "0" + o : o) }.join
87
+
88
+ # looks hyphen-delimited?
89
+ elsif event.get(field) =~ /^(?:[0-9a-f]{1,2}-){5}[0-9a-f]{1,2}$/i
90
+ octets = event.get(field).split("-")
91
+ mac = octets.map { |o| (o.length == 1 ? "0" + o : o) }.join
92
+
93
+ # looks cisco dot-delimited?
94
+ elsif event.get(field) =~ /^[0-9a-f]{1,4}\.[0-9a-f]{1,4}\.[0-9a-f]{1,4}$/i
95
+ words = event.get(field).split(".")
96
+ mac = words.map { |o| (o.length < 4 ? ("000" + o)[-4..-1] : o) }.join
97
+
98
+ # last try; could just be 12-digit hex?
99
+ elsif event.get(field) =~ /^[0-9a-f]{12}$/i
100
+ mac = event.get(field)
101
+
102
+ # give up, it doesn't look like a MAC address
103
+ else
104
+ next
105
+ end
106
+
107
+ # verify what we're left with really does look like a mac address
108
+ next unless mac.length == 12
109
+
110
+ # split up into octets (or 16 bits for cisco format)
111
+ if separator == "."
112
+ octets = mac.unpack("A4A4A4")
113
+ else
114
+ octets = mac.unpack("A2A2A2A2A2A2")
115
+ end
116
+
117
+ # push the updated value back
118
+ new_field = @match[field]
119
+ case @fixcase
120
+ when "lower" then event.set(new_field, octets.join(separator).downcase)
121
+ when "upper" then event.set(new_field, octets.join(separator).upcase)
122
+ else event.set(new_field, octets.join(separator))
123
+ end
124
+
125
+ end
126
+
127
+ filter_matched(event)
128
+ end # def filter
129
+ end # class LogStash::Filters::SanitizeMac
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-sanitize_mac'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = 'Logstash filter plugin to sanitize network MAC addresss'
6
+ s.description = 'MAC addresses can come in many different forms. This filter attempts to standardise them to make elasticsearch logs easier to search.'
7
+ s.homepage = 'https://github.com/mcnewton/logstash-filter-sanitize_mac'
8
+ s.authors = ['Matthew Newton']
9
+ s.email = 'matthew-git@newtoncomputing.co.uk'
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-plugin-api", "~> 2.0"
22
+ s.add_development_dependency 'logstash-devutils'
23
+ end
@@ -0,0 +1,117 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+ require "logstash/filters/sanitize_mac"
4
+
5
+ describe LogStash::Filters::SanitizeMac do
6
+ describe "Clean up a MAC address to hyphen and lowercase" do
7
+ let(:config) do <<-CONFIG
8
+ filter {
9
+ sanitize_mac {
10
+ match => { "client_mac" => "client_mac_sanitized" }
11
+ separator => "-"
12
+ fixcase => "lower"
13
+ }
14
+ }
15
+ CONFIG
16
+ end
17
+
18
+ sample("client_mac" => "A98c.c2ff.fe37") do
19
+ expect(subject).to include("client_mac")
20
+ expect(subject.get('client_mac_sanitized')).to eq('a9-8c-c2-ff-fe-37')
21
+ end
22
+
23
+ sample("client_mac" => "11:23:4D:66:77:aB") do
24
+ expect(subject).to include("client_mac")
25
+ expect(subject.get('client_mac_sanitized')).to eq('11-23-4d-66-77-ab')
26
+ end
27
+
28
+ sample("client_mac" => "ad561297fEEd") do
29
+ expect(subject).to include("client_mac")
30
+ expect(subject.get('client_mac_sanitized')).to eq('ad-56-12-97-fe-ed')
31
+ end
32
+ end
33
+
34
+ describe "Clean up a MAC address to colon and same case" do
35
+ let(:config) do <<-CONFIG
36
+ filter {
37
+ sanitize_mac {
38
+ match => { "client_mac" => "client_mac_sanitized" }
39
+ separator => ":"
40
+ fixcase => ""
41
+ }
42
+ }
43
+ CONFIG
44
+ end
45
+
46
+ sample("client_mac" => "A98c.c2ff.fe37") do
47
+ expect(subject).to include("client_mac")
48
+ expect(subject.get('client_mac_sanitized')).to eq('A9:8c:c2:ff:fe:37')
49
+ end
50
+
51
+ sample("client_mac" => "11:23:4D:66:77:aB") do
52
+ expect(subject).to include("client_mac")
53
+ expect(subject.get('client_mac_sanitized')).to eq('11:23:4D:66:77:aB')
54
+ end
55
+
56
+ sample("client_mac" => "ad561297fEEd") do
57
+ expect(subject).to include("client_mac")
58
+ expect(subject.get('client_mac_sanitized')).to eq('ad:56:12:97:fE:Ed')
59
+ end
60
+ end
61
+
62
+ describe "Clean up a MAC address to dots and uppercase" do
63
+ let(:config) do <<-CONFIG
64
+ filter {
65
+ sanitize_mac {
66
+ match => { "client_mac" => "client_mac_sanitized" }
67
+ separator => "."
68
+ fixcase => "upper"
69
+ }
70
+ }
71
+ CONFIG
72
+ end
73
+
74
+ sample("client_mac" => "A98c.c2ff.fe37") do
75
+ expect(subject).to include("client_mac")
76
+ expect(subject.get('client_mac_sanitized')).to eq('A98C.C2FF.FE37')
77
+ end
78
+
79
+ sample("client_mac" => "11:23:4D:66:77:aB") do
80
+ expect(subject).to include("client_mac")
81
+ expect(subject.get('client_mac_sanitized')).to eq('1123.4D66.77AB')
82
+ end
83
+
84
+ sample("client_mac" => "ad561297fEEd") do
85
+ expect(subject).to include("client_mac")
86
+ expect(subject.get('client_mac_sanitized')).to eq('AD56.1297.FEED')
87
+ end
88
+ end
89
+
90
+ describe "Clean up a MAC address to no separator and uppercase" do
91
+ let(:config) do <<-CONFIG
92
+ filter {
93
+ sanitize_mac {
94
+ match => { "client_mac" => "client_mac_sanitized" }
95
+ separator => ""
96
+ fixcase => "upper"
97
+ }
98
+ }
99
+ CONFIG
100
+ end
101
+
102
+ sample("client_mac" => "A98c.c2ff.fe37") do
103
+ expect(subject).to include("client_mac")
104
+ expect(subject.get('client_mac_sanitized')).to eq('A98CC2FFFE37')
105
+ end
106
+
107
+ sample("client_mac" => "11:23:4D:66:77:aB") do
108
+ expect(subject).to include("client_mac")
109
+ expect(subject.get('client_mac_sanitized')).to eq('11234D6677AB')
110
+ end
111
+
112
+ sample("client_mac" => "ad561297fEEd") do
113
+ expect(subject).to include("client_mac")
114
+ expect(subject.get('client_mac_sanitized')).to eq('AD561297FEED')
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-sanitize_mac
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Newton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: logstash-devutils
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: MAC addresses can come in many different forms. This filter attempts
42
+ to standardise them to make elasticsearch logs easier to search.
43
+ email: matthew-git@newtoncomputing.co.uk
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - CONTRIBUTORS
50
+ - DEVELOPER.md
51
+ - Gemfile
52
+ - LICENSE
53
+ - README.md
54
+ - lib/logstash/filters/sanitize_mac.rb
55
+ - logstash-filter-sanitize_mac.gemspec
56
+ - spec/filters/sanitize_mac_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: https://github.com/mcnewton/logstash-filter-sanitize_mac
59
+ licenses:
60
+ - Apache License (2.0)
61
+ metadata:
62
+ logstash_plugin: 'true'
63
+ logstash_group: filter
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.5.2.1
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Logstash filter plugin to sanitize network MAC addresss
84
+ test_files:
85
+ - spec/filters/sanitize_mac_spec.rb
86
+ - spec/spec_helper.rb