Skip to content

Commit e01e64d

Browse files
authored
Merge pull request #190 from mashhurs/backport-pr-188
Adding custom header support (#188)
2 parents ef38c6e + d72f5a8 commit e01e64d

File tree

6 files changed

+48
-3
lines changed

6 files changed

+48
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.17.0
2+
- Added support for custom headers [#190](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/190)
3+
14
## 3.16.2
25
- Add `x-elastic-product-origin` header to Elasticsearch requests [#185](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/185)
36

docs/index.asciidoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ This plugin supports the following configuration options plus the <<plugins-{typ
131131
| <<plugins-{type}s-{plugin}-ca_trusted_fingerprint>> |<<string,string>>|No
132132
| <<plugins-{type}s-{plugin}-cloud_auth>> |<<password,password>>|No
133133
| <<plugins-{type}s-{plugin}-cloud_id>> |<<string,string>>|No
134+
| <<plugins-{type}s-{plugin}-custom_headers>> |<<hash,hash>>|No
134135
| <<plugins-{type}s-{plugin}-docinfo_fields>> |<<hash,hash>>|No
135136
| <<plugins-{type}s-{plugin}-enable_sort>> |<<boolean,boolean>>|No
136137
| <<plugins-{type}s-{plugin}-fields>> |<<array,array>>|No
@@ -228,6 +229,16 @@ Cloud ID, from the Elastic Cloud web console. If set `hosts` should not be used.
228229
For more info, check out the
229230
{logstash-ref}/connecting-to-cloud.html[Logstash-to-Cloud documentation].
230231

232+
233+
[id="plugins-{type}s-{plugin}-custom_headers"]
234+
===== `custom_headers`
235+
236+
* Value type is <<hash,hash>>
237+
* Default value is empty
238+
239+
Pass a set of key value pairs as the headers sent in each request to Elasticsearch.
240+
These custom headers will override any headers previously set by the plugin such as the User Agent or Authorization headers.
241+
231242
[id="plugins-{type}s-{plugin}-docinfo_fields"]
232243
===== `docinfo_fields`
233244

lib/logstash/filters/elasticsearch.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class LogStash::Filters::Elasticsearch < LogStash::Filters::Base
3333
# Array of fields to copy from old event (found via elasticsearch) into new event
3434
config :fields, :validate => :array, :default => {}
3535

36+
# Custom headers for Elasticsearch requests
37+
config :custom_headers, :validate => :hash, :default => {}
38+
3639
# Hash of docinfo fields to copy from old event (found via elasticsearch) into new event
3740
config :docinfo_fields, :validate => :hash, :default => {}
3841

@@ -269,7 +272,8 @@ def client_options
269272
:ssl => client_ssl_options,
270273
:retry_on_failure => @retry_on_failure,
271274
:retry_on_status => @retry_on_status,
272-
:user_agent => prepare_user_agent
275+
:user_agent => prepare_user_agent,
276+
:custom_headers => @custom_headers
273277
}
274278
end
275279

lib/logstash/filters/elasticsearch/client.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ def initialize(logger, hosts, options = {})
2020
api_key = options.fetch(:api_key, nil)
2121
proxy = options.fetch(:proxy, nil)
2222
user_agent = options[:user_agent]
23+
custom_headers = options[:custom_headers]
24+
2325

2426
transport_options = { }
2527
transport_options[:headers] = options.fetch(:serverless, false) ? DEFAULT_EAV_HEADER.dup : {}
2628
transport_options[:headers].merge!(setup_basic_auth(user, password))
2729
transport_options[:headers].merge!(setup_api_key(api_key))
2830
transport_options[:headers].merge!({ 'user-agent' => "#{user_agent}" })
2931
transport_options[:headers].merge!(INTERNAL_ORIGIN_HEADER)
32+
transport_options[:headers].merge!(custom_headers) unless custom_headers.empty?
3033

3134
transport_options[:pool_max] = 1000
3235
transport_options[:pool_max_per_route] = 100

logstash-filter-elasticsearch.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Gem::Specification.new do |s|
22

33
s.name = 'logstash-filter-elasticsearch'
4-
s.version = '3.16.2'
4+
s.version = '3.17.0'
55
s.licenses = ['Apache License (2.0)']
66
s.summary = "Copies fields from previous log events in Elasticsearch to current events "
77
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"
88
s.authors = ["Elastic"]
99
s.email = 'info@elastic.co'
10-
s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
10+
s.homepage = "https://elastic.co/logstash"
1111
s.require_paths = ["lib"]
1212

1313
# Files

spec/filters/elasticsearch_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,30 @@
333333
end
334334
end
335335

336+
context "with custom headers" do
337+
let(:config) do
338+
{
339+
"query" => "*",
340+
"custom_headers" => { "Custom-Header-1" => "Custom Value 1", "Custom-Header-2" => "Custom Value 2" }
341+
}
342+
end
343+
344+
let(:plugin) { LogStash::Filters::Elasticsearch.new(config) }
345+
let(:client_double) { double("client") }
346+
let(:transport_double) { double("transport", options: { transport_options: { headers: config["custom_headers"] } }) }
347+
348+
before do
349+
allow(plugin).to receive(:get_client).and_return(client_double)
350+
allow(client_double).to receive(:client).and_return(transport_double)
351+
end
352+
353+
it "sets custom headers" do
354+
plugin.register
355+
client = plugin.send(:get_client).client
356+
expect(client.options[:transport_options][:headers]).to match(hash_including(config["custom_headers"]))
357+
end
358+
end
359+
336360
context "if query is on nested field" do
337361
let(:config) do
338362
{

0 commit comments

Comments
 (0)