在Linux上配置ZooKeeper的安全策略,通常涉及以下几个方面:
启用认证和授权:
配置SSL/TLS:
配置防火墙:
以下是详细的步骤:
ZooKeeper支持多种认证方式,包括SASL(Simple Authentication and Security Layer)。
编辑zoo.cfg
文件,添加以下配置:
authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
jaasLoginRenew=3600000
创建一个JAAS配置文件(例如zookeeper_jaas.conf
):
Server {
org.apache.zookeeper.server.auth.DigestLoginModule required
user_super="super_secret_password"
user_admin="admin_password";
};
在启动ZooKeeper时指定JAAS配置文件:
bin/zkServer.sh start-foreground -Djava.security.auth.login.config=/path/to/zookeeper_jaas.conf
编辑zoo.cfg
文件,添加以下配置:
aclProvider.1=org.apache.zookeeper.server.auth.DefaultACLProvider
创建一个ACL配置文件(例如zookeeper_acl.conf
):
create / "super_secret_password:super_secret_password:cdrwa"
create /configs "admin_password:admin_password:cdrwa"
在启动ZooKeeper时指定ACL配置文件:
bin/zkServer.sh start-foreground -Dzookeeper.aclProvider.1=/path/to/zookeeper_acl.conf
使用OpenSSL生成自签名证书:
# 生成CA证书
openssl req -new -x509 -days 365 -keyout ca.key -out ca.crt -subj "/C=US/ST=State/L=City/O=Organization/CN=CA"
# 生成服务器证书
openssl req -newkey rsa:2048 -days 365 -nodes -keyout server.key -out server.csr -subj "/C=US/ST=State/L=City/O=Organization/CN=localhost"
# 生成服务器证书签名请求(CSR)
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365
编辑zoo.cfg
文件,添加以下配置:
ssl.keystore.location=/path/to/server.jks
ssl.keystore.password=server_keystore_password
ssl.truststore.location=/path/to/ca.jks
ssl.truststore.password=ca_truststore_password
ssl.clientAuth=need
将生成的证书和密钥导入到Java KeyStore中:
# 导入服务器证书到keystore
keytool -import -alias server -file server.crt -keystore server.jks -storepass server_keystore_password
# 导入CA证书到truststore
keytool -import -alias ca -file ca.crt -keystore ca.jks -storepass ca_truststore_password
使用iptables
或firewalld
限制对ZooKeeper端口的访问。
# 允许本地访问
iptables -A INPUT -p tcp --dport 2181 -s 127.0.0.1 -j ACCEPT
# 允许特定IP访问
iptables -A INPUT -p tcp --dport 2181 -s 192.168.1.100 -j ACCEPT
# 拒绝其他所有访问
iptables -A INPUT -p tcp --dport 2181 -j DROP
# 允许本地访问
firewall-cmd --permanent --zone=trusted --add-source=127.0.0.1
firewall-cmd --permanent --zone=trusted --add-port=2181/tcp
# 允许特定IP访问
firewall-cmd --permanent --zone=trusted --add-source=192.168.1.100
firewall-cmd --permanent --zone=trusted --add-port=2181/tcp
# 重新加载firewalld配置
firewall-cmd --reload
通过以上步骤,你可以为ZooKeeper配置安全策略,确保其安全性和可靠性。