diff --git a/CHANGELOG.md b/CHANGELOG.md index e6d1cec..4d2eeb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/logstash/filters/elasticsearch/client.rb b/lib/logstash/filters/elasticsearch/client.rb index 3174550..9ac1a6f 100644 --- a/lib/logstash/filters/elasticsearch/client.rb +++ b/lib/logstash/filters/elasticsearch/client.rb @@ -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`. diff --git a/logstash-filter-elasticsearch.gemspec b/logstash-filter-elasticsearch.gemspec index 214b130..55c24b8 100644 --- a/logstash-filter-elasticsearch.gemspec +++ b/logstash-filter-elasticsearch.gemspec @@ -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" diff --git a/spec/filters/elasticsearch_spec.rb b/spec/filters/elasticsearch_spec.rb index 4c1deb4..eb219a0 100644 --- a/spec/filters/elasticsearch_spec.rb +++ b/spec/filters/elasticsearch_spec.rb @@ -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