找回密码
 注册
查看: 502|回复: 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变量方式

    + I( G. s8 m3 ]0 V4 S

通过设置以下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方式
    . B9 S0 T1 G0 U! g3 z9 D

    / @: j9 o' y4 C- M  O* o

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变量方式


    ; k/ U- r) ^6 B& p# O

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方式


    2 H  M6 b2 p) c4 m/ T# W9 w

在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

8 v% `8 F; w1 h% H' Z

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2022-11-16 09:54:14 | 显示全部楼层
为 ZooKeeper 配置 SSL/TLS$ F) |( y& ~  L+ k+ B7 m
使用 ThingWorx HA 群集时,可以为 ZooKeeper 配置 SSL 或 TLS:* c% i2 w( D; Q  F
配置 ZooKeeper  Z. f1 i/ h; w5 O0 h! m" s0 |- _3 e
1.请确保正在运行的 ZooKeeper 版本支持 SSL 或 TLS。. i% e* m7 {: ]$ H
2.获取您的 SSL 证书和信任存储。) Y3 a7 a$ `: m% m0 u" e8 L1 u
证书的可接受文件扩展名仅有:JKS、PEM 和 PKCS12(p12)。3 U7 ?* s5 M( Z% P
3.转至 apache-zookeeper-[version]-bin/conf 并更新或创建 zoo.cfg。
. M/ b/ }0 P! {9 `' ?1 q7 F/ r2 g4.添加以下条目:
- P2 Y. s$ D8 T! i! X2 vdataDir=/<path-to-zookeeper-data>/data
3 Y% N! h& Y3 ?. T7 rdataLogDir=/<path-to-zookeeper-datalog>/datalog
" ?8 p% H7 m% R3 T/ msecureClientPort=22817 Q: F9 w5 x$ f( d& M% ?. p% \
tickTime=2000
) ?) i# M9 S3 d) x2 TinitLimit=50 }- k4 j/ x# A2 `$ C
syncLimit=2- ~' K# |3 n! n6 o: Y! n# n8 g
autopurge.snapRetainCount=3
" |" V6 ^, I0 Wautopurge.purgeInterval=06 j6 `# H9 w6 z/ W3 L
maxClientCnxns=605 H% y- j% H' _8 O. s, }2 q
admin.enableServer=true
# W3 X) I& F7 Q! r# `( BstandaloneEnabled=false( N/ A6 }3 m3 _9 r. Q
quorumListenOnAllIPs=true" n+ U: d3 q6 T! a, T4 C
sslQuorum=true# Y' x% R. G7 j  i* U7 S7 e
要激活 ZooKeeper 节点之间的仲裁,请在 zoo.cfg 文件中设置变量 sslQuorum=true。节点将使用自动生成的 SSL 来保护仲裁。
9 f$ t2 U8 a: d1 ~2 h9 K5.修改 <path to zookeeper>/bin/zkServer.sh
: N. }$ T* p) Z( F7 Y0 S* ].5 @# K! [. |0 U9 w% a3 j" e% ]% C
export SERVER_JVMFLAGS="7 s/ o; \8 p8 G: X) i
-Dzookeeper.serverCnxnFactory=org.apache.zookeeper.server.NettyServerCnxnFactory" j8 X! j' l3 q/ H, @$ X# o
-Dzookeeper.ssl.keyStore.location=<path-to-zookeeper-certificates>/zookeeper.p12
% \  A0 R/ }* u. y-Dzookeeper.ssl.keyStore.password=<certificate-password>8 G" y! l% X- P  ?: ?
-Dzookeeper.ssl.trustStore.location=<path-to-zookeeper-certificates>/truststore.p12( y' n# I! u: s/ l
-Dzookeeper.ssl.trustStore.password=<truststore-password>
( C9 K- u. H$ m( ^-Dzookeeper.ssl.quorum.keyStore.location=<path-to-zookeeper-certificates>/zookeeper.p12
" Q- \, |0 w/ L7 P-Dzookeeper.ssl.quorum.keyStore.password=<certificate-password>+ ~/ E; e! c7 A) V! _- x" ~: V# Z
-Dzookeeper.ssl.quorum.trustStore.location=<path-to-zookeeper-certificates>/truststore.p126 S% i4 D! M  W% r
-Dzookeeper.ssl.quorum.trustStore.password=<truststore-password>% E1 m! A6 l3 E) T8 G$ d
-Dzookeeper.ssl.quorum.hostnameVerification=false
! {* C9 o7 I+ ]# x, v" g- ?6.启动 ZooKeeper:7 d# f# B. [. d4 k3 T
./zkServer.sh start3 r. _1 C4 C/ S7 ]
7.在日志中,验证配置是否正确:$ J5 ?/ k" S  p: b2 p& {: E& |9 L
tail -f apache-zookeeper-3.5.6-bin/logs/<zookeeper-log-file>, i! T4 q' K& a2 A& O/ }" `
配置 ThingWorx
. `' a4 G$ A0 b1 o0 z& U6 W1.将 ZooKeeper 证书复制到您的实例,或确保其在运行 ThingWorx 的计算机上可用。
4 ^  ^% n- N0 \0 R3 V* O) A+ [" d2.修改 platform-settings.json,使得文件末尾包含下列内容作为根元素,且与 PlatformSettingsConfig 属于同一级别。5 Y* p& S2 t1 s2 J
"ZookeeperSettings": {
1 g' V0 f8 @- q' \1 {) O    "SSLEnabled": "true",# r% M7 r9 }7 E- c( v! ]
    # If SSL is enabled, you must include the following; trust store is optional:. @! p& s/ }. V7 M; H7 z5 l" u7 R" g
    "KeyStorePath": "<path-to-zookeeper-certificates>/zookeeper.p12",
2 X+ k2 |; c3 V2 M$ B/ n1 C    "KeyStorePass": "<certificate-password>",
8 ]: h6 y+ W/ u    "TrustStorePath": "<path-to-zookeeper-certificates>/truststore.p12",
/ `5 T+ X2 r* \6 B5 H  ?2 Y# f    "TrustStorePass": "<truststore-password>"
8 b2 Z! B6 u' E  Y    "SASLEnabled": "false",* U0 u: y* ?+ n# Y# _- N* w
    # If SASL is enabled, you must include the following:
& v/ o. S% r1 x5 Q% X4 }9 i) h- x. p    "JaasConfPath": "/tmp1/jaas.conf",
, E2 j3 {* h% h) `7 |2 Z    "Krb5ConfPath": "/tmp1/krb5.conf"2 f$ v* e8 Q, }( K- f, h3 n
    }3 K4 q0 O" E( j& G4 L1 [/ {
3.搜索默认 ZooKeeper 端口 2181,并将其替换为安全端口 2281。
8 t; W( f) a7 x8 J/ O4.请确保所有 CoordinatorHosts 条目和 address-resolver > connection ports 均已更新,以便与 zoo.cfg 中的 secureClientPort 值相匹配。1 z2 V) h# Q  O- h: m
配置 Ignite
: e8 W2 r' ?# g& M. u" x" H1.将 Ignite 证书复制到您的 ThingWorx 实例,或确保其在运行 Ignite 服务器的计算机上可用。+ D3 V9 h, }% I8 L& a3 C- Q$ V' ~
2.设置 ZOOKEEPER_CONNECTION 环境变量,查找用于启动 Ignite 的 JVM_XOPTS 环境变量,并按如下所示进行更新:
- q- L2 [  r7 i& U% N# zookeeper1 represents the host name where zookeeper is available and 2281 the secure port from zoo.cfg
; _# A2 b- {$ u' Qexport ZOOKEEPER_CONNECTION=zookeeper1:2281,zookeeper2:2281,zookeeper3:2281% l$ Y# q5 f4 ^7 f8 q# Q
# update the JVM_XOPTS
( p6 f' T* R: _* zJVM_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>* C' ~* B" W& a5 ?9 s
配置 Connection Server, p. a$ M$ B$ q( q
1.将 ZooKeeper 证书复制到您的实例,或确保其在运行 Connection Server 的计算机上可用。; ?1 P/ r" Z' `+ T6 B# J
2.在 Connection Server 配置文件中更新 cx-server.discovery.connectionString 的端口,以使用安全端口。* O' a! m4 {$ f. d# S% p
例如,cx-server.discovery.connectionString = "{zookeeper-host}:2281"。+ @7 e0 h, h  f; J1 w! _0 i
3.将下列系统属性添加到 CONNECTION_SERVER_OPTS 环境变量中。' H) y3 E( t5 k4 O% @
例如:7 D2 ], p* x2 h+ H: M5 d  O
export CONNECTION_SERVER_OPTS="
- o  z) v6 O/ R$ _-Dzookeeper.clientCnxnSocket=org.apache.zookeeper.ClientCnxnSocketNetty! p* {3 s; _0 k  R
-Dzookeeper.client.secure=true
' b9 I0 E$ {- _; V: Z; T' @7 a-Dzookeeper.ssl.keyStore.location=<path-to-zookeeper-certificates>/zookeeper.p122 [: T" `" D5 N7 m7 R. {* U: d3 H
-Dzookeeper.ssl.keyStore.password=<keystore-password>5 B* X! Y5 P1 \. U: j( k% ?* U7 Z# R$ f
-Dzookeeper.ssl.trustStore.location=<path-to-zookeeper-certificates>/truststore.p12+ m- ?* {6 N; `4 f6 m
-Dzookeeper.ssl.trustStore.password=<truststore-password>"
; x( k( P( H1 T9 i$ N使用 ThingWorx 安全管理工具加密密码
6 }3 w0 x  I7 T如果希望避免将普通密码插入到 platform-settings.json 文件中,则可以使用安全工具加密 twx-keystore 内的密码。您必须分别使用适用于密钥存储和信任存储密码的 encrypt.zk.keystore.password 和 encrypt.zk.truststore.password 对密码进行加密。
3 z/ x; e6 c7 V$ k8 B./security-common-cli keystore.conf set encrypt.zk.keystore.password "ptcptc"3 c: m* E1 {5 ?! r1 T
然后,更改 platform-settings.json 文件,以使得 ThingWorx 从密钥存储中选择密码:
; q; y7 L4 Q) Y' o) {/ {"KeyStorePass": "encrypt.zk.keystore.password",9 o( S* F- E: @* f2 F* L
"TrustStorePass": "encrypt.zk.truststore.password"
. x, y$ x3 E: b0 J+ \- O
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-12 01:02 , Processed in 0.019632 second(s), 23 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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