如何使用Java API添加SCRAM-SHA-512 Kafka配置?

huangapple 未分类评论48阅读模式
英文:

How can i add SCRAM-SHA-512 kafka config using Java API?

问题

我想向我的 Kafka 集群中添加新用户,做法似乎是通过在 Zookeeper 数据库中添加 SCRAM-SHA-512/256 配置,但我只知道如何使用命令行工具来完成,大致如下:

bin/kafka-configs.sh --zookeeper zookeeper_server_address --alter --add-config "SCRAM-SHA-512=
		
输入密码查看隐藏内容

"
--entity-type users --entity-name userName

是否有使用 Kafka 的 Java API 来实现这一目标的替代方法呢?

英文:

I want to add new users to my kafka cluster, and the way to do it, seems to be by adding a SCRAM-SHA-512/256 config in the zookeeper database, but i only know how to do it using the command line utility, which will be something like this:

bin/kafka-configs.sh --zookeeper zookeeper_server_address --alter --add-config "SCRAM-SHA-512=
		
输入密码查看隐藏内容

" --entity-type users --entity-name userName

Is there an alternative to approach this using the Kafka API for Java?

答案1

得分: 0

我看到一个帖子,上面说Kafka API没有这个功能,所以我创建了一个自定义的 .sh 脚本用于创建用户(create-user.sh):

#!/bin/bash

SERVER=$1
USER=$2
PASSWORD=$3

if [ "x$SERVER" = "x" ] || [ "x$USER" = "x" ] || [ "x$PASSWORD" = "x" ]; then
    echo "usage: $0 <server:port> <user> <password>"
    exit
fi
bin/kafka-configs.sh --zookeeper $SERVER --alter --add-config "SCRAM-SHA-512=
		
输入密码查看隐藏内容

"
--entity-type users --entity-name $USER

然后我从Java程序中调用它:

ProcessBuilder pb = new ProcessBuilder("./create-user.sh", "zookeeper-server", "sampleUser", "password");
pb.environment().put("PATH", "/home/myHome/Programs/java/jdk-13.0.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");

Process p = pb.redirectErrorStream(true)
            .directory(new File("/home/myHome/Desktop/Kafka/kafka_2.12-2.4.1"))
            .start();

注意,我需要在PATH中指定一些目录,并将其作为环境变量传递给 ProcessBuilder,因为如果不这样做,它会抛出错误,指出有些命令找不到。

英文:

I saw a thread where it says that the Kafka api doasn't comes with this functionallity, so i made a custom .sh script for creating users (create-user.sh):

#!/bin/bash

SERVER=$1
USER=$2
PASSWORD=$3

if [ &quot;x$SERVER&quot; = &quot;x&quot; ] || [ &quot;x$USER&quot; = &quot;x&quot; ] || [ &quot;x$PASSWORD&quot; = &quot;x&quot; ]; then
	echo &quot;usage: $0 &lt;server:port&gt; &lt;user&gt; &lt;password&gt;&quot;
	exit
fi
bin/kafka-configs.sh --zookeeper $SERVER --alter --add-config &quot;SCRAM-SHA-512=
		
输入密码查看隐藏内容

&quot; --entity-type users --entity-name $USER

And i call it from java program:

ProcessBuilder pb = new ProcessBuilder(&quot;./create-user.sh&quot;, &quot;zookeeper-server&quot;, &quot;sampleUser&quot;, &quot;password&quot;);
		pb.environment().put(&quot;PATH&quot;, &quot;/home/myHome/Programs/java/jdk-13.0.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin&quot;);
		
Process p = pb.redirectErrorStream(true)
				.directory(new File(&quot;/home/myHome/Desktop/Kafka/kafka_2.12-2.4.1&quot;))
				.start();

Notice, i needed to specify some directories in PATH and pass it as environment var to the ProcessBuilder because if not, it will throw erros saying that ther are commands that can not find

huangapple
  • 本文由 发表于 2020年5月5日 05:20:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/61601817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定