Skip to content

Commit

Permalink
allow changing the default target field for host and headers data
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvd committed Sep 20, 2017
1 parent 8e8266f commit c5a439d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 3.1.0
- adds `request_headers_target_field` and `remote_host_target_field` configuration options with defaults to `host` and `headers` respectively

## 3.0.6
- Fix some documentation issues

Expand Down
16 changes: 16 additions & 0 deletions docs/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ The TCP port to bind to

specify a custom set of response headers

[id="plugins-{type}s-{plugin}-remote_host_target_field"]
===== `remote_host_target_field`

* Value type is <<string,string>>
* Default value is `"host"`

specify a target field for the client host of the http request

[id="plugins-{type}s-{plugin}-request_headers_target_field"]
===== `request_headers_target_field`

* Value type is <<string,string>>
* Default value is `"headers"`

specify target field for the client host of the http request

[id="plugins-{type}s-{plugin}-ssl"]
===== `ssl`

Expand Down
10 changes: 8 additions & 2 deletions lib/logstash/inputs/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ class LogStash::Inputs::Http < LogStash::Inputs::Base
# specify a custom set of response headers
config :response_headers, :validate => :hash, :default => { 'Content-Type' => 'text/plain' }

# target field for the client host of the http request
config :remote_host_target_field, :validate => :string, :default => "host"

# target field for the client host of the http request
config :request_headers_target_field, :validate => :string, :default => "headers"

# useless headers puma adds to the requests
# mostly due to rack compliance
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"]
Expand Down Expand Up @@ -138,8 +144,8 @@ def run(queue)
req = lowercase_keys(req)
body = req.delete("rack.input")
@codecs.fetch(req["content_type"], @codec).decode(body.read) do |event|
event.set("host", remote_host)
event.set("headers", req)
event.set(@remote_host_target_field, remote_host)
event.set(@request_headers_target_field, req)
decorate(event)
queue << event
end
Expand Down
2 changes: 1 addition & 1 deletion logstash-input-http.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'logstash-input-http'
s.version = '3.0.6'
s.version = '3.1.0'
s.licenses = ['Apache License (2.0)']
s.summary = "Logstash Input plugin that receives HTTP requests"
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 gemname. This gem is not a stand-alone program"
Expand Down
55 changes: 48 additions & 7 deletions spec/inputs/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,60 @@
end

describe "request handling" do
subject { LogStash::Inputs::Http.new }
subject { LogStash::Inputs::Http.new(config) }

before :each do
subject.register
t = Thread.new { subject.run(queue) }
sleep 0.01 until subject.instance_variable_get(:@server).running == 0
end

it "should include remote host in \"host\" property" do
agent.post!("http://localhost:8080/meh.json",
:headers => { "content-type" => "text/plain" },
:body => "hello")
event = queue.pop
expect(event.get("host")).to eq("127.0.0.1")
describe "remote host" do
context "by default" do
let(:config) { {} }
it "is written to the \"host\" field" do
agent.post!("http://localhost:8080/meh.json",
:headers => { "content-type" => "text/plain" },
:body => "hello")
event = queue.pop
expect(event.get("host")).to eq("127.0.0.1")
end
end
context "when using remote_host_target_field" do
let(:config) { { "remote_host_target_field" => "remote_host" } }
it "is written to the value of \"remote_host_target_field\" property" do
agent.post!("http://localhost:8080/meh.json",
:headers => { "content-type" => "text/plain" },
:body => "hello")
event = queue.pop
expect(event.get("remote_host")).to eq("127.0.0.1")
end
end
end

describe "request headers" do
context "by default" do
let(:config) { {} }
it "are written to the \"headers\" field" do
agent.post!("http://localhost:8080/meh.json",
:headers => { "content-type" => "text/plain" },
:body => "hello")
event = queue.pop
expect(event.get("headers")).to be_a(Hash)
expect(event.get("headers")).to include("request_method" => "POST")
end
end
context "when using request_headers_target_field" do
let(:config) { { "request_headers_target_field" => "request_headers" } }
it "are written to the field set in \"request_headers_target_field\"" do
agent.post!("http://localhost:8080/meh.json",
:headers => { "content-type" => "text/plain" },
:body => "hello")
event = queue.pop
expect(event.get("request_headers")).to be_a(Hash)
expect(event.get("request_headers")).to include("request_method" => "POST")
end
end
end

context "with default codec" do
Expand Down

0 comments on commit c5a439d

Please sign in to comment.