Fluentd
Search…
Plugin Helper: Server
The server plugin helper manages various types of servers.
Here is an example:
1
require 'fluent/plugin/input'
2
3
module Fluent::Plugin
4
class ExampleInput < Input
5
Fluent::Plugin.register_input('example', self)
6
7
# 1. Load server helper
8
helpers :server
9
10
# Omit `configure`, `shutdown` and other plugin APIs
11
12
def start
13
# 2. Create server
14
server_create(:title, @port) do |data|
15
#3. Process data
16
end
17
end
18
end
19
end
Copied!
The launched server is managed by the plugin helper. No need of server shutdown code in plugin's shutdown method. The plugin shutdowns the launched servers automatically.
For more details, see Transport Section.

Methods

server_create_connection(title, port, proto: nil, bind: '0.0.0.0', shared: true, backlog: nil, tls_options: nil, **socket_options, &block)

This method creates a server instance for various protocols.
The &block is invoked with the new connection as a parameter.
    title: unique symbol
    port: the port to listen to
    proto: protocol type. { :tcp, :tls }
    bind: the bind address to listen to
    shared: if true, share socket via server engine for multiple workers
    backlog: the maximum length of the queue for pending connections
    tls_options: options for TLS
      version: set TLS version :TLSv1_1 or :TLSv1_2.
      Default: :TLSv1_2
      ciphers: set the list of available cipher suites. (default:
      "ALL:!aNULL:!eNULL:!SSLv2")
      insecure: if true, set TLS verify mode NONE
      cert_verifier: if specified, pass evaluated object to OpenSSL's verify_callback. See also "cert_verifier example" section.
      verify_fqdn: if true, validate the server certificate for the hostname
      fqdn: set FQDN
      enable_system_cert_store: if true, enable system default cert store
      allow_self_signed_cert: if true, allow self-signed certificate
      cert_paths: files contain PEM-encoded certificates
    socket_options: options for socket
      resolve_name: if true, resolve the hostname
      connect: if true, connect to host
      nonblock: if true, use non-blocking I/O
      linger_timeout: the timeout (seconds) to set SO_LINGER
      recv_timeout: the timeout (seconds) to set SO_RECVTIMEO
      send_timeout: the timeout (seconds) to set SO_SNDTIMEO
      send_keepalive_packet: if true, enable TCP keep-alive via SO_KEEPALIVE. See also socket article.
Example:
1
# TCP
2
server_create_connection(:title, @port) do |conn|
3
# on connection
4
# conn is Fluent::PluginHelper::Server::TCPCallbackSocket
5
source_addr = conn.remote_host
6
source_port = conn.remote_port
7
conn.data do |data|
8
conn.write(something)
9
end
10
end
Copied!

server_create(title, port, proto: nil, bind: '0.0.0.0', shared: true, socket: nil, backlog: nil, tls_options: nil, max_bytes: nil, flags: 0, **socket_options, &callback)

This method creates a server instance for various protocols.
The &block is invoked with parameter(s) on data.
    title: unique symbol
    port: the port to listen to
    proto: protocol type. { :tcp, :udp, :tls }
    bind: the bind address to listen to
    shared: if true, share socket via server engine for multiple workers
    socket: socket instance for UDP (only for UDP)
    backlog: the maximum length of the queue for pending connections
    tls_options: options for TLS
      version: set TLS version :TLSv1_1 or :TLSv1_2. (default: :TLSv1_2)
      ciphers: set the list of available cipher suites. (default:
      "ALL:!aNULL:!eNULL:!SSLv2")
      insecure: if true, set TLS verify mode NONE
      cert_verifier: if specified, pass evaluated object to OpenSSL's verify_callback. See also "cert_verifier example" section.
      verify_fqdn: if true, validate the server certificate for the hostname
      fqdn: set FQDN
      enable_system_cert_store: if true, enable system default cert store
      allow_self_signed_cert: if true, allow self signed certificate
      cert_paths: files contain PEM-encoded certificates
    max_bytes: the maximum number of bytes to receive (required for UDP)
    flags: zero or more of the MSG_ options (UDP-only)
    socket_options: options for socket
      resolve_name: if true, resolve the hostname
      connect: if true, connect to host
      nonblock: if true, use non-blocking I/O
      linger_timeout: the timeout (seconds) to set SO_LINGER
      recv_timeout: the timeout (seconds) to set SO_RECVTIMEO
      send_timeout: the timeout (seconds) to set SO_SNDTIMEO
      send_keepalive_packet: if true, enable TCP keep-alive via SO_KEEPALIVE. See also socket article.
Code example:
1
# UDP (w/o socket)
2
server_create(:title, @port, proto: :udp, max_bytes: 2048) do |data|
3
# data is received data
4
end
5
6
# UDP (w/ socket)
7
server_create(:title, @port, proto: :udp, max_bytes: 2048) do |data, sock|
8
# data is received data
9
# sock is UDPSocket
10
end
11
12
# TCP (w/o connection)
13
server_create(:title, @port) do |data|
14
# data is received data
15
end
16
17
# TCP (w/ connection)
18
server_create(:title, @port) do |data, conn|
19
# data is received data
20
# conn is Fluent::PluginHelper::Server::TCPCallbackSocket
21
end
22
23
# TLS (w/o connection)
24
server_create(:title, @port, proto: :tls) do |data|
25
# data is received data
26
end
27
28
# TLS (w/ connection)
29
server_create(:title, @port, proto: :tls) do |data, conn|
30
# data is received data
31
# conn is Fluent::PluginHelper::Server::TLSCallbackSocket
32
end
Copied!

Configuration example

Here is some TLS configuration examples:
    1.
    Use existing certs (signed by public CA or self signed CA)
    2.
    Use certs automatically generated by Fluentd
Case 1. Use existing certs (signed by public CA or self signed CA)
If cert_path and private_key_path are specified, certs generation is disabled. The existing certs are loaded.
1
<source>
2
@type forward
3
# other plugin parameters
4
<transport tls>
5
version TLSv1_2
6
ciphers ALL:!aNULL:!eNULL:!SSLv2
7
insecure false
8
9
# For Cert signed by public CA
10
ca_path /path/to/ca_file
11
cert_path /path/to/cert_path
12
private_key_path /path/to/priv_key
13
private_key_passphrase "passphrase"
14
client_cert_auth false
15
16
# For Cert signed by self signed CA
17
ca_path /path/to/ca_path
18
cert_path /path/to/cert_path
19
private_key_path /path/to/priv_key
20
private_key_passphrase "passphrase"
21
client_cert_auth false
22
23
</transport>
24
</source>
Copied!
Case 2. Use certs automatically generated by Fluentd
If ca_cert_path and ca_private_key_path are specified, certs generation is enabled. You can customize cert generation behavior via generation_... parameters. See Generated and Signed by Private CA Certs or Self-signed Parameters about parameter details.
1
<source>
2
@type forward
3
# other plugin parameters
4
<transport tls>
5
version TLSv1_2
6
ciphers ALL:!aNULL:!eNULL:!SSLv2
7
insecure false
8
9
# For Cert generated
10
ca_cert_path /path/to/ca_cert
11
ca_private_key_path /path/to/ca_priv_key
12
ca_private_key_passphrase "ca_passphrase"
13
14
</transport>
15
</source>
Copied!

cert_verifier example

cert_verifier is supported since v1.10.0.
Configuration example:
1
<source>
2
@type forward
3
<transport tls>
4
# other parameters
5
client_cert_auth true
6
cert_verifier /path/to/my_verifier.rb
7
</transport>
8
</source>
Copied!
    my_verifier.rb example
The code must return a callable object that has a call method with two arguments. This object is used as OpenSSL's verify_callback.

Proc or lambda Object for the Simple Scenario

1
Proc.new { |ok, ctx|
2
# check code
3
4
if cond
5
true
6
else
7
false
8
end
9
}
Copied!

Use class for the Complicated Scenario

This is CN check example:
1
module Fluent
2
module Plugin
3
class InForwardCNChecker
4
def initialize
5
# Modify for actual common names
6
@allow_list = ['fluentd', 'fluentd-client', 'other-org']
7
end
8
9
def call(ok, ctx)
10
subject = ctx.chain.first.subject.to_a.find { |entry| entry.first == 'CN' }
11
if subject
12
@allow_list.include?(subject[1])
13
else
14
false
15
end
16
end
17
end
18
end
19
end
20
21
Fluent::Plugin::InForwardCNChecker.new
Copied!

Plugins using server

If this article is incorrect or outdated, or omits critical information, please let us know. Fluentd is an open-source project under Cloud Native Computing Foundation (CNCF). All components are available under the Apache 2 License.
Last modified 4d ago