Class: Fluent::SecureForwardOutput

Inherits:
ObjectBufferedOutput
  • Object
show all
Includes:
Mixin::ConfigPlaceholders
Defined in:
lib/fluent/plugin/out_secure_forward.rb,
lib/fluent/plugin/out_secure_forward.rb

Defined Under Namespace

Modules: OpenSSLUtil Classes: Node

Constant Summary collapse

DEFAULT_SECURE_CONNECT_PORT =
24284
FORWARD_HEADER =

MessagePack FixArray length = 2

[0x92].pack('C')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSecureForwardOutput

Returns a new instance of SecureForwardOutput.



65
66
67
68
69
70
71
72
# File 'lib/fluent/plugin/out_secure_forward.rb', line 65

def initialize
  super
  require 'socket'
  require 'openssl'
  require 'digest'
  require 'resolve/hostname'
  require 'securerandom'
end

Instance Attribute Details

#hostname_resolverObject (readonly)

Returns the value of attribute hostname_resolver.



63
64
65
# File 'lib/fluent/plugin/out_secure_forward.rb', line 63

def hostname_resolver
  @hostname_resolver
end

#nodesObject (readonly)

Returns the value of attribute nodes.



61
62
63
# File 'lib/fluent/plugin/out_secure_forward.rb', line 61

def nodes
  @nodes
end

#read_intervalObject (readonly)

Returns the value of attribute read_interval.



49
50
51
# File 'lib/fluent/plugin/out_secure_forward.rb', line 49

def read_interval
  @read_interval
end

#socket_intervalObject (readonly)

Returns the value of attribute socket_interval.



49
50
51
# File 'lib/fluent/plugin/out_secure_forward.rb', line 49

def socket_interval
  @socket_interval
end

Instance Method Details

#configure(conf) ⇒ Object



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
# File 'lib/fluent/plugin/out_secure_forward.rb', line 79

def configure(conf)
  super

  if @secure
    if @ca_cert_path
      raise Fluent::ConfigError, "CA cert file not found nor readable at '#{@ca_cert_path}'" unless File.readable?(@ca_cert_path)
      begin
        OpenSSL::X509::Certificate.new File.read(@ca_cert_path)
      rescue OpenSSL::X509::CertificateError => e
        raise Fluent::ConfigError, "failed to load CA cert file"
      end
    else
      raise Fluent::ConfigError, "FQDN verification required for certificates issued from public CA" unless @enable_strict_verification
      log.info "secure connection with valid certificates issued from public CA"
    end
  else
    log.warn "'insecure' mode has vulnerability for man-in-the-middle attacks."
  end

  if @keepalive && !@connection_hard_timeout
    @connection_hard_timeout = @keepalive * 1.2
  end

  @read_interval = @read_interval_msec / 1000.0
  @socket_interval = @socket_interval_msec / 1000.0

  @nodes = []
  @servers.each do |server|
    node = Node.new(self, server)
    node.first_session = true
    @nodes.push node
  end

  if @num_threads > @nodes.select{|n| not n.standby}.size
    log.warn "Too many num_threads for secure-forward: threads should be smaller or equal to non standby servers"
  end

  @next_node = 0
  @mutex = Mutex.new

  @hostname_resolver = Resolve::Hostname.new(system_resolver: true, ttl: @expire_dns_cache)

  true
end

#node_watcherObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/fluent/plugin/out_secure_forward.rb', line 159

def node_watcher
  reconnectings = Array.new(@nodes.size)
  nodes_size = @nodes.size

  loop do
    sleep @reconnect_interval

    log.trace "in node health watcher"

    (0...nodes_size).each do |i|
      log.trace "node health watcher for #{@nodes[i].host}"

      next if @nodes[i].established? && ! @nodes[i].expired? && ! @nodes[i].detached?

      next if reconnectings[i]

      reason = :expired

      unless @nodes[i].established?
        log.warn "dead connection found: #{@nodes[i].host}, reconnecting..."
        reason = :dead
      end

      node = @nodes[i]
      log.debug "reconnecting to node", host: node.host, port: node.port, state: node.state, expire: node.expire, expired: node.expired?, detached: node.detached?

      renewed = node.dup
      renewed.start

      Thread.pass # to connection thread
      reconnectings[i] = { conn: renewed, at: Time.now, reason: reason }
    end

    (0...nodes_size).each do |i|
      next unless reconnectings[i]

      log.trace "checking reconnecting node #{reconnectings[i][:conn].host}"

      if reconnectings[i][:conn].established?
        log.debug "connection established for reconnecting node"

        oldconn = @nodes[i]
        @nodes[i] = reconnectings[i][:conn]

        if reconnectings[i][:reason] == :dead
          log.warn "recovered connection to dead node: #{nodes[i].host}"
        end

        log.trace "old connection shutting down"
        oldconn.detach! if oldconn # connection object doesn't raise any exceptions
        log.trace "old connection shutted down"

        reconnectings[i] = nil
        next
      end

      # not connected yet

      next if reconnectings[i][:at] + @established_timeout > Time.now

      # not connected yet, and timeout
      timeout_conn = reconnectings[i][:conn]
      log.debug "SSL connection is not established until timemout", host: timeout_conn.host, port: timeout_conn.port, timeout: @established_timeout
      reconnectings[i] = nil
      timeout_conn.detach! if timeout_conn # connection object doesn't raise any exceptions
    end
  end
end

#select_node(permit_standby = false) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/fluent/plugin/out_secure_forward.rb', line 124

def select_node(permit_standby=false)
  tries = 0
  nodes = @nodes.size
  @mutex.synchronize {
    n = nil
    while tries <= nodes
      n = @nodes[@next_node]
      @next_node += 1
      @next_node = 0 if @next_node >= nodes

      if n && n.established? && (! n.tained?) && (! n.detached?) && (!n.standby || permit_standby)
        n.tain!
        return n
      end

      tries += 1
    end
    nil
  }
end

#send_data(node, tag, es) ⇒ Object

to forward messages



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/fluent/plugin/out_secure_forward.rb', line 263

def send_data(node, tag, es)
  ssl = node.sslsession
  # beginArray(2)
  ssl.write FORWARD_HEADER

  # writeRaw(tag)
  ssl.write tag.to_msgpack

  # beginRaw(size)
  sz = es.size
  #  # FixRaw
  #  ssl.write [0xa0 | sz].pack('C')
  #elsif sz < 65536
  #  # raw 16
  #  ssl.write [0xda, sz].pack('Cn')
  #else
  # raw 32
  ssl.write [0xdb, sz].pack('CN')
  #end

  # writeRawBody(packed_es)
  es.write_to(ssl)
end

#shutdownObject



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/fluent/plugin/out_secure_forward.rb', line 228

def shutdown
  super

  @nodewatcher.kill
  @nodewatcher.join

  @nodes.each do |node|
    node.detach!
    node.join
  end
end

#startObject



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fluent/plugin/out_secure_forward.rb', line 145

def start
  super

  log.debug "starting secure-forward"
  OpenSSL::Random.seed(SecureRandom.random_bytes(16))
  log.debug "start to connect target nodes"
  @nodes.each do |node|
    log.debug "connecting node", host: node.host, port: node.port
    node.start
  end
  @nodewatcher = Thread.new(&method(:node_watcher))
  @nodewatcher.abort_on_exception = true
end

#write_objects(tag, es) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/fluent/plugin/out_secure_forward.rb', line 240

def write_objects(tag, es)
  node = select_node || select_node(true)
  unless node
    raise "no one nodes with valid ssl session"
  end
  log.trace "selected node", host: node.host, port: node.port, standby: node.standby

  begin
    send_data(node, tag, es)
    node.release!
  rescue Errno::EPIPE, IOError, OpenSSL::SSL::SSLError => e
    log.warn "Failed to send messages to #{node.host}, parging.", error_class: e.class, error: e
    node.release!
    node.detach!

    raise # to retry #write_objects
  end
end