Skip to content

Commit 85a853b

Browse files
committed
Solved code style issues
1 parent 4a10ea5 commit 85a853b

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## 4.3.1
2-
- Support both, encoded and non encoded api-key formats on plugin configuration [#203](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/203)
2+
- Added support for encoded and non encoded api-key formats on plugin configuration [#203](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/203)
33

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

lib/logstash/filters/elasticsearch/client.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,14 @@ def setup_basic_auth(user, password)
103103
def setup_api_key(api_key)
104104
return {} unless (api_key&.value)
105105

106-
token = is_base64?(api_key.value) ? api_key.value : Base64.strict_encode64(api_key.value)
106+
token = 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
110+
def base64?(string)
111+
string == Base64.strict_encode64(Base64.strict_decode64(string))
112+
rescue ArgumentError
113+
false
116114
end
117115

118116
def get_transport_client_class

spec/filters/elasticsearch_spec.rb

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
require "logstash/filters/elasticsearch"
55
require "logstash/json"
66
require "cabin"
7-
require "manticore"
87
require "webrick"
98
require "uri"
109

@@ -325,26 +324,28 @@ def wait_receive_request
325324
end
326325

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

331-
scenarios = {
332-
'with non-encoded api-key' => LogStash::Util::Password.new('foo:bar'),
333-
'with encoded api-key' => LogStash::Util::Password.new(encoded_api_key)
334-
}
331+
shared_examples "a plugin that sets the ApiKey authorization header" do
332+
it "correctly sets the Authorization header" do
333+
plugin.register
334+
client= plugin.send(:get_client).client
335+
auth_header = extract_transport(client).options[:transport_options][:headers]['Authorization']
335336

336-
scenarios.each do |description, api_key_value|
337-
context description do
338-
let(:config) { super().merge('api_key' => api_key_value) }
337+
expect(auth_header).to eql("ApiKey #{encoded_api_key}")
338+
end
339+
end
339340

340-
it "should set authorization" do
341-
plugin.register
342-
client= plugin.send(:get_client).client
343-
auth_header = extract_transport(client).options[:transport_options][:headers]['Authorization']
341+
context "with a non-encoded API key" do
342+
let(:api_key_value) { "foo:bar" }
343+
it_behaves_like "a plugin that sets the ApiKey authorization header"
344+
end
344345

345-
expect(auth_header).to eql "ApiKey #{encoded_api_key}"
346-
end
347-
end
346+
context "with an encoded API key" do
347+
let(:api_key_value) { encoded_api_key }
348+
it_behaves_like "a plugin that sets the ApiKey authorization header"
348349
end
349350

350351
context 'user also set' do

0 commit comments

Comments
 (0)