Kafka Security
Security configuration for Apache Kafka covering authentication, authorization, and encryption.
Security Overview
Section titled “Security Overview”Kafka security encompasses three core areas:
| Security Layer | Purpose | Mechanisms |
|---|---|---|
| Authentication | Verify client identity | SASL (PLAIN, SCRAM, GSSAPI, OAUTHBEARER), mTLS |
| Authorization | Control access to resources | ACLs (Access Control Lists) |
| Encryption | Protect data confidentiality | TLS (in-transit), filesystem encryption (at-rest) |
Authentication
Section titled “Authentication”Authentication Mechanisms
Section titled “Authentication Mechanisms”| Mechanism | Description | Use Case |
|---|---|---|
| SASL/PLAIN | Username/password (plaintext) | Development, simple setups |
| SASL/SCRAM | Salted Challenge Response | Production without Kerberos |
| SASL/GSSAPI | Kerberos authentication | Enterprise environments |
| SASL/OAUTHBEARER | OAuth 2.0 tokens | Cloud-native, OAuth infrastructure |
| mTLS | Mutual TLS certificates | Certificate-based auth |
SASL/SCRAM Configuration
Section titled “SASL/SCRAM Configuration”SCRAM (Salted Challenge Response Authentication Mechanism) provides secure password-based authentication.
Broker configuration:
listeners=SASL_SSL://0.0.0.0:9093advertised.listeners=SASL_SSL://kafka1:9093
security.inter.broker.protocol=SASL_SSLsasl.mechanism.inter.broker.protocol=SCRAM-SHA-512sasl.enabled.mechanisms=SCRAM-SHA-512
# TLS configurationssl.keystore.location=/etc/kafka/ssl/kafka.keystore.jksssl.keystore.password=keystore-passwordssl.key.password=key-passwordssl.truststore.location=/etc/kafka/ssl/kafka.truststore.jksssl.truststore.password=truststore-passwordCreate SCRAM credentials:
# Create userkafka-configs.sh --bootstrap-server kafka:9092 \ --alter \ --add-config 'SCRAM-SHA-512=[password=user-password]' \ --entity-type users \ --entity-name alice
# For inter-broker communicationkafka-configs.sh --bootstrap-server kafka:9092 \ --alter \ --add-config 'SCRAM-SHA-512=[password=broker-password]' \ --entity-type users \ --entity-name kafka-brokerClient configuration:
security.protocol=SASL_SSLsasl.mechanism=SCRAM-SHA-512sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required \ username="alice" \ password="user-password";
ssl.truststore.location=/etc/kafka/ssl/client.truststore.jksssl.truststore.password=truststore-passwordmTLS Configuration
Section titled “mTLS Configuration”Mutual TLS authenticates both client and server using certificates.
Broker configuration:
listeners=SSL://0.0.0.0:9093advertised.listeners=SSL://kafka1:9093
security.inter.broker.protocol=SSL
ssl.keystore.location=/etc/kafka/ssl/kafka.keystore.jksssl.keystore.password=keystore-passwordssl.key.password=key-passwordssl.truststore.location=/etc/kafka/ssl/kafka.truststore.jksssl.truststore.password=truststore-password
# Require client certificatesssl.client.auth=requiredClient configuration:
security.protocol=SSL
ssl.keystore.location=/etc/kafka/ssl/client.keystore.jksssl.keystore.password=keystore-passwordssl.key.password=key-passwordssl.truststore.location=/etc/kafka/ssl/client.truststore.jksssl.truststore.password=truststore-passwordSASL/OAUTHBEARER Configuration
Section titled “SASL/OAUTHBEARER Configuration”OAuth 2.0 authentication for token-based systems.
Broker configuration:
listeners=SASL_SSL://0.0.0.0:9093sasl.enabled.mechanisms=OAUTHBEARER
# Custom callback handler for token validationlistener.name.sasl_ssl.oauthbearer.sasl.server.callback.handler.class=\ org.apache.kafka.common.security.oauthbearer.OAuthBearerValidatorCallbackHandler
# JWKS endpoint for token validationlistener.name.sasl_ssl.oauthbearer.sasl.jaas.config=\ org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required \ unsecuredLoginStringClaim_sub="admin";Authorization
Section titled “Authorization”ACL Fundamentals
Section titled “ACL Fundamentals”Kafka ACLs define who can perform what operations on which resources.
ACL Components
Section titled “ACL Components”| Component | Description | Examples |
|---|---|---|
| Principal | User or group identity | User:alice, User:CN=client.example.com |
| Permission | Allow or Deny | ALLOW, DENY |
| Operation | Action to perform | Read, Write, Create, Delete, Describe, Alter |
| Resource | Kafka resource | Topic:orders, Group:order-processor, Cluster:kafka-cluster |
| Host | Source IP | *, 192.168.1.100 |
Managing ACLs
Section titled “Managing ACLs”# Grant producer access to topickafka-acls.sh --bootstrap-server kafka:9092 \ --add \ --allow-principal User:producer-app \ --operation Write \ --operation Describe \ --topic orders
# Grant consumer access to topic and groupkafka-acls.sh --bootstrap-server kafka:9092 \ --add \ --allow-principal User:consumer-app \ --operation Read \ --operation Describe \ --topic orders \ --group order-processor
# Grant admin access to all topicskafka-acls.sh --bootstrap-server kafka:9092 \ --add \ --allow-principal User:admin \ --operation All \ --topic '*'
# List ACLskafka-acls.sh --bootstrap-server kafka:9092 --list
# List ACLs for specific topickafka-acls.sh --bootstrap-server kafka:9092 \ --list \ --topic orders
# Remove ACLkafka-acls.sh --bootstrap-server kafka:9092 \ --remove \ --allow-principal User:producer-app \ --operation Write \ --topic ordersCommon ACL Patterns
Section titled “Common ACL Patterns”| Use Case | ACLs Required |
|---|---|
| Producer | Write, Describe on topic; Create if auto-create enabled |
| Consumer | Read, Describe on topic; Read on consumer group |
| Transactional producer | Write on topic; Write on transactional ID; Describe on cluster |
| Kafka Connect | Read/Write on topics; Read/Write on internal topics; Describe on cluster |
| Admin | All on cluster; All on topics |
Authorizer Configuration
Section titled “Authorizer Configuration”# Enable ACL authorizerauthorizer.class.name=org.apache.kafka.metadata.authorizer.StandardAuthorizer
# Super users (bypass ACLs)super.users=User:admin;User:kafka-broker
# Default when no ACL matches (deny recommended for production)allow.everyone.if.no.acl.found=falseEncryption
Section titled “Encryption”TLS Configuration
Section titled “TLS Configuration”TLS encrypts data in transit between clients and brokers, and between brokers.
Certificate Generation
Section titled “Certificate Generation”# Generate CAopenssl req -new -x509 -keyout ca-key -out ca-cert -days 365 \ -subj "/CN=Kafka-CA"
# Generate broker keystorekeytool -keystore kafka.keystore.jks -alias kafka-broker \ -validity 365 -genkey -keyalg RSA \ -dname "CN=kafka1.example.com"
# Create CSRkeytool -keystore kafka.keystore.jks -alias kafka-broker \ -certreq -file kafka-broker.csr
# Sign certificateopenssl x509 -req -CA ca-cert -CAkey ca-key \ -in kafka-broker.csr -out kafka-broker-signed.crt \ -days 365 -CAcreateserial
# Import CA and signed certkeytool -keystore kafka.keystore.jks -alias CARoot \ -import -file ca-certkeytool -keystore kafka.keystore.jks -alias kafka-broker \ -import -file kafka-broker-signed.crt
# Create truststorekeytool -keystore kafka.truststore.jks -alias CARoot \ -import -file ca-certTLS Broker Configuration
Section titled “TLS Broker Configuration”listeners=SSL://0.0.0.0:9093advertised.listeners=SSL://kafka1.example.com:9093
security.inter.broker.protocol=SSL
ssl.keystore.location=/etc/kafka/ssl/kafka.keystore.jksssl.keystore.password=keystore-passwordssl.key.password=key-passwordssl.truststore.location=/etc/kafka/ssl/kafka.truststore.jksssl.truststore.password=truststore-password
# TLS protocol versionsssl.enabled.protocols=TLSv1.3,TLSv1.2ssl.protocol=TLSv1.3
# Cipher suites (optional - use defaults for TLS 1.3)ssl.cipher.suites=TLS_AES_256_GCM_SHA384,TLS_AES_128_GCM_SHA256TLS Performance Impact
Section titled “TLS Performance Impact”TLS Disables Zero-Copy
TLS encryption requires data to pass through user space for encryption/decryption, disabling Kafka's zero-copy optimization. This can reduce throughput by 30-50% depending on workload and hardware.
| Aspect | Without TLS | With TLS |
|---|---|---|
| Zero-copy | Enabled | Disabled |
| CPU usage | Lower | Higher (encryption overhead) |
| Throughput | Higher | 30-50% reduction typical |
| Latency | Lower | Slightly higher |
Security Best Practices
Section titled “Security Best Practices”Authentication
Section titled “Authentication”| Practice | Rationale |
|---|---|
| Use SCRAM-SHA-512 or stronger | Avoid plaintext passwords |
| Rotate credentials regularly | Limit exposure window |
| Use separate credentials per application | Limit blast radius |
| Use mTLS for service-to-service | Certificate-based identity |
Authorization
Section titled “Authorization”| Practice | Rationale |
|---|---|
| Deny by default | Explicit allow only |
| Principle of least privilege | Minimum required access |
| Use groups/roles where possible | Easier management |
| Audit ACL changes | Track access modifications |
| Separate admin credentials | Protect administrative access |
Encryption
Section titled “Encryption”| Practice | Rationale |
|---|---|
| Enable TLS for all listeners | Encrypt all traffic |
| Use TLS 1.3 where possible | Strongest protocol |
| Rotate certificates before expiry | Maintain continuity |
| Use strong cipher suites | Avoid weak encryption |
| Consider at-rest encryption | Protect stored data |
Operations
Section titled “Operations”| Practice | Rationale |
|---|---|
| Enable audit logging | Track security events |
| Monitor authentication failures | Detect attacks |
| Regular security reviews | Identify misconfigurations |
| Patch regularly | Address vulnerabilities |
| Network segmentation | Limit network exposure |
Listener Configuration
Section titled “Listener Configuration”Multiple Listeners
Section titled “Multiple Listeners”# Different security per listenerlisteners=PLAINTEXT://0.0.0.0:9092,SSL://0.0.0.0:9093,SASL_SSL://0.0.0.0:9094advertised.listeners=PLAINTEXT://kafka1:9092,SSL://kafka1:9093,SASL_SSL://kafka1:9094
# Inter-broker uses SASL_SSLsecurity.inter.broker.protocol=SASL_SSL
# Listener-specific configurationlistener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_SSL:SASL_SSL
listener.name.sasl_ssl.scram-sha-512.sasl.jaas.config=\ org.apache.kafka.common.security.scram.ScramLoginModule required \ username="kafka-broker" \ password="broker-password";Internal vs External Listeners
Section titled “Internal vs External Listeners”# Internal (within datacenter) - SASL_PLAINTEXT# External (from internet) - SASL_SSLlisteners=INTERNAL://0.0.0.0:9092,EXTERNAL://0.0.0.0:9093advertised.listeners=INTERNAL://kafka1.internal:9092,EXTERNAL://kafka1.example.com:9093listener.security.protocol.map=INTERNAL:SASL_PLAINTEXT,EXTERNAL:SASL_SSLinter.broker.listener.name=INTERNALEnabling Security on a Running Cluster
Section titled “Enabling Security on a Running Cluster”Security can be incrementally enabled on an existing cluster without downtime through a phased rolling restart approach.
Migration Phases
Section titled “Migration Phases”Phase 1: Open Secured Ports
Section titled “Phase 1: Open Secured Ports”Add secured listener while keeping PLAINTEXT open:
# server.properties - Phase 1listeners=PLAINTEXT://0.0.0.0:9091,SSL://0.0.0.0:9092Perform rolling restart of all brokers.
Phase 2: Migrate Clients
Section titled “Phase 2: Migrate Clients”Update client configurations to use the secured port:
bootstrap.servers=kafka1:9092,kafka2:9092,kafka3:9092security.protocol=SSLssl.truststore.location=/etc/kafka/ssl/client.truststore.jksssl.truststore.password=truststore-passwordRestart clients to pick up new configuration.
Phase 3: Enable Inter-Broker Security
Section titled “Phase 3: Enable Inter-Broker Security”Configure brokers to use SSL for inter-broker communication:
# server.properties - Phase 3listeners=PLAINTEXT://0.0.0.0:9091,SSL://0.0.0.0:9092security.inter.broker.protocol=SSLPerform rolling restart of all brokers.
Phase 4: Close PLAINTEXT Port
Section titled “Phase 4: Close PLAINTEXT Port”Remove the insecure listener:
# server.properties - Phase 4listeners=SSL://0.0.0.0:9092security.inter.broker.protocol=SSLPerform final rolling restart.
Adding SASL to SSL
Section titled “Adding SASL to SSL”To add SASL authentication on top of SSL encryption, open an additional SASL_SSL port:
Phase 1:
listeners=PLAINTEXT://0.0.0.0:9091,SSL://0.0.0.0:9092,SASL_SSL://0.0.0.0:9093Phase 2: Migrate clients to SASL_SSL port (9093)
Phase 3:
listeners=PLAINTEXT://0.0.0.0:9091,SSL://0.0.0.0:9092,SASL_SSL://0.0.0.0:9093security.inter.broker.protocol=SSLPhase 4:
listeners=SSL://0.0.0.0:9092,SASL_SSL://0.0.0.0:9093security.inter.broker.protocol=SSLRolling Restart Best Practices
- Stop brokers gracefully using SIGTERM
- Wait for restarted broker to rejoin ISR before proceeding to next broker
- Monitor under-replicated partitions during the process
- Keep PLAINTEXT listener open until all clients have migrated
Related Documentation
Section titled “Related Documentation”- Authentication - Authentication mechanisms
- Authorization - ACL configuration
- Encryption - TLS setup
- Operations - Security operations