Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding custom header support #188

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 4.1.0
- Added support for custom headers [#187](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/187)
flexitrev marked this conversation as resolved.
Show resolved Hide resolved

## 4.0.0
- SSL settings that were marked deprecated in version `3.15.0` are now marked obsolete, and will prevent the plugin from starting.
- These settings are:
Expand Down
11 changes: 11 additions & 0 deletions docs/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ NOTE: As of version `4.0.0` of this plugin, a number of previously deprecated se
| <<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 @@ -230,6 +231,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
3 changes: 3 additions & 0 deletions lib/logstash/filters/elasticsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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
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
2 changes: 1 addition & 1 deletion logstash-filter-elasticsearch.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|

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