kafka SSL加密 —— 筑梦之路

发布于:2024-05-18 ⋅ 阅读:(74) ⋅ 点赞:(0)

生成SSL证书文件脚本

#!/bin/bash
################################## 设置环境变量 ##############################
BASE_DIR=/mnt/disk/test                           # SSL各种生成文件的基础路径
CERT_OUTPUT_PATH="$BASE_DIR/certificates"         # 证书文件生成路径
PASSWORD=kafka1234567                             # 密码
KEY_STORE="$CERT_OUTPUT_PATH/kafka.keystore"      # Kafka keystore文件路径
TRUST_STORE="$CERT_OUTPUT_PATH/kafka.truststore"  # Kafka truststore文件路径
KEY_PASSWORD=$PASSWORD                            # keystore的key密码
STORE_PASSWORD=$PASSWORD                          # keystore的store密码
TRUST_KEY_PASSWORD=$PASSWORD                      # truststore的key密码
TRUST_STORE_PASSWORD=$PASSWORD                    # truststore的store密码
CLUSTER_NAME=test-cluster                         # 指定别名
CERT_AUTH_FILE="$CERT_OUTPUT_PATH/ca-cert"        # CA证书文件路径
CLUSTER_CERT_FILE="$CERT_OUTPUT_PATH/${CLUSTER_NAME}-cert"      # 集群证书文件路径
DAYS_VALID=365                                    # key有效期
D_NAME="CN=ChengDu, OU=YourDept, O=YourCompany, L=Beijing, ST=Beijing, C=CN" # distinguished name
##############################################################################mkdir -p $CERT_OUTPUT_PATH

echo "1. 创建集群证书到keystore......"
keytool -keystore $KEY_STORE -alias $CLUSTER_NAME -validity $DAYS_VALID -genkey -keyalg RSA \
-storepass $STORE_PASSWORD -keypass $KEY_PASSWORD -dname "$DNAME"

echo "2. 创建CA......"
openssl req -new -x509 -keyout $CERT_OUTPUT_PATH/ca-key -out "$CERT_AUTH_FILE" -days "$DAYS_VALID" \
-passin pass:"$PASSWORD" -passout pass:"$PASSWORD" \
-subj "/C=CN/ST=Beijing/L=Beijing/O=YourCompany/CN=Xi Hu"

echo "3. 导入CA文件到truststore......"
keytool -keystore "$TRUST_STORE" -alias CARoot \
-import -file "$CERT_AUTH_FILE" -storepass "$TRUST_STORE_PASSWORD" -keypass "$TRUST_KEY_PASS" -noprompt

echo "4. 从key store中导出集群证书......"
keytool -keystore "$KEY_STORE" -alias "$CLUSTER_NAME" -certreq -file "$CLUSTER_CERT_FILE" -storepass "$STORE_PASSWORD" -keypass "$KEY_PASSWORD" -noprompt

echo "5. 签发证书......"
openssl x509 -req -CA "$CERT_AUTH_FILE" -CAkey $CERT_OUTPUT_PATH/ca-key -in "$CLUSTER_CERT_FILE" \
-out "${CLUSTER_CERT_FILE}-signed" \
-days "$DAYS_VALID" -CAcreateserial -passin pass:"$PASSWORD"

echo "6. 导入CA文件到keystore......"
keytool -keystore "$KEY_STORE" -alias CARoot -import -file "$CERT_AUTH_FILE" -storepass "$STORE_PASSWORD" \
 -keypass "$KEY_PASSWORD" -noprompt

echo "7. 导入已签发证书到keystore......"
keytool -keystore "$KEY_STORE" -alias "${CLUSTER_NAME}" -import -file "${CLUSTER_CERT_FILE}-signed" \
 -storepass "$STORE_PASSWORD" -keypass "$KEY_PASSWORD" -noprompt

生成的文件说明: 

  • ca-cert:CA文件,不要把该文件拷贝到别的broker机器上!
  • test-cluster-cert-signed:CA已签发的Kafka证书文件,不要把该文件拷贝到别的broker机器上!
  • test-cluster-cert:Kafka认证文件(包含公钥和私钥),不要把该文件拷贝到别的broker机器上!
  • kafka.keystore:Kafka的keystore文件,所有clients端和broker机器上都需要!
  • kafka.truststore:Kafka的truststore文件,所有clients端和broker机器上都需要!

 配置broker端参数

• listeners=PLAINTEXT://:9092,SSL://:9093   # 这里为Kafka broker配置了两个listeners,一个是明文传输;另一个使用SSL加密进行数据传输
• advertised.listeners=PLAINTEXT://公网IP:9092,SSL://公网IP:9093  # 因为是云上环境,如果clients通过公网(或外网)去连接broker,那么advertiesd.listeners就必须配置成所在机器的公网IP
• ssl.keystore.location=/mnt/disk/test/certificates/kafka.keystore # 提供SSL keystore的文件
• ssl.keystore.password=kafka1234567 # 提供keystore密码
• ssl.truststore.location=/mnt/disk/test/certificates/kafka.truststore # 提供SSL truststore的文件
• ssl.truststore.password=kafka1234567 # 提供truststore密码
• ssl.key.password=kafka1234567        # keystore中的私钥密码
• ssl.client.auth=required             # 设置clients也要开启认证

启动服务并测试创建topic

$ bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic test --partitions 1 --replication-factor 1
Created topic "test".

配置clients端参数

1. 配置producer.config

bootstrap.servers=kafka1:9093                            # 指定9093端口,即使用SSL监听器端口
security.protocol=SSL
ssl.truststore.location=/Users/xxx/Downloads/kafka.truststore # 指定truststore文件
ssl.truststore.password=kafka1234567   
ssl.keystore.password=kafka1234567
ssl.keystore.location=/Users/xxx/Downloads/kafka.keystore # 指定keystore文件

2. 测试生产消息

$ bin/kafka-console-producer.sh --broker-list kafka1:9093 --topic test --producer.config producer.config 
>hello, world
>hello, Kafka
>a test message
......

3. 配置consumer.config

security.protocol=SSL
group.id=test-group
ssl.truststore.location=/Users/xxx/Downloads/kafka.truststore # 指定truststore文件 
ssl.truststore.password=kafka1234567 
ssl.keystore.password=kafka1234567
ssl.keystore.location=/Users/xxx/Downloads/kafka.keystore # 指定keystore文件

4. 测试消费消息

$ bin/kafka-console-consumer.sh --bootstrap-server kafka1:9093 --topic test --from-beginning --consumer.config consumer.config 
hello, world
hello, Kafka
a test message


网站公告

今日签到

点亮在社区的每一天
去签到