Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/fluent/plugin/out_remote_syslog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def configure(conf)
if @host.nil? && @host_with_port.nil?
raise ConfigError, "host or host_with_port is required"
end
# The host part of the configured value. An empty host here sends to
# the local host, so only an empty host after the expansion is an error.
@configured_host = (@host_with_port ? split_host_with_port(@host_with_port).first : @host).to_s

@formatter = formatter_create
unless @formatter.formatter_type == :text_per_line
Expand Down Expand Up @@ -110,6 +113,13 @@ def write(chunk)
port = @port
end

# A placeholder can expand to an empty string when the chunk has no
# value for its key. Retrying cannot fix such a chunk, so raise an
# unrecoverable error to move it to <secondary> or the backup at once.
if host.to_s.empty? && !@configured_host.empty?
raise Fluent::UnrecoverableError, "host is empty after placeholders are replaced: host=#{@host.inspect} host_with_port=#{@host_with_port.inspect}"
end

host_with_port = "#{host}:#{port}"

Thread.current[host_with_port] ||= create_sender(host, port)
Expand Down
63 changes: 63 additions & 0 deletions test/plugin/out_remote_syslog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,67 @@ def test_severity_mapper(data)
out = Fluent::Plugin::RemoteSyslogOutput::SeverityMapper.map(data[:in])
assert_equal(data[:out], out)
end

data("host udp", {target: %[host "${dest}"\nport 5568], protocol: "udp"})
data("host tcp", {target: %[host "${dest}"\nport 5568], protocol: "tcp"})
data("host_with_port udp", {target: %[host_with_port "${dest}:5568"], protocol: "udp"})
data("host_with_port tcp", {target: %[host_with_port "${dest}:5568"], protocol: "tcp"})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: host_with_port "${dest}" (without a port) reaches the host.nil? side of the check, because "".split(":") returns []. None of the four cases covers it. Could you add it?

Suggested change
data("host_with_port tcp", {target: %[host_with_port "${dest}:5568"], protocol: "tcp"})
data("host_with_port tcp", {target: %[host_with_port "${dest}:5568"], protocol: "tcp"})
data("host_with_port without port", {target: %[host_with_port "${dest}"], protocol: "udp"})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the host_with_port without port case in 0362839. bundle exec rake test: 47 tests, 0 failures.

data("host_with_port without port", {target: %[host_with_port "${dest}"], protocol: "udp"})
def test_write_raises_on_empty_host(data)
d = create_driver_out_syslog %[
@type remote_syslog
#{data[:target]}
protocol #{data[:protocol]}

<buffer dest>
</buffer>

<format>
@type single_value
message_key message
</format>
]

time = Fluent::EventTime.now
record = {"message" => "hello"}
chunk = Fluent::Plugin::Buffer::MemoryChunk.new(d.instance.metadata("tag", time, record))
chunk.append([d.instance.format("tag", time, record)])

error = assert_raise(Fluent::UnrecoverableError) { d.instance.write(chunk) }
assert_match(/host is empty/, error.message)
end

data("host with a port", {target: %[host ""\nport 5571], port: 5571})
data("host_with_port", {target: %[host_with_port ":5572"], port: 5572})
def test_write_with_empty_configured_host_sends_to_local_host(data)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MUST: This test fails on the windows-latest runner for all four Ruby versions: https://github.com/fluent-plugins-nursery/fluent-plugin-remote_syslog/actions/runs/45503271604/job/138967985435. TCPSocket.new raises Errno::EADDRNOTAVAIL for connect(2) for "" port 5571. Ruby resolves the empty host to 0.0.0.0, and Winsock does not connect to that address, so the message never reaches the listener. The plugin still passes the same empty host to the sender as on master, so only the delivery check depends on the platform. Could you skip this test on Windows? I asked for this test in my previous review and missed the platform difference.

Suggested change
def test_write_with_empty_configured_host_sends_to_local_host(data)
def test_write_with_empty_configured_host_sends_to_local_host(data)
omit_if(Fluent.windows?, "Windows cannot connect to 0.0.0.0")

d = create_driver_out_syslog %[
@type remote_syslog
#{data[:target]}
program minitest
protocol tcp

<format>
@type single_value
message_key message
</format>
]

server = TCPServer.new("127.0.0.1", data[:port])
received = Thread.new do
client = server.accept
line = client.gets
client.close
line
end
begin
d.run do
d.feed("tag", Fluent::EventTime.now, {"message" => "hello"})
end
assert_not_nil(received.join(5), "no message received")
assert_match(/minitest: hello/, received.value)
ensure
received.kill
server.close
end
end
end
Loading