Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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.3.1
- Added support for encoded and non encoded api-key formats on plugin configuration [#203](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/203)

## 4.3.0
- ES|QL support [#194](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/194)

Expand Down
10 changes: 8 additions & 2 deletions lib/logstash/filters/elasticsearch/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,18 @@ def setup_basic_auth(user, password)
end

def setup_api_key(api_key)
return {} unless (api_key && api_key.value)
return {} unless (api_key&.value)

token = ::Base64.strict_encode64(api_key.value)
token = base64?(api_key.value) ? api_key.value : Base64.strict_encode64(api_key.value)
{ 'Authorization' => "ApiKey #{token}" }
end

def base64?(string)
string == Base64.strict_encode64(Base64.strict_decode64(string))
rescue ArgumentError
false
end

def get_transport_client_class
# LS-core includes `elasticsearch` gem. The gem is composed of two separate gems: `elasticsearch-api` and `elasticsearch-transport`
# And now `elasticsearch-transport` is old, instead we have `elastic-transport`.
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.3.0'
s.version = '4.3.1'
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
26 changes: 20 additions & 6 deletions spec/filters/elasticsearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,28 @@ def wait_receive_request
end

context "with ssl" do
let(:config) { super().merge({ 'api_key' => LogStash::Util::Password.new('foo:bar'), "ssl_enabled" => true }) }
let(:api_key_value) { nil }
let(:config) { super().merge("ssl_enabled" => true, 'api_key' => LogStash::Util::Password.new(api_key_value)) }
let(:encoded_api_key) { Base64.strict_encode64('foo:bar') }

it "should set authorization" do
plugin.register
client = plugin.send(:get_client).client
auth_header = extract_transport(client).options[:transport_options][:headers]['Authorization']
shared_examples "a plugin that sets the ApiKey authorization header" do
it "correctly sets the Authorization header" do
plugin.register
client= plugin.send(:get_client).client
auth_header = extract_transport(client).options[:transport_options][:headers]['Authorization']

expect(auth_header).to eql("ApiKey #{encoded_api_key}")
end
end

context "with a non-encoded API key" do
let(:api_key_value) { "foo:bar" }
it_behaves_like "a plugin that sets the ApiKey authorization header"
end

expect( auth_header ).to eql "ApiKey #{Base64.strict_encode64('foo:bar')}"
context "with an encoded API key" do
let(:api_key_value) { encoded_api_key }
it_behaves_like "a plugin that sets the ApiKey authorization header"
end

context 'user also set' do
Expand Down