SASL authentication in Kafka - Commands & Configuration
listeners=SASL_PLAINTEXT://:9092 advertised.listeners=SASL_PLAINTEXT://localhost:9092 sasl.enabled.mechanisms=PLAIN sasl.mechanism.inter.broker.protocol=PLAIN security.inter.broker.protocol=SASL_PLAINTEXT listener.name.sasl_plaintext.plain.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="admin-secret";
This configuration file sets Kafka to use SASL with the PLAIN mechanism over plaintext connections.
listeners: Defines Kafka to listen for SASL_PLAINTEXT connections on port 9092.
advertised.listeners: Tells clients how to connect to Kafka.
sasl.enabled.mechanisms: Enables the PLAIN SASL mechanism.
sasl.mechanism.inter.broker.protocol: Sets the mechanism brokers use to talk to each other.
security.inter.broker.protocol: Uses SASL_PLAINTEXT for broker communication.
listener.name.sasl_plaintext.plain.sasl.jaas.config: Provides the username and password for authentication.
kafka-server-start.sh server.properties
kafka-console-producer.sh --broker-list localhost:9092 --topic test-topic --producer-property sasl.mechanism=PLAIN --producer-property security.protocol=SASL_PLAINTEXT --producer-property sasl.jaas.config='org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="admin-secret";'
--producer-property sasl.mechanism=PLAIN - Specifies the SASL mechanism to use.--producer-property security.protocol=SASL_PLAINTEXT - Specifies the security protocol for the connection.--producer-property sasl.jaas.config='org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="admin-secret";' - Provides the username and password for authentication.kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic --from-beginning --consumer-property sasl.mechanism=PLAIN --consumer-property security.protocol=SASL_PLAINTEXT --consumer-property sasl.jaas.config='org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="admin-secret";'
--consumer-property sasl.mechanism=PLAIN - Specifies the SASL mechanism to use.--consumer-property security.protocol=SASL_PLAINTEXT - Specifies the security protocol for the connection.--consumer-property sasl.jaas.config='org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="admin-secret";' - Provides the username and password for authentication.If you remember nothing else from this pattern, remember: SASL authentication requires both Kafka server and clients to be configured with matching security settings and credentials.
Practice
Solution
Step 1: Understand SASL role in Kafka
SASL is a protocol used to verify who is connecting to Kafka, ensuring only authorized users can access it.Step 2: Differentiate from other Kafka features
Compression, load balancing, and disk encryption are unrelated to SASL authentication.Final Answer:
To verify user identity before allowing access -> Option BQuick Check:
SASL = user verification [OK]
- Confusing SASL with encryption
- Thinking SASL manages data compression
- Mixing SASL with load balancing
Solution
Step 1: Identify correct property for SASL mechanism
The Kafka client usessasl.mechanismto specify the SASL mechanism like PLAIN, SCRAM, etc.Step 2: Check other options for correctness
security.protocolsets protocol type, not mechanism;sasl.configis invalid;security.mechanismis not a valid property.Final Answer:
sasl.mechanism = PLAIN -> Option CQuick Check:
sasl.mechanism sets SASL type [OK]
- Using security.protocol instead of sasl.mechanism
- Writing sasl.config instead of sasl.mechanism
- Confusing property names
props.put("security.protocol", "SASL_SSL");
props.put("sasl.mechanism", "SCRAM-SHA-256");
props.put("sasl.jaas.config", "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"user1\" password=\"pass1\";");What will happen when the client tries to connect?
Solution
Step 1: Analyze security.protocol setting
Settingsecurity.protocoltoSASL_SSLmeans the client uses SASL authentication over SSL encrypted connection.Step 2: Check SASL mechanism and JAAS config
The mechanism isSCRAM-SHA-256with proper JAAS config providing username and password, so authentication will succeed if credentials are correct.Final Answer:
Client connects securely using SCRAM-SHA-256 with encrypted channel -> Option DQuick Check:
SASL_SSL + SCRAM = secure auth [OK]
- Assuming SASL_SSL means no encryption
- Ignoring JAAS config for credentials
- Confusing SCRAM with PLAINTEXT
props.put("security.protocol", "SASL_PLAINTEXT");
props.put("sasl.mechanism", "PLAIN");
props.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"admin\" password=\"admin123\";");The client fails to authenticate. What is the most likely cause?
Solution
Step 1: Check security.protocol compatibility
SASL_PLAINTEXTmeans SASL without encryption. Many brokers disable this for security reasons.Step 2: Consider broker support
If the broker is configured to reject SASL_PLAINTEXT connections, authentication will fail even if credentials and mechanism are correct.Final Answer:
Broker does not support SASL_PLAINTEXT -> Option AQuick Check:
Broker must allow SASL_PLAINTEXT [OK]
- Assuming SSL is required for SASL_PLAINTEXT
- Misformatting sasl.jaas.config string
- Using wrong sasl.mechanism value
Solution
Step 1: Match security.protocol with SASL_SSL
To use encrypted SASL,security.protocolmust beSASL_SSL.Step 2: Confirm sasl.mechanism and JAAS config
For SCRAM-SHA-512,sasl.mechanismmust beSCRAM-SHA-512and JAAS config must useScramLoginModulewith username and password.Step 3: Eliminate incorrect options
props.put("security.protocol", "SSL"); props.put("sasl.mechanism", "SCRAM-SHA-512"); props.put("sasl.jaas.config", "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"user\" password=\"pass\";"); usesSSLwithout SASL, C uses wrong mechanism and protocol, D uses PLAIN mechanism instead of SCRAM-SHA-512.Final Answer:
Correct SASL_SSL with SCRAM-SHA-512 and proper JAAS config -> Option AQuick Check:
SASL_SSL + SCRAM-SHA-512 + JAAS = props.put("security.protocol", "SASL_SSL"); props.put("sasl.mechanism", "SCRAM-SHA-512"); props.put("sasl.jaas.config", "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"user\" password=\"pass\";"); [OK]
- Using SSL instead of SASL_SSL
- Mismatching mechanism and JAAS module
- Using wrong SASL mechanism version
Ready to prove it?
Pass the test and this pattern moves unknown
