Skip to content

Commit da0a2d9

Browse files
jsvdflexitrevkarenzone
authored
Backport support for custom headers (#217)
Co-authored-by: flexitrev <[email protected]> Co-authored-by: Karen Metts <[email protected]>
1 parent f49b710 commit da0a2d9

File tree

5 files changed

+50
-16
lines changed

5 files changed

+50
-16
lines changed

CHANGELOG.md

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

docs/index.asciidoc

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ include::{include_path}/plugin_header.asciidoc[]
2323

2424
Read from an Elasticsearch cluster, based on search query results.
2525
This is useful for replaying test logs, reindexing, etc.
26-
You can periodically schedule ingestion using a cron syntax
26+
You can periodically schedule ingestion using a cron syntax
2727
(see `schedule` setting) or run the query one time to load
2828
data into Logstash.
2929

@@ -106,6 +106,7 @@ This plugin supports the following configuration options plus the <<plugins-{typ
106106
| <<plugins-{type}s-{plugin}-cloud_auth>> |<<password,password>>|No
107107
| <<plugins-{type}s-{plugin}-cloud_id>> |<<string,string>>|No
108108
| <<plugins-{type}s-{plugin}-connect_timeout_seconds>> | <<number,number>>|No
109+
| <<plugins-{type}s-{plugin}-custom_headers>> |<<hash,hash>>|No
109110
| <<plugins-{type}s-{plugin}-docinfo>> |<<boolean,boolean>>|No
110111
| <<plugins-{type}s-{plugin}-docinfo_fields>> |<<array,array>>|No
111112
| <<plugins-{type}s-{plugin}-docinfo_target>> |<<string,string>>|No
@@ -199,8 +200,18 @@ For more info, check out the
199200
The maximum amount of time, in seconds, to wait while establishing a connection to Elasticsearch.
200201
Connect timeouts tend to occur when Elasticsearch or an intermediate proxy is overloaded with requests and has exhausted its connection pool.
201202

203+
[id="plugins-{type}s-{plugin}-custom_headers"]
204+
===== `custom_headers`
205+
206+
* Value type is <<hash,hash>>
207+
* Default value is empty
208+
209+
Pass a set of key value pairs as the headers sent in each request to an elasticsearch node.
210+
The headers will be used for any kind of request.
211+
These custom headers will override any headers previously set by the plugin such as the User Agent or Authorization headers.
212+
202213
[id="plugins-{type}s-{plugin}-docinfo"]
203-
===== `docinfo`
214+
===== `docinfo`
204215

205216
* Value type is <<boolean,boolean>>
206217
* Default value is `false`
@@ -251,7 +262,7 @@ Example
251262

252263

253264
[id="plugins-{type}s-{plugin}-docinfo_fields"]
254-
===== `docinfo_fields`
265+
===== `docinfo_fields`
255266

256267
* Value type is <<array,array>>
257268
* Default value is `["_index", "_type", "_id"]`
@@ -262,7 +273,7 @@ option lists the metadata fields to save in the current event. See
262273
more information.
263274

264275
[id="plugins-{type}s-{plugin}-docinfo_target"]
265-
===== `docinfo_target`
276+
===== `docinfo_target`
266277

267278
* Value type is <<string,string>>
268279
* Default value depends on whether <<plugins-{type}s-{plugin}-ecs_compatibility>> is enabled:
@@ -286,7 +297,7 @@ this option names the field under which to store the metadata fields as subfield
286297
Controls this plugin's compatibility with the {ecs-ref}[Elastic Common Schema (ECS)].
287298

288299
[id="plugins-{type}s-{plugin}-hosts"]
289-
===== `hosts`
300+
===== `hosts`
290301

291302
* Value type is <<array,array>>
292303
* There is no default value for this setting.
@@ -296,18 +307,18 @@ can be either IP, HOST, IP:port, or HOST:port. The port defaults to
296307
9200.
297308

298309
[id="plugins-{type}s-{plugin}-index"]
299-
===== `index`
310+
===== `index`
300311

301312
* Value type is <<string,string>>
302313
* Default value is `"logstash-*"`
303314

304-
The index or alias to search.
315+
The index or alias to search.
305316
Check out {ref}/api-conventions.html#api-multi-index[Multi Indices
306317
documentation] in the Elasticsearch documentation for info on
307318
referencing multiple indices.
308319

309320
[id="plugins-{type}s-{plugin}-password"]
310-
===== `password`
321+
===== `password`
311322

312323
* Value type is <<password,password>>
313324
* There is no default value for this setting.
@@ -327,7 +338,7 @@ An empty string is treated as if proxy was not set, this is useful when using
327338
environment variables e.g. `proxy => '${LS_PROXY:}'`.
328339

329340
[id="plugins-{type}s-{plugin}-query"]
330-
===== `query`
341+
===== `query`
331342

332343
* Value type is <<string,string>>
333344
* Default value is `'{ "sort": [ "_doc" ] }'`
@@ -375,7 +386,7 @@ The default is 0 (no retry). This value should be equal to or greater than zero.
375386
NOTE: Partial failures - such as errors in a subset of all slices - can result in the entire query being retried, which can lead to duplication of data. Avoiding this would require Logstash to store the entire result set of a query in memory which is often not possible.
376387

377388
[id="plugins-{type}s-{plugin}-schedule"]
378-
===== `schedule`
389+
===== `schedule`
379390

380391
* Value type is <<string,string>>
381392
* There is no default value for this setting.
@@ -387,7 +398,7 @@ There is no schedule by default. If no schedule is given, then the statement is
387398
exactly once.
388399

389400
[id="plugins-{type}s-{plugin}-scroll"]
390-
===== `scroll`
401+
===== `scroll`
391402

392403
* Value type is <<string,string>>
393404
* Default value is `"1m"`
@@ -410,7 +421,7 @@ The query requires at least one `sort` field, as described in the <<plugins-{typ
410421
`scroll` uses {ref}/paginate-search-results.html#scroll-search-results[scroll] API to search, which is no longer recommended.
411422

412423
[id="plugins-{type}s-{plugin}-size"]
413-
===== `size`
424+
===== `size`
414425

415426
* Value type is <<number,number>>
416427
* Default value is `1000`
@@ -598,7 +609,7 @@ It is also possible to target an entry in the event's metadata, which will be av
598609

599610

600611
[id="plugins-{type}s-{plugin}-user"]
601-
===== `user`
612+
===== `user`
602613

603614
* Value type is <<string,string>>
604615
* There is no default value for this setting.

lib/logstash/inputs/elasticsearch.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
# called `http.content_type.required`. If this option is set to `true`, and you
2525
# are using Logstash 2.4 through 5.2, you need to update the Elasticsearch input
2626
# plugin to version 4.0.2 or higher.
27-
#
27+
#
2828
# ================================================================================
29-
#
29+
#
3030
# Read from an Elasticsearch cluster, based on search query results.
3131
# This is useful for replaying test logs, reindexing, etc.
3232
# It also supports periodically scheduling lookup enrichments
@@ -166,6 +166,9 @@ class LogStash::Inputs::Elasticsearch < LogStash::Inputs::Base
166166
# http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_document_metadata.html
167167
config :docinfo_fields, :validate => :array, :default => ['_index', '_type', '_id']
168168

169+
# Custom headers for Elasticsearch requests
170+
config :custom_headers, :validate => :hash, :default => {}
171+
169172
# Basic Auth - username
170173
config :user, :validate => :string
171174

@@ -305,6 +308,7 @@ def register
305308
transport_options[:headers].merge!(setup_basic_auth(user, password))
306309
transport_options[:headers].merge!(setup_api_key(api_key))
307310
transport_options[:headers].merge!({'user-agent' => prepare_user_agent()})
311+
transport_options[:headers].merge!(@custom_headers) unless @custom_headers.empty?
308312
transport_options[:request_timeout] = @request_timeout_seconds unless @request_timeout_seconds.nil?
309313
transport_options[:connect_timeout] = @connect_timeout_seconds unless @connect_timeout_seconds.nil?
310314
transport_options[:socket_timeout] = @socket_timeout_seconds unless @socket_timeout_seconds.nil?

logstash-input-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-input-elasticsearch'
4-
s.version = '4.20.5'
4+
s.version = '4.21.0'
55
s.licenses = ['Apache License (2.0)']
66
s.summary = "Reads query results from an Elasticsearch cluster"
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/inputs/elasticsearch_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@
103103
expect( extract_transport(client).options[:transport_options][:headers] ).to match hash_including("x-elastic-product-origin"=>"logstash-input-elasticsearch")
104104
end
105105
end
106+
107+
context "with custom headers" do
108+
let(:config) do
109+
{
110+
"schedule" => "* * * * * UTC",
111+
"custom_headers" => { "Custom-Header-1" => "Custom Value 1", "Custom-Header-2" => "Custom Value 2" }
112+
}
113+
end
114+
115+
116+
it "sets custom headers" do
117+
plugin.register
118+
client = plugin.send(:client)
119+
expect( extract_transport(client).options[:transport_options][:headers] ).to match hash_including(config["custom_headers"])
120+
end
121+
end
106122
end
107123

108124
context "retry" do

0 commit comments

Comments
 (0)