Skip to content

Commit 2fb650a

Browse files
authored
feat: add zk auth (#3581)
1 parent 714369e commit 2fb650a

File tree

34 files changed

+206
-36
lines changed

34 files changed

+206
-36
lines changed

docs/en/deploy/conf.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# If you are deploying the standalone version, you do not need to configure zk_cluster and zk_root_path, just comment these two configurations. Deploying the cluster version needs to configure these two items, and the two configurations of all nodes in a cluster must be consistent
1010
#--zk_cluster=127.0.0.1:7181
1111
#--zk_root_path=/openmldb_cluster
12+
# set the username and password of zookeeper if authentication is enabled
13+
#--zk_cert=user:passwd
1214
# The address of the tablet needs to be specified in the standalone version, and this configuration can be ignored in the cluster version
1315
--tablet=127.0.0.1:9921
1416
# Configure log directory
@@ -76,6 +78,8 @@
7678
# If you start the cluster version, you need to specify the address of zk and the node path of the cluster in zk
7779
#--zk_cluster=127.0.0.1:7181
7880
#--zk_root_path=/openmldb_cluster
81+
# set the username and password of zookeeper if authentication is enabled
82+
#--zk_cert=user:passwd
7983
8084
# Configure the thread pool size, it is recommended to be consistent with the number of CPU cores
8185
--thread_pool_size=24
@@ -218,6 +222,8 @@
218222
# If the deployed openmldb is a cluster version, you need to specify the zk address and the cluster zk node directory
219223
#--zk_cluster=127.0.0.1:7181
220224
#--zk_root_path=/openmldb_cluster
225+
# set the username and password of zookeeper if authentication is enabled
226+
#--zk_cert=user:passwd
221227
222228
# configure log path
223229
--openmldb_log_dir=./logs
@@ -249,6 +255,7 @@ zookeeper.connection_timeout=5000
249255
zookeeper.max_retries=10
250256
zookeeper.base_sleep_time=1000
251257
zookeeper.max_connect_waitTime=30000
258+
#zookeeper.cert=user:passwd
252259
253260
# Spark Config
254261
spark.home=

docs/zh/deploy/conf.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# 如果是部署单机版不需要配置zk_cluster和zk_root_path,把这俩配置注释即可. 部署集群版需要配置这两项,一个集群中所有节点的这两个配置必须保持一致
1010
#--zk_cluster=127.0.0.1:7181
1111
#--zk_root_path=/openmldb_cluster
12+
# 配置zk认证的用户名和密码, 用冒号分割
13+
#--zk_cert=user:passwd
1214
# 单机版需要指定tablet的地址, 集群版此配置可忽略
1315
--tablet=127.0.0.1:9921
1416
# 配置log目录
@@ -76,6 +78,8 @@
7678
# 如果启动集群版需要指定zk的地址和集群在zk的节点路径
7779
#--zk_cluster=127.0.0.1:7181
7880
#--zk_root_path=/openmldb_cluster
81+
# 配置zk认证的用户名和密码, 用冒号分割
82+
#--zk_cert=user:passwd
7983
8084
# 配置线程池大小,建议和cpu核数一致
8185
--thread_pool_size=24
@@ -222,6 +226,8 @@
222226
# 如果部署的openmldb是集群版,需要指定zk地址和集群zk节点目录
223227
#--zk_cluster=127.0.0.1:7181
224228
#--zk_root_path=/openmldb_cluster
229+
# 配置zk认证的用户名和密码, 用冒号分割
230+
#--zk_cert=user:passwd
225231
226232
# 配置日志路径
227233
--openmldb_log_dir=./logs
@@ -254,6 +260,7 @@ zookeeper.connection_timeout=5000
254260
zookeeper.max_retries=10
255261
zookeeper.base_sleep_time=1000
256262
zookeeper.max_connect_waitTime=30000
263+
#zookeeper.cert=user:passwd
257264
258265
# Spark Config
259266
spark.home=

java/openmldb-common/src/main/java/com/_4paradigm/openmldb/common/zk/ZKClient.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
import org.apache.curator.RetryPolicy;
2121
import org.apache.curator.framework.CuratorFramework;
2222
import org.apache.curator.framework.CuratorFrameworkFactory;
23+
import org.apache.curator.framework.api.ACLProvider;
2324
import org.apache.curator.retry.ExponentialBackoffRetry;
2425
import org.apache.zookeeper.CreateMode;
26+
import org.apache.zookeeper.ZooDefs;
27+
import org.apache.zookeeper.data.ACL;
2528

2629
import java.util.concurrent.TimeUnit;
2730
import java.util.List;
@@ -46,12 +49,26 @@ public CuratorFramework getClient() {
4649
public boolean connect() throws InterruptedException {
4750
log.info("ZKClient connect with config: {}", config);
4851
RetryPolicy retryPolicy = new ExponentialBackoffRetry(config.getBaseSleepTime(), config.getMaxRetries());
49-
CuratorFramework client = CuratorFrameworkFactory.builder()
52+
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
5053
.connectString(config.getCluster())
5154
.sessionTimeoutMs(config.getSessionTimeout())
5255
.connectionTimeoutMs(config.getConnectionTimeout())
53-
.retryPolicy(retryPolicy)
54-
.build();
56+
.retryPolicy(retryPolicy);
57+
if (!config.getCert().isEmpty()) {
58+
builder.authorization("digest", config.getCert().getBytes())
59+
.aclProvider(new ACLProvider() {
60+
@Override
61+
public List<ACL> getDefaultAcl() {
62+
return ZooDefs.Ids.CREATOR_ALL_ACL;
63+
}
64+
65+
@Override
66+
public List<ACL> getAclForPath(String s) {
67+
return ZooDefs.Ids.CREATOR_ALL_ACL;
68+
}
69+
});
70+
}
71+
CuratorFramework client = builder.build();
5572
client.start();
5673
if (!client.blockUntilConnected(config.getMaxConnectWaitTime(), TimeUnit.MILLISECONDS)) {
5774
return false;

java/openmldb-common/src/main/java/com/_4paradigm/openmldb/common/zk/ZKConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ public class ZKConfig {
3232
private int baseSleepTime = 1000;
3333
@Builder.Default
3434
private int maxConnectWaitTime = 30000;
35+
@Builder.Default
36+
private String cert = "";
3537

3638
}

java/openmldb-jdbc/src/main/java/com/_4paradigm/openmldb/sdk/SdkOption.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class SdkOption {
3333
private String sparkConfPath = "";
3434
private int zkLogLevel = 3;
3535
private String zkLogFile = "";
36+
private String zkCert = "";
3637

3738
// options for standalone mode
3839
private String host = "";
@@ -70,6 +71,7 @@ public SQLRouterOptions buildSQLRouterOptions() throws SqlException {
7071
copt.setSpark_conf_path(getSparkConfPath());
7172
copt.setZk_log_level(getZkLogLevel());
7273
copt.setZk_log_file(getZkLogFile());
74+
copt.setZk_cert(getZkCert());
7375

7476
// base
7577
buildBaseOptions(copt);

java/openmldb-synctool/src/main/java/com/_4paradigm/openmldb/synctool/SyncToolConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class SyncToolConfig {
3737
// public static int CHANNEL_KEEP_ALIVE_TIME;
3838
public static String ZK_CLUSTER;
3939
public static String ZK_ROOT_PATH;
40+
public static String ZK_CERT;
4041
public static String SYNC_TASK_PROGRESS_PATH;
4142

4243
public static String HADOOP_CONF_DIR;
@@ -86,6 +87,7 @@ private static void parseFromProperties(Properties prop) {
8687
if (ZK_ROOT_PATH.isEmpty()) {
8788
throw new RuntimeException("zookeeper.root_path should not be empty");
8889
}
90+
ZK_CERT = prop.getProperty("zookeeper.cert", "");
8991

9092
HADOOP_CONF_DIR = prop.getProperty("hadoop.conf.dir", "");
9193
if (HADOOP_CONF_DIR.isEmpty()) {

java/openmldb-synctool/src/main/java/com/_4paradigm/openmldb/synctool/SyncToolImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ public SyncToolImpl(String endpoint) throws SqlException, InterruptedException {
8585
this.zkClient = new ZKClient(ZKConfig.builder()
8686
.cluster(SyncToolConfig.ZK_CLUSTER)
8787
.namespace(SyncToolConfig.ZK_ROOT_PATH)
88+
.cert(SyncToolConfig.ZK_CERT)
8889
.build());
8990
Preconditions.checkState(zkClient.connect(), "zk connect failed");
9091
SdkOption option = new SdkOption();
9192
option.setZkCluster(SyncToolConfig.ZK_CLUSTER);
9293
option.setZkPath(SyncToolConfig.ZK_ROOT_PATH);
94+
option.setZkCert(SyncToolConfig.ZK_CERT);
9395
this.router = new SqlClusterExecutor(option);
9496
this.zkCollectorPath = SyncToolConfig.ZK_ROOT_PATH + "/sync_tool/collector";
9597

java/openmldb-taskmanager/src/main/java/com/_4paradigm/openmldb/taskmanager/client/TaskManagerClient.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030
import org.apache.commons.logging.LogFactory;
3131
import org.apache.curator.framework.CuratorFramework;
3232
import org.apache.curator.framework.CuratorFrameworkFactory;
33+
import org.apache.curator.framework.api.ACLProvider;
3334
import org.apache.curator.framework.recipes.cache.NodeCache;
3435
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
3536
import org.apache.curator.retry.ExponentialBackoffRetry;
37+
import org.apache.zookeeper.ZooDefs;
38+
import org.apache.zookeeper.data.ACL;
3639
import org.apache.zookeeper.data.Stat;
3740
import java.util.ArrayList;
3841
import java.util.HashMap;
@@ -59,16 +62,34 @@ public TaskManagerClient(String endpoint) {
5962
}
6063

6164
public TaskManagerClient(String zkCluster, String zkPath) throws Exception {
65+
this(zkCluster, zkPath, "");
66+
}
67+
68+
public TaskManagerClient(String zkCluster, String zkPath, String zkCert) throws Exception {
6269
if (zkCluster == null || zkPath == null) {
6370
logger.info("Zookeeper address is wrong, please check the configuration");
6471
}
6572
String masterZnode = zkPath + "/taskmanager/leader";
6673

67-
zkClient = CuratorFrameworkFactory.builder()
74+
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
6875
.connectString(zkCluster)
6976
.sessionTimeoutMs(10000)
70-
.retryPolicy(new ExponentialBackoffRetry(1000, 10))
71-
.build();
77+
.retryPolicy(new ExponentialBackoffRetry(1000, 10));
78+
if (!zkCert.isEmpty()) {
79+
builder.authorization("digest", zkCert.getBytes())
80+
.aclProvider(new ACLProvider() {
81+
@Override
82+
public List<ACL> getDefaultAcl() {
83+
return ZooDefs.Ids.CREATOR_ALL_ACL;
84+
}
85+
86+
@Override
87+
public List<ACL> getAclForPath(String s) {
88+
return ZooDefs.Ids.CREATOR_ALL_ACL;
89+
}
90+
});
91+
}
92+
zkClient = builder.build();
7293
zkClient.start();
7394
Stat stat = zkClient.checkExists().forPath(masterZnode);
7495
if (stat != null) { // The original master exists and is directly connected to it.

java/openmldb-taskmanager/src/main/java/com/_4paradigm/openmldb/taskmanager/config/TaskManagerConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public static String getZkRootPath() {
101101
return getString("zookeeper.root_path");
102102
}
103103

104+
public static String getZkCert() {
105+
return props.getProperty("zookeeper.cert", "");
106+
}
107+
104108
public static int getZkConnectionTimeout() {
105109
return getInt("zookeeper.connection_timeout");
106110
}

java/openmldb-taskmanager/src/main/java/com/_4paradigm/openmldb/taskmanager/server/impl/TaskManagerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ private void initExternalFunction() throws InterruptedException {
8080
.connectionTimeout(TaskManagerConfig.getZkConnectionTimeout())
8181
.maxConnectWaitTime(TaskManagerConfig.getZkMaxConnectWaitTime())
8282
.maxRetries(TaskManagerConfig.getZkMaxRetries())
83+
.cert(TaskManagerConfig.getZkCert())
8384
.build());
8485
zkClient.connect();
8586

0 commit comments

Comments
 (0)