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.



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

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

Instance Attribute Details

#hostname_resolverObject (readonly)

<server>

host ipaddr/hostname
hostlabel labelname # certification common name
port 24284
shared_key .... # optional shared key
username name # if required
password pass # if required

</server>



51
52
53
# File 'lib/fluent/plugin/out_secure_forward.rb', line 51

def hostname_resolver
  @hostname_resolver
end

#nodesObject (readonly)

Returns the value of attribute nodes.



41
42
43
# File 'lib/fluent/plugin/out_secure_forward.rb', line 41

def nodes
  @nodes
end

#read_intervalObject (readonly)

Returns the value of attribute read_interval.



39
40
41
# File 'lib/fluent/plugin/out_secure_forward.rb', line 39

def read_interval
  @read_interval
end

#socket_intervalObject (readonly)

Returns the value of attribute socket_interval.



39
40
41
# File 'lib/fluent/plugin/out_secure_forward.rb', line 39

def socket_interval
  @socket_interval
end

Instance Method Details

#configure(conf) ⇒ Object



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

def configure(conf)
  super

  unless @allow_self_signed_certificate
    raise Fluent::ConfigError, "not tested yet!"
  end

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

  # read <server> tags and set to nodes
  @nodes = []
  conf.elements.each do |element|
    case element.name
    when 'server'
      unless element['host']
        raise Fluent::ConfigError, "host missing in <server>"
      end
      node_shared_key = element['shared_key'] || @shared_key
      node = Node.new(self, node_shared_key, element)
      node.first_session = true
      node.keepalive = @keepalive
      @nodes.push node
    when 'secondary'
      # ignore
    else
      raise Fluent::ConfigError, "unknown config tag name #{element.name}"
    end
  end
  @next_node = 0
  @mutex = Mutex.new

  @hostname_resolver = Resolve::Hostname.new(:system_resolver => true)

  true
end

#node_watcherObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
# File 'lib/fluent/plugin/out_secure_forward.rb', line 135

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?

      next if reconnectings[i]

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

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

      renewed = node.dup
      renewed.start

      Thread.pass # to connection thread
      reconnectings[i] = { :conn => renewed, :at => Time.now }
    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]
        log.trace "old connection shutting down"
        oldconn.shutdown 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.shutdown if timeout_conn # connection object doesn't raise any exceptions
    end
  end
end

#select_node(permit_standby = false) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fluent/plugin/out_secure_forward.rb', line 103

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

      return n if n && n.established? && (!n.standby || permit_standby)

      tries += 1
    end
    nil
  }
end

#send_data(node, tag, es) ⇒ Object

to forward messages



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/fluent/plugin/out_secure_forward.rb', line 230

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



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/fluent/plugin/out_secure_forward.rb', line 193

def shutdown
  super

  @nodewatcher.kill
  @nodewatcher.join

  @nodes.each do |node|
    node.detach = true
    node.join
  end
end

#startObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fluent/plugin/out_secure_forward.rb', line 121

def start
  super

  log.debug "starting secure-forward"
  OpenSSL::Random.seed(File.read("/dev/urandom", 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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/fluent/plugin/out_secure_forward.rb', line 205

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)
  rescue Errno::EPIPE, IOError, OpenSSL::SSL::SSLError => e
    log.warn "Failed to send messages to #{node.host}, parging.", :error_class => e.class, :error => e
    begin
      node.shutdown
    rescue => e2
      # ignore all errors
    end

    raise # to retry #write_objects
  end
end