Kafka Security Basics

7 min read

Welcome to Module 10. Thus far, we have treated Apache Kafka as an open pipe—data flows in, data flows out, and trust is implicit. In a development environment running on localhost, this is acceptable. In a production environment handling financial transactions, user data, or telemetry, running a default "PLAINTEXT" Kafka cluster is negligent.

By default, Kafka allows any client to connect, read any data, write any data, and make configuration changes. This guide creates a fortified cluster using the three pillars of security.


1. The Security Triad: Encryption, Authentication, and Authorization 🛡️

Before modifying configuration files, we must rigorously define our security objectives. In information security, these concepts form layers of defense.

The Problem

In a default Kafka setup (PLAINTEXT):

  1. Wire Sniffing: Anyone with network access can capture packets and read the message payloads.
  2. Impersonation: A malicious script can claim to be the "PaymentService" and inject fraudulent transactions.
  3. Unauthorized Access: A legitimate developer from the "Logging Team" might accidentally delete a topic belonging to the "Finance Team."

The Solution: The Triad

  1. Encryption (SSL/TLS):

    • Definition: The cryptographic process of transforming data into an unreadable format during transmission.
    • Role: Ensures Confidentiality. Even if packets are intercepted, they cannot be deciphered without the corresponding private key.
  2. Authentication (AuthN):

    • Definition: The process of verifying the identity of a user, device, or system.
    • Role: Ensures Identity. The cluster confirms that the client connecting is truly who they claim to be (e.g., via Certificates or Username/Password).
  3. Authorization (AuthZ):

    • Definition: The process of specifying access rights/privileges to resources.
    • Role: Ensures Access Control. Once a user is authenticated, this layer dictates what they are allowed to do (e.g., User A can Write to Topic B, but cannot Read from it).

Did You Know?
In 2014, a major vulnerability named "Heartbleed" was discovered in the OpenSSL cryptography library. It allowed attackers to read the memory of systems protected by TLS. This event fundamentally changed how the industry manages SSL/TLS certificates and underscored that security requires constant vigilance, not just a one-time setup.


2. Encryption in Transit: Setting up SSL/TLS 🔒

We will configure Kafka to use TLS (Transport Layer Security). This encrypts data moving between:

  1. Clients and Brokers.
  2. Inter-broker communication.

Prerequisite Concepts

  • KeyStore: A repository containing the machine's own identity (Private Key + Certificate).
  • TrustStore: A repository containing the list of Certificate Authorities (CAs) the machine trusts to sign other certificates.
  • CA (Certificate Authority): The entity that signs certificates to vouch for their authenticity.

Step-by-Step Implementation

We will use the Java keytool utility. In a production environment, you would likely use a real CA (like Verisign or an internal corporate CA). For this guide, we will generate a self-signed CA.

Step 1: Generate the Certificate Authority (CA)

bash
# Generate a CA key and certificate
openssl req -new -x509 -keyout ca-key -out ca-cert -days 365
# (Follow prompts to set a password and company details)

Step 2: Create the Server KeyStore

Each broker needs its own keystore.

bash
# Create the keystore
keytool -keystore kafka.server.keystore.jks -alias localhost -validity 365 -genkey -keyalg RSA
  • Note: Ensure the "Common Name (CN)" matches the Fully Qualified Domain Name (FQDN) of your server.

Step 3: Sign the Server Certificate

We must extract the certificate from the keystore and have our CA sign it.

bash
# 1. Export the certificate from the keystore
keytool -keystore kafka.server.keystore.jks -alias localhost -certreq -file cert-file

# 2. Sign it with the CA
openssl x509 -req -CA ca-cert -CAkey ca-key -in cert-file -out cert-signed -days 365 -CAcreateserial

# 3. Import the CA into the keystore (to establish chain of trust)
keytool -keystore kafka.server.keystore.jks -alias CARoot -import -file ca-cert

# 4. Import the signed certificate back into the keystore
keytool -keystore kafka.server.keystore.jks -alias localhost -import -file cert-signed

Step 4: Create the TrustStore

The TrustStore allows the broker to verify clients (and other brokers).

bash
keytool -keystore kafka.server.truststore.jks -alias CARoot -import -file ca-cert

Step 5: Configure server.properties

Modify the Kafka Broker configuration to enable SSL.

properties
# Add SSL to listeners
listeners=PLAINTEXT://:9092,SSL://:9093
advertised.listeners=PLAINTEXT://localhost:9092,SSL://localhost:9093

# SSL Config
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

# Security Protocol
security.inter.broker.protocol=SSL

Restart the Kafka broker. The broker is now listening on port 9093 with TLS encryption.


3. Authentication: Proving Identity with SASL & mTLS 🆔

Now that the line is encrypted, we must verify who is connecting. We will focus on SASL/SCRAM (Salted Challenge Response Authentication Mechanism), which is the modern industry standard for username/password authentication in Kafka.

The Problem

With only SSL enabled, the connection is private, but anonymous. We do not know if the connecting party is the "Admin" or a "Guest."

The Solution: SASL/SCRAM

SASL allows the exchange of credentials without sending the password in cleartext (even inside the encrypted tunnel, hashing is preferred).

Step 1: Create JAAS Configuration

Kafka uses the Java Authentication and Authorization Service (JAAS).

Create a file kafka_server_jaas.conf:

java
KafkaServer {
    org.apache.kafka.common.security.scram.ScramLoginModule required
    username="admin"
    password="admin-secret-password";
};

Step 2: Configure server.properties

Update the broker config to use SASL_SSL (Authentication over Encryption).

properties
listeners=SASL_SSL://:9093
security.inter.broker.protocol=SASL_SSL
sasl.mechanism.inter.broker.protocol=SCRAM-SHA-256
sasl.enabled.mechanisms=SCRAM-SHA-256

Step 3: Store Credentials in Zookeeper/Metadata

For SCRAM, credentials are stored in the cluster metadata, not just the config file.

bash
# Add the user 'alice' with password 'alice-secret'
bin/kafka-configs.sh --zookeeper localhost:2181 --alter --add-config 'SCRAM-SHA-256=[password=alice-secret]' --entity-type users --entity-name alice

(Note: If using KRaft mode, use --bootstrap-server instead of --zookeeper)

Step 4: Client Configuration

The client (producer/consumer) must now provide credentials to connect.

client.properties:

properties
security.protocol=SASL_SSL
sasl.mechanism=SCRAM-SHA-256
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required \
  username="alice" \
  password="alice-secret";
ssl.truststore.location=/var/private/ssl/kafka.client.truststore.jks
ssl.truststore.password=test1234

Alternative: mTLS (Mutual TLS)
Instead of SASL, you can use mTLS. In this setup, the Client also has a Keystore and presents a Certificate to the server. The server validates the client's certificate against its TrustStore. If the certificate is valid, the "Principal" is the Common Name (CN) of the certificate (e.g., CN=Alice). This is often used for server-to-server security.


4. Authorization: Controlling Access with ACLs 🚦

We have an encrypted line (SSL), and we know the user is User:alice (SASL). Now we must determine if Alice is allowed to write to the payroll topic.

The Problem

By default, authenticated users have allow.everyone.if.no.acl.found=true (depending on version and config). This means Alice could potentially delete the payroll topic or read sensitive data she shouldn't see.

The Solution: Access Control Lists (ACLs)

Kafka comes with an out-of-the-box Authorizer.
The syntax for an ACL is usually:
Principal P is [Allowed/Denied] Operation O on Resource R from Host H.

Step 1: Enable the Authorizer

In server.properties:

properties
authorizer.class.name=kafka.security.authorizer.AclAuthorizer
allow.everyone.if.no.acl.found=false

Setting this to false creates a "deny-by-default" system. No one can do anything unless explicitly granted.

Step 2: Define Super Users

You must define a super user in server.properties, or you will lock yourself out of the cluster entirely.

properties
super.users=User:admin

Step 3: Granting Permissions (The kafka-acls.sh command)

Scenario 1: Allow Alice to Produce to 'payroll'

bash
bin/kafka-acls.sh --bootstrap-server localhost:9093 --command-config admin.properties \
  --add \
  --allow-principal User:alice \
  --operation Write \
  --topic payroll

Scenario 2: Allow Bob to Consume from 'payroll' from a specific IP

bash
bin/kafka-acls.sh --bootstrap-server localhost:9093 --command-config admin.properties \
  --add \
  --allow-principal User:bob \
  --operation Read \
  --topic payroll \
  --group payroll-consumer-group \
  --allow-host 192.168.1.50

Note: We grant Read access on the Topic AND Read access on the Consumer Group. Both are required for consumers.

Scenario 3: List current ACLs

bash
bin/kafka-acls.sh --bootstrap-server localhost:9093 --command-config admin.properties --list

Resource Types

When setting ACLs, you can control access to:

  • Topic: Read/Write data.
  • Group: Join a consumer group.
  • Cluster: Create/Delete topics, alter configs.
  • TransactionalId: Use transactions.

Summary of Workflow

  1. SSL: Create Keystores/Truststores -> Config Listeners -> Secure the pipe.
  2. SASL: Create JAAS configs -> Add Users -> Verify Identity.
  3. ACLs: Enable Authorizer -> Grant specific Operations on Resources to Principals.

Industry Fact
Many large enterprises delegate Authentication to external providers like Kerberos or LDAP/Active Directory rather than managing SCRAM users directly in Kafka. This creates a Single Sign-On (SSO) experience for services. However, ACLs (Authorization) are almost always managed within Kafka itself.

References & Further Reading

Challenge

You are a Kafka administrator for a financial services company. Your goal is to write a set of kafka-acls.sh commands to implement a new, fine-grained access control policy. All commands must target the Kafka cluster's bootstrap server at broker-1:9093.

Write the necessary commands to achieve the following security requirements:

  1. Grant the principal User:producer_app permission to Write to the topic named transactions. This principal should not have any other permissions on this topic.
  2. Grant the principal User:consumer_app permission to Read from the transactions topic. This principal will consume as part of the consumer group finance_group, which also needs to be authorized for the Read operation.
  3. Grant the principal User:admin_user permission to DescribeConfigs on all topics in the cluster by using a wildcard.
  4. Grant the principal User:auditor permission to Read from the topic user_logs, but only when connecting from the IP address 192.168.1.100.

Expected result

Your solution should consist of a series of kafka-acls.sh commands that correctly configure the required permissions. Note that in a real scenario, you would need separate commands for the topic and consumer group for the consumer.

1. Producer Permissions

This command allows the producer_app to send messages to the transactions topic.

bash
kafka-acls.sh --bootstrap-server broker-1:9093 --add --allow-principal User:producer_app --operation Write --topic transactions

2. Consumer Permissions

Two distinct ACLs are required: one for reading from the topic and another to allow participation in the consumer group.

bash
kafka-acls.sh --bootstrap-server broker-1:9093 --add --allow-principal User:consumer_app --operation Read --topic transactions
kafka-acls.sh --bootstrap-server broker-1:9093 --add --allow-principal User:consumer_app --operation Read --group finance_group

3. Admin Permissions

Using a wildcard (*) for the resource name grants the permission across all existing and future topics.

bash
kafka-acls.sh --bootstrap-server broker-1:9093 --add --allow-principal User:admin_user --operation DescribeConfigs --topic '*'

4. Auditor Permissions

This command adds a host-based restriction, ensuring the auditor can only perform the specified operation when connecting from the given IP address.

bash
kafka-acls.sh --bootstrap-server broker-1:9093 --add --allow-principal User:auditor --operation Read --topic user_logs --from-host 192.168.1.100
bash

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.