gpt4 book ai didi

java - NoHostAvailableException::Java 到 Cassandra SSL 集群连接失败

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:28 27 4
gpt4 key购买 nike

我正在尝试从 Java 客户端程序连接到 3 节点 Cassandra 集群,Cassandra 集群配置为启用客户端到节点加密。我在所有三个节点中部署了三个自签名证书,并按照文档 Client-to-node encryption 将公共(public)证书导入到每个其他节点。 .当我运行客户端程序时,出现以下异常。

 Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: clm-pun-swpry4/10.133.181.157:9042 (com.datastax.driver.core.exceptions.TransportException: [clm-pun-swpry4/10.133.181.157:9042] Channel has been closed), clm-pun-swpryf/10.133.181.156:9042 (com.datastax.driver.core.exceptions.TransportException: [clm-pun-swpryf/10.133.181.156:9042] Channel has been closed), clm-pun-sqbgda/10.133.172.70:9042 (com.datastax.driver.core.exceptions.TransportException: [clm-pun-sqbgda/10.133.172.70:9042] Channel has been closed))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:233)
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:79)
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1483)
at com.datastax.driver.core.Cluster.init(Cluster.java:159)
at com.datastax.driver.core.SessionManager.initAsync(SessionManager.java:78)
at com.datastax.driver.core.SessionManager.executeAsync(SessionManager.java:139)
at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:68)
at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:43)
at clm.bmc.saas.incubator.ClientToNodeExample.main(ClientToNodeExample.java:28)

我在 cassandra.yaml 中的 Cassandra 配置(3 个节点中的每一个节点的个人自签名证书):

client_encryption_options:
enabled: true
# If enabled and optional is set to true encrypted and unencrypted connections are handled.
optional: false
keystore: /opt/secure/keystore.clm-pun-swpry4
keystore_password: changeit
# require_client_auth: false
# Set trustore and truststore_password if require_client_auth is true
# truststore: conf/.truststore
# truststore_password: cassandra
# More advanced defaults below:
protocol: TLSv1.2
algorithm: SunX509
store_type: JKS
cipher_suites: [TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA]

我的 Java 程序:


public class ClientToNodeExample {
private static final Logger LOGGER = LoggerFactory.getLogger(ClientToNodeExample.class);

public static void main(String[] args) {
ClientToNodeExample example = new ClientToNodeExample();
Session session = example.getCluster("C:\\install\\ssl\\cassandraCluster.ks", "changeit",
new String[]{"clm-pun-sqbgda", "clm-pun-swpryf", "clm-pun-swpry4"}, 9042).newSession();
ResultSet results = session.execute("SELECT * FROM entity_space.mo;");
LOGGER.info("NumberOfRows:" + results.all().size());
session.close();
}

private Cluster getCluster(String trustStoreLocation, String trustStorePassword, String[] host, int port) {
Cluster cluster;
SSLContext sslcontext = null;
try {
InputStream is = ClientToNodeExample.class.getResourceAsStream(trustStoreLocation);
KeyStore keystore = KeyStore.getInstance("jks");
char[] pwd = trustStorePassword.toCharArray();
keystore.load(is, pwd);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keystore);
TrustManager[] tm = tmf.getTrustManagers();
sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, tm, null);
} catch (Exception e) {
LOGGER.error("ERROR", e);
}
JdkSSLOptions sslOptions = JdkSSLOptions.builder().withSSLContext(sslcontext).build();
cluster = Cluster.builder().addContactPoints(host).withPort(port).withSSL(sslOptions).build();
return cluster;
}
}

但是,如果我尝试使用以下 keytool 命令检查来自每个节点的证书,我将获得证书:

keytool -printcert -sslserver clm-pun-sqbgda:9042 -rfc

任何人都可以帮助我出错的地方吗?

Cassandra version:3.11.0
cassandra-driver-core : 3.1.4

最佳答案

首先,我会尝试通过 cqlsh --ssl 进行连接(您需要执行一些额外的步骤):

  • 将服务器证书转换为 PKCS12 格式
  • 将 PKCS12 转换为 PEM
  • 修改 cqlshrc 文件以使用 PEM 证书。

如果没问题,那么设置是正确的,我会删除 sslOptions 并提供

-Djavax.net.ssl.trustStore
-Djavax.net.ssl.trustStorePassword
-Djavax.net.debug=ssl.

如果这也有效,那么问题出在您的 getCluster 方法中。

我的猜测是您的 keystore (cassandraCluster.ks) 未正确加载。

我成功地将您的代码作为 maven 项目执行, keystore 位于资源文件夹中并加载

cluster = getCluster("/client-truststore.jks", "changeit", new String[]{"127.0.0.1"}, 9042);

关于java - NoHostAvailableException::Java 到 Cassandra SSL 集群连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51472221/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com