Community Articles
Find and share helpful community-sourced technical articles
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.
Labels (2)

How to connect GetKafka to Kafka through Stunnel

Stunnel is a proxy that can make insecure network transmission secure by wrapping it with SSL. This article contains example and illustrations describing how it works and how to configure it.

Most part of it is derived from this informative Git comment I wouldn't be able to set it up without this comment. Thank you for sharing such detailed example.

How it works?

Let's see how it can be applied to NiFi GetKafka. I used two servers for this experimentation. 0 and A single Zookeeper and Kafka broker is running on 0.server. A GetKafka NiFi processor in 1.server consumes messages through Stunnel:

  1. Kafka Broker joins the Kafka cluster and declares its address as If Zookeeper is in different server (recommended) and you need to secure this connection via Stunnel as well, then you need to apply the same method as the one used between GetKafka and Zookeeper.
  2. GetKafka's Zookeeper Connection String is set to which is local Stunnel is listening to. Then the local Stunnel on 1.server proxies the request to 0.server:2181 over SSL. At 0.server, the request is proxied again by the Stunnel running on 0.server, then finally arrives at Zookeeper.
  3. Since the Kafka Broker running on 0.server declares its address as, GetKafka (Kafka client) sends request to, and the request eventually transferred to the Broker through Stunnel pair.

Here is the relevant configurations in 1.server's stunnel.conf (entire file is available here):

client = yes

accept =
connect =

accept =
connect =

And this is for 0.server (entire file is available here):

client = no

accept =
connect =

accept =
connect =




How to authorize client access?

Each Stunnel server has to have its own pem file containing a private key and a certificate. Also, a CA certificate file (or directory) is also needed to authorize client access.

  1. I used that is available in NiFi toolkit, to generate required files. Toolkit can generate three files, keystore.jks, truststore.jks and for each server. Server's key and cert can be extracted from keystore.jks. To do so, convert keystore.jks into keystore.p12 file by following commands (credit goes to this Stackoverflow) :
    # It's not important which server to run the toolkit on.
    $ ./bin/ standalone -n [0-1] -C 'CN=server,OU=mine'
    # Password for keystore.jks can be found in generated ''.
    $ keytool -importkeystore -srckeystore keystore.jks -destkeystore keystore.p12 -srcstoretype jks -deststoretype pkcs12
  2. Then extract key and cert from the p12 file:
    $ openssl pkcs12 -in keystore.p12 -nokeys -out cert.pem $ openssl pkcs12 -in keystore.p12 -nodes -nocerts -out key.pem
  3. Concatenate key and cert to create stunnel.pem, and deploy stunnel.pem to servers:
    $ cat key.pem cert.pem >> stunnel.pem
  4. I used cert.pem as the CAFile for Stunnel on 0.server.

In stunnel.conf on 0.server, following settings are needed to enable client cert verification:

verify = 3
CAFile = /etc/stunnel/certs

Refer Stunnel manual for further description on these configurations.

I confirmed that GetKafka running on 1.server can consume messages through Stunnel. If I used a cert which is not configured in the certs file on 0.server, GetKafka got timeout exception as follows:

2017-03-09 06:50:48,690 WARN [Timer-Driven Process Thread-5] o.apache.nifi.processors.kafka.GetKafka GetKafka[id=b0a21b5d-015a-1000-fbba-2648095ae625] Executor did not stop in 30 sec. Terminated.
2017-03-09 06:50:48,691 WARN [Timer-Driven Process Thread-5] o.apache.nifi.processors.kafka.GetKafka GetKafka[id=b0a21b5d-015a-1000-fbba-2648095ae625] Timed out after 60000 milliseconds while waiting to get connection
java.util.concurrent.TimeoutException: null
        at java.util.concurrent.FutureTask.get( [na:1.8.0_121]
        at org.apache.nifi.processors.kafka.GetKafka.onTrigger( ~[nifi-kafka-0-8-processors-1.1.2.jar:1.1.2]
        at org.apache.nifi.processor.AbstractProcessor.onTrigger( [nifi-api-1.1.2.jar:1.1.2]
        at org.apache.nifi.controller.StandardProcessorNode.onTrigger( [nifi-framework-core-1.1.2.jar:1.1.2]

Stunnel commands

# Install
sudo yum -y install stunnel
# Edit config
sudo vi /etc/stunnel/stunnel.conf
# Start
sudo stunnel
# Stop
sudo kill `cat /var/run/`


Although Stunnel works with GetKafka and Kafka 0.8.x, I recommend to use newer version of Kafka and ConsumeKafka NiFi processor with SSL if possible. As it's written in the Git comment, this workaround is not scalable (in terms of required administration tasks) and complicated.

Don't have an account?
Coming from Hortonworks? Activate your account here
Version history
Revision #:
1 of 1
Last update:
‎03-10-2017 12:47 AM
Updated by:
Top Kudoed Authors