logstash-filter-webservicemap 1.0.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
+ SHA1:
3
+ metadata.gz: f551159dee54bc51b368451231a8d711f91434e4
4
+ data.tar.gz: 0d42fc1b403e8a8754bf3f668213360c28f749da
5
+ SHA512:
6
+ metadata.gz: a2d21e0dc309f1345247ceb3ea0fe53108d23e69cf9e2ed5a36763d7deac40847e4c81c0e92abe4d0fb954b1a5b41613d7e58891e820a533ec2bd29427f3cf89
7
+ data.tar.gz: f3f4f3c48037e73455eafb66b640b7dd0eab8ba07294dbd4bf23ec8a1918bc48bfa7489978b18d3da0180aa337ae614eab686af7eff263bdab45a033a87e2e04
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 1.0.0
2
+ -
data/CONTRIBUTORS ADDED
@@ -0,0 +1,15 @@
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
+ Based on logstash-filter-translate
5
+ Forked from the pull Request for logstash-filter-translate and separated from there.
6
+ credits to: https://github.com/logstash-plugins/logstash-filter-translate
7
+ the PR that never merged to: https://github.com/logstash-plugins/logstash-filter-translate/pull/7
8
+
9
+ Contributors:
10
+ * Angel Luis Lopez Monterroso (angel9484)
11
+
12
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
13
+ Logstash, and you aren't on the list above and want to be, please let us know
14
+ and we'll make sure you're here. Contributions from folks like you are what make
15
+ open source awesome.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ group :test, :development do
5
+ gem 'webmock'
6
+ end
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012–2015 Elasticsearch <http://www.elastic.co>
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/NOTICE.TXT ADDED
@@ -0,0 +1,5 @@
1
+ Elasticsearch
2
+ Copyright 2012-2015 Elasticsearch
3
+
4
+ This product includes software developed by The Apache Software
5
+ Foundation (http://www.apache.org/).
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/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/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,172 @@
1
+ # encoding: utf-8
2
+ require 'logstash/filters/base'
3
+ require 'logstash/namespace'
4
+ require 'open-uri'
5
+ require 'digest/sha1'
6
+ require 'json'
7
+ require 'csv'
8
+
9
+ # A general search and replace tool which uses a Web service with a YAML, CSV or JSON response to determine replacement values.
10
+ #
11
+ # The map entries can be specified with a Web service who your request produces a YML, CSV or JSON response.
12
+ #
13
+ # Operationally, if the event field specified in the `field` configuration
14
+ # matches the EXACT contents of a map entry key, the field's value will be substituted
15
+ # with the matched key's value from the map.
16
+ #
17
+ # By default, the webservicemap filter will replace the contents of the
18
+ # maching event field (in-place). However, by using the `destination`
19
+ # configuration item, you may also specify a target event field to
20
+ # populate with the new mapd value.
21
+
22
+ class LogStash::Filters::WebServiceMap < LogStash::Filters::Base
23
+ config_name "webservicemap"
24
+
25
+ # The name of the logstash event field containing the value to be compared for a
26
+ # match by the map filter (e.g. `message`, `host`, `response_code`).
27
+ #
28
+ # If this field is an array, only the first value will be used.
29
+ config :field, :validate => :string, :required => true
30
+
31
+ # If the destination (or target) field already exists, this configuration item specifies
32
+ # whether the filter should skip mapping (default) or overwrite the target field
33
+ # value with the new mapping value.
34
+ config :override, :validate => :boolean, :default => false
35
+
36
+ # The full URI path of a Web service who generates an yml format response.
37
+ config :map_url, :validate => :string
38
+
39
+ # When using a map file or url, this setting will indicate how frequently
40
+ # (in seconds) logstash will check the YAML file or url for updates.
41
+ config :refresh_interval, :validate => :number, :default => 300
42
+
43
+ # The destination field you wish to populate with the mapd code. The default
44
+ # is a field named `mapping`. Set this to the same value as source if you want
45
+ # to do a substitution, in this case filter will allways succeed. This will clobber
46
+ # the old value of the source field!
47
+ config :destination, :validate => :string, :default => "mapping"
48
+
49
+ # In case no mapping occurs in the event (no matches), this will add a default
50
+ # mapping string, which will always populate `field`, if the match failed.
51
+ #
52
+ # For example, if we have configured `fallback => "no match"`, using this map:
53
+ # [source,ruby]
54
+ # foo: bar
55
+ #
56
+ # Then, if logstash received an event with the field `foo` set to `bar`, the destination
57
+ # field would be set to `bar`. However, if logstash received an event with `foo` set to `nope`,
58
+ # then the destination field would still be populated, but with the value of `no match`.
59
+ # This configuration can be dynamic and include parts of the event using the `%{field}` syntax.
60
+ config :fallback, :validate => :string
61
+
62
+ def get_map
63
+ @my_map
64
+ end
65
+
66
+ def set_map(map)
67
+ @my_map = map;
68
+ end
69
+
70
+ public
71
+ def register
72
+ @my_map = {}
73
+ @next_refresh = Time.now + @refresh_interval
74
+ download_ws(@map_url, true)
75
+ @logger.debug? and @logger.debug("#{self.class.name}: map - ", :map => get_map)
76
+ type = 'Exact'
77
+ @logger.debug? and @logger.debug("#{self.class.name}: map mapping method - "+type)
78
+ end
79
+
80
+ # def register
81
+
82
+ def json_loader(data)
83
+ get_map.merge!(JSON.parse(File.read(data)))
84
+ end
85
+
86
+ def csv_loader(data)
87
+ data = CSV.read(data).inject(Hash.new) do |acc, v|
88
+ acc[v[0]] = v[1]
89
+ acc
90
+ end
91
+ get_map.merge!(data)
92
+ end
93
+
94
+ def yml_loader(data)
95
+ get_map.merge!(YAML.load_file(data))
96
+ end
97
+
98
+ def load_file(registering, extension, data)
99
+ begin
100
+ if extension.equal?('.json')
101
+ return json_loader(data)
102
+ elsif extension.end_with?('.csv')
103
+ return csv_loader(data)
104
+ end
105
+ yml_loader(data)
106
+ rescue Exception => _
107
+ if registering
108
+ raise "#{self.class.name}: Bad Syntax in map file #{file_name}"
109
+ else
110
+ @logger.warn("#{self.class.name}: Bad Syntax in map file, continuing with old map", :map_path => file_name)
111
+ end
112
+ end
113
+ end
114
+
115
+ def get_extension(path)
116
+ if path.end_with?('.json')
117
+ return '.json'
118
+ elsif path.end_with?('.csv')
119
+ return '.csv'
120
+ end
121
+ '.yml'
122
+ end
123
+
124
+ public
125
+ def download_ws(path, registering=false)
126
+ extension = get_extension(path)
127
+ temp_extension = '_temp'+extension;
128
+ file_name = Digest::SHA1.hexdigest path
129
+ File.open(file_name+temp_extension, 'wb') do |saved_file|
130
+ open(path, 'rb') do |read_file|
131
+ saved_file.write(read_file.read)
132
+ end
133
+ end
134
+ begin
135
+ load_file(registering, extension, file_name+temp_extension)
136
+ FileUtils.mv(file_name+temp_extension, file_name+extension)
137
+ rescue Exception => _
138
+ FileUtils.rm_f(file_name+temp_extension)
139
+ end
140
+ end
141
+
142
+ # def download_yaml
143
+
144
+ public
145
+ def filter(event)
146
+ if @next_refresh < Time.now
147
+ download_ws(@map_url)
148
+ @next_refresh = Time.now + @refresh_interval
149
+ @logger.info('downloading and refreshing map file')
150
+ end
151
+
152
+ return unless event.include?(@field) # Skip mapping in case event does not have @event field.
153
+ return if event.include?(@destination) and not @override # Skip mapping in case @destination field already exists and @override is disabled.
154
+
155
+ begin
156
+ source = event[@field].is_a?(Array) ? event[@field].first.to_s : event[@field].to_s
157
+ matched = false
158
+ if get_map.include?(source)
159
+ event[@destination] = get_map[source]
160
+ matched = true
161
+ end
162
+
163
+ if not matched and @fallback
164
+ event[@destination] = event.sprintf(@fallback)
165
+ matched = true
166
+ end
167
+ filter_matched(event) if matched or @field == @destination
168
+ rescue Exception => e
169
+ @logger.error('Something went wrong when attempting to map from my_map', :exception => e, :field => @field, :event => event)
170
+ end
171
+ end # def filter
172
+ end # class LogStash::Filters::WebServiceMap
@@ -0,0 +1,27 @@
1
+ Gem::Specification.new do |s|
2
+
3
+ s.name = 'logstash-filter-webservicemap'
4
+ s.version = '1.0.0'
5
+ s.licenses = ['Apache License (2.0)']
6
+ s.summary = "A general search and replace tool which uses a configured web service to determine replacement values."
7
+ s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
8
+ s.authors = ["Elastic"]
9
+ s.email = 'info@elastic.co'
10
+ s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
11
+ s.require_paths = ["lib"]
12
+
13
+ # Files
14
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
15
+
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", ">= 2.0.0.beta2", "< 3.0.0"
24
+
25
+ s.add_development_dependency 'logstash-devutils'
26
+ end
27
+
@@ -0,0 +1,171 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/filters/webservicemap"
4
+ require "webmock/rspec"
5
+ require 'digest/sha1'
6
+ WebMock.disable_net_connect!(allow_localhost: true)
7
+
8
+ describe LogStash::Filters::WebServiceMap do
9
+
10
+ let(:config) { Hash.new }
11
+ subject { described_class.new(config) }
12
+
13
+ describe "webserver mapping" do
14
+ config <<-CONFIG
15
+ filter {
16
+ webservicemap {
17
+ field => "status"
18
+ destination => "mapping"
19
+ map_url => "http://dummyurl/"
20
+ }
21
+ }
22
+ CONFIG
23
+
24
+ RSpec.configure do |config|
25
+ hash = Digest::SHA1.hexdigest 'http://dummyurl/'
26
+ config.before(:each) do
27
+ FileUtils.rm_rf(hash+'.yml')
28
+ stub_request(:get, "http://dummyurl/").
29
+ with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
30
+ to_return(:status => 200, :body => "\
31
+ '200': OK\n\
32
+ '300': Redirect\n\
33
+ '400': Client Error\n\
34
+ '500': Server Error", :headers => {})
35
+ end
36
+ config.after(:all) do
37
+ FileUtils.rm_rf(hash+'.yml')
38
+ end
39
+ end
40
+
41
+ sample("status" => "200") do
42
+ insist { subject["mapping"] } == "OK"
43
+ end
44
+ end
45
+
46
+ describe "webserver mapping existing YML" do
47
+ config <<-CONFIG
48
+ filter {
49
+ webservicemap {
50
+ field => "status"
51
+ destination => "mapping"
52
+ map_url => "http://dummyurl/"
53
+ }
54
+ }
55
+ CONFIG
56
+
57
+ RSpec.configure do |config|
58
+ hash = Digest::SHA1.hexdigest 'http://dummyurl/'
59
+ config.before(:each) do
60
+ FileUtils.rm_rf(hash+'.yml')
61
+ File.open(hash+'.yml', 'wb') { |f| f.write("\
62
+ '200': OKF\n\
63
+ '300': Redirect\n\
64
+ '400': Client Error\n\
65
+ '500': Server Error") }
66
+ stub_request(:get, "http://dummyurl/").
67
+ with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
68
+ to_return(:status => 200, :body => "\
69
+ '200': OK\n\
70
+ '300': Redirect\n\
71
+ '400': Client Error\n\
72
+ '500': Server Error", :headers => {})
73
+ end
74
+ config.after(:all) do
75
+ FileUtils.rm_rf(hash+'.yml')
76
+ end
77
+ end
78
+
79
+ sample("status" => "200") do
80
+ insist { subject["mapping"] } == "OK"
81
+ end
82
+ end
83
+
84
+ describe "webserver mapping not valid" do
85
+ config <<-CONFIG
86
+ filter {
87
+ webservicemap {
88
+ field => "status"
89
+ destination => "mapping"
90
+ map_url => "http://dummyurl/"
91
+ }
92
+ }
93
+ CONFIG
94
+
95
+ RSpec.configure do |config|
96
+ hash = Digest::SHA1.hexdigest 'http://dummyurl/'
97
+ config.before(:each) do
98
+ FileUtils.rm_rf(hash+'.yml')
99
+ stub_request(:get, "http://dummyurl/").
100
+ with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
101
+ to_return(:status => 200, :body => "\
102
+ '200': OK\n\
103
+ '300': Redirect\n\
104
+ '400', Client Error\n\
105
+ '500': Server Error", :headers => {})
106
+ end
107
+ config.after(:all) do
108
+ FileUtils.rm_rf(hash+'.yml')
109
+ end
110
+ end
111
+
112
+ sample("status" => "200") do
113
+ insist { subject["mapping"] } == nil
114
+ end
115
+ end
116
+ =begin
117
+ describe "webserver mapping not valid existing YML" do
118
+ config <<-CONFIG
119
+ filter {
120
+ webservicemap {
121
+ field => "status"
122
+ destination => "mapping"
123
+ map_url => "http://dummyurl/"
124
+ }
125
+ }
126
+ CONFIG
127
+ context "init" do
128
+ let(:my_map) { {"200"=>"OKF"}}
129
+ end
130
+
131
+ RSpec.configure do |config|
132
+
133
+ hash = Digest::SHA1.hexdigest 'http://dummyurl/'
134
+ config.before(:each) do
135
+ stub_request(:get, "http://dummyurl/").
136
+ with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
137
+ to_return(:status => 200, :body => "\
138
+ '200': OK\n\
139
+ '300': Redirect\n\
140
+ '400', Client Error\n\
141
+ '500': Server Error", :headers => {})
142
+ end
143
+ config.after(:all) do
144
+ FileUtils.rm_rf(hash+'.yml')
145
+ end
146
+ end
147
+
148
+ sample("status" => "200") do
149
+ insist { subject["mapping"] } == "OKF"
150
+ end
151
+ end
152
+ =end
153
+ context "allow sprintf" do
154
+ let(:config) do
155
+ {
156
+ "field" => "status",
157
+ "destination" => "mapping",
158
+ "fallback" => "%{missing_mapping}",
159
+ "map_url" => "http://dummyurl/"
160
+ }
161
+ end
162
+
163
+ let(:event) { LogStash::Event.new("status" => "200", "missing_mapping" => "missing no match") }
164
+
165
+ it "return the exact mapping" do
166
+ subject.register
167
+ subject.filter(event)
168
+ expect(event["mapping"]).to eq("missing no match")
169
+ end
170
+ end
171
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-webservicemap
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Elastic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-12 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.0.beta2
19
+ - - <
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ name: logstash-core
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.0.beta2
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ name: logstash-devutils
40
+ prerelease: false
41
+ type: :development
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program
48
+ email: info@elastic.co
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - CHANGELOG.md
54
+ - CONTRIBUTORS
55
+ - Gemfile
56
+ - LICENSE
57
+ - NOTICE.TXT
58
+ - README.md
59
+ - lib/logstash/filters/webservicemap.rb
60
+ - logstash-filter-webservicemap.gemspec
61
+ - spec/filters/webservicemap_spec.rb
62
+ homepage: http://www.elastic.co/guide/en/logstash/current/index.html
63
+ licenses:
64
+ - Apache License (2.0)
65
+ metadata:
66
+ logstash_plugin: 'true'
67
+ logstash_group: filter
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.5
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: A general search and replace tool which uses a configured web service to determine replacement values.
88
+ test_files:
89
+ - spec/filters/webservicemap_spec.rb