Skip to content

Commit 21de990

Browse files
authored
Adding custom header support (#188)
* recreating PR 187 * update changelog * fixed client_options to include custom headers
1 parent ad77062 commit 21de990

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 4.1.0
2+
- Added support for custom headers [#188](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/188)
3+
14
## 4.0.0
25
- SSL settings that were marked deprecated in version `3.15.0` are now marked obsolete, and will prevent the plugin from starting.
36
- These settings are:

docs/index.asciidoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ NOTE: As of version `4.0.0` of this plugin, a number of previously deprecated se
134134
| <<plugins-{type}s-{plugin}-ca_trusted_fingerprint>> |<<string,string>>|No
135135
| <<plugins-{type}s-{plugin}-cloud_auth>> |<<password,password>>|No
136136
| <<plugins-{type}s-{plugin}-cloud_id>> |<<string,string>>|No
137+
| <<plugins-{type}s-{plugin}-custom_headers>> |<<hash,hash>>|No
137138
| <<plugins-{type}s-{plugin}-docinfo_fields>> |<<hash,hash>>|No
138139
| <<plugins-{type}s-{plugin}-enable_sort>> |<<boolean,boolean>>|No
139140
| <<plugins-{type}s-{plugin}-fields>> |<<array,array>>|No
@@ -230,6 +231,16 @@ Cloud ID, from the Elastic Cloud web console. If set `hosts` should not be used.
230231
For more info, check out the
231232
{logstash-ref}/connecting-to-cloud.html[Logstash-to-Cloud documentation].
232233

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

lib/logstash/filters/elasticsearch.rb

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

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

@@ -260,7 +263,8 @@ def client_options
260263
:ssl => client_ssl_options,
261264
:retry_on_failure => @retry_on_failure,
262265
:retry_on_status => @retry_on_status,
263-
:user_agent => prepare_user_agent
266+
:user_agent => prepare_user_agent,
267+
:custom_headers => @custom_headers
264268
}
265269
end
266270

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Gem::Specification.new do |s|
22

33
s.name = 'logstash-filter-elasticsearch'
4-
s.version = '4.0.0'
4+
s.version = '4.1.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"

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)