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 @@
## 5.2.1
- Added support for encoded and non encoded api-key formats on plugin configuration [#237](https://github.com/logstash-plugins/logstash-input-elasticsearch/pull/237)

## 5.2.0
- ES|QL support [#233](https://github.com/logstash-plugins/logstash-input-elasticsearch/pull/233)

Expand Down
10 changes: 8 additions & 2 deletions lib/logstash/inputs/elasticsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,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 prepare_user_agent
os_name = java.lang.System.getProperty('os.name')
os_version = java.lang.System.getProperty('os.version')
Expand Down
2 changes: 1 addition & 1 deletion logstash-input-elasticsearch.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|

s.name = 'logstash-input-elasticsearch'
s.version = '5.2.0'
s.version = '5.2.1'
s.licenses = ['Apache License (2.0)']
s.summary = "Reads query results from an Elasticsearch cluster"
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/inputs/elasticsearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -823,14 +823,28 @@ def synchronize_method!(object, method_name)
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(: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(: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