Security
Turn on TLS listeners, authenticate clients, and manage ACLs and quotas as KafkaUser resources.
The install lab left a plain listener so you could see a record move. Anything past a single-project lab needs encryption, a client identity, and a list of topics that identity may touch. Streams for Apache Kafka expresses the client as a KafkaUser. The operator creates the certificate or the password and the ACLs. You do not edit broker config by hand.
This lab switches clients to the TLS listener, creates a producer user and a consumer user, and sets a quota so one client cannot fill the brokers.
Prerequisites
- The
labcluster from Kafka on OpenShift, including thetlslistener - The user operator running (
entityOperator.userOperatoron theKafkaresource) - A client you can point at port 9093. The Spring, Quarkus or .NET producer from producers and consumers is enough
Listeners that require an identity
Authorization has to be on before ACLs do anything. type: simple is the Kafka authorizer the operator configures. The tls listener uses mutual TLS. The scram listener is for a client that can only hold a password. Both encrypt the connection.
spec:
kafka:
listeners:
- name: tls
port: 9093
type: internal
tls: true
authentication:
type: tls
- name: scram
port: 9094
type: internal
tls: true
authentication:
type: scram-sha-512
authorization:
type: simple
Remove the plain listener once a client works over TLS. A plain listener left "for debugging" is the one a pod in the cluster will use. Apply the Kafka resource and wait until it is Ready again. The operator rolls the brokers.
A user is a custom resource
The producer may write payments and nothing else. The consumer may read payments and commit offsets for groups whose names start with posting. Quotas are bytes per second and a share of broker CPU time, on the same object.
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaUser
metadata:
name: payments-producer
namespace: kafka
labels:
strimzi.io/cluster: lab
spec:
authentication:
type: tls
authorization:
type: simple
acls:
- resource:
type: topic
name: payments
patternType: literal
operations: [Write, Describe, Create]
- resource:
type: transactionalId
name: payments-producer
patternType: prefix
operations: [Write, Describe]
- resource:
type: cluster
operations: [IdempotentWrite]
quotas:
producerByteRate: 1048576
consumerByteRate: 2097152
requestPercentage: 25
---
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaUser
metadata:
name: payments-posting
namespace: kafka
labels:
strimzi.io/cluster: lab
spec:
authentication:
type: tls
authorization:
type: simple
acls:
- resource:
type: topic
name: payments
patternType: literal
operations: [Read, Describe]
- resource:
type: group
name: posting
patternType: prefix
operations: [Read]
Create on the topic is only needed if the client is allowed to create it. With a topic operator, applications should not create topics, and you can drop Create. A prefix pattern is convenient and easy to make too wide. name: payments with patternType: prefix also grants payments-archive and payments.dlt. Prefer literal unless you mean the prefix.
oc apply -f users.yaml
oc get secret payments-producer -n kafka -o jsonpath='{.data.ca\.crt}' | base64 -d | head -n 2
The user operator writes a Secret per user. For TLS it contains ca.crt, user.crt, user.key and user.p12. Mount that Secret into the producer pod and set the client's ssl.truststore and ssl.keystore to the p12 material, or to the PEM files, depending on the client. The bootstrap address becomes lab-kafka-bootstrap.kafka.svc:9093. A SCRAM user uses authentication.type: scram-sha-512 and the Secret contains password instead of a key. Same ACLs.
Confirm the boundary. The producer can append to payments. The same credentials should fail against account-balance with an authorisation error. The posting user should fail to produce. If both succeed, authorization is not enabled on the cluster, or the client is still on the plain listener.
What else to close
| Item | Practice |
|---|---|
| Listener | TLS inside the cluster. Do not expose a broker Route without TLS and authentication |
| Identity | One KafkaUser per application, not one shared user for the platform |
| ACL | Literal topic names, separate read and write users |
| Quota | Set a byte rate before the first client that loops on produce |
| Git | The KafkaUser manifest is reviewed like a Deployment. The Secret is not committed |
| Connect and Bridge | They are clients. Give them users too, from Kafka Connect |
Super-users, set on the Kafka resource, are for the operator and for break-glass. An application running as a super-user makes the ACL table decorative.
What's next
- The clients to reconfigure are in producers and consumers.
- A user that suddenly uses its whole quota shows up in monitoring.
- The same users have to exist, or be accepted, on the DR cluster in DC/DR.