logstash-filter-http 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: d5da94956185d0453f30766ff885a90eaa8f0e90954e5f1fc8e8c3654baccb01
4
+ data.tar.gz: 217cdde8ce00bb9812531ac65edbe6cb0abcacbb07a8aeff8477f35b13904b3d
5
+ SHA512:
6
+ metadata.gz: feb63179da4d54e1d0b38091b8fd54ccd3a41ffc9d3617c807930523144c7410d389de665bc119cedecf7c97e7f189f75f7806a01791b3882d57303f9b5e6cc3
7
+ data.tar.gz: 6dba46bc255f7a7205cadf94160eb0b31ab1d202592031dfd9f98b7f631f4d3afaac4470280a460024054d4f288680dc080028f0b78297d4d43d679e0d0b86bd
@@ -0,0 +1,28 @@
1
+ ## 0.5.4
2
+
3
+ - update `gemspec` to work with logstash 5.5
4
+
5
+ ## 0.5.3
6
+ - freeze all instance variables
7
+ - fix parallel processing by creating a `deep_clone` for each event
8
+ - use `LogStash::Util.deep_clone` for object cloning
9
+ - only dump body as json, if json is enabled in config (default)
10
+ - delete empty target testcase, as catched by upper logstash `LogStash::ConfigurationError`
11
+ - fix `sprintf` find and merge for more complex structures
12
+
13
+ ## 0.5.2
14
+ - Fix behavior, where a referenced field (`%{...}`) has `ruby` chars
15
+ (i.e., consisting of `:`)
16
+ - Field interpolation is done by assigning explicit values instead
17
+ of converting the `sprintf` string back into a `hash`
18
+
19
+ ## 0.5.0
20
+ - Relax constraint on logstash-core-plugin-api to >= 1.60 <= 2.99
21
+ - Require devutils >= 0 to make `bundler` update the package
22
+ - Use Event API for LS-5
23
+ - Implicit `sprintf`, deprecating the setting
24
+ - `target` is now required, dropping support to write into top-level in favor of only using new Event API
25
+ - this follows other logstash-plugins like `logstash-filter-json`
26
+ - if the response is empty, add the restfailure tags
27
+ - Some logging moved before code
28
+ - Testcases adapted to new behavior with error check
@@ -0,0 +1,11 @@
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
+ * Aaron Mildenstein (untergeek)
6
+ * Pier-Hugues Pellerin (ph)
7
+
8
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
9
+ Logstash, and you aren't on the list above and want to be, please let us know
10
+ and we'll make sure you're here. Contributions from folks like you are what make
11
+ open source awesome.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash"
6
+ use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1"
7
+
8
+ if Dir.exist?(logstash_path) && use_logstash_source
9
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
10
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
11
+ 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.
@@ -0,0 +1,92 @@
1
+ # Logstash REST Filter [![Build Status](https://travis-ci.org/logstash-plugins/logstash-filter-http.svg?branch=master)](https://travis-ci.org/logstash-plugins/logstash-filter-http)
2
+
3
+ This is a filter 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
+ This logstash filter provides an easy way to access RESTful Resources within logstash. It can be used to post data to a REST API or to gather data and save it in your log file.
10
+
11
+ ## Usage
12
+ ### 1. Installation
13
+ You can use the built-in plugin tool of Logstash to install the filter:
14
+ ```
15
+ $LS_HOME/bin/logstash-plugin install logstash-filter-rest
16
+ ```
17
+
18
+ Or you can build it yourself:
19
+ ```
20
+ git clone https://github.com/lucashenning/logstash-filter-rest.git
21
+ bundle install
22
+ gem build logstash-filter-rest.gemspec
23
+ $LS_HOME/bin/logstash-plugin install logstash-filter-rest-0.1.0.gem
24
+ ```
25
+
26
+ ### 2. Filter Configuration
27
+ Add the following inside the filter section of your logstash configuration:
28
+
29
+ ```sh
30
+ filter {
31
+ rest {
32
+ request => {
33
+ url => "http://example.com" # string (required, with field reference: "http://example.com?id=%{id}" or params, if defined)
34
+ method => "post" # string (optional, default = "get")
35
+ headers => { # hash (optional)
36
+ "key1" => "value1"
37
+ "key2" => "value2"
38
+ }
39
+ auth => {
40
+ user => "AzureDiamond"
41
+ password => "hunter2"
42
+ }
43
+ params => { # hash (optional, available for method => "get" and "post"; if post it will be transformed into body hash and posted as json)
44
+ "key1" => "value1"
45
+ "key2" => "value2"
46
+ "key3" => "%{somefield}" # sprintf is used implicitly
47
+ }
48
+ }
49
+ json => true # boolean (optional, default = true)
50
+ target => "my_key" # string (mandatory, no default)
51
+ fallback => { # hash describing a default in case of error
52
+ "key1" => "value1"
53
+ "key2" => "value2"
54
+ }
55
+ }
56
+ }
57
+ ```
58
+
59
+ Print plugin version:
60
+
61
+ ``` bash
62
+ bin/logstash-plugin list --verbose | grep rest
63
+ ```
64
+
65
+ Examples for running logstash from `cli`:
66
+
67
+ ``` bash
68
+ bin/logstash --debug -e 'input { stdin{} } filter { rest { request => { url => "https://jsonplaceholder.typicode.com/posts" method => "post" params => { "userId" => "%{message}" } headers => { "Content-Type" => "application/json" } } target => 'rest' } } output {stdout { codec => rubydebug }}'
69
+ ```
70
+
71
+ ``` bash
72
+ bin/logstash --debug -e 'input { stdin{} } filter { rest { request => { url => "https://jsonplaceholder.typicode.com/posts" method => "post" body => { "userId" => "%{message}" } headers => { "Content-Type" => "application/json" } } target => 'rest' } } output {stdout { codec => rubydebug }}'
73
+ ```
74
+
75
+ ``` bash
76
+ bin/logstash --debug -e 'input { stdin{} } filter { rest { request => { url => "http://jsonplaceholder.typicode.com/users/%{message}" } target => 'rest' } } output {stdout { codec => rubydebug }}'
77
+ ```
78
+
79
+ ``` bash
80
+ bin/logstash --debug -e 'input { stdin{} } filter { rest { request => { url => "https://jsonplaceholder.typicode.com/posts" method => "get" params => { "userId" => "%{message}" } headers => { "Content-Type" => "application/json" } } target => 'rest' } } output {stdout { codec => rubydebug }}'
81
+ ```
82
+
83
+
84
+ ## Contributing
85
+
86
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
87
+
88
+ 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.
89
+
90
+ It is more important to the community that you are able to contribute.
91
+
92
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elasticsearch/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,120 @@
1
+ # encoding: utf-8
2
+ require 'logstash/filters/base'
3
+ require 'logstash/namespace'
4
+ require 'logstash/plugin_mixins/http_client'
5
+ require 'logstash/json'
6
+
7
+ # Logstash HTTP Filter
8
+ # This filter calls a defined URL and saves the answer into a specified field.
9
+ #
10
+ class LogStash::Filters::Http < LogStash::Filters::Base
11
+ include LogStash::PluginMixins::HttpClient
12
+
13
+ config_name 'http'
14
+
15
+ VALID_VERBS = ['GET', 'HEAD', 'PATCH', 'DELETE', 'POST']
16
+
17
+ config :url, :validate => :string, :required => true
18
+ config :verb, :validate => VALID_VERBS, :required => false, :default => 'GET'
19
+ config :headers, :validate => :hash, :required => false, :default => {}
20
+ config :query, :validate => :hash, :required => false, :default => {}
21
+ config :body, :required => false
22
+ config :body_format, :validate => ['text', 'json'], :default => "text"
23
+
24
+ config :target_body, :validate => :string, :default => "body"
25
+ config :target_headers, :validate => :string, :default => "headers"
26
+
27
+ # Append values to the `tags` field when there has been no
28
+ # successful match or json parsing error
29
+ config :tag_on_request_failure, :validate => :array, :default => ['_httprequestfailure']
30
+ config :tag_on_json_failure, :validate => :array, :default => ['_jsonparsefailure']
31
+
32
+ def register
33
+ # nothing to see here
34
+ @verb = verb.downcase
35
+ end
36
+
37
+ def filter(event)
38
+ url_for_event = event.sprintf(@url)
39
+ headers_sprintfed = sprintf_object(event, @headers)
40
+ if body_format == "json"
41
+ headers_sprintfed["content-type"] = "application/json"
42
+ else
43
+ headers_sprintfed["content-type"] = "text/plain"
44
+ end
45
+ query_sprintfed = sprintf_object(event, @query)
46
+ body_sprintfed = sprintf_object(event, @body)
47
+ # we don't need to serialize strings and numbers
48
+ if @body_format == "json" && body_sprintfed.kind_of?(Enumerable)
49
+ body_sprintfed = LogStash::Json.dump(body_sprintfed)
50
+ end
51
+
52
+ options = { :headers => headers_sprintfed, :query => query_sprintfed, :body => body_sprintfed }
53
+
54
+ @logger.debug? && @logger.debug('processing request', :url => url_for_event, :headers => headers_sprintfed, :parameters => parameters_sprintfed)
55
+ client_error = nil
56
+
57
+ begin
58
+ code, response_headers, response_body = request_http(@verb, url_for_event, options)
59
+ rescue => e
60
+ client_error = e
61
+ end
62
+
63
+ if client_error
64
+ @logger.error('error during HTTP request',
65
+ :url => url_for_event, :body => @body,
66
+ :client_error => client_error.message)
67
+ @tag_on_request_failure.each { |tag| event.tag(tag) }
68
+ elsif !code.between?(200, 299)
69
+ @logger.error('error during HTTP request',
70
+ :url => url_for_event, :code => code,
71
+ :response => response_body)
72
+ @tag_on_request_failure.each { |tag| event.tag(tag) }
73
+ else
74
+ @logger.debug? && @logger.debug('success received',
75
+ :code => code, :body => response_body)
76
+ process_response(response_body, response_headers, event)
77
+ filter_matched(event)
78
+ end
79
+ end # def filter
80
+
81
+ private
82
+ def request_http(verb, url, options = {})
83
+ response = client.http(verb, url, options)
84
+ [response.code, response.headers, response.body]
85
+ end
86
+
87
+ def sprintf_object(event, obj)
88
+ case obj
89
+ when Array
90
+ obj.map {|el| sprintf_object(event, el) }
91
+ when Hash
92
+ obj.inject({}) {|acc, (k,v)| acc[sprintf_object(event, k)] = sprintf_object(event, v); acc }
93
+ when String
94
+ event.sprintf(obj)
95
+ else
96
+ obj
97
+ end
98
+ end
99
+
100
+ def process_response(body, headers, event)
101
+ content_type, _ = headers.fetch("content-type", "").split(";")
102
+ event.set(@target_headers, headers)
103
+ if content_type == "application/json"
104
+ begin
105
+ parsed = LogStash::Json.load(body)
106
+ event.set(@target_body, parsed)
107
+ rescue => e
108
+ if @logger.debug?
109
+ @logger.warn('JSON parsing error', :message => e.message, :body => body)
110
+ else
111
+ @logger.warn('JSON parsing error', :message => e.message)
112
+ end
113
+ @tag_on_json_failure.each { |tag| event.tag(tag) }
114
+ end
115
+ else
116
+ event.set(@target_body, body.strip)
117
+ end
118
+ end
119
+
120
+ end # class LogStash::Filters::Rest
@@ -0,0 +1,33 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-http'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = 'This filter requests data from a RESTful Web Service.'
6
+ s.description = 'This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install logstash-filter-http. This gem is not a stand-alone program'
7
+ s.authors = ['Lucas Henning', 'Gandalf Buscher']
8
+ s.email = 'mail@hurb.de'
9
+ s.homepage = 'https://github.com/lucashenning/logstash-filter-http/'
10
+ s.require_paths = ['lib']
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*',
14
+ 'spec/**/*',
15
+ 'vendor/**/*',
16
+ '*.gemspec',
17
+ '*.md',
18
+ 'CONTRIBUTORS',
19
+ 'Gemfile',
20
+ 'LICENSE',
21
+ 'NOTICE.TXT']
22
+ # Tests
23
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
24
+
25
+ # Special flag to let us know this is actually a logstash plugin
26
+ s.metadata = { 'logstash_plugin' => 'true', 'logstash_group' => 'filter' }
27
+
28
+ # Gem dependencies
29
+ s.add_runtime_dependency 'logstash-core-plugin-api', '>= 1.60', '<= 2.99'
30
+ s.add_runtime_dependency 'logstash-mixin-http_client', '>= 5.0.0', '< 9.0.0'
31
+
32
+ s.add_development_dependency 'logstash-devutils', '>= 0', '< 2.0.0'
33
+ end
@@ -0,0 +1,507 @@
1
+ require 'logstash/devutils/rspec/spec_helper'
2
+ require 'logstash/filters/http'
3
+
4
+ describe LogStash::Filters::Http do
5
+ subject { described_class.new(config) }
6
+ let(:event) { LogStash::Event.new(data) }
7
+ let(:data) { { "message" => "test" } }
8
+
9
+ describe 'response body handling' do
10
+ before(:each) { subject.register }
11
+ let(:url) { 'http://laceholder.typicode.com/users/10' }
12
+ let(:config) do
13
+ { "url" => url, "target_body" => 'rest' }
14
+ end
15
+ before(:each) do
16
+ allow(subject).to receive(:request_http).and_return(response)
17
+ subject.filter(event)
18
+ end
19
+
20
+ context "when body is text" do
21
+ let(:response) { [200, {}, "Bom dia"] }
22
+
23
+ it "fetches and writes body to target" do
24
+ expect(event.get('rest')).to eq("Bom dia")
25
+ end
26
+ end
27
+ context "when body is JSON" do
28
+ context "and headers are set correctly" do
29
+ let(:response) { [200, {"content-type" => "application/json"}, "{\"id\": 10}"] }
30
+
31
+ it "fetches and writes body to target" do
32
+ expect(event.get('[rest][id]')).to eq(10)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ describe 'URL parameter' do
38
+ before(:each) { subject.register }
39
+ context "when url contains field references" do
40
+ let(:config) do
41
+ { "url" => "http://stringsize.com/%{message}", "target_body" => "size" }
42
+ end
43
+ let(:response) { [200, {}, "4"] }
44
+
45
+ it "interpolates request url using event data" do
46
+ expect(subject).to receive(:request_http).with(anything, "http://stringsize.com/test", anything).and_return(response)
47
+ subject.filter(event)
48
+ end
49
+ end
50
+ end
51
+ context 'when request returns 404' do
52
+ before(:each) { subject.register }
53
+ let(:config) do
54
+ {
55
+ 'url' => 'http://httpstat.us/404',
56
+ 'target_body' => 'rest'
57
+ }
58
+ end
59
+ let(:response) { [404, {}, ""] }
60
+
61
+ before(:each) do
62
+ allow(subject).to receive(:request_http).and_return(response)
63
+ subject.filter(event)
64
+ end
65
+
66
+ it "tags the event with _httprequestfailure" do
67
+ expect(event).to_not include('rest')
68
+ expect(event.get('tags')).to include('_httprequestfailure')
69
+ end
70
+ end
71
+ describe "headers" do
72
+ before(:each) { subject.register }
73
+ let(:response) { [200, {}, "Bom dia"] }
74
+ context "when set" do
75
+ let(:headers) { { "Cache-Control" => "nocache" } }
76
+ let(:config) do
77
+ {
78
+ "url" => "http://stringsize.com",
79
+ "target_body" => "size",
80
+ "headers" => headers
81
+ }
82
+ end
83
+ it "are included in the request" do
84
+ expect(subject).to receive(:request_http) do |verb, url, options|
85
+ expect(options.fetch(:headers, {})).to include(headers)
86
+ end.and_return(response)
87
+ subject.filter(event)
88
+ end
89
+ end
90
+ end
91
+ describe "query string parameters" do
92
+ before(:each) { subject.register }
93
+ let(:response) { [200, {}, "Bom dia"] }
94
+ context "when set" do
95
+ let(:query) { { "color" => "green" } }
96
+ let(:config) do
97
+ {
98
+ "url" => "http://stringsize.com/%{message}",
99
+ "target_body" => "size",
100
+ "query" => query
101
+ }
102
+ end
103
+ it "are included in the request" do
104
+ expect(subject).to receive(:request_http).with(anything, anything, include(:query => query)).and_return(response)
105
+ subject.filter(event)
106
+ end
107
+ end
108
+ end
109
+ describe "request body" do
110
+ before(:each) { subject.register }
111
+ let(:response) { [200, {}, "Bom dia"] }
112
+ let(:config) do
113
+ {
114
+ "url" => "http://stringsize.com",
115
+ "body" => body
116
+ }
117
+ end
118
+
119
+ describe "format" do
120
+ let(:config) do
121
+ {
122
+ "url" => "http://stringsize.com",
123
+ "body_format" => body_format,
124
+ "body" => body
125
+ }
126
+ end
127
+
128
+ context "when is json" do
129
+ let(:body_format) { "json" }
130
+ let(:body) do
131
+ { "hey" => "you" }
132
+ end
133
+ let(:body_json) { LogStash::Json.dump(body) }
134
+
135
+ it "serializes the body to json" do
136
+ expect(subject).to receive(:request_http) do |verb, url, options|
137
+ expect(options).to include(:body => body_json)
138
+ end.and_return(response)
139
+ subject.filter(event)
140
+ end
141
+ it "sets content-type to application/json" do
142
+ expect(subject).to receive(:request_http) do |verb, url, options|
143
+ expect(options).to include(:headers => { "content-type" => "application/json"})
144
+ end.and_return(response)
145
+ subject.filter(event)
146
+ end
147
+ end
148
+ context "when is text" do
149
+ let(:body_format) { "text" }
150
+ let(:body) { "Hey, you!" }
151
+
152
+ it "uses the text as body for the request" do
153
+ expect(subject).to receive(:request_http) do |verb, url, options|
154
+ expect(options).to include(:body => body)
155
+ end.and_return(response)
156
+ subject.filter(event)
157
+ end
158
+ it "sets content-type to text/plain" do
159
+ expect(subject).to receive(:request_http) do |verb, url, options|
160
+ expect(options).to include(:headers => { "content-type" => "text/plain"})
161
+ end.and_return(response)
162
+ subject.filter(event)
163
+ end
164
+ end
165
+ end
166
+ context "when using field references" do
167
+ let(:body_format) { "json" }
168
+ let(:body) do
169
+ { "%{key1}" => [ "%{[field1]}", "another_value", { "key" => "other-%{[nested][field2]}" } ] }
170
+ end
171
+ let(:body_json) { LogStash::Json.dump(body) }
172
+ let(:data) do
173
+ {
174
+ "message" => "ola",
175
+ "key1" => "mykey",
176
+ "field1" => "normal value",
177
+ "nested" => { "field2" => "value2" }
178
+ }
179
+ end
180
+
181
+ it "fills the body with event data" do
182
+ expect(subject).to receive(:request_http) do |verb, url, options|
183
+ body = options.fetch(:body, {})
184
+ expect(body.keys).to include("mykey")
185
+ expect(body.fetch("mykey")).to eq(["normal value", "another_value", { "key" => "other-value2" }])
186
+ end.and_return(response)
187
+ subject.filter(event)
188
+ end
189
+ end
190
+ end
191
+ describe "verb" do
192
+ let(:response) { [200, {}, "Bom dia"] }
193
+ let(:config) do
194
+ {
195
+ "verb" => verb,
196
+ "url" => "http://stringsize.com",
197
+ "target_body" => "size"
198
+ }
199
+ end
200
+ ["GET", "HEAD", "POST", "DELETE"].each do |verb_string|
201
+ let(:verb) { verb_string }
202
+ context "when verb #{verb_string} is set" do
203
+ before(:each) { subject.register }
204
+ it "it is used in the request" do
205
+ expect(subject).to receive(:request_http).with(verb.downcase, anything, anything).and_return(response)
206
+ subject.filter(event)
207
+ end
208
+ end
209
+ end
210
+ context "when using an invalid verb" do
211
+ let(:verb) { "something else" }
212
+ it "it is used in the request" do
213
+ expect { described_class.new(config) }.to raise_error ::LogStash::ConfigurationError
214
+ end
215
+ end
216
+ end
217
+ end
218
+
219
+ =begin
220
+ # TODO refactor remaning tests to avoid insist + whole pipeline instantiation
221
+ describe 'empty response' do
222
+ let(:config) do <<-CONFIG
223
+ filter {
224
+ rest {
225
+ request => {
226
+ url => 'https://jsonplaceholder.typicode.com/posts'
227
+ params => {
228
+ userId => 0
229
+ }
230
+ headers => {
231
+ 'Content-Type' => 'application/json'
232
+ }
233
+ }
234
+ target => 'rest'
235
+ }
236
+ }
237
+ CONFIG
238
+ end
239
+
240
+ sample('message' => 'some text') do
241
+ expect(subject).to_not include('rest')
242
+ expect(subject.get('tags')).to include('_restfailure')
243
+ end
244
+ end
245
+ describe 'Set to Rest Filter Get with params sprintf' do
246
+ let(:config) do <<-CONFIG
247
+ filter {
248
+ rest {
249
+ request => {
250
+ url => 'https://jsonplaceholder.typicode.com/posts'
251
+ params => {
252
+ userId => "%{message}"
253
+ id => "%{message}"
254
+ }
255
+ headers => {
256
+ 'Content-Type' => 'application/json'
257
+ }
258
+ }
259
+ json => true
260
+ target => 'rest'
261
+ }
262
+ }
263
+ CONFIG
264
+ end
265
+
266
+ sample('message' => '1') do
267
+ expect(subject).to include('rest')
268
+ expect(subject.get('[rest][0]')).to include('userId')
269
+ expect(subject.get('[rest][0][userId]')).to eq(1)
270
+ expect(subject.get('[rest][0][id]')).to eq(1)
271
+ expect(subject.get('rest').length).to eq(1)
272
+ expect(subject.get('rest')).to_not include('fallback')
273
+ end
274
+ end
275
+ describe 'Set to Rest Filter Post with params' do
276
+ let(:config) do <<-CONFIG
277
+ filter {
278
+ rest {
279
+ request => {
280
+ url => 'https://jsonplaceholder.typicode.com/posts'
281
+ method => 'post'
282
+ params => {
283
+ title => 'foo'
284
+ body => 'bar'
285
+ userId => 42
286
+ }
287
+ headers => {
288
+ 'Content-Type' => 'application/json'
289
+ }
290
+ }
291
+ json => true
292
+ target => 'rest'
293
+ }
294
+ }
295
+ CONFIG
296
+ end
297
+
298
+ sample('message' => 'some text') do
299
+ expect(subject).to include('rest')
300
+ expect(subject.get('rest')).to include('id')
301
+ expect(subject.get('[rest][userId]')).to eq(42)
302
+ expect(subject.get('rest')).to_not include('fallback')
303
+ end
304
+ end
305
+ describe 'Set to Rest Filter Post with params sprintf' do
306
+ let(:config) do <<-CONFIG
307
+ filter {
308
+ rest {
309
+ request => {
310
+ url => 'https://jsonplaceholder.typicode.com/posts'
311
+ method => 'post'
312
+ params => {
313
+ title => '%{message}'
314
+ body => 'bar'
315
+ userId => "%{message}"
316
+ }
317
+ headers => {
318
+ 'Content-Type' => 'application/json'
319
+ }
320
+ }
321
+ json => true
322
+ target => 'rest'
323
+ }
324
+ }
325
+ CONFIG
326
+ end
327
+
328
+ sample('message' => '42') do
329
+ expect(subject).to include('rest')
330
+ expect(subject.get('rest')).to include('id')
331
+ expect(subject.get('[rest][title]')).to eq(42)
332
+ expect(subject.get('[rest][userId]')).to eq(42)
333
+ expect(subject.get('rest')).to_not include('fallback')
334
+ end
335
+ sample('message' => ':5e?#!-_') do
336
+ expect(subject).to include('rest')
337
+ expect(subject.get('rest')).to include('id')
338
+ expect(subject.get('[rest][title]')).to eq(':5e?#!-_')
339
+ expect(subject.get('[rest][userId]')).to eq(':5e?#!-_')
340
+ expect(subject.get('rest')).to_not include('fallback')
341
+ end
342
+ sample('message' => ':4c43=>') do
343
+ expect(subject).to include('rest')
344
+ expect(subject.get('rest')).to include('id')
345
+ expect(subject.get('[rest][title]')).to eq(':4c43=>')
346
+ expect(subject.get('[rest][userId]')).to eq(':4c43=>')
347
+ expect(subject.get('rest')).to_not include('fallback')
348
+ end
349
+ end
350
+ describe 'Set to Rest Filter Post with body sprintf' do
351
+ let(:config) do <<-CONFIG
352
+ filter {
353
+ rest {
354
+ request => {
355
+ url => 'https://jsonplaceholder.typicode.com/posts'
356
+ method => 'post'
357
+ body => {
358
+ title => 'foo'
359
+ body => 'bar'
360
+ userId => "%{message}"
361
+ }
362
+ headers => {
363
+ 'Content-Type' => 'application/json'
364
+ }
365
+ }
366
+ json => true
367
+ target => 'rest'
368
+ }
369
+ }
370
+ CONFIG
371
+ end
372
+
373
+ sample('message' => '42') do
374
+ expect(subject).to include('rest')
375
+ expect(subject.get('rest')).to include('id')
376
+ expect(subject.get('[rest][userId]')).to eq(42)
377
+ expect(subject.get('rest')).to_not include('fallback')
378
+ end
379
+ end
380
+ describe 'Set to Rest Filter Post with body sprintf nested params' do
381
+ let(:config) do <<-CONFIG
382
+ filter {
383
+ rest {
384
+ request => {
385
+ url => 'https://jsonplaceholder.typicode.com/posts'
386
+ method => 'post'
387
+ body => {
388
+ key1 => [
389
+ {
390
+ "filterType" => "text"
391
+ "text" => "salmon"
392
+ "boolean" => false
393
+ },
394
+ {
395
+ "filterType" => "unique"
396
+ }
397
+ ]
398
+ key2 => [
399
+ {
400
+ "message" => "123%{message}"
401
+ "boolean" => true
402
+ }
403
+ ]
404
+ key3 => [
405
+ {
406
+ "text" => "%{message}123"
407
+ "filterType" => "text"
408
+ "number" => 44
409
+ },
410
+ {
411
+ "filterType" => "unique"
412
+ "null" => nil
413
+ }
414
+ ]
415
+ userId => "%{message}"
416
+ }
417
+ headers => {
418
+ 'Content-Type' => 'application/json'
419
+ }
420
+ }
421
+ target => 'rest'
422
+ }
423
+ }
424
+ CONFIG
425
+ end
426
+
427
+ sample('message' => '42') do
428
+ expect(subject).to include('rest')
429
+ expect(subject.get('rest')).to include('key1')
430
+ expect(subject.get('[rest][key1][0][boolean]')).to eq('false')
431
+ expect(subject.get('[rest][key1][1][filterType]')).to eq('unique')
432
+ expect(subject.get('[rest][key2][0][message]')).to eq('12342')
433
+ expect(subject.get('[rest][key2][0][boolean]')).to eq('true')
434
+ expect(subject.get('[rest][key3][0][text]')).to eq('42123')
435
+ expect(subject.get('[rest][key3][0][filterType]')).to eq('text')
436
+ expect(subject.get('[rest][key3][0][number]')).to eq(44)
437
+ expect(subject.get('[rest][key3][1][filterType]')).to eq('unique')
438
+ expect(subject.get('[rest][key3][1][null]')).to eq('nil')
439
+ expect(subject.get('[rest][userId]')).to eq(42)
440
+ expect(subject.get('rest')).to_not include('fallback')
441
+ end
442
+ end
443
+ describe 'fallback' do
444
+ let(:config) do <<-CONFIG
445
+ filter {
446
+ rest {
447
+ request => {
448
+ url => 'http://jsonplaceholder.typicode.com/users/0'
449
+ }
450
+ json => true
451
+ fallback => {
452
+ 'fallback1' => true
453
+ 'fallback2' => true
454
+ }
455
+ target => 'rest'
456
+ }
457
+ }
458
+ CONFIG
459
+ end
460
+
461
+ sample('message' => 'some text') do
462
+ expect(subject).to include('rest')
463
+ expect(subject.get('rest')).to include('fallback1')
464
+ expect(subject.get('rest')).to include('fallback2')
465
+ expect(subject.get('rest')).to_not include('id')
466
+ end
467
+ end
468
+ describe 'empty target exception' do
469
+ let(:config) do <<-CONFIG
470
+ filter {
471
+ rest {
472
+ request => {
473
+ url => 'http://jsonplaceholder.typicode.com/users/0'
474
+ }
475
+ json => true
476
+ fallback => {
477
+ 'fallback1' => true
478
+ 'fallback2' => true
479
+ }
480
+ target => ''
481
+ }
482
+ }
483
+ CONFIG
484
+ end
485
+ sample('message' => 'some text') do
486
+ expect { subject }.to raise_error(LogStash::ConfigurationError)
487
+ end
488
+ end
489
+ describe 'http client throws exception' do
490
+ let(:config) do <<-CONFIG
491
+ filter {
492
+ rest {
493
+ request => {
494
+ url => 'invalid_url'
495
+ }
496
+ target => 'rest'
497
+ }
498
+ }
499
+ CONFIG
500
+ end
501
+ sample('message' => 'some text') do
502
+ expect(subject).to_not include('rest')
503
+ expect(subject.get('tags')).to include('_restfailure')
504
+ end
505
+ end
506
+ end
507
+ =end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Henning
8
+ - Gandalf Buscher
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-12-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.60'
20
+ - - "<="
21
+ - !ruby/object:Gem::Version
22
+ version: '2.99'
23
+ name: logstash-core-plugin-api
24
+ prerelease: false
25
+ type: :runtime
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: '1.60'
31
+ - - "<="
32
+ - !ruby/object:Gem::Version
33
+ version: '2.99'
34
+ - !ruby/object:Gem::Dependency
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 5.0.0
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: 9.0.0
43
+ name: logstash-mixin-http_client
44
+ prerelease: false
45
+ type: :runtime
46
+ version_requirements: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 5.0.0
51
+ - - "<"
52
+ - !ruby/object:Gem::Version
53
+ version: 9.0.0
54
+ - !ruby/object:Gem::Dependency
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ - - "<"
61
+ - !ruby/object:Gem::Version
62
+ version: 2.0.0
63
+ name: logstash-devutils
64
+ prerelease: false
65
+ type: :development
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ - - "<"
72
+ - !ruby/object:Gem::Version
73
+ version: 2.0.0
74
+ description: This gem is a logstash plugin required to be installed on top of the
75
+ Logstash core pipeline using $LS_HOME/bin/logstash-plugin install logstash-filter-http.
76
+ This gem is not a stand-alone program
77
+ email: mail@hurb.de
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - CHANGELOG.md
83
+ - CONTRIBUTORS
84
+ - Gemfile
85
+ - LICENSE
86
+ - README.md
87
+ - lib/logstash/filters/http.rb
88
+ - logstash-filter-http.gemspec
89
+ - spec/filters/http_spec.rb
90
+ homepage: https://github.com/lucashenning/logstash-filter-http/
91
+ licenses:
92
+ - Apache License (2.0)
93
+ metadata:
94
+ logstash_plugin: 'true'
95
+ logstash_group: filter
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.13
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: This filter requests data from a RESTful Web Service.
116
+ test_files:
117
+ - spec/filters/http_spec.rb