Class: Fluent::SecureTcpInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_securetcp.rb

Instance Method Summary collapse

Constructor Details

#initializeSecureTcpInput

Returns a new instance of SecureTcpInput.



7
8
9
10
11
12
# File 'lib/fluent/plugin/in_securetcp.rb', line 7

def initialize
	super
	require "openssl"
	require "socket"
	require 'concurrent/executor/cached_thread_pool'
end

Instance Method Details

#configure(conf) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fluent/plugin/in_securetcp.rb', line 24

def configure(conf)		
  	super
	if !@tag && !@ssl_certificate && !@ssl_key
		      raise ConfigError,  "'tag' and 'ssl_certificate' and 'ssl_key'  parameter is required on secure tcp input"
	end
	if @format
		@parser = Plugin.new_parser(@format)
		@parser.configure(conf)
	end
	@connections = []
end

#runObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fluent/plugin/in_securetcp.rb', line 62

def run
		until @server.to_io.closed?			
     begin       
       conn = @server.accept
       next if conn.nil?
         if @max_connections
           @connections.reject! { |c| c.closed? }
           if @connections.size >= @max_connections
             conn.close # close for retry 
             sleep 1
             next
           end          
           @connections << conn
         end
         
         @thread_pool.post {
           begin
             while buf = conn.gets
               buf = buf.chomp
               to_server = buf 
               time = Time.now.getutc
               if @format
                 if @format = "json" && @add_ip 
                     sock_domain, remote_port, remote_hostname, remote_ip = conn.peeraddr
                     peer_ip = "\"peer_ip\":\"#{remote_ip}\","  
                     n = to_server.length
                     to_server = "{#{peer_ip}#{to_server.slice(1,n)}"
                  end
                 @parser.parse(to_server) { |time, record|
                   unless time && record 
                     log.warn "*pattern not match: #{msg.inspect} *"
                     next
                   end
                   
                   router.emit(tag,time,record)
                   }
               else
                 if @add_ip 
                   buff = conn.peeraddr.to_s + " - " + buff
                 end  
                 router.emit(tag,time,buf) 
               end
             end
           rescue OpenSSL::SSL::SSLError => e
             conn.close
             log.error "unexpected error", :error => e.to_s
             sleep 1
           rescue => e
               log.error "unexpected error:", :error => e.to_s
               next if e.message == "Connection reset by peer" #supress backtrace on connection reset
               log.error_backtrace
           end
         }
      rescue OpenSSL::SSL::SSLError => e
         conn.close
         log.error "unexpected error", :error => e.to_s
         sleep 1
      rescue => e
        if !@server.to_io.closed? #supress shutdown errors
           log.error "unexpected error", :error => e.to_s
           next if e.message == "Connection reset by peer" #supress backtrace on connection reset
           log.error_backtrace   
        end
      end   
    end
end

#shutdownObject



52
53
54
55
56
57
58
59
60
# File 'lib/fluent/plugin/in_securetcp.rb', line 52

def shutdown
    @server.shutdown rescue nil
	@server.close rescue nil
  @tcp_client.shutdown rescue nil
	@tcp_client.close rescue nil      
	@thread_pool.shutdown
	@thread.join
	super
end

#startObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fluent/plugin/in_securetcp.rb', line 36

def start
	super
	tcp_client          = TCPServer.new(bind,port)
	ssl_context         = OpenSSL::SSL::SSLContext.new
	ssl_context.cert    = OpenSSL::X509::Certificate.new(File.read(@ssl_certificate))
    if @ssl_key_passphrase 
	  ssl_context.key     = OpenSSL::PKey::RSA.new(File.read(@ssl_key),@ssl_key_passphrase)
    else 
      ssl_context.key     = OpenSSL::PKey::RSA.new(File.read(@ssl_key))
    end
	@server             = OpenSSL::SSL::SSLServer.new(tcp_client, ssl_context)
	@thread_pool        = Concurrent::CachedThreadPool.new(:idletime => 50) 
	@thread             = Thread.new(&method(:run))
    log.info "SSL listening on port: #{bind}:#{port} *"
end