Skip to content

Commit 6864955

Browse files
committed
Add cookies and basic_auth_eager options
Make automatic cookie management and to use eager/preemptive Basic Authentication configurable.
1 parent 443b07b commit 6864955

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

Diff for: lib/logstash/outputs/elasticsearch.rb

+11
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,17 @@ class LogStash::Outputs::ElasticSearch < LogStash::Outputs::Base
229229
# Custom Headers to send on each request to elasticsearch nodes
230230
config :custom_headers, :validate => :hash, :default => {}
231231

232+
# Enable automatic cookie management between requests
233+
config :cookies, :validate => :boolean, :default => false
234+
235+
# Eagerly offer the Authorization header before the server challenges for it when using Basic Auth
236+
config :basic_auth_eager, :validate => :boolean, :default => true
237+
238+
def build_client
239+
params["metric"] = metric
240+
@client ||= ::LogStash::Outputs::ElasticSearch::HttpClientBuilder.build(@logger, @hosts, params)
241+
end
242+
232243
def build_client
233244
params["metric"] = metric
234245
@client ||= ::LogStash::Outputs::ElasticSearch::HttpClientBuilder.build(@logger, @hosts, params)

Diff for: lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def perform_request(url, method, path, params={}, body=nil)
6060
# We have to unescape the password here since manticore won't do it
6161
# for us unless its part of the URL
6262
:password => CGI.unescape(url.password),
63-
:eager => true
63+
:eager => params["basic_auth_eager"]
6464
}
6565
end
6666

Diff for: lib/logstash/outputs/elasticsearch/http_client_builder.rb

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module LogStash; module Outputs; class ElasticSearch;
44
module HttpClientBuilder
55
def self.build(logger, hosts, params)
66
client_settings = {
7+
:cookies => params["cookies"],
78
:pool_max => params["pool_max"],
89
:pool_max_per_route => params["pool_max_per_route"],
910
:check_connection_timeout => params["validate_after_inactivity"],
@@ -146,6 +147,7 @@ def self.setup_basic_auth(logger, params)
146147
return {} unless user && password && password.value
147148

148149
{
150+
:basic_auth_eager => params["basic_auth_eager"],
149151
:user => CGI.escape(user),
150152
:password => CGI.escape(password.value)
151153
}

Diff for: spec/unit/http_client_builder_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
allow(secured).to receive(:value).and_return(password)
1414
secured
1515
end
16+
let(:basic_auth_eager) { true }
1617
let(:options) { {"user" => user, "password" => password} }
1718
let(:logger) { mock("logger") }
18-
let(:auth_setup) { klass.setup_basic_auth(double("logger"), {"user" => user, "password" => password_secured}) }
19+
let(:auth_setup) { klass.setup_basic_auth(double("logger"), {"user" => user, "password" => password_secured, "basic_auth_eager" => basic_auth_eager}) }
1920

2021
it "should return the user escaped" do
2122
expect(auth_setup[:user]).to eql(CGI.escape(user))

Diff for: spec/unit/outputs/elasticsearch/http_client/manticore_adapter_spec.rb

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
describe LogStash::Outputs::ElasticSearch::HttpClient::ManticoreAdapter do
55
let(:logger) { Cabin::Channel.get }
6-
let(:options) { {} }
6+
let(:options) { {
7+
"basic_auth_eager" => true
8+
} }
79

810
subject { described_class.new(logger, options) }
911

@@ -19,6 +21,10 @@
1921
describe "auth" do
2022
let(:user) { "myuser" }
2123
let(:password) { "mypassword" }
24+
let(:basic_auth_eager) { true }
25+
let(:params) { {
26+
"basic_auth_eager" => basic_auth_eager
27+
} }
2228
let(:noauth_uri) { clone = uri.clone; clone.user=nil; clone.password=nil; clone }
2329
let(:uri) { ::LogStash::Util::SafeURI.new("http://#{user}:#{password}@localhost:9200") }
2430

@@ -32,15 +38,16 @@
3238

3339
expect(subject.manticore).to receive(:get).
3440
with(expected_uri.to_s, {
41+
"basic_auth_eager" => basic_auth_eager,
3542
:headers => {"Content-Type" => "application/json"},
3643
:auth => {
3744
:user => user,
3845
:password => password,
39-
:eager => true
46+
:eager => basic_auth_eager
4047
}
4148
}).and_return resp
4249

43-
subject.perform_request(uri, :get, "/")
50+
subject.perform_request(uri, :get, "/", params)
4451
end
4552
end
4653

0 commit comments

Comments
 (0)