How to authenticate/authorize a consumer in Kafka for a topic before it consumes the message
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Apache Kafka, which is a robust streaming and queuing technology widely used for handling real-time data feeds, security is pivotal. The security mechanism that includes authentication and authorization ensures that only legitimate consumers can read messages from a specified topic. Here, we will discuss the methodology for setting up and managing the authentication and authorization processes in Kafka.
Authentication with Apache Kafka
Authentication is the process of verifying the identity of a user or service, and Kafka supports multiple methods for this:
- SASL (Simple Authentication and Security Layer): SASL allows Kafka to support various authentication mechanisms such as GSSAPI (Kerberos), PLAIN, SCRAM, and more.
- SSL/TLS: This provides encryption and also allows clients to authenticate using SSL certificates.
- mTLS (Mutual TLS Authentication): Extends SSL/TLS by requiring both the client and the server to authenticate themselves to each other.
Setting Up SASL/PLAIN for Kafka
SASL/PLAIN is a straightforward authentication approach where only the username and password are needed. Below are simplified steps to enable SASL/PLAIN:
- Configure Kafka Brokers: Edit the Kafka broker config file
server.properties:
- Create JAAS Configuration File: This file will store the username and passwords for Kafka:
- Configure Kafka Client: The consumer will need similar SASL settings:
Authorization in Kafka
After authenticating a client, Kafka needs to authorize them to perform specific actions like reading from a topic. Kafka primarily uses ACLs (Access Control Lists) for authorization.
Configuring ACLs
ACLs define what authenticated users are allowed to do. Below is how you can set these up:
- Enable Authorizer in Kafka Configuration: In the
server.propertiesfile, enable the following:
- Adding ACLs: ACLs can be added using the Kafka command line tools. For instance, to allow user
kafkaclientto read from topicexampleTopic, you'd run:
Summary Table
| Feature | Supported Mechanics | Configuration File | Example |
| Authentication | SASL/PLAIN, SSL/TLS, mTLS | server.properties, JAAS | sasl.mechanism=PLAIN
security.protocol=SASL_PLAINTEXT |
| Authorization | ACLs | server.properties | authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
kafka-acls --add --allow-principal... |
Additional Considerations for Kafka Security
- Data Encryption: Utilize SSL/TLS for data encryption in transit between brokers and clients.
- Network Segregation: Keep Kafka clusters within protected network environments to reduce the surface of attacks.
- Logging and Monitoring: Integrate Kafka with monitoring tools to actively watch for unauthorized access attempts.
Conclusion
Setting up secure authentication and robust authorization in Kafka is essential for protecting sensitive data streams and ensuring that only authorized consumers access designated topics. By integrating properly configured security mechanisms and constantly updating them to cope with evolving security threats, organizations can confidently use Kafka as a part of their data architecture.

