How to control access to Apache Kafka with short-lived credentials

November 26, 2026
How to control access to Apache Kafka with short-lived credentials

How to control access to Apache Kafka with short-lived credentials

November 26, 2026
How to control access to Apache Kafka with short-lived credentialsHow to control access to Apache Kafka with short-lived credentials

TL;DR

  • Most of our customers already get database credentials from OpenBao or Vault that expire on their own. Kafka is usually left behind with a SCRAM password or a certificate that lasts for years.
  • Kafka has no dynamic secrets engine and it doesn't need one. Keep the ACLs static and make the identity short-lived instead, using the OpenBao PKI engine and mTLS.
  • In our lab every client gets a two minute certificate, OpenBao Agent renews it in the background and nobody ever touches a password.
  • The catch: Kafka only checks the certificate when a connection opens. A short TTL stops new connections, it does not cut off the ones already open.

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.

Short-lived certificate flow from OpenBao to Kafka OpenBao Agent logs in with AppRole, OpenBao PKI issues a two minute certificate for CN=billing-service, the agent writes key and certificate to one PEM file, the application connects to Kafka over mTLS, and the broker maps the CN to User:billing-service, which a static ACL allows to write to billing.events. SHORT-LIVED: RENEWED EVERY FEW MINUTES OpenBao PKI role: billing-service ttl 2m, max_ttl 10m OpenBao Agent AppRole login pkiCert renews at ~70% billing-service.pem key (PKCS#8) + cert CN=billing-service STATIC: NEVER CHANGES Application producer / consumer ssl.keystore.type=PEM Kafka broker ssl.client.auth=required CN → User:billing-service ACL User:billing-service Write billing.events mTLS The certificate expires. The principal and its ACL stay the same.
Figure 1. The credential is short-lived, the permission is static. Source: the kafka-mtls-openbao lab.

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.

What the lab tested, and what happened
AttemptResult
Billing AppRole requests CN=payments-serviceRefused by the PKI role
Billing AppRole requests a 24 hour certificateCapped at the 10 minute max_ttl, with a warning
Self-signed certificate with CN=billing-serviceRefused at the TLS handshake
Agent running across a full TTLRenewed with a new serial, writes still accepted
New connection after notAfterRefused at the TLS handshake
Connection opened before notAfterKept 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.

Frequently asked questions

Does OpenBao or Vault have a Kafka secrets engine?

There is no official one. You could create SCRAM users and ACLs through the Kafka Admin API on every lease, but that writes to the cluster metadata each time a lease is issued or expires. The simpler pattern is to keep ACLs static on a fixed principal and make the identity short-lived, using the OpenBao PKI engine to issue client certificates for mTLS.

How does Kafka map a client certificate to an ACL principal?

With ssl.principal.mapping.rules. The rule RULE:^CN=([a-zA-Z0-9._-]+)$/$1/,DEFAULT turns a certificate with CN=billing-service into User:billing-service. Without a rule Kafka uses the whole distinguished name as the user, so ACLs end up full of CN=... strings.

Does a short certificate TTL cut off access to Kafka?

Only for new connections. Kafka checks the client certificate during the TLS handshake and never again, so a connection opened before the certificate expires keeps working. In the lab a producer kept writing for almost two minutes after expiry. connections.max.reauth.ms applies only to SASL, not mTLS. To cut access off immediately, remove the ACL.

Do Kafka clients pick up a renewed certificate automatically?

No. The Java client does not reload its keystore while it is running. Recreate producers and consumers on a schedule comfortably inside the certificate TTL, or restart the application. A custom ssl.engine.factory.class can reload the files, but a periodic restart is simpler for most teams.

Why does the OpenBao Agent template request PKCS#8 keys?

Kafka's PEM keystore only reads PKCS#8 private keys, so the template sets private_key_format=pkcs8. The key and certificate must also come from the same template, because every pkiCert call issues a new certificate and two templates would produce a key that does not match its certificate.

The first certificate takes an afternoon. The rollout takes planning.

Getting the first certificate out of OpenBao and into Kafka takes an afternoon. Rolling it out across every producer and consumer without an outage is the part that takes planning. We run Kafka and OpenBao in production for customers 24x7 and we'd be happy to help you move off static Kafka passwords.

Get in touch at digitalis.io/contact.

References and related reading

Subscribe to newsletter

Subscribe to receive the latest blog posts to your inbox every week.

By subscribing you agree to with our Privacy Policy.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Ready to Transform 

Your Business?