安全配置

Apache Kafka 支援客戶端與 Broker 之間的安全連線。要利用此特性,請遵循 Apache Kafka 文件 以及 Confluent 文件中 Kafka 0.9 的 安全指南。使用 spring.cloud.stream.kafka.binder.configuration 選項為 Binder 建立的所有客戶端設定安全屬性。

例如,要將 security.protocol 設定為 SASL_SSL,請設定以下屬性

spring.cloud.stream.kafka.binder.configuration.security.protocol=SASL_SSL

所有其他安全屬性都可以透過類似的方式設定。

使用 Kerberos 時,請遵循 參考文件 中的說明來建立和引用 JAAS 配置。

Spring Cloud Stream 支援透過 JAAS 配置檔案和 Spring Boot 屬性將 JAAS 配置資訊傳遞給應用。

使用 JAAS 配置檔案

JAAS 和 (可選的) krb5 檔案位置可以透過系統屬性為 Spring Cloud Stream 應用設定。以下示例展示瞭如何使用 JAAS 配置檔案啟動一個啟用 SASL 和 Kerberos 的 Spring Cloud Stream 應用

 java -Djava.security.auth.login.config=/path.to/kafka_client_jaas.conf -jar log.jar \
   --spring.cloud.stream.kafka.binder.brokers=secure.server:9092 \
   --spring.cloud.stream.bindings.input.destination=stream.ticktock \
   --spring.cloud.stream.kafka.binder.configuration.security.protocol=SASL_PLAINTEXT

使用 Spring Boot 屬性

作為使用 JAAS 配置檔案的替代方案,Spring Cloud Stream 提供了一種機制,可以透過 Spring Boot 屬性為 Spring Cloud Stream 應用設定 JAAS 配置。

可以使用以下屬性來配置 Kafka 客戶端的登入上下文

spring.cloud.stream.kafka.binder.jaas.loginModule

登入模組名稱。通常情況下無需設定。

預設值:com.sun.security.auth.module.Krb5LoginModule

spring.cloud.stream.kafka.binder.jaas.controlFlag

登入模組的控制標誌。

預設值:required

spring.cloud.stream.kafka.binder.jaas.options

包含登入模組選項的鍵/值對 Map。

預設值:空 Map。

以下示例展示瞭如何使用 Spring Boot 配置屬性啟動一個啟用 SASL 和 Kerberos 的 Spring Cloud Stream 應用

 java --spring.cloud.stream.kafka.binder.brokers=secure.server:9092 \
   --spring.cloud.stream.bindings.input.destination=stream.ticktock \
   --spring.cloud.stream.kafka.binder.autoCreateTopics=false \
   --spring.cloud.stream.kafka.binder.configuration.security.protocol=SASL_PLAINTEXT \
   --spring.cloud.stream.kafka.binder.jaas.options.useKeyTab=true \
   --spring.cloud.stream.kafka.binder.jaas.options.storeKey=true \
   --spring.cloud.stream.kafka.binder.jaas.options.keyTab=/etc/security/keytabs/kafka_client.keytab \
   --spring.cloud.stream.kafka.binder.jaas.options.principal=kafka-client-1@EXAMPLE.COM

前面的示例相當於以下 JAAS 檔案

KafkaClient {
    com.sun.security.auth.module.Krb5LoginModule required
    useKeyTab=true
    storeKey=true
    keyTab="/etc/security/keytabs/kafka_client.keytab"
    principal="[email protected]";
};

如果所需的主題已存在於 Broker 上或將由管理員建立,則可以關閉自動建立,此時只需傳送客戶端 JAAS 屬性。

請勿在同一個應用中混合使用 JAAS 配置檔案和 Spring Boot 屬性。如果已存在 -Djava.security.auth.login.config 系統屬性,Spring Cloud Stream 會忽略 Spring Boot 屬性。
在使用 Kerberos 時,請謹慎使用 autoCreateTopicsautoAddPartitions。通常,應用可能使用在 Kafka 和 Zookeeper 中沒有管理員許可權的主體(principals)。因此,依賴 Spring Cloud Stream 建立/修改主題可能會失敗。在安全環境中,強烈建議使用 Kafka 工具進行主題建立和 ACL 管理。

多 Binder 配置和 JAAS

連線到需要各自獨立 JAAS 配置的多個叢集時,請使用屬性 sasl.jaas.config 設定 JAAS 配置。當應用中存在此屬性時,它將優先於上述其他策略。有關更多詳細資訊,請參閱此 KIP-85

例如,如果您的應用中有兩個需要獨立 JAAS 配置的叢集,則可以使用以下模板

spring.cloud.stream:
    binders:
        kafka1:
          type: kafka
          environment:
             spring:
               cloud:
                 stream:
                  kafka:
                    binder:
                      brokers: localhost:9092
                      configuration.sasl.jaas.config: "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"admin\" password=\"admin-secret\";"
        kafka2:
          type: kafka
          environment:
            spring:
              cloud:
                stream:
                  kafka:
                    binder:
                      brokers: localhost:9093
                      configuration.sasl.jaas.config: "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"user1\" password=\"user1-secret\";"
    kafka.binder:
        configuration:
          security.protocol: SASL_PLAINTEXT
          sasl.mechanism: PLAIN

請注意,在上述配置中,兩個 Kafka 叢集及其各自的 sasl.jaas.config 值都不同。

有關如何設定和執行此類應用的更多詳細資訊,請參閱此 示例應用