Skip to content

Commit ddc575a

Browse files
authored
Tolerate failure (#183) (#218)
* Tolerate failure (#183) * test setup: ensure presence of /etc/protocols * test setup: actually run secure_integration tests When SECURE_INTEGRATION is speicified, the (non-secure) `:integration` specs are excluded, so we cannot have the `:secure_integration` specs wrapped in a context flagged as `:integration`. * test setup: regnerate test certs (and add regen script) * test setup: give ES the full cert chain In order for the `ca_trusted_fingerprint` specs to work with the CA's fingerprint, ES needs to be configured to present a cert chain that includes the CA. * resilience: prevent failures from crashing plugin When an Event cannot be created directly from the hit, or when the docinfo cannot be merged into a non-hash field in the hit, emit an Event tagged with `_elasticsearch_input_failure` that contains the JSON-encoded hit in `[event][original]` instead of crashing. * add link to changelog * remove orphan method from refactor * add link to PR in CHANGELOG.md
1 parent 675f759 commit ddc575a

File tree

14 files changed

+233
-49
lines changed

14 files changed

+233
-49
lines changed

.ci/Dockerfile.elasticsearch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ COPY --chown=elasticsearch:elasticsearch spec/fixtures/test_certs/* $es_path/con
1313

1414
RUN if [ "$SECURE_INTEGRATION" = "true" ] ; then echo "xpack.security.http.ssl.enabled: true" >> $es_yml; fi
1515
RUN if [ "$SECURE_INTEGRATION" = "true" ] ; then echo "xpack.security.http.ssl.key: $es_path/config/test_certs/es.key" >> $es_yml; fi
16-
RUN if [ "$SECURE_INTEGRATION" = "true" ] ; then echo "xpack.security.http.ssl.certificate: $es_path/config/test_certs/es.crt" >> $es_yml; fi
16+
RUN if [ "$SECURE_INTEGRATION" = "true" ] ; then echo "xpack.security.http.ssl.certificate: $es_path/config/test_certs/es.chain.crt" >> $es_yml; fi
1717
RUN if [ "$SECURE_INTEGRATION" = "true" ] ; then echo "xpack.security.http.ssl.certificate_authorities: [ '$es_path/config/test_certs/ca.crt' ]" >> $es_yml; fi
1818
RUN if [ "$SECURE_INTEGRATION" = "true" ] ; then echo "xpack.security.http.ssl.verification_mode: certificate" >> $es_yml; fi
1919
RUN if [ "$SECURE_INTEGRATION" = "true" ] && [ -n "$ES_SSL_SUPPORTED_PROTOCOLS" ] ; then echo "xpack.security.http.ssl.supported_protocols: ${ES_SSL_SUPPORTED_PROTOCOLS}" >> $es_yml; fi

.ci/setup.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# user_agent requires /etc/protocols, which is provided by netbase.
2+
# https://github.com/jruby/jruby/issues/3955
3+
if [ ! -f "/etc/protocols" ]; then
4+
if [ $(command -v apt-get) ]; then
5+
echo "installing netbase with apt-get"
6+
sudo apt-get install -y netbase
7+
else
8+
echo "installing netbase with yum"
9+
sudo yum install -y netbase
10+
fi
11+
fi

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 5.0.1
2+
- Fix: prevent plugin crash when hits contain illegal structure [#218](https://github.com/logstash-plugins/logstash-input-elasticsearch/pull/218)
3+
- When a hit cannot be converted to an event, the input now emits an event tagged with `_elasticsearch_input_failure` with an `[event][original]` containing a JSON-encoded string representation of the entire hit.
4+
15
## 5.0.0
26
- SSL settings that were marked deprecated in version `4.17.0` are now marked obsolete, and will prevent the plugin from starting.
37
- These settings are:

docs/index.asciidoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ The plugin logs a warning when ECS is enabled and `target` isn't set.
9393

9494
TIP: Set the `target` option to avoid potential schema conflicts.
9595

96+
[id="plugins-{type}s-{plugin}-failure-handling"]
97+
==== Failure handling
98+
99+
When this input plugin cannot create a structured `Event` from a hit result, it will instead create an `Event` that is tagged with `_elasticsearch_input_failure` whose `[event][original]` is a JSON-encoded string representation of the entire hit.
100+
101+
Common causes are:
102+
103+
- When the hit result contains top-level fields that are {logstash-ref}/processing.html#reserved-fields[reserved in Logstash] but do not have the expected shape. Use the <<plugins-{type}s-{plugin}-target>> directive to avoid conflicts with the top-level namespace.
104+
- When <<plugins-{type}s-{plugin}-docinfo>> is enabled and the docinfo fields cannot be merged into the hit result. Combine <<plugins-{type}s-{plugin}-target>> and <<plugins-{type}s-{plugin}-docinfo_target>> to avoid conflict.
105+
96106
[id="plugins-{type}s-{plugin}-options"]
97107
==== Elasticsearch Input configuration options
98108

lib/logstash/inputs/elasticsearch.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,21 +346,29 @@ def run(output_queue)
346346
# This can be called externally from the query_executor
347347
public
348348
def push_hit(hit, output_queue, root_field = '_source')
349-
event = targeted_event_factory.new_event hit[root_field]
350-
set_docinfo_fields(hit, event) if @docinfo
349+
event = event_from_hit(hit, root_field)
351350
decorate(event)
352351
output_queue << event
353352
end
354353

354+
def event_from_hit(hit, root_field)
355+
event = targeted_event_factory.new_event hit[root_field]
356+
set_docinfo_fields(hit, event) if @docinfo
357+
358+
event
359+
rescue => e
360+
serialized_hit = hit.to_json
361+
logger.warn("Event creation error, original data now in [event][original] field", message: e.message, exception: e.class, data: serialized_hit)
362+
return event_factory.new_event('event' => { 'original' => serialized_hit }, 'tags' => ['_elasticsearch_input_failure'])
363+
end
364+
355365
def set_docinfo_fields(hit, event)
356366
# do not assume event[@docinfo_target] to be in-place updatable. first get it, update it, then at the end set it in the event.
357367
docinfo_target = event.get(@docinfo_target) || {}
358368

359369
unless docinfo_target.is_a?(Hash)
360-
@logger.error("Incompatible Event, incompatible type for the docinfo_target=#{@docinfo_target} field in the `_source` document, expected a hash got:", :docinfo_target_type => docinfo_target.class, :event => event.to_hash_with_metadata)
361-
362-
# TODO: (colin) I am not sure raising is a good strategy here?
363-
raise Exception.new("Elasticsearch input: incompatible event")
370+
# expect error to be handled by `#event_from_hit`
371+
fail RuntimeError, "Incompatible event; unable to merge docinfo fields into docinfo_target=`#{@docinfo_target}`"
364372
end
365373

366374
@docinfo_fields.each do |field|

logstash-input-elasticsearch.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Gem::Specification.new do |s|
22

33
s.name = 'logstash-input-elasticsearch'
4-
s.version = '5.0.0'
4+
s.version = '5.0.1'
55
s.licenses = ['Apache License (2.0)']
66
s.summary = "Reads query results from an Elasticsearch cluster"
77
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"

spec/fixtures/test_certs/GENERATED_AT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2024-12-26T22:27:15+00:00

spec/fixtures/test_certs/ca.crt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
-----BEGIN CERTIFICATE-----
2-
MIIDSTCCAjGgAwIBAgIUUcAg9c8B8jiliCkOEJyqoAHrmccwDQYJKoZIhvcNAQEL
3-
BQAwNDEyMDAGA1UEAxMpRWxhc3RpYyBDZXJ0aWZpY2F0ZSBUb29sIEF1dG9nZW5l
4-
cmF0ZWQgQ0EwHhcNMjEwODEyMDUxNDU1WhcNMjQwODExMDUxNDU1WjA0MTIwMAYD
5-
VQQDEylFbGFzdGljIENlcnRpZmljYXRlIFRvb2wgQXV0b2dlbmVyYXRlZCBDQTCC
6-
ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK1HuusRuGNsztd4EQvqwcMr
7-
8XvnNNaalerpMOorCGySEFrNf0HxDIVMGMCrOv1F8SvlcGq3XANs2MJ4F2xhhLZr
8-
PpqVHx+QnSZ66lu5R89QVSuMh/dCMxhNBlOA/dDlvy+EJBl9H791UGy/ChhSgaBd
9-
OKVyGkhjErRTeMIq7rR7UG6GL/fV+JGy41UiLrm1KQP7/XVD9UzZfGq/hylFkTPe
10-
oox5BUxdxUdDZ2creOID+agtIYuJVIkelKPQ+ljBY3kWBRexqJQsvyNUs1gZpjpz
11-
YUCzuVcXDRuJXYQXGqWXhsBPfJv+ZcSyMIBUfWT/G13cWU1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gP5qC0h
10+
i4lUiR6Uo9D6WMFjeRYFF7GolCy/I1SzWBmmOnNhQLO5VxcNG4ldhBcapZeGwE98
11+
m/5lxLIwgFR9ZP8bXdxZTWLC58/LQ2NqOjA9mwIDAQABozIwMDAPBgNVHRMBAf8E
12+
BTADAQH/MB0GA1UdDgQWBBTIJMnuftpfkxNCOkbF0R4xgcKQRjANBgkqhkiG9w0B
13+
AQsFAAOCAQEAhfg/cmXc4Uh90yiXU8jOW8saQjTsq4ZMDQiLfJsNmNNYmHFN0vhv
14+
lJRI1STdy7+GpjS5QbrMjQIxWSS8X8xysE4Rt81IrWmLuao35TRFyoiE1seBQ5sz
15+
p/BxZUe57JvWi9dyzv2df4UfWFdGBhzdr80odZmz4i5VIv6qCKJKsGikcuLpepmp
16+
E/UKnKHeR/dFWsxzA9P2OzHTUNBMOOA2PyAUL49pwoChwJeOWN/zAgwMWLbuHFG0
17+
IN0u8swAmeH98QdvzbhiOatGNpqfTNvQEDc19yVjfXKpBVZQ79WtronYSqrbrUa1
18+
T2zD8bIVP7CdddD/UmpT1SSKh4PJxudy5Q==
2019
-----END CERTIFICATE-----
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
195a7e7b1bc29f3d7913a918a44721704d27fa56facea0cd72a8093c7107c283
1+
b1e955819b0d14f64f863adb103c248ddacf2e17bea48d04ee4b57c64814ccc4

spec/fixtures/test_certs/es.chain.crt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDIzCCAgugAwIBAgIBATANBgkqhkiG9w0BAQsFADA0MTIwMAYDVQQDEylFbGFz
3+
dGljIENlcnRpZmljYXRlIFRvb2wgQXV0b2dlbmVyYXRlZCBDQTAeFw0yNDEyMjYy
4+
MjI3MTVaFw0yNTEyMjYyMjI3MTVaMA0xCzAJBgNVBAMTAmVzMIIBIjANBgkqhkiG
5+
9w0BAQEFA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lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
-----BEGIN CERTIFICATE-----
2-
MIIDNjCCAh6gAwIBAgIUF9wE+oqGSbm4UVn1y9gEjzyaJFswDQYJKoZIhvcNAQEL
3-
BQAwNDEyMDAGA1UEAxMpRWxhc3RpYyBDZXJ0aWZpY2F0ZSBUb29sIEF1dG9nZW5l
4-
cmF0ZWQgQ0EwHhcNMjEwODEyMDUxNTI3WhcNMjQwODExMDUxNTI3WjANMQswCQYD
5-
VQQDEwJlczCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2S2by0lgyu
6-
1JfgGgZ41PNXbH2qMPMzowguVVdtZ16WM0CaEG7lnLxmMcC+2Q7NnGuFnPAVQo9T
7-
Q3bh7j+1PkCJVHUKZfJIeWtGc9+qXBcO1MhedfwM1osSa4bfwM85G+XKWbRNtmSt
8-
CoUuKArIyZkzdBAAQLBoQyPf3DIza1Au4j9Hb3zrswD6e7n2PN4ffIyil1GFduLJ
9-
2275qqFiOhkEDUhv7BKNftVBh/89O/5lSqAQGuQ1aDRr8TdHwhO71u4ZIU/Pn6yX
10-
LGBWrQG53+qpdCsxGvJTfbtIEYUDTN83CirIxDKJgc1QXOEldylztHf4xnQ7ZarJ
11-
tqF6pUzHbRsCAwEAAaNnMGUwHQYDVR0OBBYEFFQUK+6Cg2kExRj1xSDzEi4kkgKX
12-
MB8GA1UdIwQYMBaAFMgkye5+2l+TE0I6RsXRHjGBwpBGMBgGA1UdEQQRMA+CDWVs
13-
YXN0aWNzZWFyY2gwCQYDVR0TBAIwADANBgkqhkiG9w0BAQsFAAOCAQEAinaknZIc
14-
7xtQNwUwa+kdET+I4lMz+TJw9vTjGKPJqe082n81ycKU5b+a/OndG90z+dTwhShW
15-
f0oZdIe/1rDCdiRU4ceCZA4ybKrFDIbW8gOKZOx9rsgEx9XNELj4ocZTBqxjQmNE
16-
Ho91fli5aEm0EL2vJgejh4hcfDeElQ6go9g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U
15+
T1cP6VC/brmqP/dd5y7VWE7Lp+Wd5CxKl/WY+9chmgc+a1fW/lnPEJJ6pca1Bo8b
16+
byIL0yY2IUv4R2eh1IyQl9oGH1GOPLgO7cY04eajxYcOVA2eDSItoyDtrJfkFP/P
17+
UXtC1JAkvWKuujFEiBj0AannhroWlp3gvChhBwCuCAU0KXD6g8BE8tn6oT1+FW7J
18+
avSfHxAe+VHtYhF8sJ8jrdm0d7E4GKS9UR/pkLAL1JuRdJ1VkPx3
2019
-----END CERTIFICATE-----

spec/fixtures/test_certs/renew.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
cd "$(dirname "$0")"
5+
6+
openssl x509 -x509toreq -in ca.crt -copy_extensions copyall -signkey ca.key -out ca.csr
7+
openssl x509 -req -copy_extensions copyall -days 365 -in ca.csr -set_serial 0x01 -signkey ca.key -out ca.crt && rm ca.csr
8+
openssl x509 -in ca.crt -outform der | sha256sum | awk '{print $1}' > ca.der.sha256
9+
10+
openssl x509 -x509toreq -in es.crt -copy_extensions copyall -signkey es.key -out es.csr
11+
openssl x509 -req -copy_extensions copyall -days 365 -in es.csr -set_serial 0x01 -CA ca.crt -CAkey ca.key -out es.crt && rm es.csr
12+
cat es.crt ca.crt > es.chain.crt
13+
14+
# output ISO8601 timestamp to file
15+
date -Iseconds > GENERATED_AT

spec/inputs/elasticsearch_spec.rb

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,28 @@ def synchronize_method!(object, method_name)
666666
context 'if the `docinfo_target` exist but is not of type hash' do
667667
let(:config) { base_config.merge 'docinfo' => true, "docinfo_target" => 'metadata_with_string' }
668668
let(:do_register) { false }
669+
let(:mock_queue) { double('Queue', :<< => nil) }
670+
let(:hit) { response.dig('hits', 'hits').first }
671+
672+
it 'emits a tagged event with JSON-serialized event in [event][original]' do
673+
allow(plugin).to receive(:logger).and_return(double('Logger').as_null_object)
669674

670-
it 'raises an exception if the `docinfo_target` exist but is not of type hash' do
671-
expect(client).not_to receive(:clear_scroll)
672675
plugin.register
673-
expect { plugin.run([]) }.to raise_error(Exception, /incompatible event/)
676+
plugin.run(mock_queue)
677+
678+
expect(mock_queue).to have_received(:<<) do |event|
679+
expect(event).to be_a_kind_of LogStash::Event
680+
681+
expect(event.get('tags')).to include("_elasticsearch_input_failure")
682+
expect(event.get('[event][original]')).to be_a_kind_of String
683+
expect(JSON.load(event.get('[event][original]'))).to eq hit
684+
end
685+
686+
expect(plugin.logger)
687+
.to have_received(:warn).with(
688+
a_string_including("Event creation error, original data now in [event][original] field"),
689+
a_hash_including(:message => a_string_including('unable to merge docinfo fields into docinfo_target=`metadata_with_string`'),
690+
:data => a_string_including('"_id":"C5b2xLQwTZa76jBmHIbwHQ"')))
674691
end
675692

676693
end
@@ -1248,6 +1265,88 @@ def wait_receive_request
12481265
end
12491266
end
12501267

1268+
context '#push_hit' do
1269+
let(:config) do
1270+
{
1271+
'docinfo' => true, # include ids
1272+
'docinfo_target' => '[@metadata][docinfo]'
1273+
}
1274+
end
1275+
1276+
let(:hit) do
1277+
JSON.load(<<~EOJSON)
1278+
{
1279+
"_index" : "test_bulk_index_2",
1280+
"_type" : "_doc",
1281+
"_id" : "sHe6A3wBesqF7ydicQvG",
1282+
"_score" : 1.0,
1283+
"_source" : {
1284+
"@timestamp" : "2021-09-20T15:02:02.557Z",
1285+
"message" : "ping",
1286+
"@version" : "17",
1287+
"sequence" : 7,
1288+
"host" : {
1289+
"name" : "maybe.local",
1290+
"ip" : "127.0.0.1"
1291+
}
1292+
}
1293+
}
1294+
EOJSON
1295+
end
1296+
1297+
let(:mock_queue) { double('queue', :<< => nil) }
1298+
1299+
it 'pushes a generated event to the queue' do
1300+
plugin.send(:push_hit, hit, mock_queue)
1301+
expect(mock_queue).to have_received(:<<) do |event|
1302+
expect(event).to be_a_kind_of LogStash::Event
1303+
1304+
# fields overriding defaults
1305+
expect(event.timestamp.to_s).to eq("2021-09-20T15:02:02.557Z")
1306+
expect(event.get('@version')).to eq("17")
1307+
1308+
# structure from hit's _source
1309+
expect(event.get('message')).to eq("ping")
1310+
expect(event.get('sequence')).to eq(7)
1311+
expect(event.get('[host][name]')).to eq("maybe.local")
1312+
expect(event.get('[host][ip]')).to eq("127.0.0.1")
1313+
1314+
# docinfo fields
1315+
expect(event.get('[@metadata][docinfo][_index]')).to eq("test_bulk_index_2")
1316+
expect(event.get('[@metadata][docinfo][_type]')).to eq("_doc")
1317+
expect(event.get('[@metadata][docinfo][_id]')).to eq("sHe6A3wBesqF7ydicQvG")
1318+
end
1319+
end
1320+
1321+
context 'when event creation fails' do
1322+
before(:each) do
1323+
allow(plugin).to receive(:logger).and_return(double('Logger').as_null_object)
1324+
1325+
allow(plugin.event_factory).to receive(:new_event).and_call_original
1326+
allow(plugin.event_factory).to receive(:new_event).with(a_hash_including hit['_source']).and_raise(RuntimeError, 'intentional')
1327+
end
1328+
1329+
it 'pushes a tagged event containing a JSON-encoded hit in [event][original]' do
1330+
plugin.send(:push_hit, hit, mock_queue)
1331+
1332+
expect(mock_queue).to have_received(:<<) do |event|
1333+
expect(event).to be_a_kind_of LogStash::Event
1334+
1335+
expect(event.get('tags')).to include("_elasticsearch_input_failure")
1336+
expect(event.get('[event][original]')).to be_a_kind_of String
1337+
expect(JSON.load(event.get('[event][original]'))).to eq hit
1338+
end
1339+
1340+
expect(plugin.logger)
1341+
.to have_received(:warn).with(
1342+
a_string_including("Event creation error, original data now in [event][original] field"),
1343+
a_hash_including(:message => a_string_including('intentional'),
1344+
:data => a_string_including('"_id":"sHe6A3wBesqF7ydicQvG"')))
1345+
1346+
end
1347+
end
1348+
end
1349+
12511350
# @note can be removed once we depends on elasticsearch gem >= 6.x
12521351
def extract_transport(client) # on 7.x client.transport is a ES::Transport::Client
12531352
client.transport.respond_to?(:transport) ? client.transport.transport : client.transport

spec/inputs/integration/elasticsearch_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require "logstash/inputs/elasticsearch"
55
require_relative "../../../spec/es_helper"
66

7-
describe LogStash::Inputs::Elasticsearch, :integration => true do
7+
describe LogStash::Inputs::Elasticsearch do
88

99
SECURE_INTEGRATION = ENV['SECURE_INTEGRATION'].eql? 'true'
1010

0 commit comments

Comments
 (0)