(一)Kafka 安全之使用 SASL 进行身份验证 —— JAAS 配置、SASL 配置

目录

一. 前言

二. JAAS 配置(JAAS configuration)

2.1. Kafka Broker 的 JAAS 配置

2.2. Kafka 客户端的 JAAS 配置

2.2.1. 使用客户端配置属性的 JAAS 配置

2.2.2. 使用静态配置文件的 JAAS 配置

三. SASL 配置(SASL configuration)

3.1. SASL 机制(SASL mechanisms)

3.2. Kafka Broker 的 SASL 配置

3.3. Kafka 客户端的 SASL 配置


一. 前言

    SASL 是用来认证 C/S 模式也就是服务器与客户端的一种认证机制,全称 Simple Authentication and Security Layer。这就是一种凭据认证方式。通俗的话来讲就是让服务器知道连接进来的客户端的身份是谁。

    比如凭借阅证到图书馆借书,而每个借阅证都有独立的 ID,通过 ID 定位谁是谁,而不是特别关心谁拿到了借阅证,只需要正确的借阅证即可。所以 SASL 就是服务器存储了客户端的身份证书和如何校验密码是否正确,而且仅在于身份认证过程,认证完毕后即可进行相关的服务操作。

二. JAAS 配置(JAAS configuration)

原文引用:Kafka uses the Java Authentication and Authorization Service (JAAS) for SASL configuration.

Kafka 使用 Java 身份验证和授权服务(JAAS)进行 SASL 配置。

2.1. Kafka Broker 的 JAAS 配置

原文引用:KafkaServer is the section name in the JAAS file used by each KafkaServer/Broker. This section provides SASL configuration options for the broker including any SASL client connections made by the broker for inter-broker communication. If multiple listeners are configured to use SASL, the section name may be prefixed with the listener name in lower-case followed by a period, e.g. sasl_ssl.KafkaServer.

    KafkaServer 是每个 KafkaServer/Broker 使用的 JAAS 文件中的段落名。本节提供 Broker 的SASL 配置选项,包括 Broker 为 Broker 间通信建立的任何 SASL 客户端连接。如果将多个监听器配置为使用 SASL,则段落名称可以以小写的监听器名称为前缀,后跟句点,例如 sasl_ssl.KafkaServer

原文引用:Client section is used to authenticate a SASL connection with zookeeper. It also allows the brokers to set SASL ACL on zookeeper nodes which locks these nodes down so that only the brokers can modify it. It is necessary to have the same principal name across all brokers. If you want to use a section name other than Client, set the system property zookeeper.sasl.clientconfig to the appropriate name (e.g., -Dzookeeper.sasl.clientconfig=ZkClient).

    客户端部分用于验证与 ZooKeeper 的 SASL 连接。它还允许 Broker 在 ZooKeeper 节点上设置SASL ACL,从而锁定这些节点,以便只有 Broker 可以修改它。有必要在所有 Broker 中具有相同的主体名称。如果要使用 Client 以外的段落名称,请将系统属性 zookeeper.sasl.clientconfig 设置为适当的名称(例如,-Dzookeeper.sasl.clientconfig=ZkClient)。

原文引用:ZooKeeper uses "zookeeper" as the service name by default. If you want to change this, set the system property zookeeper.sasl.client.username to the appropriate name (e.g., -Dzookeeper.sasl.client.username=zk).

    ZooKeeper 默认使用“zookeeper”作为服务名称。如果要更改此设置,请将系统属性zookeeper.sasl.client.username 设置为适当的名称(例如,-Dzookeeper.sasl.client.username=zk)。

原文引用:Brokers may also configure JAAS using the broker configuration property sasl.jaas.config. The property name must be prefixed with the listener prefix including the SASL mechanism, i.e. listener.name.{listenerName}.{saslMechanism}.sasl.jaas.config. Only one login module may be specified in the config value. If multiple mechanisms are configured on a listener, configs must be provided for each mechanism using the listener and mechanism prefix. For example,

    Broker 还可以使用 Broker 配置属性 sasl.jaas.config 来配置 JAAS。属性名称必须以监听器前缀为前缀,包括 SASL 机制,即 listener.name.{listenerName}.{saslMechanism}.sasl.jaas.config。在配置值中只能指定一个登录模块。如果在监听器上配置了多个机制,则必须使用监听器和机制前缀为每个机制提供配置。例如

listener.name.sasl_ssl.scram-sha-256.sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required \
    username="admin" \
    password="admin-secret";
listener.name.sasl_ssl.plain.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
    username="admin" \
    password="admin-secret" \
    user_admin="admin-secret" \
    user_alice="alice-secret";

原文引用:If JAAS configuration is defined at different levels, the order of precedence used is:

  • Broker configuration property listener.name.{listenerName}.{saslMechanism}.sasl.jaas.config
  • {listenerName}.KafkaServer section of static JAAS configuration
  • KafkaServer section of static JAAS configuration

如果在不同级别定义了 JAAS 配置,则使用的优先顺序为:

  • Broker 配置属性 listener.name.{listenerName}.{saslMechanism}.sasl.jaas.config
  • JAAS 静态配置部分 {listenerName}.KafkaServer
  • JAAS 静态配置部分 KafkaServer

原文引用:Note that ZooKeeper JAAS config may only be configured using static JAAS configuration.

See GSSAPI (Kerberos)PLAINSCRAM or OAUTHBEARER for example broker configurations.

请注意,ZooKeeper JAAS 配置只能使用静态 JAAS 配置进行配置。

有关 Broker 配置的示例,请参阅 GSSAPI(Kerberos)、PLAIN、SCRAM 或 OAUTHBEARER。

2.2. Kafka 客户端的 JAAS 配置

原文引用:Clients may configure JAAS using the client configuration property sasl.jaas.config or using the static JAAS config file similar to brokers.

    客户端可以使用客户端配置属性 sasl.jas.config 或类似于 Broker 的静态 JAAS 配置文件来配置JAAS。

2.2.1. 使用客户端配置属性的 JAAS 配置

原文引用:Clients may specify JAAS configuration as a producer or consumer property without creating a physical configuration file. This mode also enables different producers and consumers within the same JVM to use different credentials by specifying different properties for each client. If both static JAAS configuration system property java.security.auth.login.config and client property sasl.jaas.config are specified, the client property will be used.

See GSSAPI (Kerberos)PLAINSCRAM or OAUTHBEARER for example configurations.

    客户端可以将 JAAS 配置指定为生产者或消费者属性,而无需创建物理配置文件。这种模式还允许同一 JVM 中的不同生产者和消费者通过为每个客户端指定不同的属性来使用不同的凭据。如果同时指定了静态 JAAS 配置系统属性 java.security.auth.login.config 和客户端属性sasl.jas.config,则将使用客户端属性。

有关配置示例,请参阅 GSSAPI(Kerberos)、PLAIN、SCRAM 或 OAUTHBEARER。

2.2.2. 使用静态配置文件的 JAAS 配置

原文引用:To configure SASL authentication on the clients using static JAAS config file:

  1. Add a JAAS config file with a client login section named KafkaClient. Configure a login module in KafkaClient for the selected mechanism as described in the examples for setting up GSSAPI (Kerberos)PLAINSCRAM or OAUTHBEARER. For example, GSSAPI credentials may be configured as:

要使用静态 JAAS 配置文件在客户端上配置 SASL 身份验证,请执行以下操作:

  1. 添加一个 JAAS 配置文件,其中包含一个名为 KafkaClient 的客户端登录部分。在KafkaClient 中为所选机制配置登录模块,如设置 GSSAPI(Kerberos)、PLAIN、SCRAM或 OAUTHBEARER 的示例中所述。例如,GSSAPI 凭据可以配置为:
    KafkaClient {
        com.sun.security.auth.module.Krb5LoginModule required
        useKeyTab=true
        storeKey=true
        keyTab="/etc/security/keytabs/kafka_client.keytab"
        principal="kafka-client-1@EXAMPLE.COM";
    };

 原文引用:

  1. Pass the JAAS config file location as JVM parameter to each client JVM. For example:
  1. 将 JAAS 配置文件位置作为 JVM 参数传递给每个客户端 JVM。例如
    -Djava.security.auth.login.config=/etc/kafka/kafka_client_jaas.conf

三. SASL 配置(SASL configuration)

原文引用:SASL may be used with PLAINTEXT or SSL as the transport layer using the security protocol SASL_PLAINTEXT or SASL_SSL respectively. If SASL_SSL is used, then SSL must also be configured.

    SASL 可以与 PLAINTEXT 或 SSL 一起使用,作为分别使用安全协议 SASL_PLAINTEXT 或 SASL_SSL 的传输层。如果使用 SASL_SSL,则还必须配置 SSL。

3.1. SASL 机制(SASL mechanisms)

原文引用:Kafka supports the following SASL mechanisms:

Kafka 支持以下 SASL 机制:

  • GSSAPI(Kerberos)
  • PLAIN
  • SCRAM-SHA-256
  • SCRAM-SHA-512
  • OAUTHBEARER

3.2. Kafka Broker 的 SASL 配置

原文引用:1. Configure a SASL port in server.properties, by adding at least one of SASL_PLAINTEXT or SASL_SSL to the listeners parameter, which contains one or more comma-separated values:

1. 在 server.properties 中配置 SASL 端口,方法是将 SASL_PLAINTEXT 或 SASL_SSL 中的至少一个添加到 listener 参数中,该参数包含一个或多个逗号分隔的值:

listeners=SASL_PLAINTEXT://host.name:port

原文引用:If you are only configuring a SASL port (or if you want the Kafka brokers to authenticate each other using SASL) then make sure you set the same SASL protocol for inter-broker communication:

    如果您只配置 SASL 端口(或者如果您希望 Kafka Broker 使用 SASL 相互验证),请确保为 Broker 间通信设置相同的 SASL 协议:

security.inter.broker.protocol=SASL_PLAINTEXT (or SASL_SSL)

原文引用:2. Select one or more supported mechanisms to enable in the broker and follow the steps to configure SASL for the mechanism. To enable multiple mechanisms in the broker, follow the steps here.

2. 选择一个或多个要在 Broker 中启用的支持机制,然后按照步骤为该机制配置 SASL。要在 Broker 中启用多个机制,请按照此处的步骤操作。

3.3. Kafka 客户端的 SASL 配置

原文引用:SASL authentication is only supported for the new Java Kafka producer and consumer, the older API is not supported.

To configure SASL authentication on the clients, select a SASL mechanism that is enabled in the broker for client authentication and follow the steps to configure SASL for the selected mechanism.

    SASL 身份验证仅支持新的 Java Kafka 生产者和消费者,不支持旧的 API。

    要在客户端上配置 SASL 身份验证,请选择在 Broker 中启用的用于客户端身份验证的 SASL 机制,然后按照步骤为所选机制配置 SASL。

原文引用:Note: When establishing connections to brokers via SASL, clients may perform a reverse DNS lookup of the broker address. Due to how the JRE implements reverse DNS lookups, clients may observe slow SASL handshakes if fully qualified domain names are not used, for both the client's bootstrap.servers and a broker's advertised.listeners.

注意:当通过 SASL 与 Broker 建立连接时,客户端可能会对 Broker 地址执行反向 DNS 查找。由于 JRE 是实现反向 DNS 查找的,如果客户端的 bootstrap.servers 和 Broker 的advertised.listeners 都没有使用完全限定的域名,则客户端可能会观察到 SASL 握手缓慢。

  • 21
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流华追梦

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值