Kafka
Authentication
Authorization
Message Consumption
Consumer Security

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:

  1. SASL (Simple Authentication and Security Layer): SASL allows Kafka to support various authentication mechanisms such as GSSAPI (Kerberos), PLAIN, SCRAM, and more.
  2. SSL/TLS: This provides encryption and also allows clients to authenticate using SSL certificates.
  3. 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:

  1. Configure Kafka Brokers: Edit the Kafka broker config file server.properties:
properties
1   listeners=SASL_PLAINTEXT://:9092
2   security.inter.broker.protocol=SASL_PLAINTEXT
3   sasl.enabled.mechanisms=PLAIN
4   sasl.mechanism.inter.broker.protocol=PLAIN
  1. Create JAAS Configuration File: This file will store the username and passwords for Kafka:
java
1   KafkaServer {
2   org.apache.kafka.common.security.plain.PlainLoginModule required
3   username="admin"
4   password="admin-secret"
5   user_admin="admin-secret"
6   user_kafkaclient="client-secret";
7   };
  1. Configure Kafka Client: The consumer will need similar SASL settings:
java
1   security.protocol=SASL_PLAINTEXT
2   sasl.mechanism=PLAIN
3   sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required 
4   username="kafkaclient" 
5   password="client-secret";

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:

  1. Enable Authorizer in Kafka Configuration: In the server.properties file, enable the following:
properties
   authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
   super.users=User:admin
  1. Adding ACLs: ACLs can be added using the Kafka command line tools. For instance, to allow user kafkaclient to read from topic exampleTopic, you'd run:
bash
   kafka-acls --authorizer-properties zookeeper.connect=localhost:2181 \
              --add --allow-principal User:kafkaclient \
              --operation Read --topic exampleTopic

Summary Table

FeatureSupported MechanicsConfiguration FileExample
AuthenticationSASL/PLAIN, SSL/TLS, mTLSserver.properties, JAASsasl.mechanism=PLAIN security.protocol=SASL_PLAINTEXT
AuthorizationACLsserver.propertiesauthorizer.class.name=kafka.security.auth.SimpleAclAuthorizer kafka-acls --add --allow-principal...

Additional Considerations for Kafka Security

  1. Data Encryption: Utilize SSL/TLS for data encryption in transit between brokers and clients.
  2. Network Segregation: Keep Kafka clusters within protected network environments to reduce the surface of attacks.
  3. 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.


Course illustration
Course illustration

All Rights Reserved.