From 9fefde9c645c5c676310c975f66a1783065047c7 Mon Sep 17 00:00:00 2001 From: Andrea Selva Date: Mon, 11 Oct 2021 10:02:05 +0200 Subject: [PATCH] Verify connection to Elasticsearch (#150) Update Elasticsearch ruby client to version 7.14 and manage the not valid product error --- .travis.yml | 3 --- CHANGELOG.md | 3 +++ lib/logstash/filters/elasticsearch.rb | 6 +++++- logstash-filter-elasticsearch.gemspec | 4 ++-- spec/filters/elasticsearch_spec.rb | 30 +++++++++++++++++++++++---- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 214999b..5372982 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,12 +2,9 @@ import: - logstash-plugins/.ci:travis/travis.yml@1.x env: - - INTEGRATION=false ELASTIC_STACK_VERSION=6.x - INTEGRATION=false ELASTIC_STACK_VERSION=7.x - - INTEGRATION=true ELASTIC_STACK_VERSION=6.x ELASTIC_PASSWORD=password ELASTIC_SECURITY_ENABLED=true - INTEGRATION=true ELASTIC_STACK_VERSION=7.x - INTEGRATION=true ELASTIC_STACK_VERSION=7.x ELASTIC_PASSWORD=password ELASTIC_SECURITY_ENABLED=true - INTEGRATION=true ELASTIC_STACK_VERSION=7.x SNAPSHOT=true - INTEGRATION=true ELASTIC_STACK_VERSION=8.x SNAPSHOT=true - - INTEGRATION=true ELASTIC_STACK_VERSION=7.10.2 ELASTIC_PASSWORD=password ELASTIC_SECURITY_ENABLED=true - INTEGRATION=true ELASTIC_STACK_VERSION=7.14.0 ELASTIC_PASSWORD=password ELASTIC_SECURITY_ENABLED=true diff --git a/CHANGELOG.md b/CHANGELOG.md index 66e5e49..1747944 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 3.11.0 + - Feat: update Elasticsearch client to 7.14.0 [#150](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/150) + ## 3.10.0 - Feat: add user-agent header passed to the Elasticsearch HTTP connection [#152](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/152) diff --git a/lib/logstash/filters/elasticsearch.rb b/lib/logstash/filters/elasticsearch.rb index 695060e..4a9eeed 100644 --- a/lib/logstash/filters/elasticsearch.rb +++ b/lib/logstash/filters/elasticsearch.rb @@ -320,6 +320,10 @@ def parse_user_password_from_cloud_auth(cloud_auth) end def test_connection! - get_client.client.ping + begin + get_client.client.ping + rescue Elasticsearch::UnsupportedProductError + raise LogStash::ConfigurationError, "Could not connect to a compatible version of Elasticsearch" + end end end #class LogStash::Filters::Elasticsearch diff --git a/logstash-filter-elasticsearch.gemspec b/logstash-filter-elasticsearch.gemspec index 2326a44..c53d2db 100644 --- a/logstash-filter-elasticsearch.gemspec +++ b/logstash-filter-elasticsearch.gemspec @@ -1,7 +1,7 @@ Gem::Specification.new do |s| s.name = 'logstash-filter-elasticsearch' - s.version = '3.10.0' + s.version = '3.11.0' s.licenses = ['Apache License (2.0)'] s.summary = "Copies fields from previous log events in Elasticsearch to current events " 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" @@ -21,7 +21,7 @@ Gem::Specification.new do |s| # Gem dependencies s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99" - s.add_runtime_dependency 'elasticsearch', ">= 5.0.5" # LS >= 6.7 and < 7.14 all used version 5.0.5 + s.add_runtime_dependency 'elasticsearch', ">= 7.14.0" # LS >= 6.7 and < 7.14 all used version 5.0.5 s.add_runtime_dependency 'manticore', ">= 0.7.1" s.add_development_dependency 'cabin', ['~> 0.6'] s.add_development_dependency 'webrick' diff --git a/spec/filters/elasticsearch_spec.rb b/spec/filters/elasticsearch_spec.rb index 3db5c7d..8625f90 100644 --- a/spec/filters/elasticsearch_spec.rb +++ b/spec/filters/elasticsearch_spec.rb @@ -12,12 +12,34 @@ context "registration" do let(:plugin) { LogStash::Plugin.lookup("filter", "elasticsearch").new({}) } - before do - allow(plugin).to receive(:test_connection!) + + context "against authentic Elasticsearch" do + before do + allow(plugin).to receive(:test_connection!) + end + + it "should not raise an exception" do + expect {plugin.register}.to_not raise_error + end end - it "should not raise an exception" do - expect {plugin.register}.to_not raise_error + context "against not authentic Elasticsearch" do + let(:failing_client) do + client = double("client") + allow(client).to receive(:ping).and_raise Elasticsearch::UnsupportedProductError + + client_wrapper = double("filter_client") + allow(client_wrapper).to receive(:client).and_return client + client_wrapper + end + + before do + allow(plugin).to receive(:get_client).and_return(failing_client) + end + + it "should raise ConfigurationError" do + expect {plugin.register}.to raise_error(LogStash::ConfigurationError) + end end end