找回密码
 注册
查看: 500|回复: 1

zkServer添加ssl协议支持方式

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2022-11-16 09:53:14 | 显示全部楼层 |阅读模式

ZooKeeper的默认网络通信没有加密,开源社区在ZOOKEEPER-2125中加入了netty以支持SSL。

值得一提的是,和其他一些开源组件一样,ZooKeeper的SASL认证配置和SSL配置都可以通过JVM变量配置。这样一来配置起来比较方便,但也带来一些问题。例如在一个JVM中启动多个ZooKeeper时,会有配置冲突。

客户端配置
  • JVM变量方式

    9 w$ f$ ^0 n) y1 B' z

通过设置以下JVM变量启用Netty:

zookeeper.clientCnxnSocket="org.apache.zookeeper.ClientCnxnSocketNetty"

通过设置以下JVM变量启用安全通信:

zookeeper.client.secure=true

设置“secure”后的客户端只能访问服务器的“secureClientPort“

设置keystore 和 truststorJVM环境变量:

zookeeper.ssl.keyStore.location="/path/to/your/keystore"

zookeeper.ssl.keyStore.password="keystore_password"

zookeeper.ssl.trustStore.location="/path/to/your/truststore"

zookeeper.ssl.trustStore.password="truststore_password"

  • ZKClientConfig方式
    * S5 }- p# ]5 l4 N. F

    $ c9 C( _7 W" q

ZKClientConfig clientConfig = new ZKClientConfig();

conf.setProperty(ZKClientConfig.SECURE_CLIENT, "true");

conf.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, "org.apache.zookeeper.ClientCnxnSocketNetty");

conf.setProperty(ZKClientConfig.SSL_KEYSTORE_LOCATION, "KeyStorePath");

conf.setProperty(ZKClientConfig.SSL_KEYSTORE_PASSWD, "KeyStorePasswd");

conf.setProperty(ZKClientConfig.SSL_TRUSTSTORE_LOCATION, "TrustStorePath");

conf.setProperty(ZKClientConfig.SSL_TRUSTSTORE_PASSWD, "TrustStorePasswd");

借助ZKClientConfig社区还实现了在同一个JVM中启动多个客户端分别连接开启Kerberos认证的不同的ZooKeeper集群,或分别连接开启认证与未开启认证的ZooKeeper集群,有机会单开一篇文章介绍。

Server端
  • JVM变量方式


    ; h" P+ a% T, n4 v, Q

Server可以直接在zkServer.sh中增加配置:

export SERVER_JVMFLAGS="

-Dzookeeper.serverCnxnFactory=org.apache.zookeeper.server.NettyServerCnxnFactory

-Dzookeeper.ssl.keyStore.location=testKeyStore.jks

-Dzookeeper.ssl.keyStore.password=testpass

-Dzookeeper.ssl.trustStore.location=testTrustStore.jks

-Dzookeeper.ssl.trustStore.password=testpass"

export CLIENT_JVMFLAGS="

-Dzookeeper.clientCnxnSocket=org.apache.zookeeper.ClientCnxnSocketNetty

-Dzookeeper.client.secure=true

-Dzookeeper.ssl.keyStore.location=testKeyStore.jks

-Dzookeeper.ssl.keyStore.password=testpass

-Dzookeeper.ssl.trustStore.location=testTrustStore.jks

-Dzookeeper.ssl.trustStore.password=testpass"

端口在zoo.cfg中添加:

secureClientPort=2281

所有SSL模式客户端都应该连接到这一端口

  • zoo.cfg方式


    . s* f6 Q" }) d  y

在zoo.cfg中添加:

secureClientPort=3183

serverCnxnFactory=org.apache.zookeeper.server.NettyServerCnxnFactory

ssl.keyStore.location=testKeyStore.jks

ssl.keyStore.password=testpass

ssl.trustStore.location=testTrustStore.jks

ssl.trustStore.password=testpass

6 y6 y9 {! S4 I  w# X" N

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2022-11-16 09:54:14 | 显示全部楼层
为 ZooKeeper 配置 SSL/TLS
# D2 ?- l! X1 E" l- v5 k使用 ThingWorx HA 群集时,可以为 ZooKeeper 配置 SSL 或 TLS:, K9 ~+ k: V: D
配置 ZooKeeper& Q) t& F1 _9 b- P) f* S; ?9 X
1.请确保正在运行的 ZooKeeper 版本支持 SSL 或 TLS。
. Z7 R6 B% K" a3 F2.获取您的 SSL 证书和信任存储。
$ ^9 R8 X! b, j7 A) J3 t* n证书的可接受文件扩展名仅有:JKS、PEM 和 PKCS12(p12)。
- Q  }# u0 }; J4 F* `6 K; \$ e3.转至 apache-zookeeper-[version]-bin/conf 并更新或创建 zoo.cfg。& k* h; ~) q8 \5 T
4.添加以下条目:
3 j% N8 w+ r. _& B/ wdataDir=/<path-to-zookeeper-data>/data4 O, o8 L% B! L$ n6 _0 g
dataLogDir=/<path-to-zookeeper-datalog>/datalog
( Y+ B6 v/ |8 H/ TsecureClientPort=2281: ?- h3 F5 i0 Y$ C) ?! c
tickTime=2000
/ D. {) ~" p$ B7 f2 s8 V' ZinitLimit=5
* _% e( g8 `( G+ f4 z/ SsyncLimit=2
4 a+ ]$ ~6 ^  b, m6 y/ g$ f% Xautopurge.snapRetainCount=3
9 E7 v$ o4 H) K0 ?  Nautopurge.purgeInterval=0
6 b# P6 l3 F3 @3 o+ q5 a1 P2 P6 D5 QmaxClientCnxns=60
, h" r* q* z2 k& k6 ~5 a2 I3 Cadmin.enableServer=true9 C# g: c4 t2 ?" h' n8 {
standaloneEnabled=false
5 Z4 N  C) }" p; e1 W6 LquorumListenOnAllIPs=true( c/ C' f% m/ x- B5 y1 b( y1 ~
sslQuorum=true$ ?; |. @* T0 L9 i
要激活 ZooKeeper 节点之间的仲裁,请在 zoo.cfg 文件中设置变量 sslQuorum=true。节点将使用自动生成的 SSL 来保护仲裁。/ y8 U+ k8 |) Q! ^: S' T
5.修改 <path to zookeeper>/bin/zkServer.sh
2 J+ e/ [+ W4 `8 f0 T: L, ?.
7 ?# X3 N  x* O; X9 D, V) texport SERVER_JVMFLAGS="
; ]" y; v9 K* [1 x$ S9 H9 f  i-Dzookeeper.serverCnxnFactory=org.apache.zookeeper.server.NettyServerCnxnFactory
) s; M) `6 E8 l: h0 Y-Dzookeeper.ssl.keyStore.location=<path-to-zookeeper-certificates>/zookeeper.p12
. h( S( ?5 M: P" n-Dzookeeper.ssl.keyStore.password=<certificate-password>
7 g! c' l$ b( g0 Q-Dzookeeper.ssl.trustStore.location=<path-to-zookeeper-certificates>/truststore.p12
: k  w6 w2 v) H/ y-Dzookeeper.ssl.trustStore.password=<truststore-password>/ B3 e, @8 n8 x
-Dzookeeper.ssl.quorum.keyStore.location=<path-to-zookeeper-certificates>/zookeeper.p124 K* g/ `( c+ G
-Dzookeeper.ssl.quorum.keyStore.password=<certificate-password>. z. n  [6 V) ]1 Z, X
-Dzookeeper.ssl.quorum.trustStore.location=<path-to-zookeeper-certificates>/truststore.p12( d- V; j) [/ N
-Dzookeeper.ssl.quorum.trustStore.password=<truststore-password>. K/ L2 Y) e( b( W1 ~$ l, @1 B# e
-Dzookeeper.ssl.quorum.hostnameVerification=false  y6 J( z7 I& S- c# h; ?9 G
6.启动 ZooKeeper:1 s7 }5 i/ i* {! n1 }, B0 C4 x" X: W
./zkServer.sh start
) T$ `3 n% U& F# c7.在日志中,验证配置是否正确:8 z) X/ b+ }9 Y0 k- u: Q
tail -f apache-zookeeper-3.5.6-bin/logs/<zookeeper-log-file>
( B; P  h2 l/ I' ^, r  D  _配置 ThingWorx' |+ Y. m5 m# b9 V  E, |# W. _
1.将 ZooKeeper 证书复制到您的实例,或确保其在运行 ThingWorx 的计算机上可用。3 _3 {& h: o1 ^, K
2.修改 platform-settings.json,使得文件末尾包含下列内容作为根元素,且与 PlatformSettingsConfig 属于同一级别。
* h7 p: u* P4 @& R5 ]"ZookeeperSettings": {8 N& {: o+ B3 q6 }, I
    "SSLEnabled": "true",
; V0 ^. e2 a( R% y- ?+ ~, h4 q    # If SSL is enabled, you must include the following; trust store is optional:- f9 M: K" Q+ U: @
    "KeyStorePath": "<path-to-zookeeper-certificates>/zookeeper.p12",! _7 G# L: v  `0 ]1 ]0 H/ X  g
    "KeyStorePass": "<certificate-password>",4 Q" ]: l( H$ U3 a2 x# ?% W
    "TrustStorePath": "<path-to-zookeeper-certificates>/truststore.p12",
+ b$ u3 q% t, I8 p    "TrustStorePass": "<truststore-password>"
( w' ~$ O; n, B( G  W: F& R    "SASLEnabled": "false",
3 k7 b( l& [# O6 s% p    # If SASL is enabled, you must include the following:
( `  k$ `1 Q0 A. C    "JaasConfPath": "/tmp1/jaas.conf",
( Q3 h2 j( s1 b( q' I) w& x    "Krb5ConfPath": "/tmp1/krb5.conf"
* U6 w- V$ S# q  P6 A    }3 C+ L+ [) o$ S
3.搜索默认 ZooKeeper 端口 2181,并将其替换为安全端口 2281。2 E7 C) S6 F. ~( N4 `8 u# N
4.请确保所有 CoordinatorHosts 条目和 address-resolver > connection ports 均已更新,以便与 zoo.cfg 中的 secureClientPort 值相匹配。
0 h* X! E7 n' b2 Y% V配置 Ignite" @- s0 X: o  N9 e1 e4 Z* B. O
1.将 Ignite 证书复制到您的 ThingWorx 实例,或确保其在运行 Ignite 服务器的计算机上可用。
% g" i. p, `- x5 `" J2.设置 ZOOKEEPER_CONNECTION 环境变量,查找用于启动 Ignite 的 JVM_XOPTS 环境变量,并按如下所示进行更新:
2 ^5 n/ W4 H& Q0 `; Y# zookeeper1 represents the host name where zookeeper is available and 2281 the secure port from zoo.cfg
5 [3 R0 M; [- E  B) kexport ZOOKEEPER_CONNECTION=zookeeper1:2281,zookeeper2:2281,zookeeper3:2281
9 k; Z+ M- @3 J9 V6 Y* i# update the JVM_XOPTS
4 z& L2 B7 ]4 i- m: V, }! HJVM_XOPTS=-Dzookeeper.clientCnxnSocket=org.apache.zookeeper.ClientCnxnSocketNetty -Dzookeeper.client.secure=true -Dzookeeper.ssl.keyStore.location=<path-to-zookeeper-certificates>/zookeeper.p12 -Dzookeeper.ssl.keyStore.password=<keystore-password> -Dzookeeper.ssl.trustStore.location=<path-to-zookeeper-certificates>/truststore.p12 -Dzookeeper.ssl.trustStore.password=<truststore-password>
  G% q9 m) }* c: H' k" S配置 Connection Server2 r* ^+ i, I. t& [, y  v
1.将 ZooKeeper 证书复制到您的实例,或确保其在运行 Connection Server 的计算机上可用。
7 f  y2 }! D# s' R% V2.在 Connection Server 配置文件中更新 cx-server.discovery.connectionString 的端口,以使用安全端口。! Q6 S, V4 Y0 L) h
例如,cx-server.discovery.connectionString = "{zookeeper-host}:2281"。3 a8 _2 k! O2 F4 ~4 H# l9 T
3.将下列系统属性添加到 CONNECTION_SERVER_OPTS 环境变量中。4 q: Q3 }$ c4 L: b( \
例如:
' w  F# y4 \4 Y4 Dexport CONNECTION_SERVER_OPTS="5 n& R. G2 Q1 z7 A0 R7 j2 V+ Y
-Dzookeeper.clientCnxnSocket=org.apache.zookeeper.ClientCnxnSocketNetty
- m8 U+ r- Z/ d* u/ ~7 k-Dzookeeper.client.secure=true+ V6 o( ^" J4 i- j  Y
-Dzookeeper.ssl.keyStore.location=<path-to-zookeeper-certificates>/zookeeper.p123 d6 o' x6 i' i! }+ o
-Dzookeeper.ssl.keyStore.password=<keystore-password>; [2 B! n8 H) ?1 C6 J+ U
-Dzookeeper.ssl.trustStore.location=<path-to-zookeeper-certificates>/truststore.p12* e# {  O- b! [7 l7 k3 U2 I
-Dzookeeper.ssl.trustStore.password=<truststore-password>"
. R5 q0 p* G' `. W6 z7 d; |. j使用 ThingWorx 安全管理工具加密密码
3 k2 f$ Z' t( c/ K如果希望避免将普通密码插入到 platform-settings.json 文件中,则可以使用安全工具加密 twx-keystore 内的密码。您必须分别使用适用于密钥存储和信任存储密码的 encrypt.zk.keystore.password 和 encrypt.zk.truststore.password 对密码进行加密。/ O* U/ y4 `6 p; R( }4 A' N
./security-common-cli keystore.conf set encrypt.zk.keystore.password "ptcptc"
  Z4 x$ y$ O6 Y" n; q& N然后,更改 platform-settings.json 文件,以使得 ThingWorx 从密钥存储中选择密码:
- K* F! N" \2 Q0 p8 j$ Z: m2 F* y"KeyStorePass": "encrypt.zk.keystore.password",- U3 f$ Z3 s9 K# H
"TrustStorePass": "encrypt.zk.truststore.password"/ ]5 }+ Y+ V" Q: w* ]) Y/ V
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-11 23:03 , Processed in 0.019875 second(s), 23 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表