From bb023d0fde60b083b0a4a544875c59c2a3c48742 Mon Sep 17 00:00:00 2001 From: andsel Date: Mon, 24 May 2021 16:58:55 +0200 Subject: [PATCH] Added log of ECS enabled while 'target' option is not specified explicitly for logstash-filter-jdbc_static --- lib/logstash/filters/jdbc/lookup.rb | 4 ++++ lib/logstash/filters/jdbc_static.rb | 5 +++++ spec/filters/jdbc/lookup_spec.rb | 31 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/lib/logstash/filters/jdbc/lookup.rb b/lib/logstash/filters/jdbc/lookup.rb index ff7ac80..d24d3bd 100644 --- a/lib/logstash/filters/jdbc/lookup.rb +++ b/lib/logstash/filters/jdbc/lookup.rb @@ -56,6 +56,10 @@ def initialize(options, globals, default_id) @target = options["target"] @id_used_as_target = @target.nil? if @id_used_as_target + # target shouldn't be nil if ecs_compatibility is not :disabled + if globals[:ecs_compatibility] != :disabled + logger.info("When ECS compatibility is enabled also target option must be valued") + end @target = @id end @options = options diff --git a/lib/logstash/filters/jdbc_static.rb b/lib/logstash/filters/jdbc_static.rb index c1b5a9d..eb8bcaa 100644 --- a/lib/logstash/filters/jdbc_static.rb +++ b/lib/logstash/filters/jdbc_static.rb @@ -2,6 +2,7 @@ require "logstash-integration-jdbc_jars" require "logstash/filters/base" require "logstash/namespace" +require "logstash/plugin_mixins/ecs_compatibility_support" require_relative "jdbc/loader" require_relative "jdbc/loader_schedule" require_relative "jdbc/repeating_load_runner" @@ -14,6 +15,9 @@ # module LogStash module Filters class JdbcStatic < LogStash::Filters::Base + # adds ecs_compatibility config which could be :disabled or :v1 + include LogStash::PluginMixins::ECSCompatibilitySupport(:disabled, :v1) + config_name "jdbc_static" # Define the loaders, an Array of Hashes, to fetch remote data and create local tables. @@ -214,6 +218,7 @@ def global_lookup_options(options = Hash.new) options["lookup_jdbc_driver_class"] = @lookup_jdbc_driver_class options["lookup_jdbc_driver_library"] = @lookup_jdbc_driver_library options["lookup_jdbc_connection_string"] = @lookup_jdbc_connection_string + options["ecs_compatibility"] = @ecs_compatibility options end diff --git a/spec/filters/jdbc/lookup_spec.rb b/spec/filters/jdbc/lookup_spec.rb index 67ddda9..ada5f0d 100644 --- a/spec/filters/jdbc/lookup_spec.rb +++ b/spec/filters/jdbc/lookup_spec.rb @@ -248,6 +248,37 @@ module LogStash module Filters module Jdbc expect(subject.valid?).to be_falsey end end + + describe "validation of target option" do + let(:lookup_hash) do + { + "query" => "select * from servers WHERE ip LIKE ? AND os LIKE ?", + "prepared_parameters" => ["%%{[ip]}"], + } + end + + it "should log a warn when ECS is enabled and target not defined" do + + class LoggableLookup < Lookup + + @@TEST_LOGGER = nil + + def self.logger=(log) + @@TEST_LOGGER = log + end + + def self.logger + @@TEST_LOGGER + end + end + + spy_logger = double("logger") + expect(spy_logger).to receive(:info).once.with("When ECS compatibility is enabled also target option must be valued") + LoggableLookup.logger = spy_logger + + LoggableLookup.new(lookup_hash, {:ecs_compatibility => 'v1'}, "lookup-1") + end + end end end end end