
Apache Kafka 4.1.0 (KRaft) with Keycloak OAuth2 authentication using Strimzi - bypasses CVE-2025-27817 URL allowlist restriction
Production-ready Apache Kafka 4.1.0 (KRaft mode) with Keycloak 26.1.1 OAuth2/OIDC authentication using Strimzi Kafka image.
This is an evolution of the previous POC with significant improvements:
Apache Kafka 4.0.0+ introduced URL allowlist (org.apache.kafka.sasl.oauthbearer.allowed.urls) as JVM system property to fix SSRF/arbitrary file read vulnerability. This breaks standard OAuth usage in native Apache Kafka clients.
Solution: Strimzi Kafka OAuth library doesn't implement this restriction, enabling OAuth functionality with Kafka 4.1.0.
# Generate SSL certificates
cd kafka-security
./generate-certs.sh
cd ..
# Start services
docker compose up -d
# Verify Keycloak
curl http://localhost:8080/health/ready
# Setup Keycloak realm and clients
./scripts/setup-keycloak.sh
# Test OAuth producer
source ~/.venv/bin/activate
uv pip install confluent-kafka
python tests/quick_test.py
keycloak:8080 (HTTP) ←→ kafka-broker:9093 (SASL_SSL/OAuth)
↔ kafka-broker:19092 (PLAINTEXT/inter-broker)
↔ kafka-broker:29093 (PLAINTEXT/KRaft controller)
kafka-security/ca-cert + ca-keykafka-security/broker/kafka.server.keystore.jks (contains server cert + private key)kafka-security/broker/kafka.server.truststore.jks (contains CA cert)changeit (all keystores/truststores)# Broker certificate
CN=kafka-broker
SAN=DNS:kafka-broker,DNS:localhost,IP:127.0.0.1
# Validity: 3650 days
# Key algorithm: RSA 2048-bit
# Signature algorithm: SHA256withRSA
kafka-broker (confidential)
kafka-brokersetup-keycloak.shkafka-broker to JWT aud claimpreferred_username in tokenkafka-producer (confidential)
kafka-producerclient_credentialskafka-consumer (confidential)
kafka-consumerclient_credentialsPOST http://localhost:8080/realms/kafka-realm/protocol/openid-connect/token
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
&client_id=kafka-producer
&client_secret=<secret>
&scope=profile email
{
"aud": ["kafka-broker", "account"],
"iss": "http://localhost:8080/realms/kafka-realm",
"azp": "kafka-producer",
"preferred_username": "service-account-kafka-producer",
"scope": "profile email"
}
# Node identity
node.id=1
process.roles=broker,controller
controller.quorum.voters=1@kafka-broker:29093
# Listeners
listeners=SASL_SSL://0.0.0.0:9093,PLAINTEXT://0.0.0.0:19092,CONTROLLER://0.0.0.0:29093
advertised.listeners=SASL_SSL://localhost:9093,PLAINTEXT://kafka-broker:19092
listener.security.protocol.map=SASL_SSL:SASL_SSL,PLAINTEXT:PLAINTEXT,CONTROLLER:PLAINTEXT
inter.broker.listener.name=PLAINTEXT
controller.listener.names=CONTROLLER
# SASL mechanism
sasl.enabled.mechanisms=OAUTHBEARER
# Strimzi OAuth handlers (per-listener for SASL_SSL)
listener.name.sasl_ssl.oauthbearer.sasl.login.callback.handler.class=io.strimzi.kafka.oauth.client.JaasClientOauthLoginCallbackHandler
listener.name.sasl_ssl.oauthbearer.sasl.server.callback.handler.class=io.strimzi.kafka.oauth.server.JaasServerOauthValidatorCallbackHandler
# OAuth configuration via JAAS
listener.name.sasl_ssl.oauthbearer.sasl.jaas.config=org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required \
oauth.client.id="kafka-broker" \
oauth.client.secret="<secret>" \
oauth.token.endpoint.uri="http://keycloak:8080/realms/kafka-realm/protocol/openid-connect/token" \
oauth.valid.issuer.uri="http://localhost:8080/realms/kafka-realm" \
oauth.jwks.endpoint.uri="http://keycloak:8080/realms/kafka-realm/protocol/openid-connect/certs" \
oauth.username.claim="preferred_username";
oauth.client.id: Client identifier for token acquisitionoauth.client.secret: Client secret for token acquisitionoauth.token.endpoint.uri: Keycloak token endpoint (broker uses internal hostname keycloak:8080)oauth.valid.issuer.uri: Expected JWT issuer (must match token iss claim, uses external localhost:8080)oauth.jwks.endpoint.uri: JWKS endpoint for JWT signature validationoauth.username.claim: JWT claim for principal extractionauthorizer.class.name=org.apache.kafka.metadata.authorizer.StandardAuthorizer
super.users=User:kafka-broker;User:ANONYMOUS
allow.everyone.if.no.acl.found=true
Note: Currently permissive for testing. Production should use ACLs.
from confluent_kafka import Producer
conf = {
'bootstrap.servers': 'localhost:9093',
'security.protocol': 'SASL_SSL',
'sasl.mechanisms': 'OAUTHBEARER',
'sasl.oauthbearer.method': 'oidc',
'sasl.oauthbearer.client.id': 'kafka-producer',
'sasl.oauthbearer.client.secret': '<secret>',
'sasl.oauthbearer.token.endpoint.url': 'http://localhost:8080/realms/kafka-realm/protocol/openid-connect/token',
'ssl.ca.location': 'kafka-security/ca-cert',
'ssl.endpoint.identification.algorithm': 'none',
}
producer = Producer(conf)
producer.produce('topic', b'message')
producer.flush()
from confluent_kafka import Consumer
conf = {
'bootstrap.servers': 'localhost:9093',
'group.id': 'test-group',
'security.protocol': 'SASL_SSL',
'sasl.mechanisms': 'OAUTHBEARER',
'sasl.oauthbearer.method': 'oidc',
'sasl.oauthbearer.client.id': 'kafka-consumer',
'sasl.oauthbearer.client.secret': '<secret>',
'sasl.oauthbearer.token.endpoint.url': 'http://localhost:8080/realms/kafka-realm/protocol/openid-connect/token',
'ssl.ca.location': 'kafka-security/ca-cert',
'ssl.endpoint.identification.algorithm': 'none',
'auto.offset.reset': 'earliest',
}
consumer = Consumer(conf)
consumer.subscribe(['topic'])
while True:
msg = consumer.poll(1.0)
if msg: print(msg.value())
confluent-kafka-python uses librdkafka (C library) which implements OAuth via sasl.oauthbearer.method=oidc. This implementation doesn't check the org.apache.kafka.sasl.oauthbearer.allowed.urls system property that blocks native Apache Kafka Java clients.
TOKEN=$(curl -s -X POST http://localhost:8080/realms/kafka-realm/protocol/openid-connect/token \
-d "grant_type=client_credentials" \
-d "client_id=kafka-producer" \
-d "client_secret=<secret>" | jq -r .access_token)
echo $TOKEN | cut -d. -f2 | base64 -d 2>/dev/null | jq .
Expected claims:
{
"aud": ["kafka-broker", "account"],
"iss": "http://localhost:8080/realms/kafka-realm",
"azp": "kafka-producer",
"preferred_username": "service-account-kafka-producer"
}
docker logs kafka-broker 2>&1 | grep -E "Strimzi|JWTSignatureValidator|OAUTHBEARER"
Expected:
[io.strimzi.kafka.oauth.validator.JWTSignatureValidator] JWKS keys change detected
docker exec kafka-broker netstat -tlnp | grep java
Expected:
tcp6 0.0.0.0:9093 LISTEN (SASL_SSL)
tcp6 0.0.0.0:19092 LISTEN (PLAINTEXT)
tcp6 0.0.0.0:29093 LISTEN (CONTROLLER)
docker exec kafka-broker cat /var/lib/kafka/data/meta.properties
Expected:
version=1
cluster.id=kafka-cluster-01
node.id=1
Issue: {"status":"invalid_token"}
oauth.jwks.endpoint.uri is reachable from broker containerdocker exec kafka-broker curl http://keycloak:8080/realms/kafka-realm/protocol/openid-connect/certsIssue: Token audience mismatch
aud claim doesn't contain kafka-broker./scripts/setup-keycloak.sh to add audience mapperaud claim includes kafka-brokerIssue: Token issuer mismatch
iss doesn't match oauth.valid.issuer.urioauth.valid.issuer.uri=http://localhost:8080/realms/kafka-realm (external hostname)http://keycloak:8080 for token endpoint but validates against http://localhost:8080 issuerIssue: Native Java Kafka clients fail with URL allowlist error
JWT tokens from Keycloak have 5-minute expiry. Strimzi OAuth automatically handles refresh:
oauth.refresh.token: Not used (client_credentials grant)sasl.oauthbearer.jwks.endpoint.refresh.ms=3600000 # 1 hour
sasl.oauthbearer.jwks.endpoint.retry.backoff.ms=100
sasl.oauthbearer.jwks.endpoint.retry.backoff.max.ms=10000
connections.max.idle.ms=600000
connection.failed.authentication.delay.ms=1000
ssl.endpoint.identification.algorithm=https (remove none)allow.everyone.if.no.acl.found=true)kafka-acls --bootstrap-server localhost:9093 \
--command-config admin.properties \
--add --allow-principal User:kafka-producer \
--operation Write --topic '*'
oauth.token.endpoint.uri and oauth.jwks.endpoint.uri to HTTPS URLs.
├── docker-compose.yml # Orchestration
├── .env # Secrets (gitignored)
├── kafka-config/
│ ├── kraft-config.properties # Kafka broker configuration
│ ├── producer.properties # Producer OAuth config (for CLI tools)
│ └── consumer.properties # Consumer OAuth config (for CLI tools)
├── kafka-security/
│ ├── generate-certs.sh # SSL certificate generator
│ ├── ca-cert # Root CA certificate
│ ├── ca-key # Root CA private key
│ └── broker/
│ ├── kafka.server.keystore.jks
│ └── kafka.server.truststore.jks
├── scripts/
│ └── setup-keycloak.sh # Keycloak realm/client setup
└── tests/
└── quick_test.py # OAuth validation test
The Strimzi Kafka image (quay.io/strimzi/kafka:0.48.0-kafka-4.1.0) is used instead of the official Apache Kafka image because:
io.strimzi.kafka.oauth.*)Image breakdown:
Broker configuration has two URLs:
oauth.token.endpoint.uri=http://keycloak:8080/... (internal Docker network)oauth.valid.issuer.uri=http://localhost:8080/... (external, matches JWT iss claim)This is because:
Broker extracts principal from JWT preferred_username claim:
service-account-kafka-producer → User:service-account-kafka-producer
ACLs reference this principal for authorization.
| Component | Version | Notes |
|---|---|---|
| Apache Kafka | 4.1.0 | KRaft mode (no ZooKeeper) |
| Strimzi Kafka Image | 0.48.0 | Docker image: quay.io/strimzi/kafka:0.48.0-kafka-4.1.0 |
| Strimzi OAuth Library | 0.17.0 | Pre-bundled in Strimzi Kafka 0.48.0 image |
| Keycloak | 26.1.1 | Latest LTS |
| librdkafka | 2.12.0+ | OIDC OAuth support |
| confluent-kafka-python | 2.12.0+ | Matches librdkafka version |