You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by re...@apache.org on 2022/07/06 02:33:13 UTC
[flink] branch release-1.15 updated: [FLINK-27865][docs] Add example for configuring SASL and SSL in Kafka DataStream and SQL connector (#19904)
This is an automated email from the ASF dual-hosted git repository.
renqs pushed a commit to branch release-1.15
in repository https://gitbox.apache.org/repos/asf/flink.git
The following commit(s) were added to refs/heads/release-1.15 by this push:
new 5cc879676e8 [FLINK-27865][docs] Add example for configuring SASL and SSL in Kafka DataStream and SQL connector (#19904)
5cc879676e8 is described below
commit 5cc879676e8092defc750b98b4592fec10890ccf
Author: Qingsheng Ren <re...@gmail.com>
AuthorDate: Thu Jun 23 10:58:50 2022 +0800
[FLINK-27865][docs] Add example for configuring SASL and SSL in Kafka DataStream and SQL connector (#19904)
---
.../content.zh/docs/connectors/datastream/kafka.md | 42 ++++++++++++++---
docs/content.zh/docs/connectors/table/kafka.md | 49 ++++++++++++++++++++
docs/content/docs/connectors/datastream/kafka.md | 44 ++++++++++++++----
docs/content/docs/connectors/table/kafka.md | 52 ++++++++++++++++++++++
4 files changed, 172 insertions(+), 15 deletions(-)
diff --git a/docs/content.zh/docs/connectors/datastream/kafka.md b/docs/content.zh/docs/connectors/datastream/kafka.md
index 39010fbf791..2819e48fe4f 100644
--- a/docs/content.zh/docs/connectors/datastream/kafka.md
+++ b/docs/content.zh/docs/connectors/datastream/kafka.md
@@ -149,13 +149,6 @@ Kafka consumer 的配置可以参考 [Apache Kafka 文档](http://kafka.apache.o
- ```auto.offset.reset.strategy``` 被 OffsetsInitializer#getAutoOffsetResetStrategy() 覆盖
- ```partition.discovery.interval.ms``` 会在批模式下被覆盖为 -1
-下面的代码片段展示了如何配置 Kafka consumer 以使用“PLAIN”作为 SASL 机制并提供 JAAS 配置:
-```java
-KafkaSource.builder()
- .setProperty("sasl.mechanism", "PLAIN")
- .setProperty("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"username\" password=\"password\";");
-```
-
### 动态分区检查
为了在不重启 Flink 作业的情况下处理 Topic 扩容或新建 Topic 等场景,可以将 Kafka Source 配置为在提供的 Topic / Partition
订阅模式下定期检查新分区。要启用动态分区检查,请将 ```partition.discovery.interval.ms``` 设置为非负值:
@@ -269,6 +262,41 @@ Kafka consumer 的所有指标都注册在指标组 ```KafkaSourceReader.KafkaCo
关于 Kafka consumer 的指标,您可以参考 [Apache Kafka 文档](http://kafka.apache.org/documentation/#consumer_monitoring)
了解更多详细信息。
+### 安全
+要启用加密和认证相关的安全配置,只需将安全配置作为其他属性配置在 Kafka source 上即可。下面的代码片段展示了如何配置 Kafka source 以使用
+PLAIN 作为 SASL 机制并提供 JAAS 配置:
+
+```java
+KafkaSource.builder()
+ .setProperty("security.protocol", "SASL_PLAINTEXT")
+ .setProperty("sasl.mechanism", "PLAIN")
+ .setProperty("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"username\" password=\"password\";");
+```
+
+另一个更复杂的例子,使用 SASL_SSL 作为安全协议并使用 SCRAM-SHA-256 作为 SASL 机制:
+```java
+KafkaSource.builder()
+ .setProperty("security.protocol", "SASL_SSL")
+ // SSL 配置
+ // 配置服务端提供的 truststore (CA 证书) 的路径
+ // Configure the path of truststore (CA) provided by the server
+ .setProperty("ssl.truststore.location", "/path/to/kafka.client.truststore.jks")
+ .setProperty("ssl.truststore.password", "test1234")
+ // 如果要求客户端认证,则需要配置 keystore (私钥) 的路径
+ // Configure the path of keystore (private key) if client authentication is required
+ .setProperty("ssl.keystore.location", "/path/to/kafka.client.keystore.jks")
+ .setProperty("ssl.keystore.password", "test1234")
+ // SASL 配置
+ // 将 SASL 机制配置为 as SCRAM-SHA-256
+ .setProperty("sasl.mechanism", "SCRAM-SHA-256")
+ // 配置 JAAS
+ .setProperty("sasl.jaas.config", "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"username\" password=\"password\";");
+```
+如果在作业 JAR 中 Kafka 客户端依赖的类路径被重置了(relocate class),登录模块(login module)的类路径可能会不同,因此请根据登录模块在
+JAR 中实际的类路径来改写以上配置。
+
+关于安全配置的详细描述,请参阅 <a href="https://kafka.apache.org/documentation/#security">Apache Kafka 文档中的"安全"一节</a>。
+
### 实现细节
{{< hint info >}}
如果你对 Kafka source 在新的 Source API 中的设计感兴趣,可阅读该部分作为参考。关于新 Source API 的细节,[Source
diff --git a/docs/content.zh/docs/connectors/table/kafka.md b/docs/content.zh/docs/connectors/table/kafka.md
index d02d1cfba2b..4455d7ac642 100644
--- a/docs/content.zh/docs/connectors/table/kafka.md
+++ b/docs/content.zh/docs/connectors/table/kafka.md
@@ -530,6 +530,55 @@ Source 输出的 watermark 由读取的分区中最小的 watermark 决定。
请参阅 [Kafka watermark 策略]({{< ref "docs/dev/datastream/event-time/generating_watermarks" >}}#watermark-策略和-kafka-连接器) 以获取更多细节。
+### 安全
+要启用加密和认证相关的安全配置,只需将安全配置加上 "properties." 前缀配置在 Kafka 表上即可。下面的代码片段展示了如何配置 Kafka 表以使用
+PLAIN 作为 SASL 机制并提供 JAAS 配置:
+```sql
+CREATE TABLE KafkaTable (
+ `user_id` BIGINT,
+ `item_id` BIGINT,
+ `behavior` STRING,
+ `ts` TIMESTAMP(3) METADATA FROM 'timestamp'
+) WITH (
+ 'connector' = 'kafka',
+ ...
+ 'properties.security.protocol' = 'SASL_PLAINTEXT',
+ 'properties.sasl.mechanism' = 'PLAIN',
+ 'properties.sasl.jaas.config' = 'org.apache.kafka.common.security.plain.PlainLoginModule required username=\"username\" password=\"password\";'
+)
+```
+另一个更复杂的例子,使用 SASL_SSL 作为安全协议并使用 SCRAM-SHA-256 作为 SASL 机制:
+```sql
+CREATE TABLE KafkaTable (
+ `user_id` BIGINT,
+ `item_id` BIGINT,
+ `behavior` STRING,
+ `ts` TIMESTAMP(3) METADATA FROM 'timestamp'
+) WITH (
+ 'connector' = 'kafka',
+ ...
+ 'properties.security.protocol' = 'SASL_SSL',
+ /* SSL 配置 */
+ /* 配置服务端提供的 truststore (CA 证书) 的路径 */
+ 'properties.ssl.truststore.location' = '/path/to/kafka.client.truststore.jks',
+ 'properties.ssl.truststore.password' = 'test1234',
+ /* 如果要求客户端认证,则需要配置 keystore (私钥) 的路径 */
+ 'properties.ssl.keystore.location' = '/path/to/kafka.client.keystore.jks',
+ 'properties.ssl.keystore.password' = 'test1234',
+ /* SASL 配置 */
+ /* 将 SASL 机制配置为 as SCRAM-SHA-256 */
+ 'properties.sasl.mechanism' = 'SCRAM-SHA-256',
+ /* 配置 JAAS */
+ 'properties.sasl.jaas.config' = 'org.apache.kafka.common.security.scram.ScramLoginModule required username=\"username\" password=\"password\";'
+)
+```
+
+如果在作业 JAR 中 Kafka 客户端依赖的类路径被重置了(relocate class),登录模块(login module)的类路径可能会不同,因此请根据登录模块在
+JAR 中实际的类路径来改写以上配置。例如在 SQL client JAR 中,Kafka client 依赖被重置在了 `org.apache.flink.kafka.shaded.org.apache.kafka` 路径下,
+因此 plain 登录模块的类路径应写为 `org.apache.flink.kafka.shaded.org.apache.kafka.common.security.plain.PlainLoginModule`。
+
+关于安全配置的详细描述,请参阅 <a href="https://kafka.apache.org/documentation/#security">Apache Kafka 文档中的"安全"一节</a>。
+
数据类型映射
----------------
diff --git a/docs/content/docs/connectors/datastream/kafka.md b/docs/content/docs/connectors/datastream/kafka.md
index 2b62ebd02f2..fc908de035f 100644
--- a/docs/content/docs/connectors/datastream/kafka.md
+++ b/docs/content/docs/connectors/datastream/kafka.md
@@ -165,14 +165,6 @@ it is configured:
- ```partition.discovery.interval.ms``` is overridden to -1 when
```setBounded(OffsetsInitializer)``` has been invoked
-The code snippet below shows configuring KafkaConsumer to use "PLAIN" as SASL mechanism and provide
-JAAS configuration:
-```java
-KafkaSource.builder()
- .setProperty("sasl.mechanism", "PLAIN")
- .setProperty("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"username\" password=\"password\";");
-```
-
### Dynamic Partition Discovery
In order to handle scenarios like topic scaling-out or topic creation without restarting the Flink
job, Kafka source can be configured to periodically discover new partitions under provided
@@ -314,6 +306,42 @@ available metrics are correctly forwarded to the metrics system. You must ensure
```client.id.prefix``` for every ```KafkaSource``` is configured and that no other
```KafkaConsumer``` in your job uses the same ```client.id```.
+### Security
+In order to enable security configurations including encryption and authentication, you just need to setup security
+configurations as additional properties to the Kafka source. The code snippet below shows configuring Kafka source to
+use PLAIN as SASL mechanism and provide JAAS configuration:
+
+```java
+KafkaSource.builder()
+ .setProperty("security.protocol", "SASL_PLAINTEXT")
+ .setProperty("sasl.mechanism", "PLAIN")
+ .setProperty("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"username\" password=\"password\";");
+```
+
+For a more complex example, use SASL_SSL as the security protocol and use SCRAM-SHA-256 as SASL mechanism:
+```java
+KafkaSource.builder()
+ .setProperty("security.protocol", "SASL_SSL")
+ // SSL configurations
+ // Configure the path of truststore (CA) provided by the server
+ .setProperty("ssl.truststore.location", "/path/to/kafka.client.truststore.jks")
+ .setProperty("ssl.truststore.password", "test1234")
+ // Configure the path of keystore (private key) if client authentication is required
+ .setProperty("ssl.keystore.location", "/path/to/kafka.client.keystore.jks")
+ .setProperty("ssl.keystore.password", "test1234")
+ // SASL configurations
+ // Set SASL mechanism as SCRAM-SHA-256
+ .setProperty("sasl.mechanism", "SCRAM-SHA-256")
+ // Set JAAS configurations
+ .setProperty("sasl.jaas.config", "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"username\" password=\"password\";");
+```
+
+Please note that the class path of the login module in `sasl.jaas.config` might be different if you relocate Kafka
+client dependencies in the job JAR, so you may need to rewrite it with the actual class path of the module in the JAR.
+
+For detailed explanations of security configurations, please refer to
+<a href="https://kafka.apache.org/documentation/#security">the "Security" section in Apache Kafka documentation</a>.
+
### Behind the Scene
{{< hint info >}}
If you are interested in how Kafka source works under the design of new data source API, you may
diff --git a/docs/content/docs/connectors/table/kafka.md b/docs/content/docs/connectors/table/kafka.md
index 85fb96d8603..eb4bc4d3d06 100644
--- a/docs/content/docs/connectors/table/kafka.md
+++ b/docs/content/docs/connectors/table/kafka.md
@@ -584,6 +584,58 @@ option in the table configuration.
Please refer to [Kafka watermark strategies]({{< ref "docs/dev/datastream/event-time/generating_watermarks" >}}#watermark-strategies-and-the-kafka-connector)
for more details.
+### Security
+In order to enable security configurations including encryption and authentication, you just need to setup security
+configurations with "properties." prefix in table options. The code snippet below shows configuring Kafka table to
+use PLAIN as SASL mechanism and provide JAAS configuration:
+```sql
+CREATE TABLE KafkaTable (
+ `user_id` BIGINT,
+ `item_id` BIGINT,
+ `behavior` STRING,
+ `ts` TIMESTAMP(3) METADATA FROM 'timestamp'
+) WITH (
+ 'connector' = 'kafka',
+ ...
+ 'properties.security.protocol' = 'SASL_PLAINTEXT',
+ 'properties.sasl.mechanism' = 'PLAIN',
+ 'properties.sasl.jaas.config' = 'org.apache.kafka.common.security.plain.PlainLoginModule required username=\"username\" password=\"password\";'
+)
+```
+For a more complex example, use SASL_SSL as the security protocol and use SCRAM-SHA-256 as SASL mechanism:
+```sql
+CREATE TABLE KafkaTable (
+ `user_id` BIGINT,
+ `item_id` BIGINT,
+ `behavior` STRING,
+ `ts` TIMESTAMP(3) METADATA FROM 'timestamp'
+) WITH (
+ 'connector' = 'kafka',
+ ...
+ 'properties.security.protocol' = 'SASL_SSL',
+ /* SSL configurations */
+ /* Configure the path of truststore (CA) provided by the server */
+ 'properties.ssl.truststore.location' = '/path/to/kafka.client.truststore.jks',
+ 'properties.ssl.truststore.password' = 'test1234',
+ /* Configure the path of keystore (private key) if client authentication is required */
+ 'properties.ssl.keystore.location' = '/path/to/kafka.client.keystore.jks',
+ 'properties.ssl.keystore.password' = 'test1234',
+ /* SASL configurations */
+ /* Set SASL mechanism as SCRAM-SHA-256 */
+ 'properties.sasl.mechanism' = 'SCRAM-SHA-256',
+ /* Set JAAS configurations */
+ 'properties.sasl.jaas.config' = 'org.apache.kafka.common.security.scram.ScramLoginModule required username=\"username\" password=\"password\";'
+)
+```
+
+Please note that the class path of the login module in `sasl.jaas.config` might be different if you relocate Kafka
+client dependencies, so you may need to rewrite it with the actual class path of the module in the JAR.
+For example if you are using SQL client JAR, which has relocate Kafka client dependencies to `org.apache.flink.kafka.shaded.org.apache.kafka`,
+the path of plain login module should be `org.apache.flink.kafka.shaded.org.apache.kafka.common.security.plain.PlainLoginModule` instead.
+
+For detailed explanations of security configurations, please refer to
+<a href="https://kafka.apache.org/documentation/#security">the "Security" section in Apache Kafka documentation</a>.
+
Data Type Mapping
----------------