出发点
公司Kafka一直没做安全验证,由于是诱捕程序故需要面向外网连接,需要增加Kafka连接验证,保证Kafka不被非法连接,故开始研究Kafka安全验证
使用Kafka版本为2.4.0版本,主要参考官方文档
官网
官网对2.4版本安全验证介绍以及使用方式地址:
https://kafka.apache.org/24/documentation.html#security
具体流程
使用 SASL/PLAIN 进行身份验证
SASL/PLAIN 是一种简单的用户名/密码身份验证机制,通常与 TLS 一起使用以进行加密以实现安全身份验证。 Kafka 支持 SASL/PLAIN 的默认实现,可以扩展用于生产用途,如此处所述。
用户名用作 ACL 等配置的身份验证。
配置 Kafka 代理
将一个经过适当修改的 JAAS 文件添加到每个 Kafka 代理的配置目录中,类似于下面的文件,在这个例子中我们称之为 kafka_server_jaas.conf: 此配置定义了两个用户(admin 和 alice)。代理使用 KafkaServer 部分中的属性用户名和密码来启动与其他代理的连接。在此示例中,admin 是代理间通信的用户。属性集user_用户名定义 连接到代理的所有用户的密码,代理验证所有客户端连接,包括 来自使用这些属性的其他经纪人的人。
重要配置kafka_server_jaas.conf
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="admin-secret"
user_admin="admin-secret"
user_alice="alice-secret";
};
将 JAAS 配置文件位置作为 JVM 参数传递给每个 Kafka 代理:
注意
以下配置需要添加到Kafka启动脚本中以添加JVM虚拟机运行参数
需要改为自己的kafka_server_jaas.conf配置文件路径
-Djava.security.auth.login.config=/etc/kafka/kafka_server_jaas.conf
我自己的Kafka的路径为/opt/kafka/bin/kafka-server-start.sh
具体内容如下
主要配置为KAFKA_HEAP_OPTS="-Xmx1G -Xms1G -Djava.security.auth.login.config=/opt/kafka/config/kafka_server_jaas.conf"
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
if [ $# -lt 1 ];
then
echo "USAGE: $0 [-daemon] server.properties [--override property=value]*"
exit 1
fi
base_dir=$(dirname $0)
if [ "x$KAFKA_LOG4J_OPTS" = "x" ]; then
export KAFKA_LOG4J_OPTS="-Dlog4j.configuration=file:$base_dir/../config/log4j.properties"
fi
if <

文章介绍了如何在Kafka2.4.0中启用SASL/PLAIN身份验证以增强安全性,包括修改JAAS配置文件,设置Kafka代理和客户端的参数,以及在Java客户端中进行连接配置。重点涉及KAFKA_SERVER_JAAS_CONF的设置和SASL相关的配置项。
最低0.47元/天 解锁文章
723

被折叠的 条评论
为什么被折叠?



