Debian Kafka 权限设置实操指南
一 前置准备
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="admin-secret"
user_admin="admin-secret"
user_alice="alice-secret";
};
listeners=SASL_PLAINTEXT://:9092
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.mechanism.inter.broker.protocol=PLAIN
sasl.enabled.mechanisms=PLAIN
authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
super.users=User:admin
Environment="KAFKA_OPTS=-Djava.security.auth.login.config=/etc/kafka/kafka_server_jaas.conf"
然后执行:sudo systemctl daemon-reload
sudo systemctl restart kafka
以上步骤完成后,Broker 侧认证与授权入口已就绪,后续通过 ACL 对用户进行细粒度授权。
二 基于 ACL 的权限配置
bin/kafka-acls.sh --authorizer-properties zookeeper.connect=localhost:2181 \
--add --allow-principal User:alice --operation Write --topic test-topic
bin/kafka-acls.sh --authorizer-properties zookeeper.connect=localhost:2181 \
--add --allow-principal User:bob --operation Read --topic test-topic --group test-consumer-group
bin/kafka-acls.sh --authorizer-properties zookeeper.connect=localhost:2181 \
--add --allow-principal User:alice --producer --topic test-topic
bin/kafka-acls.sh --authorizer-properties zookeeper.connect=localhost:2181 \
--add --allow-principal User:bob --consumer --topic test-topic --group test-consumer-group
bin/kafka-acls.sh --authorizer-properties zookeeper.connect=localhost:2181 --list --topic test-topic
bin/kafka-acls.sh --authorizer-properties zookeeper.connect=localhost:2181 --remove --topic test-topic
| 主体 | 资源 | 权限 | 说明 |
|---|---|---|---|
| User:alice | Topic:test-topic | Write | 允许生产消息 |
| User:bob | Topic:test-topic | Read | 允许消费消息 |
| User:bob | Group:test-consumer-group | Read | 允许加入该消费组 |
| User:admin | 任意 | 全部 | 作为 super.users 不受 ACL 限制 |
以上命令与矩阵可直接用于日常授权与运维;注意消费者必须同时具备对 Topic 的 Read 与对 group.id 对应消费组的 Read 权限。
三 客户端连接与认证
security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
username="alice" password="alice-secret";
# 生产
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 \
--topic test-topic --producer.config client-sasl-plain.properties
# 消费
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 \
--topic test-topic --from-beginning --consumer.config client-sasl-plain.properties
四 常见问题与加固建议
sudo ufw allow 9092/tcp