Skip to content

Commit

Permalink
test codec against class name string to prevent class equivalence bug…
Browse files Browse the repository at this point in the history
… with a Delegator

Fixes elastic#11401
  • Loading branch information
colinsurprenant committed Dec 12, 2019
1 parent 71e702c commit 702efea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
7 changes: 4 additions & 3 deletions logstash-core/lib/logstash/inputs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ def fix_streaming_codecs
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
Expand Down
28 changes: 28 additions & 0 deletions logstash-core/spec/logstash/inputs/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,32 @@ def register; end
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

0 comments on commit 702efea

Please sign in to comment.