From 573536192079a0b72731f6de1044a4c14258aa66 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Wed, 5 Jul 2017 17:12:41 +0900 Subject: [PATCH 1/2] Add resolve_hostname config param in in_syslog, fix #1614 --- lib/fluent/plugin/in_syslog.rb | 13 +++++++++++-- test/plugin/test_in_syslog.rb | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/fluent/plugin/in_syslog.rb b/lib/fluent/plugin/in_syslog.rb index af08e4044f..06969fd9f2 100644 --- a/lib/fluent/plugin/in_syslog.rb +++ b/lib/fluent/plugin/in_syslog.rb @@ -93,6 +93,8 @@ def initialize config_param :source_host_key, :string, default: 'source_host'.freeze, deprecated: "use source_hostname_key instead" desc "The field name of the client's hostname." config_param :source_hostname_key, :string, default: nil + desc 'Try to resolve hostname from IP addresses or not.' + config_param :resolve_hostname, :bool, default: nil desc 'The field name of the priority.' config_param :priority_key, :string, default: nil desc 'The field name of the facility.' @@ -131,6 +133,13 @@ def configure(conf) if @source_hostname_key.nil? && @include_source_host @source_hostname_key = @source_host_key end + if @source_hostname_key + if @resolve_hostname.nil? + @resolve_hostname = true + elsif !@resolve_hostname # user specifies "false" in configure + raise Fluent::ConfigError, "resolve_hostname must be true with source_hostname_key" + end + end end def start @@ -236,10 +245,10 @@ def listen(callback) if @protocol_type == :udp @usock = SocketUtil.create_udp_socket(@bind) @usock.bind(@bind, @port) - SocketUtil::UdpHandler.new(@usock, log, @message_length_limit, callback, !!@source_hostname_key) + SocketUtil::UdpHandler.new(@usock, log, @message_length_limit, callback, @resolve_hostname) else # syslog family add "\n" to each message and this seems only way to split messages in tcp stream - Coolio::TCPServer.new(@bind, @port, SocketUtil::TcpHandler, log, "\n", callback, !!@source_hostname_key) + Coolio::TCPServer.new(@bind, @port, SocketUtil::TcpHandler, log, "\n", callback, @resolve_hostname) end end diff --git a/test/plugin/test_in_syslog.rb b/test/plugin/test_in_syslog.rb index 6658f69c49..0e67c2b049 100755 --- a/test/plugin/test_in_syslog.rb +++ b/test/plugin/test_in_syslog.rb @@ -35,6 +35,24 @@ def test_configure } end + sub_test_case 'source_hostname_key and source_address_key features' do + test 'resolve_hostname must be true with source_hostname_key' do + assert_raise(Fluent::ConfigError) { + create_driver(CONFIG + < 'resolve_hostname true', + 'source_hostname_key' => 'source_hostname_key source_host') + def test_configure_reslove_hostname(param) + d = create_driver([CONFIG, param].join("\n")) + assert_true d.instance.resolve_hostname + end + end + def test_time_format configs = {'127.0.0.1' => CONFIG} configs.merge!('::1' => IPv6_CONFIG) if ipv6_enabled? From 106f6931465b24a657accabd2c2cf7ea35b1e6b0 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 6 Jul 2017 12:11:46 +0900 Subject: [PATCH 2/2] Add support for source_address_key config parameter --- lib/fluent/plugin/in_syslog.rb | 3 ++ test/plugin/test_in_syslog.rb | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/lib/fluent/plugin/in_syslog.rb b/lib/fluent/plugin/in_syslog.rb index 06969fd9f2..d6566cfcac 100644 --- a/lib/fluent/plugin/in_syslog.rb +++ b/lib/fluent/plugin/in_syslog.rb @@ -93,6 +93,8 @@ def initialize config_param :source_host_key, :string, default: 'source_host'.freeze, deprecated: "use source_hostname_key instead" desc "The field name of the client's hostname." config_param :source_hostname_key, :string, default: nil + desc "The field name of the client's source address." + config_param :source_address_key, :string, default: nil desc 'Try to resolve hostname from IP addresses or not.' config_param :resolve_hostname, :bool, default: nil desc 'The field name of the priority.' @@ -232,6 +234,7 @@ def parse_text(text, addr, pri = nil) record[@priority_key] = priority if @priority_key record[@facility_key] = facility if @facility_key record[@source_hostname_key] = addr[2] if @source_hostname_key + record[@source_address_key] = addr[3] if @source_address_key tag = "#{@tag}.#{facility}.#{priority}" emit(tag, time, record) diff --git a/test/plugin/test_in_syslog.rb b/test/plugin/test_in_syslog.rb index 0e67c2b049..3818fb1819 100755 --- a/test/plugin/test_in_syslog.rb +++ b/test/plugin/test_in_syslog.rb @@ -45,6 +45,56 @@ def test_configure } end + LOCALHOST_HOSTNAME_GETTER = ->(){sock = UDPSocket.new(::Socket::AF_INET); sock.do_not_reverse_lookup = false; sock.connect("127.0.0.1", 2048); sock.peeraddr[2] } + LOCALHOST_HOSTNAME = LOCALHOST_HOSTNAME_GETTER.call + DUMMY_SOCK = Struct.new(:remote_host, :remote_addr, :remote_port).new(LOCALHOST_HOSTNAME, "127.0.0.1", 0) + data( + both: [:hostname, :address], + hostname: [:hostname], + address: [:address], + ) + test 'source_hostname_key and source_address_key parameter feature should add record(s)' do |keys| + conf = CONFIG.dup + if keys.include?(:hostname) + conf << < 'resolve_hostname true', 'source_hostname_key' => 'source_hostname_key source_host') def test_configure_reslove_hostname(param)