test codec against class name string to prevent class equivalence bug with a Delegator

Fixes #11401
This commit is contained in:
Colin Surprenant 2019-12-06 16:32:35 -05:00
parent 8fd34b0681
commit c69e61fd2f
2 changed files with 32 additions and 3 deletions

View file

@ -131,11 +131,12 @@ class LogStash::Inputs::Base < LogStash::Plugin
require "logstash/codecs/line"
require "logstash/codecs/json"
require "logstash/codecs/json_lines"
case @codec
when LogStash::Codecs::Plain
case @codec.class.name
when "LogStash::Codecs::Plain"
@logger.info("Automatically switching from #{@codec.class.config_name} to line codec", :plugin => self.class.config_name)
@codec = LogStash::Codecs::Line.new("charset" => @codec.charset)
when LogStash::Codecs::JSON
when "LogStash::Codecs::JSON"
@logger.info("Automatically switching from #{@codec.class.config_name} to json_lines codec", :plugin => self.class.config_name)
@codec = LogStash::Codecs::JSONLines.new("charset" => @codec.charset)
end

View file

@ -113,4 +113,32 @@ describe "LogStash::Inputs::Base#fix_streaming_codecs" do
tcp.instance_eval { fix_streaming_codecs }
expect(tcp.codec.charset).to eq("CP1252")
end
it "should switch plain codec to line" do
require "logstash/inputs/tcp"
require "logstash/codecs/plain"
require "logstash/codecs/line"
# it is important to use "codec" => "plain" here and not the LogStash::Codecs::Plain instance so that
# the config parsing wrap the codec into the delagator which was causing the codec identification bug
# per https://github.com/elastic/logstash/issues/11140
tcp = LogStash::Inputs::Tcp.new("codec" => "plain", "port" => 0)
tcp.register
expect(tcp.codec.class.name).to eq("LogStash::Codecs::Line")
end
it "should switch json codec to json_lines" do
require "logstash/inputs/tcp"
require "logstash/codecs/plain"
require "logstash/codecs/line"
# it is important to use "codec" => "json" here and not the LogStash::Codecs::Plain instance so that
# the config parsing wrap the codec into the delagator which was causing the codec identification bug
# per https://github.com/elastic/logstash/issues/11140
tcp = LogStash::Inputs::Tcp.new("codec" => "json", "port" => 0)
tcp.register
expect(tcp.codec.class.name).to eq("LogStash::Codecs::JSONLines")
end
end