tls, kerberos, sasl, and authorizer in apache kafka 0.9 - enabling new encryption, authorization, and authentication featuresapache kafka is frequently used to store critical data making it one of the most important components of a company's data infrastructure. our goal is to make it possible to run kafka as a . multi-tenancy is an essential requirement in achieving this vision and, in turn, security features are crucial for multi-tenancy.
previous to 0.9, kafka had no built-in security features. one could lock down access at the network level but this is not viable for a big shared multi-tenant cluster being used across a large company. consequently securing kafka has been one of the most requested features. security is of particular importance in today's world where cyber-attacks are a common occurrence and the threat of data breaches is a reality for businesses of all sizes, and at all levels from individual users to whole government entities.
four key security features were added in , which is included in the:
administrators can require client authentication using either kerberos or transport layer security (tls) client certificates, so that kafka brokers know who is making each request
a unix-like permissions system can be used to control which users can access which data.
network communication can be encrypted, allowing messages to be securely sent across untrusted networks.
administrators can require authentication for communication between kafka brokers and zookeeper.
in this post, we will discuss how to secure kafka using these features. for simplicity, we will assume a brand new cluster; the confluent documentation describes . with regards to clients, we will focus on the console and java clients (a future blog post will cover ).
it’s worth noting that the security features were implemented in a backwards-compatible manner and are disabled by default. in addition, only the new java clients (and librdkafka) have been augmented with support for security. for the most part, enabling security is simply a matter of configuration and no code changes are required.
defining the solution
there are a number of different ways to secure a kafka cluster depending on one’s requirements. in this post, we will show one possible approach, but describes the various options in more detail.
for client/broker and inter-broker communication, we will:
- require tls or kerberos authentication
- encrypt network traffic via tls
- perform authorization via access control lists (acls)
for broker/zookeeper communication, we will only require kerberos authentication as tls is only supported in zookeeper 3.5, which is still at the alpha release stage.
network segmentation should be used to restrict access to zookeeper. depending on performance and security requirements, kafka brokers could be accessible internally, exposed to the public internet or via a proxy (in some environments, public internet traffic must go through two separate security stacks in order to make it harder for attackers to exploit bugs in a particular security stack). a simple example can be seen in the following diagram:
before we start
first a note on terminology. secure sockets layer (ssl) is the predecessor of tls and it has been deprecated since june 2015. however, for historical reasons, kafka (like java) uses the term ssl instead of tls in configuration and code, which can be a bit confusing. we will stick to tls in this document.
before we start, we need to generate the tls keys and certificates, create the kerberos principals and potentially configure the java development kit (jdk) so that it supports stronger encryption algorithms.
tls keys and certificates
we need to generate a key and certificate for each broker and client in the cluster. the common name (cn) of the broker certificate must match the fully qualified domain name (fqdn) of the server as the client compares the cn with the dns domain name to ensure that it is connecting to the desired broker (instead of a malicious one).
at this point, each broker has a public-private key pair and an unsigned certificate to identify itself. to prevent forged certificates, it is important for each certificate to be signed by a certificate authority (ca). as long as the ca is a genuine and trusted authority, the clients have high assurance that they are connecting to authentic brokers.
in contrast to the keystore, which stores each application’s identity, the truststore stores all the certificates that the application should trust. importing a certificate into one’s truststore also means trusting all certificates that are signed by that certificate. this attribute is called the chain of trust, and it is particularly useful when deploying tls on a large kafka cluster. you can sign all certificates in the cluster with a single ca, and have all machines share the same truststore that contains the ca certificate. that way all machines can authenticate all other machines. a slightly more complex alternative is to use two cas, one to sign brokers’ keys and another to sign clients’ keys.
for this exercise, we will generate our own ca, which is simply a public-private key pair and certificate and we will add the same ca certificate to each client and broker’s truststore.
the following bash script generates the keystore and truststore for brokers (kafka.server.keystore.jks and kafka.server.truststore.jks) and clients (kafka.client.keystore.jks and kafka.client.truststore.jks):
#!/bin/bash
password=test1234
validity=365
keytool -keystore kafka.server.keystore.jks -alias localhost -validity $validity -genkey
openssl req -new -x509 -keyout ca-key -out ca-cert -days $validity
keytool -keystore kafka.server.truststore.jks -alias caroot -import -file ca-cert
keytool -keystore kafka.client.truststore.jks -alias caroot -import -file ca-cert
keytool -keystore kafka.server.keystore.jks -alias localhost -certreq -file cert-file
openssl x509 -req -ca ca-cert -cakey ca-key -in cert-file -out cert-signed -days $validity -cacreateserial -passin pass:$password
keytool -keystore kafka.server.keystore.jks -alias caroot -import -file ca-cert
keytool -keystore kafka.server.keystore.jks -alias localhost -import -file cert-signed
keytool -keystore kafka.client.keystore.jks -alias localhost -validity $validity -genkey
keytool -keystore kafka.client.keystore.jks -alias localhost -certreq -file cert-file
openssl x509 -req -ca ca-cert -cakey ca-key -in cert-file -out cert-signed -days $validity -cacreateserial -passin pass:$password
keytool -keystore kafka.client.keystore.jks -alias caroot -import -file ca-cert
keytool -keystore kafka.client.keystore.jks -alias localhost -import -file cert-signed
kerberos
if your organization is already using a kerberos server, it can also be used for kafka. otherwise you will need to install one. your linux vendor likely has packages for kerberos and a guide on how to install and configure it (e.g. , ).
if you are using the organization’s kerberos server, ask your kerberos administrator for a principal for each kafka broker in your cluster and for every operating system user that will access kafka with kerberos authentication (via clients or tools).
if you have installed your own kerberos, you will need to create these principals yourself using the following commands:
sudo /usr/sbin/kadmin.local -q 'addprinc -randkey kafka/{hostname}@{realm}'
sudo /usr/sbin/kadmin.local -q "ktadd -k /etc/security/keytabs/{keytabname}.keytab kafka/{hostname}@{realm}"
it is a kerberos requirement that all your hosts can be resolved by their fqdns.
stronger encryption
due to import regulations in some countries, the oracle implementation of java limits the strength of cryptographic algorithms available by default. if stronger algorithms are needed (for example, aes with 256-bit keys), the must be obtained and installed in the jdk/jre. this affects both tls and sasl/kerberos, see the for more information.
configuring the zookeeper ensemble
the zookeeper server configuration is relatively straightforward. we enable kerberos authentication via the simple authentication and security layer (sasl). in order to do that, we set the authentication provider, require sasl authentication and configure the login renewal period in zookeeper.properties:
authprovider.1=org.apache.zookeeper.server.auth.saslauthenticationprovider
requireclientauthscheme=sasl
jaasloginrenew=3600000
we also need to configure kerberos via the zookeeper_jaas.conf file as follows:
server {
com.sun.security.auth.module.krb5loginmodule required
usekeytab=true
keytab="/path/to/server/keytab"
storekey=true
useticketcache=false
principal="zookeeper/yourzkhostname";
};
finally, we need to pass the path to the jaas file when starting the zookeeper server:
-djava.security.auth.login.config=/path/to/server/jaas/file.conf
configuring kafka brokers
we start by configuring the desired security protocols and ports in server.properties:
listeners=ssl://:9093,sasl_ssl://:9094
we have not enabled an unsecured (plaintext) port as we want to ensure that all broker/client and inter-broker network communication is encrypted. we choose ssl as the security protocol for inter-broker communication (sasl_ssl is the other possible option given the configured listeners):
security.inter.broker.protocol=ssl
we know that it is difficult to simultaneously upgrade all systems to the new secure clients, so we allow administrators to support a mix of secure and unsecured clients. this can be done by adding a plaintext port to listeners, but care has to be taken to restrict access to this port to trusted clients only. network segmentation and/or authorization acls can be used to restrict access to trusted ips in such cases. we will not cover this in more detail as we will not enable a plaintext port in our example.
we’ll now go over protocol-specific configuration settings.
tls
we require tls client authentication and configure key, keystore and truststore details:
ssl.client.auth=required
ssl.keystore.location=/var/private/ssl/kafka.server.keystore.jks
ssl.keystore.password=test1234
ssl.key.password=test1234
ssl.truststore.location=/var/private/ssl/kafka.server.truststore.jks
ssl.truststore.password=test1234
since we are storing passwords in the broker config, it is important to restrict access via filesystem permissions.
sasl/kerberos
we will enable sasl/kerberos for broker/client and broker/zookeeper communication.
most of the configuration for sasl lives in jaas configuration files containing akafkaserver section for authentication between client and broker and a client section for authentication between broker and zookeeper:
kafkaserver {
com.sun.security.auth.module.krb5loginmodule required
usekeytab=true
storekey=true
keytab="/etc/security/keytabs/kafka_server.keytab"
principal="kafka/kafka1.hostname.com@example.com";
};
// zookeeper client authentication
client {
com.sun.security.auth.module.krb5loginmodule required
usekeytab=true
storekey=true
keytab="/etc/security/keytabs/kafka_server.keytab"
principal="kafka/kafka1.hostname.com@example.com";
};
note that each broker should have its own keytab and the same primary name should be used across all brokers. in the above example, the principal iskafka/kafka1.hostname.com@example.com and the primary name is kafka. the keytabs configured in the jaas file must be readable by the operating system user who is starting the kafka broker.
we must also configure the service name in server.properties with the primary name of the kafka brokers:
sasl.kerberos.service.name=kafka
when it comes to zookeeper authentication, if we set the configuration property zookeeper.set.acl in each broker to true, the metadata stored in zookeeper is such that only brokers will be able to modify the corresponding znodes, but znodes are world readable. the rationale behind this decision is that the data stored in zookeeper is not sensitive, but inappropriate manipulation of znodes can cause cluster disruption.
authorization and acls
kafka ships with a pluggable authorizer and an out-of-box authorizer implementation that uses zookeeper to store all the acls.
kafka acls are defined in the general format of “principal p is [allowed/denied] operation o from host h on resource r". the operations available are both for clients (producers, consumers, admin) and inter-broker operations of a cluster. in a secure cluster, both client requests and inter-broker operations require authorization.
we enable the default authorizer by setting the following in server.properties:
authorizer.class.name=kafka.security.auth.simpleaclauthorizer
the default behavior is such that if a resource has no associated acls, then no one is allowed to access the resource, except super users. setting broker principals as super users is a convenient way to give them the required access to perform inter-broker operations:
super.users=user:bob;user:alice
by default, the tls user name will be of the form “cn=host1.example.com,ou=,o=confluent,l=london,st=london,c=gb". one can change that by setting a customized principalbuilder in server.properties like the following:
principal.builder.class=customizedprincipalbuilderclass
by default, the sasl user name will be the primary part of the kerberos principal. one can change that by setting sasl.kerberos.principal.to.local.rules to a customized rule in server.properties.
we can use kafka-acls (the kafka authorizer cli) to add, remove or list acls. please runkafka-acls --help for detailed information on the supported options.
the most common use cases for acl management are adding/removing a principal as a producer or consumer and there are convenience options to handle these cases. in order to add user:bob as a producer of test-topic we can execute the following:
kafka-acls --authorizer-properties zookeeper.connect=localhost:2181 \
--add --allow-principal user:bob \
--producer --topic test-topic
similarly to add alice as a consumer of test-topic with consumer group group-1 we specify the --consumer and --group options:
kafka-acls --authorizer-properties zookeeper.connect=localhost:2181 \
--add --allow-principal user:bob \
--consumer --topic test-topic --group group-1
configuring kafka clients
tls is only supported by new kafka producer and consumer, the older apis are not supported. enabling security is simply a matter of configuration, no code changes are required.
tls
the configs for tls will be the same for both producer and consumer. we have to set the desired security protocol as well as the truststore and keystore information since we are using mutual authentication:
security.protocol=ssl
ssl.truststore.location=/var/private/ssl/kafka.client.truststore.jks
ssl.truststore.password=test1234
ssl.keystore.location=/var/private/ssl/kafka.client.keystore.jks
ssl.keystore.password=test1234
ssl.key.password=test1234
given that passwords are being stored in the client config, it is important to restrict access to the file via filesystem permissions.
sasl/kerberos
clients (producers, consumers, connect workers, etc) will authenticate to the cluster with their own principal (usually with the same name as the user running the client), so we need to obtain or create these principals as needed. then we create a jaas file for each principal. the kafkaclient section describes how the clients like producer and consumer can connect to the kafka broker. the following is an example configuration for a client using a keytab (recommended for long-running processes):
kafkaclient {
com.sun.security.auth.module.krb5loginmodule required
usekeytab=true
storekey=true
keytab="/etc/security/keytabs/kafka_client.keytab"
principal="kafka-client-1@example.com";
};
for command-line utilities like kafka-console-consumer or kafka-console-producer, kinit can be used along with useticketcache=true as in:
kafkaclient {
com.sun.security.auth.module.krb5loginmodule required
useticketcache=true;
};
the security protocol and service name are set in producer.properties and/orconsumer.properties. we also have to include the truststore details since we are usingsasl_ssl instead of sasl_plaintext:
security.protocol=sasl_ssl
sasl.kerberos.service.name=kafka
ssl.truststore.location=/var/private/ssl/kafka.client.truststore.jks
ssl.truststore.password=test1234
# keystore configuration should not be needed, see note below
ssl.keystore.location=/var/private/ssl/kafka.client.keystore.jks
ssl.keystore.password=test1234
ssl.key.password=test1234
the keystore configuration is needed in 0.9.0.0 due to a , which has been fixed and will be included in 0.9.0.1.
finally, we pass the name of the jaas file as a jvm parameter to the client jvm:
-djava.security.auth.login.config=/etc/kafka/kafka_client_jaas.conf
the keytabs configured in the kafka_client_jaas.conf must be readable by the operating system user who is starting kafka client.
putting it all together
we have created a vagrant setup based on centos 7.2 that includes a kerberos server, kafka and openjdk 1.8.0 to make it easier to test all the pieces together. please install and (if you haven’t already) and then:
clone the git repository: git clone
change directory: cd securing-kafka-blog
start and provision the vagrant environment: vagrant up
connect to the vm via ssh: vagrant ssh
zookeeper and broker
before we start zookeeper and the kafka broker, let’s take a look at their config files:
/etc/kafka/zookeeper.properties
datadir=/var/lib/zookeeper
clientport=2181
authprovider.1=org.apache.zookeeper.server.auth.saslauthenticationprovider
requireclientauthscheme=sasl
jaasloginrenew=3600000
/etc/kafka/zookeeper_jaas.conf
server {
com.sun.security.auth.module.krb5loginmodule required
usekeytab=true
keytab="/etc/security/keytabs/zookeeper.keytab"
storekey=true
useticketcache=false
principal="zookeeper/kafka.example.com@example.com";
};
/etc/kafka/server.properties
broker.id=0
listeners=ssl://:9093,sasl_ssl://:9095
security.inter.broker.protocol=ssl
zookeeper.connect=kafka.example.com:2181
log.dirs=/var/lib/kafka
zookeeper.set.acl=true
ssl.client.auth=required
ssl.keystore.location=/etc/security/tls/kafka.server.keystore.jks
ssl.keystore.password=test1234
ssl.key.password=test1234
ssl.truststore.location=/etc/security/tls/kafka.server.truststore.jks
ssl.truststore.password=test1234
sasl.kerberos.service.name=kafka
authorizer.class.name=kafka.security.auth.simpleaclauthorizer
super.users=user:cn=kafka.example.com,ou=,o=confluent,l=london,st=london,c=gb
/etc/kafka/kafka_server_jaas.conf
kafkaserver {
com.sun.security.auth.module.krb5loginmodule required
usekeytab=true
storekey=true
keytab="/etc/security/keytabs/kafka.keytab"
principal="kafka/kafka.example.com@example.com";
};
client {
com.sun.security.auth.module.krb5loginmodule required
usekeytab=true
storekey=true
keytab="/etc/security/keytabs/kafka.keytab"
principal="kafka/kafka.example.com@example.com";
};
and the start script (/usr/sbin/start-zk-and-kafka) looks like:
export kafka_heap_opts='-xmx256m'
export kafka_opts='-djava.security.auth.login.config=/etc/kafka/zookeeper_jaas.conf'
/usr/bin/zookeeper-server-start /etc/kafka/zookeeper.properties &
sleep 5
export kafka_opts='-djava.security.auth.login.config=/etc/kafka/kafka_server_jaas.conf'
/usr/bin/kafka-server-start /etc/kafka/server.properties &
after executing executing the script (sudo /usr/sbin/start-zk-and-kafka), the following should be in the server.log:
info registered broker 0 at path /brokers/ids/0 with addresses: ssl -> endpoint(kafka.example.com,9093,ssl),sasl_ssl -> endpoint(kafka.example.com,9095,sasl_ssl)
create topic
because we configured zookeeper to require sasl authentication, we need to set thejava.security.auth.login.config system property while starting the kafka-topics tool:
export kafka_opts="-djava.security.auth.login.config=/etc/kafka/kafka_server_jaas.conf"
kafka-topics --create --topic securing-kafka --replication-factor 1 --partitions 3 --zookeeper kafka.example.com:2181
we used the server principal and keytab for this example, but you may want to create a separate principal and keytab for tools such as this.
set acls
we authorize the clients to access the newly created topic:
export kafka_opts="-djava.security.auth.login.config=/etc/kafka/kafka_server_jaas.conf"
kafka-acls --authorizer-properties zookeeper.connect=kafka.example.com:2181 \
--add --allow-principal user:kafkaclient \
--producer --topic securing-kafka
kafka-acls --authorizer-properties zookeeper.connect=kafka.example.com:2181 \
--add --allow-principal user:kafkaclient \
--consumer --topic securing-kafka --group securing-kafka-group
console clients
the client configuration is slightly different depending on whether we want the client to use tls or sasl/kerberos.
tls
the console producer is a convenient way to send a small amount of data to the broker:
kafka-console-producer --broker-list kafka.example.com:9093 --topic securing-kafka --producer.config /etc/kafka/producer_ssl.properties
message1
message2
message3
producer_ssl.properties
bootstrap.servers=kafka.example.com:9093
security.protocol=ssl
ssl.truststore.location=/etc/security/tls/kafka.client.truststore.jks
ssl.truststore.password=test1234
ssl.keystore.location=/etc/security/tls/kafka.client.keystore.jks
ssl.keystore.password=test1234
ssl.key.password=test1234
the console consumer is a convenient way to consume messages:
kafka-console-consumer --bootstrap-server kafka.example.com:9093 --topic securing-kafka --new-consumer --from-beginning --consumer.config /etc/kafka/consumer_ssl.properties
consumer_ssl.properties
bootstrap.servers=kafka.example.com:9093
group.id=securing-kafka-group
security.protocol=ssl
ssl.truststore.location=/etc/security/tls/kafka.client.truststore.jks
ssl.truststore.password=test1234
ssl.keystore.location=/etc/security/tls/kafka.client.keystore.jks
ssl.keystore.password=test1234
ssl.key.password=test1234
the output should be:
message1
message2
message3
sasl/kerberos
using the console clients via sasl/kerberos is similar, but we also need to pass the location of the jaas file via a system property. an example for the console consumer follows:
export kafka_opts="-djava.security.auth.login.config=/etc/kafka/kafka_client_jaas.conf"
kafka-console-consumer --bootstrap-server kafka.example.com:9095 --topic securing-kafka --new-consumer --from-beginning --consumer.config /etc/kafka/consumer_sasl.properties
consumer_sasl.properties
bootstrap.servers=kafka.example.com:9095
group.id=securing-kafka-group
security.protocol=sasl_ssl
sasl.kerberos.service.name=kafka
ssl.truststore.location=/etc/security/tls/kafka.client.truststore.jks
ssl.truststore.password=test1234
ssl.keystore.location=/etc/security/tls/kafka.client.keystore.jks
ssl.keystore.password=test1234
ssl.key.password=test1234
java clients
configuring the new java clients (both producer and consumer) is simply a matter of passing the configs used in the "console clients" example above to the relevant client constructor. this can be done by either loading the properties from a file or by setting the properties programmatically. an example of setting the properties programmatically for a consumer configured to use tls follows:
properties props = new properties();
props.setproperty(consumerconfig.bootstrap_servers_config, "localhost:9093");
props.setproperty(consumerconfig.group_id_config, "securing-kafka-group");
props.setproperty(commonclientconfigs.security_protocol_config, "ssl");
props.setproperty(sslconfigs.ssl_truststore_location_config, "/etc/security/tls/kafka.client.truststore.jks");
props.setproperty(sslconfigs.ssl_truststore_password_config, "test1234");
props.setproperty(sslconfigs.ssl_keystore_password_config, "/etc/security/tls/kafka.client.keystore.jks");
props.setproperty(sslconfigs.ssl_keystore_location_config, "test1234");
props.setproperty(sslconfigs.ssl_key_password_config, "test1234");
new kafkaconsumer(props);
debugging problems
it is a common occurrence that things don't work on the first attempt when it comes to configuring security systems. debugging output can be quite helpful in order to diagnose the cause of the problem:
can be enabled via the the javax.net.debug system property, egexport kafka_opts=-djavax.net.debug=all
sasl debug output can be enabled via the sun.security.krb5.debug system property, eg export kafka_opts=-dsun.security.krb5.debug=true
kafka authentication logging can be enabled by changing warn to debug in the following line of the log4j.properties file included in the kafka distribution (in/etc/kafka/log4j.properties in the confluent platform):
log4j.logger.kafka.authorizer.logger=warn, authorizerappender
conclusion
we have shown how the security features introduced in apache kafka 0.9 (part of confluent platform 2.0) can be used to secure a kafka cluster. we focused on ensuring that authentication was required for all network communication and network encryption was applied to all broker/client and inter-broker network traffic.
these security features were a very good first step, but we will be making them better, faster and simpler to operate in subsequent releases. as ever, we would love to hear your feedback in the or .
acknowledgements
we would like to thank ben stopford, flavio junqueira, gwen shapira, joel koshy, jun rao, parth brahmbhatt, rajini sivaram, and sriharsha chintalapani for their contributions to the security features introduced in apache kafka 0.9, and our colleagues at confluent for feedback on the draft version of this post.