From f1bf6ae681aa7754abbc68392d4919bc3c3f35cd Mon Sep 17 00:00:00 2001 From: Andrea Selva Date: Mon, 28 Jun 2021 16:48:01 +0200 Subject: [PATCH] Fix prepared_statement_bind_values to resolve nested event's fields (#76) JDBC streaming filter uses the setting `prepared_statement_bind_values` to bind event's fields or constants to SQL prepared statements. It resolves fields with the notation `[field_name]` but it's not able to match the nested form: `[field][nested]`, this PR changes the match regexp to fix it. --- CHANGELOG.md | 3 +++ .../jdbc_streaming/parameter_handler.rb | 2 +- logstash-integration-jdbc.gemspec | 2 +- .../jdbc_streaming/parameter_handler_spec.rb | 23 +++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 spec/plugin_mixins/jdbc_streaming/parameter_handler_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 32e81c2..4916643 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 5.1.2 + - Fix `prepared_statement_bind_values` in streaming filter to resolve nested event's fields[#76](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/76) + ## 5.1.1 - [DOC] Changed docs to indicate that logstash-jdbc-static requires local_table [#56](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/56). Fixes [#55](https://github.com/logstash-plugins/logstash-integration-jdbc/issues/55). diff --git a/lib/logstash/plugin_mixins/jdbc_streaming/parameter_handler.rb b/lib/logstash/plugin_mixins/jdbc_streaming/parameter_handler.rb index ab7747c..b1816a1 100644 --- a/lib/logstash/plugin_mixins/jdbc_streaming/parameter_handler.rb +++ b/lib/logstash/plugin_mixins/jdbc_streaming/parameter_handler.rb @@ -26,7 +26,7 @@ def self.build_bind_value_handler(given_value) return InterpolatedParameter.new(given_value) end - if given_value =~ /\A\s*\[[^\]]+\]\s*\z/ + if given_value =~ /\A(\s*\[[^\]]+\]\s*)*\z/ return FieldParameter.new(given_value) end diff --git a/logstash-integration-jdbc.gemspec b/logstash-integration-jdbc.gemspec index 6cc89aa..3e177b5 100755 --- a/logstash-integration-jdbc.gemspec +++ b/logstash-integration-jdbc.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'logstash-integration-jdbc' - s.version = '5.1.1' + s.version = '5.1.2' s.licenses = ['Apache License (2.0)'] s.summary = "Integration with JDBC - input and filter plugins" 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" diff --git a/spec/plugin_mixins/jdbc_streaming/parameter_handler_spec.rb b/spec/plugin_mixins/jdbc_streaming/parameter_handler_spec.rb new file mode 100644 index 0000000..0ef5d3c --- /dev/null +++ b/spec/plugin_mixins/jdbc_streaming/parameter_handler_spec.rb @@ -0,0 +1,23 @@ +# encoding: utf-8 +require "logstash/devutils/rspec/spec_helper" +require "logstash/plugin_mixins/jdbc_streaming/parameter_handler" + + +describe LogStash::PluginMixins::JdbcStreaming::ParameterHandler do + context "resolve field reference" do + let(:event) { ::LogStash::Event.new("field" => "field_value") } + + it "should resolve root field" do + handler = LogStash::PluginMixins::JdbcStreaming::ParameterHandler.build_bind_value_handler "[field]" + handler.extract_from(event) + expect(handler.extract_from(event)).to eq "field_value" + end + + it "should resolve nested field" do + event = ::LogStash::Event.from_json("{\"field\": {\"nested\": \"nested_field\"}}").first + handler = LogStash::PluginMixins::JdbcStreaming::ParameterHandler.build_bind_value_handler "[field][nested]" + handler.extract_from(event) + expect(handler.extract_from(event)).to eq "nested_field" + end + end +end \ No newline at end of file