Skip to content

Commit a0451cb

Browse files
committed
normalise hosts to URI::Generics, which reliably preserve defaults
An upstream bug in the Elasticsearch Ruby Client's handling of `String` host arguments that begin with a schema (e.g., `https://localhost`) causes it to default to port 80 or 443, depending on the schema, instead of Elasticsearch's port 9200. Since the Elasticsearch Ruby Client will accept a `URI` in this case, and will correctly handle falling through to appropriate defaults, we normalise to `URI::Generic`, which does not have a default port. We absorb the `ssl => true` case into this normalisation, as its previous implementation prevented the use of non-default ports in the array provided to `hosts`. Supersedes: #104 Resolves: #110
1 parent a8ae485 commit a0451cb

File tree

4 files changed

+234
-12
lines changed

4 files changed

+234
-12
lines changed

docs/index.asciidoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ Comma-delimited list of `<field>:<direction>` pairs that define the sort order
281281
* Value type is <<boolean,boolean>>
282282
* Default value is `false`
283283

284-
SSL
284+
Force SSL/TLS secured communication to Elasticsearch cluster.
285+
Leaving this unspecified will use whatever scheme is specified in the URLs listed in <<plugins-{type}s-{plugin}-hosts>>, where mixed schemes are supported.
286+
If SSL is set to `true`, the plugin will refuse to start if any of the hosts specifies an `http://` scheme.
285287

286288
[id="plugins-{type}s-{plugin}-tag_on_failure"]
287289
===== `tag_on_failure`

lib/logstash/filters/elasticsearch.rb

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def register
7171
@query_dsl = file.read
7272
end
7373

74+
@normalised_hosts = normalise_hosts(@hosts, @ssl)
75+
7476
test_connection!
7577
end # def register
7678

@@ -140,8 +142,7 @@ def filter(event)
140142
private
141143
def client_options
142144
{
143-
:ssl => @ssl,
144-
:hosts => @hosts,
145+
:hosts => @normalised_hosts,
145146
:ca_file => @ca_file,
146147
:logger => @logger
147148
}
@@ -191,4 +192,48 @@ def extract_total_from_hits(hits)
191192
def test_connection!
192193
get_client.client.ping
193194
end
195+
196+
private
197+
198+
##
199+
# Map the provided array-of-strings to an array of `URI::Generic`
200+
# instances, which the Elasticsearch client can use to establish
201+
# connections.
202+
#
203+
# @param hosts [Array<String>]: one or more hosts, each is one of:
204+
# - a bare hostname or ip, optionally
205+
# followed by a colon and port number
206+
# - a qualified URL with http/https schema
207+
# @param force_ssl [Boolean]: true to force SSL; will cause failure if one
208+
# or more hosts explicitly supplies non-SSL
209+
# scheme (e.g., `http`).
210+
#
211+
# @return [Array<URI::Generic>]
212+
def normalise_hosts(hosts, force_ssl)
213+
hosts.map do |input|
214+
if force_ssl && input.start_with?('http://')
215+
logger.error("Plugin configured to force SSL with `ssl => true`, " +
216+
"but a host explicitly declared non-https URL `#{input}`")
217+
218+
raise LogStash::ConfigurationError, "Aborting due to conflicting configuration"
219+
end
220+
221+
if input.start_with?('http://','https://')
222+
URI::Generic.new(*URI.split(input))
223+
else
224+
host, port = input.split(':')
225+
URI::Generic.new(
226+
force_ssl ? 'https' : 'http',
227+
nil, # userinfo,
228+
host,
229+
port,
230+
nil, # registry
231+
nil, # path
232+
nil, # opaque
233+
nil, # query
234+
nil # fragment
235+
)
236+
end
237+
end
238+
end
194239
end #class LogStash::Filters::Elasticsearch

lib/logstash/filters/elasticsearch/client.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@ class ElasticsearchClient
1111
attr_reader :client
1212

1313
def initialize(user, password, options={})
14-
ssl = options.fetch(:ssl, false)
15-
hosts = options[:hosts]
16-
@logger = options[:logger]
14+
hosts = options.fetch(:hosts)
15+
@logger = options.fetch(:logger)
1716

1817
transport_options = {}
1918
if user && password
2019
token = ::Base64.strict_encode64("#{user}:#{password.value}")
2120
transport_options[:headers] = { Authorization: "Basic #{token}" }
2221
end
2322

24-
hosts.map! {|h| { host: h, scheme: 'https' } } if ssl
2523
# set ca_file even if ssl isn't on, since the host can be an https url
2624
ssl_options = { ssl: true, ca_file: options[:ca_file] } if options[:ca_file]
2725
ssl_options ||= {}

spec/filters/elasticsearch_spec.rb

Lines changed: 182 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,194 @@
44
require "logstash/filters/elasticsearch"
55
require "logstash/json"
66

7+
RSpec::Matchers.define(:hash_with_member) do |member, matcher=nil|
8+
match do |actual|
9+
expect(actual).to be_a Hash
10+
expect(actual).to include member
11+
12+
value = actual[member]
13+
14+
block_arg && block_arg.call(value)
15+
16+
matcher.nil? || matcher.matches?(value)
17+
end
18+
19+
description do
20+
desc = "hash with member `#{member.inspect}`"
21+
caveats = []
22+
caveats << 'the provided block' unless block_arg.nil?
23+
caveats << matcher.description unless matcher.nil?
24+
25+
desc += ' satisfying ' unless caveats.empty?
26+
desc += caveats.join(' and ')
27+
desc
28+
end
29+
end
30+
731
describe LogStash::Filters::Elasticsearch do
832

933
context "registration" do
1034

11-
let(:plugin) { LogStash::Plugin.lookup("filter", "elasticsearch").new({}) }
12-
before do
13-
allow(plugin).to receive(:test_connection!)
35+
let(:plugin_class) { LogStash::Plugin.lookup("filter", "elasticsearch") }
36+
let(:plugin) { plugin_class.new(config) }
37+
let(:config) { Hash.new }
38+
39+
context 'with defaults' do
40+
before do
41+
allow(plugin).to receive(:test_connection!)
42+
end
43+
44+
it "should not raise an exception" do
45+
expect {plugin.register}.to_not raise_error
46+
end
1447
end
1548

16-
it "should not raise an exception" do
17-
expect {plugin.register}.to_not raise_error
49+
context 'hosts' do
50+
let(:config) do
51+
super().merge(
52+
'hosts' => hosts
53+
)
54+
end
55+
let(:hosts) do
56+
fail NotImplementedError, 'spec or spec group must define `hosts`.'
57+
end
58+
59+
let(:client_stub) { double(:client).as_null_object }
60+
let(:logger_stub) { double(:logger).as_null_object }
61+
62+
before(:each) do
63+
allow(plugin).to receive(:logger).and_return(logger_stub)
64+
end
65+
66+
context 'with schema://hostname' do
67+
let(:hosts) { ['http://foo.local', 'http://bar.local'] }
68+
69+
it 'creates client with URIs that do not include a port' do
70+
expect(::Elasticsearch::Client).to receive(:new) do |options|
71+
expect(options).to include :hosts
72+
expect(options[:hosts]).to be_an Array
73+
expect(options[:hosts]).to include(having_attributes(host: 'foo.local', scheme: 'http', port: nil))
74+
expect(options[:hosts]).to include(having_attributes(host: 'bar.local', scheme: 'http', port: nil))
75+
end.and_return(client_stub)
76+
77+
plugin.register
78+
end
79+
end
80+
81+
context 'with `ssl => true`' do
82+
let(:config) { super().merge('ssl' => 'true') }
83+
context 'and one or more explicitly-http hosts' do
84+
let(:hosts) { ['https://foo.local', 'http://bar.local'] }
85+
86+
it 'raises an exception' do
87+
expect { plugin.register }.to raise_error(LogStash::ConfigurationError)
88+
end
89+
90+
it 'emits a helpful log message' do
91+
plugin.register rescue nil
92+
expect(plugin.logger).to have_received(:error).with(match(/force SSL/))
93+
end
94+
end
95+
96+
context 'and all explicitly-https hosts' do
97+
let(:hosts) { ['https://foo.local', 'https://bar.local'] }
98+
99+
it 'sets the schemas on all to https' do
100+
expect(::Elasticsearch::Client).to receive(:new) do |options|
101+
expect(options).to include :hosts
102+
expect(options[:hosts]).to be_an Array
103+
options[:hosts].each do |host|
104+
expect(host).to be_an URI
105+
expect(host.scheme).to eq 'https'
106+
end
107+
end.and_return(client_stub)
108+
109+
plugin.register
110+
end
111+
end
112+
113+
context 'and one or more schemaless hosts' do
114+
let(:hosts) { ['https://foo.local', 'bar.local'] }
115+
116+
it 'sets the schemas on all to https' do
117+
expect(::Elasticsearch::Client).to receive(:new) do |options|
118+
expect(options).to include :hosts
119+
expect(options[:hosts]).to be_an Array
120+
options[:hosts].each do |host|
121+
expect(host).to be_an URI
122+
expect(host.scheme).to eq 'https'
123+
end
124+
end.and_return(client_stub)
125+
126+
plugin.register
127+
end
128+
end
129+
end
130+
131+
{
132+
'with `ssl => false' => {'ssl' => 'false'},
133+
'without `ssl` directive' => {}
134+
}.each do |context_string, config_override|
135+
context(context_string) do
136+
let(:config) { super().merge(config_override) }
137+
138+
context 'with a mix of http and https hosts' do
139+
let(:hosts) { ['https://foo.local', 'http://bar.local'] }
140+
it 'does not modify the protocol' do
141+
expect(::Elasticsearch::Client).to receive(:new) do |options|
142+
expect(options).to include :hosts
143+
expect(options[:hosts]).to be_an Array
144+
expect(options[:hosts]).to include(having_attributes(host: 'foo.local', scheme: 'https'))
145+
expect(options[:hosts]).to include(having_attributes(host: 'bar.local', scheme: 'http'))
146+
end.and_return(client_stub)
147+
148+
plugin.register
149+
end
150+
end
151+
152+
context 'with https-only hosts' do
153+
let(:hosts) { ['https://foo.local', 'https://bar.local'] }
154+
it 'does not modify the protocol' do
155+
expect(::Elasticsearch::Client).to receive(:new) do |options|
156+
expect(options).to include :hosts
157+
expect(options[:hosts]).to be_an Array
158+
expect(options[:hosts]).to include(having_attributes(host: 'foo.local', scheme: 'https'))
159+
expect(options[:hosts]).to include(having_attributes(host: 'bar.local', scheme: 'https'))
160+
end.and_return(client_stub)
161+
162+
plugin.register
163+
end
164+
end
165+
166+
context 'with http-only hosts' do
167+
let(:hosts) { ['http://foo.local', 'http://bar.local'] }
168+
it 'does not modify the protocol' do
169+
expect(::Elasticsearch::Client).to receive(:new) do |options|
170+
expect(options).to include :hosts
171+
expect(options[:hosts]).to be_an Array
172+
expect(options[:hosts]).to include(having_attributes(host: 'foo.local', scheme: 'http'))
173+
expect(options[:hosts]).to include(having_attributes(host: 'bar.local', scheme: 'http'))
174+
end.and_return(client_stub)
175+
176+
plugin.register
177+
end
178+
end
179+
180+
context 'with one or more schemaless hosts' do
181+
let(:hosts) { ['foo.local', 'bar.local'] }
182+
it 'defaults to the http protocol' do
183+
expect(::Elasticsearch::Client).to receive(:new) do |options|
184+
expect(options).to include :hosts
185+
expect(options[:hosts]).to be_an Array
186+
expect(options[:hosts]).to include(having_attributes(host: 'foo.local', scheme: 'http'))
187+
expect(options[:hosts]).to include(having_attributes(host: 'bar.local', scheme: 'http'))
188+
end.and_return(client_stub)
189+
190+
plugin.register
191+
end
192+
end
193+
end
194+
end
18195
end
19196
end
20197

0 commit comments

Comments
 (0)