logstash-filter-ipinfo-db 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: 7d4b15b3ce79a6ecd0b011923889d32b44641fba964e95c9c0f74876a912adf7
4
+ data.tar.gz: 0d82645149e34368e0fe2e6f413056e7e7d85138b513fed4897b9839753d99d6
5
+ SHA512:
6
+ metadata.gz: 22039f0817ef442d79a460705a0b4182f4af4f0b579085ba9840db049cc6972b39ddd6223df3f51fc0fab2f57b3daf0109274970ff4310ec78b45b6590cc2ccc
7
+ data.tar.gz: c00630b0b205cd8abf935700331011a3a5e35b3df46208411f6fd9b74ea352d093cfd38e95fe7a63ab10679677b3aec486fdab91d3386382493fd546950e8e16
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ## 0.1.0
2
+
3
+ - Plugin created with the logstash plugin generator
4
+ - Initial devlopment
5
+ - Test case for plugin
6
+ - Documentation
7
+ - Review Preparation
8
+
data/CONTRIBUTORS ADDED
@@ -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
+ * Muhammad Awais Murad - awais@ipinfo.io
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.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-filter-ipinfo-db
2
+ Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ gem 'maxmind-db'
5
+ gem 'down'
6
+ logstash_path = ENV['LOGSTASH_PATH'] || '/usr/share/logstash'
7
+
8
+ if Dir.exist?(logstash_path)
9
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
10
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
11
+ end
12
+
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/main/CONTRIBUTING.md) file.
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require 'down'
4
+ require 'maxmind/db'
5
+
6
+ $MMDB_FILE_DOWNLOAD_URL = "https://ipinfo.io/data/free/country_asn.mmdb?token=c0c038dbe0e4e7"
7
+ $MMDB_FILE_NAME = "country_asn.mmdb"
8
+
9
+ # This ipinfo-db filter will replace the contents of the default
10
+ # message field with whatever you specify in the configuration.
11
+ #
12
+ # It is only intended to be used as an ipinfo-db.
13
+ class LogStash::Filters::IpinfoDb < LogStash::Filters::Base
14
+
15
+ # Setting the config_name here is required. This is how you
16
+ # configure this filter from your Logstash config.
17
+ #
18
+ # filter {
19
+ # ipinfo-db {
20
+ # source => "ip"
21
+ # }
22
+ # }
23
+ #
24
+ config_name "ipinfo-db"
25
+
26
+ # The field containing the IP address or hostname to map via ipinfo.io.
27
+ config :source, :validate => :string, :required => true
28
+
29
+ # The field to write the lookup results into. If not specified, the default
30
+ # value will be used.
31
+ config :target, :validate => :string, :default => "ipinfo-db"
32
+
33
+ # Tags the event on failure to look up ipinfo information. This can be used in later analysis.
34
+ # If not specified, the default value will be used.
35
+ config :tag_on_failure, :validate => :array, :default => ["_ipinfo_db_lookup_failure"]
36
+
37
+ public
38
+ def register
39
+ setup_database
40
+ end # def register
41
+
42
+ public
43
+ def filter(event)
44
+ @logger.debug? && @logger.debug("Running ipinfo-db lookup", :event => event)
45
+ ip = event.get(@source)
46
+
47
+ begin
48
+ ret = @db.get(ip)
49
+ event.set(@target, ret.to_hash)
50
+ filter_matched(event)
51
+ rescue => e
52
+ tag_unsuccessful_lookup(event)
53
+ end
54
+ end
55
+
56
+ def tag_unsuccessful_lookup(event)
57
+ @logger.debug? && @logger.debug("IP #{event.get(@source)} was not found in the database", :event => event)
58
+ @tag_on_failure.each{|tag| event.tag(tag)}
59
+ end
60
+
61
+ # Download .mmdb file and load it into mmdb reader
62
+ def setup_database
63
+ # Download mmdb file from ipinfo.io if not found
64
+ if !File.exist?($MMDB_FILE_NAME)
65
+ tempfile = Down.download($MMDB_FILE_DOWNLOAD_URL)
66
+ FileUtils.mv(tempfile.path, "./#{tempfile.original_filename}")
67
+ end
68
+ # Load .mmdb file in MaxMindDB reader
69
+ @db = MaxMind::DB.new($MMDB_FILE_NAME, mode: MaxMind::DB::MODE_MEMORY)
70
+ end
71
+
72
+ end # class LogStash::Filters::IpinfoDB
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-ipinfo-db'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'Logstash Filter Plugin for IPinfoDb'
6
+ s.description = ''
7
+ s.homepage = 'https://ipinfo.io'
8
+ s.authors = ['Muhammad Awais Murad']
9
+ s.email = 'awais@ipinfo.io'
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_runtime_dependency "down"
23
+ s.add_runtime_dependency "maxmind-db"
24
+ s.add_development_dependency 'logstash-devutils'
25
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+ require "logstash/filters/ipinfo-db"
4
+
5
+ describe LogStash::Filters::IpinfoDb do
6
+ describe "ipinfo lookup test" do
7
+ config <<-CONFIG
8
+ filter {
9
+ ipinfo-db {
10
+ source => "ip"
11
+ }
12
+ }
13
+ CONFIG
14
+
15
+ sample("ip" => "173.9.34.107") do
16
+ expect(subject.get("[ipinfo-db][country]")).to eq('US')
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-ipinfo-db
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Muhammad Awais Murad
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-10 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'
33
+ name: down
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ name: maxmind-db
48
+ prerelease: false
49
+ type: :runtime
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ name: logstash-devutils
62
+ prerelease: false
63
+ type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: ''
70
+ email: awais@ipinfo.io
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - CHANGELOG.md
76
+ - CONTRIBUTORS
77
+ - DEVELOPER.md
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - lib/logstash/filters/ipinfo-db.rb
82
+ - logstash-filter-ipinfo-db.gemspec
83
+ - spec/filters/ipinfo-db_spec.rb
84
+ - spec/spec_helper.rb
85
+ homepage: https://ipinfo.io
86
+ licenses:
87
+ - Apache-2.0
88
+ metadata:
89
+ logstash_plugin: 'true'
90
+ logstash_group: filter
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.7.10
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Logstash Filter Plugin for IPinfoDb
111
+ test_files:
112
+ - spec/filters/ipinfo-db_spec.rb
113
+ - spec/spec_helper.rb