Skip to content

Commit 957b22b

Browse files
committed
Support both, encoded and non encoded api-key formats on plugin configuration
1 parent 62da4f3 commit 957b22b

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

lib/logstash/filters/elasticsearch/client.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,20 @@ def setup_basic_auth(user, password)
101101
end
102102

103103
def setup_api_key(api_key)
104-
return {} unless (api_key && api_key.value)
104+
return {} unless (api_key&.value)
105105

106-
token = ::Base64.strict_encode64(api_key.value)
106+
token = is_base64?(api_key.value) ? api_key.value : Base64.strict_encode64(api_key.value)
107107
{ 'Authorization' => "ApiKey #{token}" }
108108
end
109109

110+
def is_base64?(string)
111+
begin
112+
string == Base64.strict_encode64(Base64.strict_decode64(string))
113+
rescue ArgumentError
114+
false
115+
end
116+
end
117+
110118
def get_transport_client_class
111119
# LS-core includes `elasticsearch` gem. The gem is composed of two separate gems: `elasticsearch-api` and `elasticsearch-transport`
112120
# And now `elasticsearch-transport` is old, instead we have `elastic-transport`.

spec/filters/elasticsearch_spec.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,26 @@ def wait_receive_request
324324
end
325325

326326
context "with ssl" do
327-
let(:config) { super().merge({ 'api_key' => LogStash::Util::Password.new('foo:bar'), "ssl_enabled" => true }) }
327+
let(:config) { super().merge("ssl_enabled" => true) }
328+
encoded_api_key = Base64.strict_encode64('foo:bar')
328329

329-
it "should set authorization" do
330-
plugin.register
331-
client = plugin.send(:get_client).client
332-
auth_header = extract_transport(client).options[:transport_options][:headers]['Authorization']
330+
scenarios = {
331+
'with non-encoded api-key' => LogStash::Util::Password.new('foo:bar'),
332+
'with encoded api-key' => LogStash::Util::Password.new(encoded_api_key)
333+
}
334+
335+
scenarios.each do |description, api_key_value|
336+
context description do
337+
let(:config) { super().merge('api_key' => api_key_value) }
333338

334-
expect( auth_header ).to eql "ApiKey #{Base64.strict_encode64('foo:bar')}"
339+
it "should set authorization" do
340+
plugin.register
341+
client= plugin.send(:get_client).client
342+
auth_header = extract_transport(client).options[:transport_options][:headers]['Authorization']
343+
344+
expect(auth_header).to eql "ApiKey #{encoded_api_key}"
345+
end
346+
end
335347
end
336348

337349
context 'user also set' do

0 commit comments

Comments
 (0)