logstash-filter-accesswatch 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
+ SHA256:
3
+ metadata.gz: 34157f7c1c0392ebb20368b31fbf0007b79df4109c016e77a107732b4b41fbe2
4
+ data.tar.gz: cf687eb6c068bf62d241b838429ad1972a742e5318e41a27492b7c44d2a97078
5
+ SHA512:
6
+ metadata.gz: b280d78583d33c7768afc4ec852f3bc26da8979c1713df177cb746978d765f59ef361985d5a5e47f3cbc26088f9abd6eacdf1ca5479d7c53e8b488fafc903e68
7
+ data.tar.gz: c5717ab2193781ee5024f5a28f6757b330c1c631f64c11c56192e32107a5bc739efaa3e8cd7a8e6bf71b726b9a2e180d5cc7a98ebf8224dc3145f69cf05ff4fe
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
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.
data/README.md ADDED
@@ -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,132 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+ require 'json'
5
+ require 'set'
6
+ require 'ipaddr'
7
+ require 'interval_tree'
8
+ require 'digest'
9
+
10
+ # The Access Watch filter adds information about robots visiting
11
+ # your website based on data from our robots database.
12
+ #
13
+ # The following fields might be created:
14
+ # [identity][type] "robot" If the visitor is a robot.
15
+ # [reputation][status] string The reputation of the visitor (see below).
16
+ # [robot][id] number A unique robot identifier
17
+ # [robot][name] string A robot's name to display to the user.
18
+ # [robot][url] string A link to the robot's page on the Access Watch database.
19
+ #
20
+ # Access Watch defines the following reputation statuses:
21
+ #
22
+ # nice perfect, as far as we know you can trust this entity
23
+ # ok all right, so far no reason to worry about this entity
24
+ # suspicious warning, nothing really bad, but the entity is on our radar
25
+ # bad danger, there is good reasons to watch or block this entity
26
+ #
27
+ # This filter requires the Access Watch `robots.json` file to run.
28
+ #
29
+
30
+ class LogStash::Filters::Accesswatch < LogStash::Filters::Base
31
+
32
+ config_name "accesswatch"
33
+
34
+ # The path to the Access Watch database file.
35
+ #
36
+ # If not specified, this will default to './robots.json'.
37
+ #
38
+ config :db_path, :validate => :path, :default => "./robots.json"
39
+
40
+ # The field containing the IP address.
41
+ config :ip_source, :validate => :string, :required => true
42
+
43
+ # The field containing the User-Agent string.
44
+ config :ua_source, :validate => :string, :required => true
45
+
46
+ # Transform a CIDR described as a 2-array [start size]
47
+ # into a Ruby 3-dotted range.
48
+ private
49
+ def cidr2range(cidr)
50
+ first = cidr[0]
51
+ last = first + cidr[1]
52
+ (first...last)
53
+ end
54
+
55
+ # Group elements of a collection by each value of a multi-valued attribute
56
+ private
57
+ def group_by_multi(coll, key)
58
+ res = Hash.new {|hash, key| hash[key] = Array.new}
59
+ coll.each {|el|
60
+ if !el[key].nil?
61
+ el[key].each {|val|
62
+ res[val].push(el)
63
+ }
64
+ end
65
+ }
66
+ return res
67
+ end
68
+
69
+ private
70
+ def build_indices(filename)
71
+ file = File.read(filename)
72
+ robots = JSON.parse(file)
73
+ robots.each {|robot|
74
+ if !robot['cidrs'].nil?
75
+ robot['cidrs'] = robot['cidrs'].collect {|cidr| cidr2range(cidr)}
76
+ end
77
+ }
78
+ @ip2robots = group_by_multi(robots, 'ips')
79
+ @cidr2robots = group_by_multi(robots, 'cidrs')
80
+ @ip2cidrs = IntervalTree::Tree.new(@cidr2robots.keys)
81
+ @ua2robots = group_by_multi(robots, 'uas')
82
+ end
83
+
84
+ public
85
+ def register
86
+ build_indices(@db_path)
87
+ end
88
+
89
+ # Take a User-Agent string and an IP address and return a robot description, or nil.
90
+ private
91
+ def detect(ua, ip)
92
+ # Look for robots with the same IP addressor CIDR
93
+ ip_candidates = []
94
+ cidr_candidates = []
95
+ if ip
96
+ i = ip.ipv4? ? ip.ipv4_mapped.to_i : ip.to_i # convert IP to arbitrary length integer
97
+ ip_candidates = @ip2robots[i]
98
+ cidrs = @ip2cidrs.search(i)
99
+ cidr_candidates = cidrs.collect {|cidr| @cidr2robots[cidr]}.reduce([], :concat) unless cidrs.nil?
100
+ end
101
+ # Look for robots with the same User-Agent
102
+ ua_candidates = []
103
+ if ua
104
+ ua_candidates = @ua2robots[Digest::MD5.hexdigest(ua)]
105
+ end
106
+ # Make a final decision
107
+ robots = ((ip_candidates | cidr_candidates) & ua_candidates)
108
+ if !robots.empty?
109
+ robot = robots[0]
110
+ url = "https://access.watch/database/robots/#{robot['reputation']}/#{robot['urlid'] or robot['id']}"
111
+ {'identity' => {'type' => 'robot'},
112
+ 'robot' => {'id' => robot['id'],
113
+ 'name' => robot['name'],
114
+ 'url' => url},
115
+ 'reputation' => {'status' => robot['reputation']}}
116
+ end
117
+ end
118
+
119
+ public
120
+ def filter(event)
121
+ ip_s = event.get(@ip_source)
122
+ ip = IPAddr.new ip_s unless ip_s.nil?
123
+ robot = detect(event.get(@ua_source), ip)
124
+ if robot
125
+ event.set('identity', robot['identity']) unless robot['identity'].nil?
126
+ event.set('robot', robot['robot']) unless robot['robot'].nil?
127
+ event.set('reputation', robot['reputation']) unless robot['reputation'].nil?
128
+ end
129
+ filter_matched(event)
130
+ end
131
+
132
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |s|
3
+ s.name = 'logstash-filter-accesswatch'
4
+ s.version = '0.1.0'
5
+ s.licenses = ['Apache-2.0']
6
+ s.summary = 'The Logstash filter plugin for Access Watch (http://access.watch).'
7
+ s.description = 'The Access Watch filter adds information about robots visiting your website based on data from our robots database.'
8
+ s.homepage = 'http://access.watch'
9
+ s.authors = ['Benoît Fleury']
10
+ s.email = 'benoit@access.watch'
11
+ s.require_paths = ['lib']
12
+
13
+ # Files
14
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','Gemfile','LICENSE']
15
+ # Tests
16
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
17
+
18
+ # Special flag to let us know this is actually a logstash plugin
19
+ s.metadata = { 'logstash_plugin' => 'true',
20
+ 'logstash_group' => 'filter' }
21
+
22
+ # Gem dependencies
23
+ s.add_runtime_dependency 'logstash-core-plugin-api', '~> 2.0'
24
+ s.add_runtime_dependency 'augmented_interval_tree', '~> 0.1.1'
25
+
26
+ s.add_development_dependency 'logstash-devutils', '1.3.3'
27
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-accesswatch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Benoît Fleury
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-18 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'
19
+ name: logstash-core-plugin-api
20
+ prerelease: false
21
+ type: :runtime
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
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: 0.1.1
33
+ name: augmented_interval_tree
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.1
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.3.3
47
+ name: logstash-devutils
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.3
55
+ description: The Access Watch filter adds information about robots visiting your website
56
+ based on data from our robots database.
57
+ email: benoit@access.watch
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - CHANGELOG.md
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - lib/logstash/filters/accesswatch.rb
67
+ - logstash-filter-accesswatch.gemspec
68
+ homepage: http://access.watch
69
+ licenses:
70
+ - Apache-2.0
71
+ metadata:
72
+ logstash_plugin: 'true'
73
+ logstash_group: filter
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.6.11
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: The Logstash filter plugin for Access Watch (http://access.watch).
94
+ test_files: []