logstash-filter-weblookup 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
+ SHA256:
3
+ metadata.gz: a1c310e4e0feb0bf4c56096f6e5bad6a720df01f4ebd66f868b5ddc97d0052c6
4
+ data.tar.gz: 37b23ae97012cf03a00b9c07226da8ee51d363b546ad21978e3a4b795b68d597
5
+ SHA512:
6
+ metadata.gz: 8151672a15f02a90c590de31ded5968896e68b1e5aaf6809beae5e8f803629c749c5949dacd5c8c5a77fbceca847d40c74d7c4a5c6fd86c2e776b9085694dbbd
7
+ data.tar.gz: b48a47c202874450f09d6d1e5390cf4ac123917ecf48283b4271e87b0eb31d19c1fd767b14cf9ef09299cd7a88ddc4bdf5e01305ddbdbe741ef4ff3787c0b4ca
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
@@ -0,0 +1,10 @@
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
+ * Jan Geertsma - jan@janmg.com
6
+
7
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
8
+ Logstash, and you aren't on the list above and want to be, please let us know
9
+ and we'll make sure you're here. Contributions from folks like you are what make
10
+ open source awesome.
@@ -0,0 +1,2 @@
1
+ # logstash-filter-lookup
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,180 @@
1
+ # encoding: utf-8
2
+ require 'logstash/filters/base'
3
+ require 'redis'
4
+ require 'json'
5
+ require 'net/http'
6
+ require 'connection_pool'
7
+ require 'addressable/uri'
8
+
9
+ class LogStash::Filters::Webookup < LogStash::Filters::Base
10
+
11
+ # This is how you configure this filter from your Logstash config.
12
+ # [source,ruby]
13
+ # ----------------------------------
14
+ # filter {
15
+ # weblookup {
16
+ # fields => ["ClientIP"]
17
+ # url => "http://127.0.0.1/ripe.php?ip=<item>&TOKEN=token"
18
+ # list => {
19
+ # '127.0.0.1' => '{"ip":"127.0.0.1", "subnet":"127.0.0.0/8", "netname":"localnet", "hostname":"localhost"}',
20
+ # '192.168.0.1' => '{"ip":"192.168.0.1", "subnet":"192.168.0.0/24", "netname":"private", "hostname":"router"}'
21
+ # }
22
+ # use_redis => false
23
+ # }
24
+ # }
25
+ # ----------------------------------
26
+ #
27
+ config_name "weblookup"
28
+
29
+ config :fields, :validate => :array, :required => true, :default => ["ClientIP"]
30
+ # {"ip":"8.8.8.8","netname":"Google","subnet":"8.8.8.0\/24","hostname":"google-public-dns-a.google.com"}
31
+ # In the query parameter has the <ip> tag will be replaced by the IP address to lookup, other parameters are optional and according to your lookup service.
32
+ config :destinations, :validate => :array, :required => false, :default => ["message"]
33
+
34
+ # {"ip":"8.8.8.8","netname":"Google","subnet":"8.8.8.0\/24","hostname":"google-public-dns-a.google.com"}
35
+ # In the query parameter has the <ip> tag will be replaced by the IP address to lookup, other parameters are optional and according to your lookup service.
36
+ config :url, :validate => :string, :required => false, :default => "http://localhost/ripe.php?ip=<item>&TOKEN=token"
37
+
38
+ # Optional ruby hash with the key as a string and the value as a string in the form of a JSON object. These key's will not be looked up.
39
+ config :list, :validate => :hash, :required => false
40
+
41
+ # Optional Redis IP cache
42
+ config :use_redis, :validate => :boolean, :required => false, :default => false
43
+ config :redis_path, :validate => :string, :required => false
44
+ config :redis_expiry, :validate => :number, :required => false, :default => 604800
45
+ config :normalize, :validate => :boolean, :required => false, :default => false
46
+
47
+ HTTP_OPTIONS = {
48
+ keep_alive_timeout: 300
49
+ }
50
+
51
+ public
52
+ def register
53
+ if use_redis
54
+ unless redis_path.to_s.strip.empty?
55
+ @red = Redis.new(path: redis_path)
56
+ else
57
+ @red = Redis.new()
58
+ end
59
+ end
60
+
61
+ # input fields and destinations
62
+ @is_one_destination=false
63
+ if destinations.size == 1
64
+ @logger.info("one destination found, it is #{destinations[0]}")
65
+ @is_one_destination=true
66
+ else
67
+ if destinations.size != fields.size
68
+ @logger.error("Configuration error, there must be an equal amount of destinations and fields, defaulting to using the field as a root for the new values. e.g. if the lookup is done on the value of [\"ClientIP\"] the destination will be [\"ClientIP\"][\"Key\"]")
69
+ destinations=fields
70
+ end
71
+ end
72
+
73
+ # http connectionpool
74
+ @uri = Addressable::URI.parse(url)
75
+ @uri.merge!(HTTP_OPTIONS)
76
+ #@http = Net::HTTP.new(uri.host, uri.port, HTTP_OPTIONS)
77
+ @uri.port=80 if (@uri.port.nil? && @uri.scheme=="http")
78
+ @uri.port=443 if (@uri.port.nil? && @uri.scheme=="https")
79
+ # find the key where the value is <item>, otherwise just use the value
80
+ @params = @uri.query_values(Hash)
81
+ @params.each do |key, value|
82
+ if value="\<item\>"
83
+ @ip=key
84
+ @params.delete(key)
85
+ logger.info("the ip key in the uri is #{@ip}")
86
+ end
87
+ end
88
+ @connpool = ConnectionPool.new(size: 4, timeout: 180) {
89
+ Net::HTTP.new(@uri.host, @uri.port)
90
+ }
91
+ end # def register
92
+
93
+ def filter(event)
94
+ if destinations[0] == "srcdst"
95
+ # ... do special sauce
96
+ src = parse(event.get(fields[0]).to_s)
97
+ dst = parse(event.get(fields[1]).to_s)
98
+ srcdst = { :srcnet => src["netname"], :srchost => src["hostname"], :dstnet => dst["netname"], :dsthost => dst["hostname"] }
99
+ event.set("srcdst", srcdst)
100
+ event.get("[srcdst]").each {|k, v| event.set(k, v) }
101
+ event.remove("[srcdst]")
102
+ @logger.trace("processed: #{event.get(fields[0]).to_s} #{src} #{event.get(fields[1]).to_s} #{dst} #{srcdst}")
103
+ else
104
+ fields.each_with_index do |field, index|
105
+ # @logger.info(event.get("["+field+"]"))
106
+ json = parse(event.get(field).to_s)
107
+ event.set("["+destinations[index]+"]", json)
108
+ end
109
+ end
110
+ # filter_matched should go in the last line of our successful code
111
+ filter_matched(event)
112
+ end # def filter
113
+
114
+
115
+
116
+ private
117
+ def parse(field)
118
+ x = find(field)
119
+ begin
120
+ json = JSON.parse(x)
121
+ rescue JSON::ParserError
122
+ json = x
123
+ end
124
+ end
125
+
126
+ def find(item)
127
+ res = nil
128
+ # Is item in list? (list is an optional hash)
129
+ unless list.nil?
130
+ return list[item]
131
+ end
132
+ # Is item in redis?
133
+ unless @red.nil?
134
+ res = @red.get(item)
135
+ unless res.nil?
136
+ return res
137
+ end
138
+ end
139
+
140
+ # find the key where the value is <item>, otherwise just use the value
141
+ current_uri = @uri
142
+ current_uri.query_values = @params.merge({@ip => item})
143
+ #logger.info(@uri.to_s)
144
+ @connpool.with do |conn|
145
+ res = conn.request_get(current_uri).read_body
146
+ #logger.info(res.to_s)
147
+ unless @red.nil?
148
+ @red.set(item, res)
149
+ @red.expire(item,redis_expiry)
150
+ end
151
+ end
152
+ return res
153
+ end
154
+
155
+ def normalize(event)
156
+ event.set("net", JSON.parse(net))
157
+ event.get("[records][properties]").each {|k, v| event.set(k, v) }
158
+ event.remove("[records]")
159
+ event.remove("[message]")
160
+ return event
161
+ end
162
+
163
+ # From https://github.com/angel9484/logstash-filter-lookup
164
+ def json_loader(data)
165
+ get_map.merge!(JSON.parse(File.read(data)))
166
+ end
167
+
168
+ def csv_loader(data)
169
+ data = CSV.read(data).inject(Hash.new) do |acc, v|
170
+ acc[v[0]] = v[1]
171
+ acc
172
+ end
173
+ get_map.merge!(data)
174
+ end
175
+
176
+ def yml_loader(data)
177
+ get_map.merge!(YAML.load_file(data))
178
+ end
179
+
180
+ end # class LogStash::Filters::Lookup
@@ -0,0 +1,27 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-weblookup'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'This logstash filter plugin takes one or more fields and enriches with a lookup value from a list, redis cache or webservice'
6
+ s.description = <<-EOF
7
+ This gem is a Logstash plugin. During filter it takes one or more fields and uses that as input to query additional information. The original purpose is to enrich IP addresses with matching subnet, netname and hostname, but it is generic so that any field can be looked up. The function is similar to the translate filter's dictionary lookup, which supports files and regex. The jdbc_streaming filter plugin is also very useful if the data resides in a database. This plugins features are web based lookups and redis caching, for fast lookups.
8
+ EOF
9
+ s.homepage = 'https://github.com/janmg/logstash-filter-weblookup'
10
+ s.authors = ['Jan Geertsma']
11
+ s.email = 'jan@janmg.com'
12
+ s.require_paths = ['lib']
13
+
14
+ # Files
15
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
16
+ # Tests
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+
19
+ # Special flag to let us know this is actually a logstash plugin
20
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
21
+
22
+ # Gem dependencies
23
+ s.add_runtime_dependency 'logstash-core-plugin-api', '~> 2.0'
24
+ s.add_runtime_dependency 'connection_pool', '~> 2.2'
25
+ #s.add_runtime_dependency 'addressable', '~> 2.3.8'
26
+ s.add_development_dependency 'logstash-devutils', '~> 0'
27
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+ require 'logstash/filters/lookup'
4
+ require 'redis'
5
+
6
+ class LogStash::Codecs::JSON
7
+ end
8
+
9
+ describe LogStash::Filters::Lookup do
10
+ describe "test ip lookup" do
11
+ let(:config) do <<-CONFIG
12
+ filter {
13
+ lookup {
14
+ fields => ["ClientIP"]
15
+ list => {
16
+ '127.0.0.1' => '{"ip":"127.0.0.1", "subnet":"127.0.0.0/8", "netname":"localnet", "hostname"="localhost"}'
17
+ '192.168.0.1' => '{"ip":"192.168.0.1", "subnet":"192.168.0.0/24", "netname":"private", "hostname":"router"}'
18
+ }
19
+ }
20
+ }
21
+ CONFIG
22
+ end
23
+
24
+ message = '{"ClientIP" : "23.100.57.65"}'
25
+ sample("message" => message) do
26
+ #insist { subject["clientIP"][0].to_s } == "192.168.0.1"
27
+ insist { find(subject["ClientIP"]) } == '{"ip":"192.168.0.1", "subnet":"192.168.0.0/24", "netname":"private", "hostname":"router"}'
28
+ end
29
+
30
+ sample("message" => message) do
31
+ #ip = subject[fields][0].to_s
32
+ ip = '23.100.57.65'
33
+ @red = Redis.new
34
+ @red.del(ip)
35
+ insist { find(ip) } == '{"ip":"23.100.57.65","netname":"Azure europenorth","subnet":"23.100.48.0\/20","hostname":"monitoring"}'
36
+ @red.del(ip)
37
+ @red.add(ip,'{"ip":"23.100.57.65","netname":"dummy"}')
38
+ insist { find(ip) } == '{"ip":"23.100.57.65","netname":"dummy"}'
39
+ @red.del(ip)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+ require "logstash/filters/lookup"
4
+
5
+ describe LogStash::Filters::Lookup do
6
+ describe "Set to Hello World" do
7
+ let(:config) do <<-CONFIG
8
+ filter {
9
+ lookup {
10
+ message => "Hello World"
11
+ }
12
+ }
13
+ CONFIG
14
+ end
15
+
16
+ sample("message" => "some text") do
17
+ expect(subject).to include("message")
18
+ expect(subject.get('message')).to eq('Hello World')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-weblookup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan Geertsma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-13 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: '2.2'
33
+ name: connection_pool
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
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: '0'
55
+ description: " This gem is a Logstash plugin. During filter it takes one or more fields\
56
+ \ and uses that as input to query additional information. The original purpose is\
57
+ \ to enrich IP addresses with matching subnet, netname and hostname, but it is generic\
58
+ \ so that any field can be looked up. The function is similar to the translate filter's\
59
+ \ dictionary lookup, which supports files and regex. The jdbc_streaming filter plugin\
60
+ \ is also very useful if the data resides in a database. This plugins features are\
61
+ \ web based lookups and redis caching, for fast lookups.\n"
62
+ email: jan@janmg.com
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - CHANGELOG.md
68
+ - CONTRIBUTORS
69
+ - DEVELOPER.md
70
+ - Gemfile
71
+ - LICENSE
72
+ - README.md
73
+ - lib/logstash/filters/weblookup.rb
74
+ - logstash-filter-weblookup.gemspec
75
+ - spec/filters/lookup_spec.rb
76
+ - spec/filters/lookup_spec.rb-orig
77
+ - spec/spec_helper.rb
78
+ homepage: https://github.com/janmg/logstash-filter-weblookup
79
+ licenses:
80
+ - Apache-2.0
81
+ metadata:
82
+ logstash_plugin: 'true'
83
+ logstash_group: filter
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.7.9
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: This logstash filter plugin takes one or more fields and enriches with a
104
+ lookup value from a list, redis cache or webservice
105
+ test_files:
106
+ - spec/filters/lookup_spec.rb
107
+ - spec/filters/lookup_spec.rb-orig
108
+ - spec/spec_helper.rb