In this lesson, you will learn how to verify the identity of your Kafka clients by implementing SASL (Simple Authentication and Security Layer) mechanisms to ensure only trusted applications can access your data streams.
While TLS encryption (which you learned in lesson 25) secures the pipe by encrypting data in transit, it doesn’t tell the broker who is on the other end. SASL bridges this gap by requiring clients to provide credentials that the broker validates against a directory or internal database.
Choosing Your Mechanism
We typically focus on two common SASL mechanisms:
- SASL/PLAIN: Sends a username and password in cleartext (usually over a TLS-encrypted channel). It is simple to set up but relies entirely on your TLS layer for security.
- SASL/SCRAM: Uses a challenge-response handshake that never sends the actual password over the wire, making it significantly more robust.
Implementation Logic
To enable this, you must configure both your brokers and your clients. On the broker side, you modify your server.properties to specify the listeners and the security protocol. For example, to enable SCRAM, you would configure the sasl.enabled.mechanisms and listener.security.protocol.map properties:
listeners=SASL_SSL://kafka.example.com:9093
sasl.enabled.mechanisms=SCRAM-SHA-512
security.inter.broker.protocol=SASL_SSL
On the client side, your application needs a Java Authentication and Authorization Service (JAAS) configuration file. This tells the client which mechanism to use and provides the necessary credentials:
sasl.mechanism=SCRAM-SHA-512
security.protocol=SASL_SSL
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required \
username="producer-app" \
password="super-secret-password";
When a client attempts to connect, the broker initiates the handshake. If the handshake fails, the broker rejects the connection immediately, preventing the unauthorized client from ever requesting metadata or consuming records. By layering SASL authentication on top of your existing TLS encryption, you create a "verify-then-encrypt" pipeline that keeps your cluster isolated from unauthorized traffic and malicious actors.
Key Takeaway: SASL authentication enforces client identity by requiring a handshake process, effectively closing the door to unauthenticated applications before they can interact with your Kafka topics.