安全配置
Apache Kafka 支援客戶端和代理之間的安全連線。要利用此功能,請遵循 Apache Kafka 文件 中的指南以及 Kafka 0.9 Confluent 文件中的安全指南。使用 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 配置檔案
可以透過使用系統屬性為 Spring Cloud Stream 應用程式設定 JAAS 和(可選)krb5 檔案的位置。以下示例演示瞭如何使用 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
-
包含登入模組選項的鍵/值對對映。
預設值:空對映。
以下示例演示瞭如何使用 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]";
};
如果所需的 topic 已經存在於代理上或將由管理員建立,則可以關閉自動建立,並且只需傳送客戶端 JAAS 屬性。
請勿在同一應用程式中混合使用 JAAS 配置檔案和 Spring Boot 屬性。如果已存在 -Djava.security.auth.login.config 系統屬性,則 Spring Cloud Stream 將忽略 Spring Boot 屬性。 |
在使用 autoCreateTopics 和 autoAddPartitions 與 Kerberos 時要小心。通常,應用程式可能使用在 Kafka 和 Zookeeper 中沒有管理許可權的主體。因此,依靠 Spring Cloud Stream 建立/修改 topic 可能會失敗。在安全環境中,我們強烈建議透過使用 Kafka 工具以管理方式建立 topic 和管理 ACL。 |
多繫結器配置和 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 值都不同。
有關如何設定和執行此類應用程式的更多詳細資訊,請參閱此示例應用程式。