首页主机资讯Kafka配置中的安全认证怎么实现

Kafka配置中的安全认证怎么实现

时间2025-12-01 10:25:04发布访客分类主机资讯浏览1499
导读:Kafka安全认证实现指南 一、总体方案与选择 常见认证方式包括: SASL/PLAIN:基于用户名/密码,配置简单,但凭据在网络中以明文传输,建议仅与 TLS 一起使用。 SASL/SCRAM:挑战-响应机制,凭据可动态管理,安全性与...

Kafka安全认证实现指南

一、总体方案与选择

  • 常见认证方式包括:
    • SASL/PLAIN:基于用户名/密码,配置简单,但凭据在网络中以明文传输,建议仅与 TLS 一起使用。
    • SASL/SCRAM:挑战-响应机制,凭据可动态管理,安全性与可运维性更好,适合生产。
    • SASL/GSSAPI(Kerberos):企业统一认证,适合大规模与多系统集成。
    • SSL/TLS 客户端证书认证:通过证书进行双向认证,适合高安全场景。
  • 信道加密建议:
    • 内网可用 SASL_PLAINTEXT(前提是已启用 SASL 认证)。
    • 公网或跨域建议 SASL_SSLSSL(启用 ssl.client.auth=required 做双向认证)。

二、SASL/SCRAM 配置步骤(推荐)

  • 服务端配置
    • 启用机制与监听器:
      • listeners=SASL_PLAINTEXT://:9092(或 SASL_SSL://:9093
      • sasl.enabled.mechanisms=SCRAM-SHA-256(或 SCRAM-SHA-512
      • sasl.mechanism.inter.broker.protocol=SCRAM-SHA-256/512
    • JAAS 配置(KafkaServer 上下文,使用 ScramLoginModule):
      • KafkaServer { org.apache.kafka.common.security.scram.ScramLoginModule required; } ;
    • 授权(ACL,示例开启超级用户):
      • authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
      • super.users=User:admin
  • 创建用户(可在集群运行期动态新增)
    • kafka-configs.sh --bootstrap-server broker:port --alter
      –add-config ‘SCRAM-SHA-256=[password=YourPwd]
      –entity-type users --entity-name alice
  • 客户端配置
    • JAAS(kafka_client_jaas.conf):
      • KafkaClient { org.apache.kafka.common.security.scram.ScramLoginModule required username=“alice” password=“YourPwd”; } ;
    • 连接参数(示例为 SCRAM-SHA-512 + SASL_SSL):
      • security.protocol=SASL_SSL
      • sasl.mechanism=SCRAM-SHA-512
      • sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username=“alice” password=“YourPwd”;
      • ssl.truststore.location=/path/client.truststore.jks
      • ssl.truststore.password=YourTrustPwd
      • ssl.endpoint.identification.algorithm=(云上常见需置空以跳过域名校验)

三、SASL/PLAIN 配置步骤(简单但不加密)

  • 服务端配置
    • listeners=SASL_PLAINTEXT://:9092
    • sasl.enabled.mechanisms=PLAIN
    • sasl.mechanism.inter.broker.protocol=PLAIN
    • JAAS(KafkaServer 上下文,PlainLoginModule 中定义用户数据库与 inter-broker 账号):
      • KafkaServer { org.apache.kafka.common.security.plain.PlainLoginModule required username=“admin” password=“admin-secret” user_admin=“admin-secret” user_bob=“bob-secret”; } ;
    • 授权(ACL):
      • authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
      • super.users=User:admin
  • 客户端配置
    • JAAS(KafkaClient 上下文):
      • KafkaClient { org.apache.kafka.common.security.plain.PlainLoginModule required username=“bob” password=“bob-secret”; } ;
    • 连接参数(示例):
      • security.protocol=SASL_PLAINTEXT
      • sasl.mechanism=PLAIN
      • sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username=“bob” password=“bob-secret”;
  • 安全建议:PLAIN 仅与 TLS 一起使用,否则凭据明文传输。

四、SSL 客户端证书认证与双向认证

  • 生成与导入证书(示例)
    • 服务端密钥库:
      • keytool -genkey -keystore server.keystore.jks -alias localhost -validity 3650 -keyalg RSA
    • CA 证书:
      • openssl req -new -x509 -keyout ca-key -out ca-cert -days 3650
    • 客户端信任服务端 CA:
      • keytool -keystore client.truststore.jks -alias CARoot -import -file ca-cert
    • 服务端信任客户端证书(双向认证必需):
      • keytool -keystore server.truststore.jks -alias CARoot -import -file ca-cert
  • 服务端配置(server.properties)
    • listeners=SSL://:9093
    • ssl.keystore.location=/path/server.keystore.jks
    • ssl.keystore.password=YourKeystorePwd
    • ssl.key.password=YourKeyPwd
    • ssl.truststore.location=/path/server.truststore.jks
    • ssl.truststore.password=YourTrustPwd
    • ssl.client.auth=required(开启双向认证)
  • 客户端配置
    • security.protocol=SSL
    • ssl.truststore.location=/path/client.truststore.jks
    • ssl.truststore.password=YourTrustPwd
    • ssl.keystore.location=/path/client.keystore.jks
    • ssl.keystore.password=YourKeystorePwd
    • ssl.key.password=YourKeyPwd
    • ssl.endpoint.identification.algorithm=(云上常见需置空)

五、授权与运维要点

  • 授权(ACL)
    • 开启 ACL:authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
    • 建议设置超级用户:super.users=User:admin
    • 常用 ACL 示例:
      • 授予用户 alice 对主题 test 的生产权限
        • kafka-acls.sh --bootstrap-server broker:port --add
          –allow-principal User:alice --operation Write --topic test
      • 授予用户 bob 对消费组 g1 的消费权限
        • kafka-acls.sh --bootstrap-server broker:port --add
          –allow-principal User:bob --operation Read --group g1 --topic test
  • 运维提示
    • SASL/PLAIN 仅与 TLS 搭配SCRAM 支持运行时新增用户,更利于动态运维。
    • 多监听器时,为每个监听器分别启用 SASL,并正确设置 listener.security.protocol.mapsasl.enabled.mechanismssasl.mechanism.inter.broker.protocol
    • 云上接入常见需将 ssl.endpoint.identification.algorithm 置空以跳过主机名校验;自管集群建议开启校验提升安全。
    • 使用脚本工具时通过 –command-config 指定客户端配置文件(包含 security.protocol、sasl.、ssl. 等)。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Kafka配置中的安全认证怎么实现
本文地址: https://pptw.com/jishu/759857.html
如何优化Kafka的启动速度 Kafka配置中的内存设置技巧

游客 回复需填写必要信息