Skip to content

Commit 11c27d6

Browse files
committed
Support both, encoded and non encoded api-key formats on plugin configuration
1 parent 7618e9b commit 11c27d6

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export ES_VERSION=5.1.1
5959

6060
- Edit Logstash `Gemfile` and add the local plugin path, for example:
6161
```ruby
62-
gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
62+
gem "logstash-output-elasticsearch", :path => "/your/local/logstash-output-elasticsearch"
6363
```
6464
- Install plugin
6565
```sh
@@ -72,8 +72,10 @@ bin/plugin install --no-verify
7272
```
7373
- Run Logstash with your plugin
7474
```sh
75-
bin/logstash -e 'filter {awesome {}}'
75+
bin/logstash -e 'output { elasticsearch { hosts => ["http://example:9200"] user => "example" password => "example" } }'
7676
```
77+
** Note **: This is a simple example for elasticsearch output plugin config, for more detailed information check [Logstash documentation](https://www.elastic.co/docs/reference/logstash/plugins/plugins-outputs-elasticsearch).
78+
7779
At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
7880

7981
#### 2.2 Run in an installed Logstash
@@ -82,7 +84,7 @@ You can use the same **2.1** method to run your plugin in an installed Logstash
8284

8385
- Build your plugin gem
8486
```sh
85-
gem build logstash-filter-awesome.gemspec
87+
gem build logstash-output-elasticsearch.gemspec
8688
```
8789
- Install the plugin from the Logstash home
8890
```sh

lib/logstash/outputs/elasticsearch/http_client_builder.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,14 @@ def self.setup_basic_auth(logger, params)
188188
def self.setup_api_key(logger, params)
189189
api_key = params["api_key"]
190190

191-
return {} unless (api_key && api_key.value)
191+
return {} unless (api_key&.value)
192192

193-
{ "Authorization" => "ApiKey " + Base64.strict_encode64(api_key.value) }
193+
api_key_value = api_key.value
194+
if api_key_value =~ /\A[^:]+:[^:]+\z/
195+
api_key_value = Base64.strict_encode64(api_key_value)
196+
end
197+
198+
{ "Authorization" => "ApiKey " + api_key_value }
194199
end
195200

196201
private

spec/unit/http_client_builder_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,44 @@
2626
end
2727
end
2828

29+
describe "auth setup with api-key" do
30+
let(:klass) { LogStash::Outputs::ElasticSearch::HttpClientBuilder }
31+
32+
context "when api-key is not encoded (id:api-key)" do
33+
let(:api_key) { "id:api-key" }
34+
let(:api_key_secured) do
35+
secured = double("api_key")
36+
allow(secured).to receive(:value).and_return(api_key)
37+
secured
38+
end
39+
let(:options) { { "api_key" => api_key_secured } }
40+
let(:logger) { double("logger") }
41+
let(:api_key_header) { klass.setup_api_key(logger, options) }
42+
43+
it "returns the correct encoded api-key header" do
44+
expected = "ApiKey #{Base64.strict_encode64(api_key)}"
45+
expect(api_key_header["Authorization"]).to eql(expected)
46+
end
47+
end
48+
49+
context "when api-key is already encoded" do
50+
let(:api_key) { Base64.strict_encode64("id:api-key") }
51+
let(:api_key_secured) do
52+
secured = double("api_key")
53+
allow(secured).to receive(:value).and_return(api_key)
54+
secured
55+
end
56+
let(:options) { { "api_key" => api_key_secured } }
57+
let(:logger) { double("logger") }
58+
let(:api_key_header) { klass.setup_api_key(logger, options) }
59+
60+
it "returns the api-key header as is" do
61+
expected = "ApiKey #{api_key}"
62+
expect(api_key_header["Authorization"]).to eql(expected)
63+
end
64+
end
65+
end
66+
2967
describe "customizing action paths" do
3068
let(:hosts) { [ ::LogStash::Util::SafeURI.new("http://localhost:9200") ] }
3169
let(:options) { {"hosts" => hosts } }

0 commit comments

Comments
 (0)