Skip to content

Commit 9842249

Browse files
committed
Support both, encoded and non encoded api-key formats on plugin configuration
1 parent 061fec8 commit 9842249

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

lib/logstash/inputs/elasticsearch.rb

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

565565
def setup_api_key(api_key)
566-
return {} unless (api_key && api_key.value)
566+
return {} unless (api_key&.value)
567567

568-
token = ::Base64.strict_encode64(api_key.value)
568+
token = is_base64?(api_key.value) ? api_key.value : Base64.strict_encode64(api_key.value)
569569
{ 'Authorization' => "ApiKey #{token}" }
570570
end
571571

572+
def is_base64?(string)
573+
begin
574+
string == Base64.strict_encode64(Base64.strict_decode64(string))
575+
rescue ArgumentError
576+
false
577+
end
578+
end
579+
572580
def prepare_user_agent
573581
os_name = java.lang.System.getProperty('os.name')
574582
os_version = java.lang.System.getProperty('os.version')

spec/inputs/elasticsearch_spec.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -823,14 +823,26 @@ def synchronize_method!(object, method_name)
823823
end
824824

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

828-
it "should set authorization" do
829-
plugin.register
830-
client = plugin.send(:client)
831-
auth_header = extract_transport(client).options[:transport_options][:headers]['Authorization']
829+
scenarios = {
830+
'with non-encoded api-key' => LogStash::Util::Password.new('foo:bar'),
831+
'with encoded api-key' => LogStash::Util::Password.new(encoded_api_key)
832+
}
833+
834+
scenarios.each do |description, api_key_value|
835+
context description do
836+
let(:config) { super().merge('api_key' => api_key_value) }
832837

833-
expect( auth_header ).to eql "ApiKey #{Base64.strict_encode64('foo:bar')}"
838+
it "should set authorization" do
839+
plugin.register
840+
client = plugin.send(:client)
841+
auth_header = extract_transport(client).options[:transport_options][:headers]['Authorization']
842+
843+
expect(auth_header).to eql "ApiKey #{encoded_api_key}"
844+
end
845+
end
834846
end
835847

836848
context 'user also set' do

0 commit comments

Comments
 (0)