Skip to content

Commit

Permalink
Merge pull request #190 from mashhurs/backport-pr-188
Browse files Browse the repository at this point in the history
Adding custom header support (#188)
  • Loading branch information
mashhurs authored Jan 29, 2025
2 parents ef38c6e + d72f5a8 commit e01e64d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 3.17.0
- Added support for custom headers [#190](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/190)

## 3.16.2
- Add `x-elastic-product-origin` header to Elasticsearch requests [#185](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/185)

Expand Down
11 changes: 11 additions & 0 deletions docs/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ This plugin supports the following configuration options plus the <<plugins-{typ
| <<plugins-{type}s-{plugin}-ca_trusted_fingerprint>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-cloud_auth>> |<<password,password>>|No
| <<plugins-{type}s-{plugin}-cloud_id>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-custom_headers>> |<<hash,hash>>|No
| <<plugins-{type}s-{plugin}-docinfo_fields>> |<<hash,hash>>|No
| <<plugins-{type}s-{plugin}-enable_sort>> |<<boolean,boolean>>|No
| <<plugins-{type}s-{plugin}-fields>> |<<array,array>>|No
Expand Down Expand Up @@ -228,6 +229,16 @@ Cloud ID, from the Elastic Cloud web console. If set `hosts` should not be used.
For more info, check out the
{logstash-ref}/connecting-to-cloud.html[Logstash-to-Cloud documentation].


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

* Value type is <<hash,hash>>
* Default value is empty

Pass a set of key value pairs as the headers sent in each request to Elasticsearch.
These custom headers will override any headers previously set by the plugin such as the User Agent or Authorization headers.

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

Expand Down
6 changes: 5 additions & 1 deletion lib/logstash/filters/elasticsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class LogStash::Filters::Elasticsearch < LogStash::Filters::Base
# Array of fields to copy from old event (found via elasticsearch) into new event
config :fields, :validate => :array, :default => {}

# Custom headers for Elasticsearch requests
config :custom_headers, :validate => :hash, :default => {}

# Hash of docinfo fields to copy from old event (found via elasticsearch) into new event
config :docinfo_fields, :validate => :hash, :default => {}

Expand Down Expand Up @@ -269,7 +272,8 @@ def client_options
:ssl => client_ssl_options,
:retry_on_failure => @retry_on_failure,
:retry_on_status => @retry_on_status,
:user_agent => prepare_user_agent
:user_agent => prepare_user_agent,
:custom_headers => @custom_headers
}
end

Expand Down
3 changes: 3 additions & 0 deletions lib/logstash/filters/elasticsearch/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ def initialize(logger, hosts, options = {})
api_key = options.fetch(:api_key, nil)
proxy = options.fetch(:proxy, nil)
user_agent = options[:user_agent]
custom_headers = options[:custom_headers]


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

transport_options[:pool_max] = 1000
transport_options[:pool_max_per_route] = 100
Expand Down
4 changes: 2 additions & 2 deletions logstash-filter-elasticsearch.gemspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Gem::Specification.new do |s|

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

# Files
Expand Down
24 changes: 24 additions & 0 deletions spec/filters/elasticsearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,30 @@
end
end

context "with custom headers" do
let(:config) do
{
"query" => "*",
"custom_headers" => { "Custom-Header-1" => "Custom Value 1", "Custom-Header-2" => "Custom Value 2" }
}
end

let(:plugin) { LogStash::Filters::Elasticsearch.new(config) }
let(:client_double) { double("client") }
let(:transport_double) { double("transport", options: { transport_options: { headers: config["custom_headers"] } }) }

before do
allow(plugin).to receive(:get_client).and_return(client_double)
allow(client_double).to receive(:client).and_return(transport_double)
end

it "sets custom headers" do
plugin.register
client = plugin.send(:get_client).client
expect(client.options[:transport_options][:headers]).to match(hash_including(config["custom_headers"]))
end
end

context "if query is on nested field" do
let(:config) do
{
Expand Down

0 comments on commit e01e64d

Please sign in to comment.