Security Goals
| Goal | Kafka feature |
| Encryption in transit | TLS between clients and brokers, and between brokers |
| Authentication (who are you?) | SASL (SCRAM, PLAIN, GSSAPI/Kerberos, OAUTHBEARER) or mutual TLS (client certificates) |
| Authorization (what may you do?) | ACLs (next lesson) |
| Auditing | Authorizer logs, broker request logs, external audit tools |
| Encryption at rest | Disk or volume encryption (LUKS, cloud KMS), application-level encryption for sensitive fields |
Security Protocols
| Protocol | Encrypted | Authenticated | Use |
PLAINTEXT | No | No | Local development only |
SSL | Yes (TLS) | Optional with client certificates | TLS only or mutual TLS |
SASL_PLAINTEXT | No | Yes | Avoid in production (passwords visible) |
SASL_SSL | Yes | Yes | Recommended for production |
Step 1: Create a Certificate Authority and Broker Certificates
bash
# 1. private CA (for production use your company PKI or a cloud CA)
openssl req -new -x509 -keyout ca-key.pem -out ca-cert.pem -days 3650 -subj "/CN=Kafka-CA" -passout pass:capass
# 2. broker keystore with a key pair. SAN must contain every name clients use to reach the broker
keytool -genkeypair -alias broker1 -keystore broker1.keystore.jks -storepass changeit -keypass changeit \
-keyalg RSA -keysize 2048 -validity 365 -dname "CN=broker1.example.com" -ext SAN=DNS:broker1.example.com,DNS:broker1,IP:10.0.1.11
# 3. certificate signing request, sign with the CA
keytool -certreq -alias broker1 -keystore broker1.keystore.jks -storepass changeit -file broker1.csr
openssl x509 -req -CA ca-cert.pem -CAkey ca-key.pem -in broker1.csr -out broker1-signed.pem -days 365 -CAcreateserial -passin pass:capass \
-extfile <(printf "subjectAltName=DNS:broker1.example.com,DNS:broker1,IP:10.0.1.11")
# 4. import CA and signed certificate into the keystore, CA into the truststore
keytool -importcert -alias CARoot -file ca-cert.pem -keystore broker1.keystore.jks -storepass changeit -noprompt
keytool -importcert -alias broker1 -file broker1-signed.pem -keystore broker1.keystore.jks -storepass changeit -noprompt
keytool -importcert -alias CARoot -file ca-cert.pem -keystore truststore.jks -storepass changeit -noprompt
| File | Contents |
| Keystore | The broker's own private key and certificate chain (proves identity) |
| Truststore | CA certificates that this program trusts (verifies others) |
Common mistake: The certificate's SAN (subject alternative name) must match the hostname clients use. If clients connect to broker1.example.com and the SAN has only localhost, TLS fails with a hostname verification error.
Step 2: Broker TLS Configuration
text
# broker.properties
listeners=SASL_SSL://broker1.example.com:9092,CONTROLLER://ctrl1:9093
advertised.listeners=SASL_SSL://broker1.example.com:9092
inter.broker.listener.name=SASL_SSL
listener.security.protocol.map=SASL_SSL:SASL_SSL,CONTROLLER:SASL_SSL
ssl.keystore.location=/etc/kafka/ssl/broker1.keystore.jks
ssl.keystore.password=${file:/etc/kafka/secrets/ssl.properties:keystore} # use a config provider or a secrets manager, not plain text
ssl.key.password=${file:/etc/kafka/secrets/ssl.properties:key}
ssl.truststore.location=/etc/kafka/ssl/truststore.jks
ssl.truststore.password=${file:/etc/kafka/secrets/ssl.properties:truststore}
ssl.endpoint.identification.algorithm=https # verify hostnames
ssl.enabled.protocols=TLSv1.3,TLSv1.2
# ssl.client.auth=required # turns on mutual TLS (client certificates)
Step 3: SASL/SCRAM Authentication
SCRAM (Salted Challenge Response) stores salted password hashes in the cluster metadata. Passwords never travel in plain form, and users can be added without restarting brokers. It is the most common choice for username and password logins.
bash
# create users (works against a running cluster; for the first admin use the bootstrap option when formatting storage)
bin/kafka-configs.sh --bootstrap-server broker1:9092 --command-config admin.properties --alter \
--add-config 'SCRAM-SHA-512=[iterations=8192,password=S3cret-orders]' --entity-type users --entity-name orders-service
bin/kafka-configs.sh --bootstrap-server broker1:9092 --command-config admin.properties --describe --entity-type users
text
# broker.properties additions
sasl.enabled.mechanisms=SCRAM-SHA-512
sasl.mechanism.inter.broker.protocol=SCRAM-SHA-512
sasl.mechanism.controller.protocol=SCRAM-SHA-512
listener.name.sasl_ssl.scram-sha-512.sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username="broker" password="broker-secret";
Step 4: Client Configuration
text
# client.properties (producers, consumers, admin tools)
bootstrap.servers=broker1.example.com:9092,broker2.example.com:9092
security.protocol=SASL_SSL
sasl.mechanism=SCRAM-SHA-512
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username="orders-service" password="S3cret-orders";
ssl.truststore.location=/etc/app/truststore.jks
ssl.truststore.password=changeit
ssl.endpoint.identification.algorithm=https
bash
bin/kafka-console-producer.sh --bootstrap-server broker1.example.com:9092 --topic orders --producer.config client.properties
bin/kafka-topics.sh --bootstrap-server broker1.example.com:9092 --command-config client.properties --list
Mutual TLS (Client Certificates)
With ssl.client.auth=required, each client has a keystore and certificate signed by the CA. The broker maps the certificate distinguished name (for example CN=orders-service,OU=shop) to the principal used by ACLs, with ssl.principal.mapping.rules to shorten it.
text
# broker
ssl.client.auth=required
ssl.principal.mapping.rules=RULE:^CN=([^,]+),.*$/$1/,DEFAULT
# client
security.protocol=SSL
ssl.keystore.location=/etc/app/orders-service.keystore.jks
ssl.keystore.password=...
ssl.key.password=...
Other Mechanisms
| Mechanism | Notes |
| SASL/PLAIN | Username and password in the JAAS file. Only with TLS, usually with an external password check. Simple but less flexible than SCRAM |
| SASL/GSSAPI (Kerberos) | Enterprise single sign-on environments |
| SASL/OAUTHBEARER | Tokens from an identity provider (OIDC), good for cloud and central identity. Use refresh and validation callbacks |
| Cloud IAM (for example MSK IAM) | Managed services offer their own auth plugins |
Troubleshooting
| Error | Likely cause |
SSLHandshakeException: PKIX path building failed | Truststore does not contain the CA that signed the broker certificate |
No subject alternative names matching / hostname mismatch | Wrong SAN, or clients use another name or IP than in the certificate |
SaslAuthenticationException: Authentication failed | Wrong username, password or mechanism, or the user was not created |
Unexpected handshake request with client mechanism | Client and broker protocols or mechanisms do not match |
| Connection reset immediately | Client uses PLAINTEXT against an SSL listener (or the reverse) |
| Inter-broker errors after enabling security | Inter-broker listener and credentials not configured, use a rolling approach with an extra listener first |
bash
openssl s_client -connect broker1.example.com:9092 -servername broker1.example.com </dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName
keytool -list -v -keystore /etc/kafka/ssl/broker1.keystore.jks | grep -E "Alias|Valid|DNS"
Enable security without downtime: Add a second listener (for example SASL_SSL on port 9094) next to the plaintext one, move clients over one by one, switch inter-broker traffic, then remove the plaintext listener.
Conclusion
Use SASL_SSL in production: TLS for encryption and SCRAM (or OAUTHBEARER, Kerberos, mutual TLS) for identity. Create certificates with correct SANs, keep secrets out of config files, and roll out security with an extra listener to avoid downtime.
Quick Summary: SASL_SSL; correct SAN; SCRAM users via kafka-configs; client.properties; add listener to migrate.