We have written quite a bit about dynamic credentials over the last year, from the database engine in OpenBao to SSH certificates that are valid for half an hour. Those posts were popular and it is now something many of our customers do as a matter of course. The application asks OpenBao for a Postgres or Cassandra user when it starts, the user is deleted when the lease ends and a password found in a log next week no longer works.
Then I ask about Kafka and there's usually a short silence. More often than not the answer is a client.properties file with a SCRAM password in it, copied into three repositories and a Helm values file. Nobody remembers when it was last changed. Kafka tends to sit outside the secrets management story even in teams that have done everything else right.
The benefits are the same as for databases. A leaked credential expires on its own and there is no rotation project every year. Removing access is a case of not renewing and OpenBao keeps a record of who asked for which identity and when. I'd never actually built this for Kafka myself so I built a lab first. This post walks through it.
Kafka needs a short-lived identity, not a secrets engine
The database engine works because it can create a user and drop it again. You could do the same for Kafka by creating SCRAM users with kafka-configs and adding ACLs with the Admin API but there is no official engine for it. You'd also be writing users and ACLs to the cluster metadata every time a lease is issued or expires. That is a lot of churn for something that never really changes.
What changes is the credential, not the permission. The billing service is always allowed to write to billing.events. So we split the two. The ACL is bound to a fixed principal, User:billing-service, and stays there forever. The thing that expires is the certificate that proves you are the billing service.
The lab runs OpenBao 2.4 and Kafka 4.1 in Docker Compose
Everything is in github.com/digitalis-io/kafka-mtls-openbao. It is a Docker Compose file with four containers: OpenBao 2.4 in dev mode, a single Kafka 4.1 node in KRaft mode, an OpenBao Agent and a small application that produces a message every 15 seconds.
git clone https://github.com/digitalis-io/kafka-mtls-openbao.git
cd kafka-mtls-openbao
./setup.sh
./test.sh
Setup takes about a minute. The tests take around five because they wait for a real rotation and a real expiry rather than faking either.
A principal mapping rule turns the certificate CN into the Kafka user
The broker only has SSL listeners and refuses any connection without a client certificate. The part that makes this work is the principal mapping rule. Without it Kafka uses the whole distinguished name as the user and your ACLs end up full of CN=... strings.
ssl.client.auth=required
ssl.keystore.type=PEM
ssl.truststore.type=PEM
ssl.principal.mapping.rules=RULE:^CN=([a-zA-Z0-9._-]+)$/$1/,DEFAULT
authorizer.class.name=org.apache.kafka.metadata.authorizer.StandardAuthorizer
allow.everyone.if.no.acl.found=false
A certificate with CN=billing-service becomes User:billing-service. I've used PEM keystores throughout because Kafka reads them directly. The files OpenBao writes go straight to the broker and the clients with no JKS conversion in between. Because every listener, including the controller, requires a client certificate, the broker authenticates to itself over mTLS too, so its own principal has to be listed in super.users.
The ACL itself is as boring as it should be:
kafka-acls.sh --add --allow-principal User:billing-service \
--operation Write --operation Describe --topic billing.events
One OpenBao role per application is where the security lives
This is where the security actually lives. Anyone holding a certificate signed by your CA with the right CN is the billing service as far as Kafka is concerned. OpenBao must only hand that CN to the billing service.
bao write pki/roles/billing-service \
allowed_domains=billing-service allow_bare_domains=true allow_subdomains=false \
client_flag=true server_flag=false key_type=ec key_bits=256 \
ttl=2m max_ttl=10m no_store=true
The role only issues one name and only client certificates. Two minutes is silly for production but it makes rotation easy to see in a lab. no_store stops OpenBao writing every certificate it issues to storage, which matters once you have hundreds of clients renewing every few hours. The application logs in with AppRole and its policy allows pki/issue/billing-service and nothing else.
From our testing it holds up nicely. When the billing AppRole asks for CN=payments-service OpenBao refuses with common name payments-service not allowed by this role. Asking for a 24 hour certificate gets you 10 minutes and a warning. A self-signed certificate with exactly the same CN is rejected at the TLS handshake. The broker's truststore holds only the OpenBao CA certificate. A certificate that doesn't chain back to that CA is refused whatever name it carries.
| Attempt | Result |
|---|---|
Billing AppRole requests CN=payments-service | Refused by the PKI role |
| Billing AppRole requests a 24 hour certificate | Capped at the 10 minute max_ttl, with a warning |
Self-signed certificate with CN=billing-service | Refused at the TLS handshake |
| Agent running across a full TTL | Renewed with a new serial, writes still accepted |
New connection after notAfter | Refused at the TLS handshake |
Connection opened before notAfter | Kept writing for almost two minutes after expiry |
Source: test.sh and long-lived.sh in the kafka-mtls-openbao repository.
OpenBao Agent renews the certificate before it expires
Nobody should be running bao write by hand to keep an application alive. The agent logs in, writes the certificate to a shared volume and issues a new one before the old one expires.
template {
destination = "/certs/billing-service.pem"
contents = <<-EOT
{{- with pkiCert "pki/issue/billing-service" "common_name=billing-service" "ttl=2m" "private_key_format=pkcs8" -}}
{{ .Key }}
{{ .Cert }}
{{- end }}
EOT
}
Two things caught my eye here. Kafka's PEM keystore only reads PKCS#8 keys, hence private_key_format=pkcs8. And the key and certificate have to come out of the same template. Every pkiCert call issues a brand new certificate and two templates would give you a key that doesn't match its certificate. In the lab the agent renewed at roughly 70% of the certificate's lifetime and the application didn't notice a thing.
An expired certificate does not close an open Kafka connection
This was the most interesting result and the one I'd want everyone to know before they rely on short TTLs. Kafka checks the client certificate during the TLS handshake and never again. I stopped the agent, opened one producer and kept sending a message every ten seconds:
certificate notAfter 22:03:20
first message 22:01:53
last message 22:05:04
The broker happily accepted messages for almost two minutes after the certificate had expired because the connection was already open. A new connection with the same certificate was refused straight away. Kafka does have connections.max.reauth.ms to force clients to authenticate again but it only applies to SASL, not to mTLS.
The flip side is that the Java client doesn't reload its keystore while it's running so a long-lived producer won't pick up the new certificate either. The lab gets away with it because the demo application starts a new process for every message. A real application needs to recreate its producers and consumers on a schedule comfortably inside the certificate TTL, or be restarted by whatever runs it. A custom ssl.engine.factory.class can reload the files too but most teams will find a periodic restart far easier to live with.
In other words, a short TTL limits how long a stolen certificate is useful for opening new connections. It is not instant revocation. If you need to cut somebody off right now, removing the ACL is still the fastest lever you have.
The real risk is a renewal that fails quietly
Kafka connections are long-lived by default. To be honest that means rotation buys you less here than it does with a database. What worries me more is something breaking quietly. If the agent stops renewing nothing fails until the application next reconnects. That could be days later in the middle of the night. It is the main reason I'd force a reconnect every few hours rather than wait for the next deploy. A broken renewal then shows up within hours while someone is around to fix it. I'd also alert on the agent failing to render a certificate and on any certificate on disk getting close to expiry.
For production, use an intermediate CA and Kubernetes auth
The lab cuts a few corners on purpose and the README lists them. For a real cluster I'd issue from an intermediate CA rather than the root and run OpenBao highly available with proper storage instead of dev mode. On Kubernetes, for example with Strimzi, I'd use Kubernetes auth rather than AppRole so the pod's service account is the only thing it needs to log in. The broker certificates should rotate too. Kafka lets you reload the broker keystore with kafka-configs without restarting it. TTLs of a few hours are a sensible starting point.
Next: SASL OAUTHBEARER with tokens signed by OpenBao
Next up I'll look at the other option, SASL OAUTHBEARER with tokens signed by OpenBao, which gets rid of certificate files on the client altogether.




