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.



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

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>



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

def hostname_resolver
  @hostname_resolver
end

#nodesObject (readonly)

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

#read_intervalObject (readonly)

Returns the value of attribute read_interval.



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

def read_interval
  @read_interval
end

#socket_intervalObject (readonly)

Returns the value of attribute socket_interval.



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

def socket_interval
  @socket_interval
end

Instance Method Details

#configure(conf) ⇒ Object



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

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
    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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/fluent/plugin/out_secure_forward.rb', line 126

def node_watcher
  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?

      $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?

      @nodes[i] = node.dup
      @nodes[i].start
      begin
        node.shutdown
      rescue => e
        $log.warn "error in shutdown of dead connection", :error_class => e.class, :error => e
      end
    end
  end
end

#select_node(permit_standby = false) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fluent/plugin/out_secure_forward.rb', line 95

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



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/fluent/plugin/out_secure_forward.rb', line 184

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



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fluent/plugin/out_secure_forward.rb', line 153

def shutdown
  super

  @nodewatcher.kill
  @nodewatcher.join

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

#startObject



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fluent/plugin/out_secure_forward.rb', line 113

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))
end

#write_objects(tag, es) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/fluent/plugin/out_secure_forward.rb', line 165

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
    node.shutdown
  end
end