logstash-filter-phpipam 0.5.1

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: c59a75c38c05ad39dff02abca85f6cd80984335f72e054f1f1fa89dccca96f5a
4
+ data.tar.gz: f4d8722f7726151514c5a0bb02afcdb11c01b59ec892ee0ab7452699263d6d5f
5
+ SHA512:
6
+ metadata.gz: 6c771f8f94a30d0ec188d2378c8906d0c3d02a0a62886375ec6ef5a2d3bba43532c2fcc1deb851e3976eddca29cb9d177ecf2a4be62af1ad670a59c24b694d51
7
+ data.tar.gz: ff3fe80c111102e8356232559bf7feec7bff26cdedbc3f8bc6bc285dcb65295228f3caf16da60541523726709cd6b9759a1f7d9ce100ead4d6204628dd04924d
@@ -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
+ * -
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-phpipam
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,196 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logstash/filters/base'
4
+
5
+ require 'ipaddr'
6
+ require 'json'
7
+ require 'net/http'
8
+ require 'openssl'
9
+ require 'uri'
10
+
11
+ # A Logstash filter that looks up an IP-address, and returns results from phpIPAM
12
+ class LogStash::Filters::Phpipam < LogStash::Filters::Base
13
+ config_name 'phpipam'
14
+
15
+ # Full host path to connect to, e.g. 'https://phpipam.domain.local:3000'
16
+ config :host, validate: :string, required: true
17
+
18
+ # Application id of the API application (Administration -> API)
19
+ config :app_id, validate: :string, required: true
20
+
21
+ # Username and password to use for the connection
22
+ config :username, validate: :string, default: ''
23
+ config :password, validate: :string, default: ''
24
+
25
+ # Whether to use authentication or not
26
+ config :auth, validate: :boolean, required: true, default: true
27
+
28
+ # IP-address field to look up
29
+ config :source, validate: :string, required: true
30
+
31
+ # Target field to place all values
32
+ config :target, validate: :string, default: 'phpipam'
33
+
34
+ def register
35
+ # Validate auth
36
+ raise LogStash::ConfigurationError, 'Authentication was enabled, but no user/pass found' if @auth && (@username.empty? || @password.empty?)
37
+
38
+ # Get a session token
39
+ @token = send_rest_request('POST', "api/#{@app_id}/user/")['token'] if @auth
40
+
41
+ @target = normalize_target(@target)
42
+ end
43
+
44
+ def filter(event)
45
+ ip = event.get(@source)
46
+
47
+ return unless valid_ip?(ip, event)
48
+
49
+ # Get data from phpIPAM
50
+ event_data = phpipam_data(ip)
51
+
52
+ return if !event_data['error'].nil? && event_data['error']
53
+
54
+ # Set the data to the target path
55
+ event.set(@target, event_data)
56
+
57
+ # filter_matched should go in the last line of our successful code
58
+ filter_matched(event)
59
+ end
60
+
61
+ # make sure @target is in the format [field name] if defined,
62
+ # i.e. not empty and surrounded by brakets
63
+ # @param target: the target to normalize
64
+ # @return [string]
65
+ def normalize_target(target)
66
+ target = "[#{target}]" if target && target !~ %r{^\[[^\[\]]+\]$}
67
+ target
68
+ end
69
+
70
+ # Validates a IP-address
71
+ # @param ip: an IP-address
72
+ # @param event: The Logstash event variable
73
+ # @return [bool]
74
+ def valid_ip?(ip, event)
75
+ IPAddr.new(ip)
76
+
77
+ @logger.debug? && @logger.debug('Valid IP', ip: ip)
78
+
79
+ # Return true. Rescue would take over if a non-valid IP was parsed
80
+ true
81
+ rescue StandardError
82
+ @logger.debug? && @logger.debug('NOT a valid IP', ip: ip)
83
+ event.tag('_phpipam_invalid_ip')
84
+ false
85
+ end
86
+
87
+ # Sends a GET method REST request.
88
+ # @param method: which HTTP method to use (POST, GET)
89
+ # @param url_path: path to connect to
90
+ # @param basic_auth: whether to use basic_auth or not
91
+ # @return [hash]
92
+ def send_rest_request(method, url_path)
93
+ @logger.debug? && @logger.debug('Sending request', host: @host, path: url_path)
94
+
95
+ url = URI("#{@host}/#{url_path}")
96
+
97
+ http = Net::HTTP.new(url.host, url.port)
98
+ http.use_ssl = url.scheme == 'https'
99
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
100
+
101
+ request = case method
102
+ when 'POST' then Net::HTTP::Post.new(url)
103
+ when 'GET' then Net::HTTP::Get.new(url)
104
+ end
105
+
106
+ request['accept'] = 'application/json'
107
+ request['content-type'] = 'application/json'
108
+ request['phpipam-token'] = @token unless @token.nil?
109
+ request.basic_auth(@username, @password) if @token.nil? && @auth
110
+
111
+ begin
112
+ response = http.request(request)
113
+ rescue StandardError
114
+ raise LogStash::ConfigurationError, I18n.t(
115
+ 'logstash.runner.configuration.invalid_plugin_register',
116
+ plugin: 'filter',
117
+ type: 'phpipam',
118
+ error: 'Could not connect to configured host',
119
+ )
120
+ end
121
+
122
+ # Parse the body
123
+ rsp = JSON.parse(response.body)
124
+
125
+ # Raise an error if not a code 200 is returned
126
+ raise LogStash::ConfigurationError, "#{rsp['code']}:#{rsp['message']}" if rsp['code'] != 200
127
+
128
+ # Return error if no data field is present, else return the data
129
+ rsp = if rsp['data'].nil?
130
+ { 'error' => true }
131
+ else
132
+ rsp['data'].is_a?(Array) ? rsp['data'][0] : rsp['data']
133
+ end
134
+
135
+ @logger.debug? && @logger.debug('Got response', body: response.body, data: rsp)
136
+ rsp
137
+ end
138
+
139
+ # Checks whether the value is nil or empty
140
+ # @param value: a value to check
141
+ # @return [bool]
142
+ def nil_or_empty?(value)
143
+ value.nil? || value.empty?
144
+ end
145
+
146
+ # Queries phpIPAM and formats the data
147
+ # @param ip: an IP-address to query
148
+ # @return [hash]
149
+ def phpipam_data(ip)
150
+ # Fetch base data needed from phpIPAM
151
+ ip_data = send_rest_request('GET', "api/#{@app_id}/addresses/search/#{ip}/")
152
+
153
+ # If the IP wasn't found, return and do nuthin'
154
+ return { 'error' => true } if !ip_data['error'].nil? && ip_data['error']
155
+
156
+ subnet_data = send_rest_request('GET', "api/#{@app_id}/subnets/#{ip_data['subnetId']}/") unless nil_or_empty?(ip_data['subnetId'])
157
+ vlan_data = send_rest_request('GET', "api/#{@app_id}/vlans/#{subnet_data['vlanId']}/") unless nil_or_empty?(subnet_data['vlanId'])
158
+
159
+ # Base hash to format data in
160
+ base = {
161
+ 'ip' => {},
162
+ }
163
+
164
+ # IP information
165
+ base['ip']['id'] = ip_data['id'].to_i
166
+ base['ip']['address'] = ip_data['ip']
167
+ base['ip']['description'] = ip_data['description'] unless nil_or_empty?(ip_data['description'])
168
+ base['ip']['hostname'] = ip_data['hostname'] unless nil_or_empty?(ip_data['hostname'])
169
+ base['ip']['mac'] = ip_data['mac'] unless nil_or_empty?(ip_data['mac'])
170
+ base['ip']['note'] = ip_data['note'] unless nil_or_empty?(ip_data['note'])
171
+ base['ip']['owner'] = ip_data['owner'] unless nil_or_empty?(ip_data['owner'])
172
+
173
+ # Subnet information
174
+ if !defined?(subnet_data).nil? && subnet_data['error'].nil?
175
+ base['subnet'] = {}
176
+ base['subnet']['id'] = ip_data['subnetId'].to_i
177
+ base['subnet']['section_id'] = subnet_data['sectionId'].to_i
178
+ base['subnet']['bitmask'] = subnet_data['calculation']['Subnet bitmask'].to_i
179
+ base['subnet']['wildcard'] = subnet_data['calculation']['Subnet wildcard']
180
+ base['subnet']['netmask'] = subnet_data['calculation']['Subnet netmask']
181
+ base['subnet']['network'] = subnet_data['calculation']['Network']
182
+ end
183
+
184
+ # VLAN information
185
+ if !defined?(vlan_data).nil? && vlan_data['error'].nil?
186
+ base['vlan'] = {}
187
+ base['vlan']['id'] = subnet_data['vlanId'].to_i
188
+ base['vlan']['number'] = vlan_data['number'].to_i unless nil_or_empty?(vlan_data['number'])
189
+ base['vlan']['name'] = vlan_data['name'] unless nil_or_empty?(vlan_data['name'])
190
+ base['vlan']['description'] = vlan_data['description'] unless nil_or_empty?(vlan_data['description'])
191
+ end
192
+
193
+ # all your base are belong to us
194
+ base
195
+ end
196
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'logstash-filter-phpipam'
5
+ s.version = '0.5.1'
6
+ s.licenses = ['Apache-2.0']
7
+ s.summary = 'A Logstash filter that returns results from phpIPAM'
8
+ s.description = 'A Logstash filter that looks up an IP-address, and returns results from phpIPAM'
9
+ s.homepage = 'https://github.com/magnuslarsen/logstash-filter-phpipam'
10
+ s.authors = ['magnuslarsen']
11
+ s.email = ''
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_development_dependency 'logstash-devutils'
25
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ # I don't know how to rspec this...
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logstash/devutils/rspec/spec_helper'
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-phpipam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - magnuslarsen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-31 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: A Logstash filter that looks up an IP-address, and returns results from
42
+ phpIPAM
43
+ email: ''
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/phpipam.rb
55
+ - logstash-filter-phpipam.gemspec
56
+ - spec/filters/phpipam_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: https://github.com/magnuslarsen/logstash-filter-phpipam
59
+ licenses:
60
+ - Apache-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
+ rubygems_version: 3.0.4
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: A Logstash filter that returns results from phpIPAM
83
+ test_files:
84
+ - spec/filters/phpipam_spec.rb
85
+ - spec/spec_helper.rb