Security
Encryption with TLS, authentication with SASL, and authorization with ACLs
This lab cluster uses PLAINTEXT listeners: no encryption, no
authentication, every client can do anything. That keeps the labs simple,
and it is how many internal clusters still run, which is exactly the
problem. The book's Securing Kafka chapter
is the full reference; this lesson is the practical checklist.
Three layers
| Layer | Question | Mechanism |
|---|---|---|
| Encryption | Can someone on the network read the data? | TLS on listeners |
| Authentication | Who is this client? | mTLS certificates, or SASL (SCRAM, OAUTHBEARER, GSSAPI/Kerberos) |
| Authorization | What may they do? | ACLs (StandardAuthorizer in KRaft) |
Each listener has a security protocol: PLAINTEXT, SSL,
SASL_PLAINTEXT, or SASL_SSL. This lab maps every listener to
PLAINTEXT in KAFKA_LISTENER_SECURITY_PROTOCOL_MAP; a secured cluster
would use SASL_SSL for clients.
Encryption: TLS
listeners=EXTERNAL://:9092,INTERNAL://:19092,CONTROLLER://:29093
listener.security.protocol.map=EXTERNAL:SASL_SSL,INTERNAL:SASL_SSL,CONTROLLER:SSL
ssl.keystore.location=/etc/kafka/secrets/broker.keystore.p12
ssl.keystore.password=...
ssl.truststore.location=/etc/kafka/secrets/truststore.p12
ssl.client.auth=none # or "required" for mutual TLSTLS disables zero-copy (sendfile) on that listener, because data must
be encrypted in the JVM, so expect higher broker CPU. It's usually worth it.
Authentication: SASL/SCRAM
SCRAM stores salted password hashes in the cluster metadata and needs no external system:
# create a user (credentials live in KRaft metadata)
kafka-configs.sh --bootstrap-server ... --alter --entity-type users --entity-name billing-svc \
--add-config 'SCRAM-SHA-512=[password=change-me]'
# broker
sasl.enabled.mechanisms=SCRAM-SHA-512
sasl.mechanism.inter.broker.protocol=SCRAM-SHA-512
# client (librdkafka names)
security.protocol=SASL_SSL
sasl.mechanisms=SCRAM-SHA-512
sasl.username=billing-svc
sasl.password=change-meOAUTHBEARER (OIDC tokens from your identity provider) is the common choice in larger organisations; managed Kafka services often use their cloud's IAM.
Authorization: ACLs
With an authorizer enabled, everything is denied unless an ACL allows it:
authorizer.class.name=org.apache.kafka.metadata.authorizer.StandardAuthorizer
super.users=User:admin;User:broker
allow.everyone.if.no.acl.found=falseConsumers need Read on the topic and on the group. Transactional
producers need Write on their transactional id. Missing one of
these gives TopicAuthorizationFailed or GroupAuthorizationFailed.
Prefixed ACLs (--resource-pattern-type prefixed) let one rule cover
orders.* topics.
The lab's broker logs already contain a kafka-authorizer.log (it's in
/opt/kafka/logs), but with no authorizer configured nothing is ever
checked. A common migration mistake is enabling the authorizer before
creating ACLs for every existing client. With
allow.everyone.if.no.acl.found=false, every producer and consumer
without an ACL stops at once. Create the ACLs first, then enable
enforcement, broker by broker.
Other controls
- Quotas limit a client's produce/fetch bandwidth and request rate,
so one misbehaving service can't saturate brokers:
kafka-configs.sh --alter --entity-type clients --entity-name analytics --add-config producer_byte_rate=10485760. - Secure the ecosystem too: Schema Registry, Connect's REST API (it can run arbitrary connectors with stored credentials), kafka-ui, and the JMX ports all need auth.
- Don't put secrets in connector configs in plain text. Use Connect's
config providers (
${file:/secrets/db.properties:password}).
Checklist
- TLS on every listener that leaves a trusted host.
- SASL (SCRAM or OAUTHBEARER) or mTLS per service; no shared credentials.
- An authorizer with deny-by-default and least-privilege ACLs.
- Quotas for noisy clients.
- Auth on Schema Registry, Connect, and the UI.
- Credentials rotated through a secret store.
Lab
On this cluster, add a quota and watch it bite (security changes to the lab cluster would break the other labs):
kafka-configs.sh --bootstrap-server localhost:19092 --alter --entity-type clients \
--entity-name perf --add-config producer_byte_rate=1048576 # 1 MB/s
kafka-producer-perf-test.sh --topic demo --num-records 80000 --record-size 1000 --throughput -1 \
--command-property bootstrap.servers=localhost:19092 client.id=perf
80000 records sent, 3413 records/sec (3.26 MB/sec), 6532 ms avg latency ...
# remove it afterwards
kafka-configs.sh --bootstrap-server localhost:19092 --alter --entity-type clients \
--entity-name perf --delete-config producer_byte_rateTwo things the first attempts at this lab showed:
- Quotas are per broker. 1 MB/s on each of 3 brokers gave 3.26 MB/s in total.
- Rates are averaged over a window (
quota.window.num× 1 s, 11 s by default). Runs of 5 MB and 20 MB finished at full speed (15-30 MB/s), because their average over the window stayed under the limit. Only sustained traffic is throttled, and brokers throttle by delaying responses, which is why latency jumped to seconds.