logstash-input-http 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 032fe90380dce11dd20661a8aac9084aeaf1d98c
4
+ data.tar.gz: 4d54c4885e13df803bb65be27fadab2ffce058dc
5
+ SHA512:
6
+ metadata.gz: bc1a5f6af20ffee231471deb4f3f424c60891053acdb89d9b6c88e53c5775874af31451322f812631e1ec7d1a8522692a51647b9ab34a4dc349b20a4972a073b
7
+ data.tar.gz: d6f67bf01ae8bc102de7eb14bb877a03daacb99d27367e4805343540d5f8ad8d778ac1bfc74b45982a68c5fe3f5ffe8916ebd4f9e95591583c3e193cda6472eb
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ Gemfile.bak
4
+ .bundle
5
+ vendor
File without changes
@@ -0,0 +1 @@
1
+ # logstash-input-http
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
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,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elasticsearch/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.elasticsearch.org/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/elasticsearch/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/elasticsearch/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1 @@
1
+ require "logstash/devutils/rake"
@@ -0,0 +1,137 @@
1
+ # encoding: utf-8
2
+ require "logstash/inputs/base"
3
+ require "logstash/namespace"
4
+ require "stud/interval"
5
+ require "socket" # for Socket.gethostname
6
+ require "puma/server"
7
+ require "puma/minissl"
8
+
9
+ class Puma::Server
10
+ # ensure this method doesn't mess up our vanilla request
11
+ def normalize_env(env, client); end
12
+ end
13
+
14
+ # This output opens a web server and listens to HTTP requests,
15
+ # converting them to LogStash::Event instances.
16
+ #
17
+ class LogStash::Inputs::Http < LogStash::Inputs::Base
18
+ config_name "http"
19
+
20
+ # If undefined, Logstash will complain, even if codec is unused.
21
+ default :codec, "plain"
22
+
23
+ # Which host or ip to bind to
24
+ config :host, :validate => :string, :default => "0.0.0.0"
25
+
26
+ # Which TCP port to bind to
27
+ config :port, :validate => :number, :default => 8080
28
+
29
+ # Maximum number of threads to use
30
+ config :threads, :validate => :number, :default => 4
31
+
32
+ # Basic Auth
33
+ config :user, :validate => :string, :required => false
34
+ config :password, :validate => :password, :required => false
35
+
36
+ # SSL Configurations
37
+ #
38
+ # Enable SSL
39
+ config :ssl, :validate => :boolean, :default => false
40
+
41
+ # The JKS keystore to validate the client's certificates
42
+ config :keystore, :validate => :path
43
+
44
+ #TODO: config :cacert, :validate => :path
45
+
46
+ # Set the truststore password
47
+ config :keystore_password, :validate => :password
48
+
49
+ # here you can describe how to decode specific content-types
50
+ # by default the default codec will be used
51
+ config :additional_codecs, :validate => :hash, :default => { "application/json" => "json" }
52
+
53
+ # useless headers puma adds to the requests
54
+ # mostly due to rack compliance
55
+ REJECTED_HEADERS = ["puma.socket", "rack.hijack?", "rack.hijack", "rack.url_scheme", "rack.after_reply", "rack.version", "rack.errors", "rack.multithread", "rack.multiprocess", "rack.run_once", "SCRIPT_NAME", "QUERY_STRING", "SERVER_PROTOCOL", "SERVER_SOFTWARE", "GATEWAY_INTERFACE"]
56
+
57
+ RESPONSE_HEADERS = {'Content-Type' => 'text/plain'}
58
+
59
+ public
60
+ def register
61
+ @server = ::Puma::Server.new(nil) # we'll set the rack handler later
62
+ if @user && @password then
63
+ token = Base64.strict_encode64("#{@user}:#{@password.value}")
64
+ @auth_token = "Basic #{token}"
65
+ end
66
+ if @ssl
67
+ if @keystore.nil? || @keystore_password.nil?
68
+ raise(LogStash::ConfigurationError, "Settings :keystore and :keystore_password are required because :ssl is enabled.")
69
+ end
70
+ ctx = Puma::MiniSSL::Context.new
71
+ ctx.keystore = @keystore
72
+ ctx.keystore_pass = @keystore_password.value
73
+ @server.add_ssl_listener(@host, @port, ctx)
74
+ else
75
+ @server.add_tcp_listener(@host, @port)
76
+ end
77
+ @server.min_threads = 0
78
+ @server.max_threads = @threads
79
+ @codecs = Hash.new
80
+ @additional_codecs.each do |content_type, codec|
81
+ @codecs[content_type] = LogStash::Plugin.lookup("codec", codec).new
82
+ end
83
+ end # def register
84
+
85
+ def run(queue)
86
+
87
+ # proc needs to be defined at this context
88
+ # to capture @codecs, @logger and lowercase_keys
89
+ p = Proc.new do |req|
90
+ begin
91
+ REJECTED_HEADERS.each {|k| req.delete(k) }
92
+ req = lowercase_keys(req)
93
+ body = req.delete("rack.input")
94
+ @codecs.fetch(req["content_type"], @codec).decode(body.read) do |event|
95
+ event["headers"] = req
96
+ decorate(event)
97
+ queue << event
98
+ end
99
+ ['200', RESPONSE_HEADERS, ['ok']]
100
+ rescue => e
101
+ @logger.error("unable to process event #{req.inspect}. exception => #{e.inspect}")
102
+ ['500', RESPONSE_HEADERS, ['internal error']]
103
+ end
104
+ end
105
+
106
+ auth = Proc.new do |username, password|
107
+ username == @user && password == @password.value
108
+ end if (@user && @password)
109
+
110
+ @server.app = Rack::Builder.new do
111
+ use(Rack::Auth::Basic, &auth) if auth
112
+ run(p)
113
+ end
114
+ @server.run.join
115
+ end
116
+
117
+ private
118
+ def lowercase_keys(hash)
119
+ new_hash = {}
120
+ hash.each_pair do |k,v|
121
+ new_hash[k.downcase] = v
122
+ end
123
+ new_hash
124
+ end
125
+
126
+ public
127
+ def teardown
128
+ if @server
129
+ @server.stop(true)
130
+ begin
131
+ @server.binder.close if @server.binder
132
+ rescue IOError
133
+ end
134
+ end
135
+ end
136
+
137
+ end # class LogStash::Inputs::Http
@@ -0,0 +1,29 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-input-http'
3
+ s.version = '0.0.1'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = "Logstash Input plugin that receives HTTP requests"
6
+ 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"
7
+ s.authors = ["Elastic"]
8
+ s.email = 'info@elastic.co'
9
+ s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = `git ls-files`.split($\)
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" => "input" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core", '>= 1.4.0', '< 2.0.0'
22
+ s.add_runtime_dependency 'logstash-codec-plain'
23
+ s.add_runtime_dependency 'stud'
24
+ s.add_runtime_dependency 'puma'
25
+
26
+ s.add_development_dependency 'logstash-devutils'
27
+ s.add_development_dependency 'logstash-codec-json'
28
+ s.add_development_dependency 'ftw'
29
+ end
@@ -0,0 +1,139 @@
1
+ require "logstash/devutils/rspec/spec_helper"
2
+ require "logstash/inputs/http"
3
+ require "json"
4
+ require "ftw"
5
+ require "stud/temporary"
6
+
7
+ describe LogStash::Inputs::Http do
8
+
9
+ let(:agent) { FTW::Agent.new }
10
+ let(:queue) { Queue.new }
11
+
12
+ after :each do
13
+ subject.teardown
14
+ end
15
+
16
+ context "with default codec" do
17
+ subject { LogStash::Inputs::Http.new }
18
+ context "when receiving a text/plain request" do
19
+ it "should process the request normally" do
20
+ subject.register
21
+ Thread.new { subject.run(queue) }
22
+ agent.post!("http://localhost:8080/meh.json",
23
+ :headers => { "content-type" => "text/plain" },
24
+ :body => "hello")
25
+ event = queue.pop
26
+ expect(event["message"]).to eq("hello")
27
+ end
28
+ end
29
+ context "when receiving an application/json request" do
30
+ it "should parse the json body" do
31
+ subject.register
32
+ Thread.new { subject.run(queue) }
33
+ agent.post!("http://localhost:8080/meh.json",
34
+ :headers => { "content-type" => "application/json" },
35
+ :body => { "message_body" => "Hello" }.to_json)
36
+ event = queue.pop
37
+ expect(event["message_body"]).to eq("Hello")
38
+ end
39
+ end
40
+ end
41
+
42
+ context "with json codec" do
43
+ subject { LogStash::Inputs::Http.new("codec" => "json") }
44
+ it "should parse the json body" do
45
+ subject.register
46
+ Thread.new { subject.run(queue) }
47
+ agent.post!("http://localhost:8080/meh.json", :body => { "message" => "Hello" }.to_json)
48
+ event = queue.pop
49
+ expect(event["message"]).to eq("Hello")
50
+ end
51
+ end
52
+
53
+ context "when using a custom codec mapping" do
54
+ subject { LogStash::Inputs::Http.new("additional_codecs" => { "application/json" => "plain" }) }
55
+ it "should decode the message accordingly" do
56
+ body = { "message" => "Hello" }.to_json
57
+ subject.register
58
+ Thread.new { subject.run(queue) }
59
+ agent.post!("http://localhost:8080/meh.json",
60
+ :headers => { "content-type" => "application/json" },
61
+ :body => body)
62
+ event = queue.pop
63
+ expect(event["message"]).to eq(body)
64
+ end
65
+ end
66
+
67
+ context "with :ssl => false" do
68
+ subject { LogStash::Inputs::Http.new("ssl" => false) }
69
+ it "should not raise exception" do
70
+ expect { subject.register }.to_not raise_exception
71
+ end
72
+ end
73
+ context "with :ssl => true" do
74
+ context "without :keystore and :keystore_password" do
75
+ subject { LogStash::Inputs::Http.new("ssl" => true) }
76
+ it "should raise exception" do
77
+ expect { subject.register }.to raise_exception(LogStash::ConfigurationError)
78
+ end
79
+ end
80
+ context "with :keystore and :keystore_password" do
81
+ let(:keystore) { Stud::Temporary.file }
82
+ subject { LogStash::Inputs::Http.new("ssl" => true,
83
+ "keystore" => keystore.path,
84
+ "keystore_password" => "pass") }
85
+ it "should not raise exception" do
86
+ expect { subject.register }.to_not raise_exception
87
+ end
88
+ end
89
+ end
90
+ describe "basic auth" do
91
+ user = "test"; password = "pwd"
92
+ subject { LogStash::Inputs::Http.new("user" => user, "password" => password) }
93
+ let(:auth_token) { Base64.strict_encode64("#{user}:#{password}") }
94
+ before :each do
95
+ subject.register
96
+ Thread.new { subject.run(queue) }
97
+ end
98
+ context "when client doesn't present auth token" do
99
+ let!(:response) { agent.post!("http://localhost:8080/meh", :body => "hi") }
100
+ it "should respond with 401" do
101
+ expect(response.status).to eq(401)
102
+ end
103
+ it "should not generate an event" do
104
+ expect(queue).to be_empty
105
+ end
106
+ end
107
+ context "when client presents incorrect auth token" do
108
+ let!(:response) do
109
+ agent.post!("http://localhost:8080/meh",
110
+ :headers => {
111
+ "content-type" => "text/plain",
112
+ "authorization" => "Basic meh"
113
+ },
114
+ :body => "hi")
115
+ end
116
+ it "should respond with 401" do
117
+ expect(response.status).to eq(401)
118
+ end
119
+ it "should not generate an event" do
120
+ expect(queue).to be_empty
121
+ end
122
+ end
123
+ context "when client presents correct auth token" do
124
+ let!(:response) do
125
+ agent.post!("http://localhost:8080/meh",
126
+ :headers => {
127
+ "content-type" => "text/plain",
128
+ "authorization" => "Basic #{auth_token}"
129
+ }, :body => "hi")
130
+ end
131
+ it "should respond with 200" do
132
+ expect(response.status).to eq(200)
133
+ end
134
+ it "should generate an event" do
135
+ expect(queue).to_not be_empty
136
+ end
137
+ end
138
+ end
139
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-input-http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Elastic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.0
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.0
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: 1.4.0
28
+ - - <
29
+ - !ruby/object:Gem::Version
30
+ version: 2.0.0
31
+ prerelease: false
32
+ type: :runtime
33
+ - !ruby/object:Gem::Dependency
34
+ name: logstash-codec-plain
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ prerelease: false
46
+ type: :runtime
47
+ - !ruby/object:Gem::Dependency
48
+ name: stud
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ prerelease: false
60
+ type: :runtime
61
+ - !ruby/object:Gem::Dependency
62
+ name: puma
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirement: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ prerelease: false
74
+ type: :runtime
75
+ - !ruby/object:Gem::Dependency
76
+ name: logstash-devutils
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirement: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ prerelease: false
88
+ type: :development
89
+ - !ruby/object:Gem::Dependency
90
+ name: logstash-codec-json
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirement: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ prerelease: false
102
+ type: :development
103
+ - !ruby/object:Gem::Dependency
104
+ name: ftw
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirement: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ prerelease: false
116
+ type: :development
117
+ 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
118
+ email: info@elastic.co
119
+ executables: []
120
+ extensions: []
121
+ extra_rdoc_files: []
122
+ files:
123
+ - .gitignore
124
+ - CHANGELOG.md
125
+ - DEVELOPER.md
126
+ - Gemfile
127
+ - LICENSE
128
+ - README.md
129
+ - Rakefile
130
+ - lib/logstash/inputs/http.rb
131
+ - logstash-input-http.gemspec
132
+ - spec/inputs/http_spec.rb
133
+ homepage: http://www.elastic.co/guide/en/logstash/current/index.html
134
+ licenses:
135
+ - Apache License (2.0)
136
+ metadata:
137
+ logstash_plugin: 'true'
138
+ logstash_group: input
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.4.5
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Logstash Input plugin that receives HTTP requests
159
+ test_files:
160
+ - spec/inputs/http_spec.rb