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

时间:2018-07-23 05:46:28

标签: java ssl cassandra cassandra-3.0 datastax-java-driver

我正在尝试通过Java客户端程序连接到3节点的Cassandra群集,并配置了启用了客户端到节点加密的Cassandra群集。我在所有三个节点上部署了三个自签名证书,然后根据文档Client-to-node encryption将公共证书导入到其他每个节点。运行客户端程序时,出现以下异常。

 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

1 个答案:

答案 0 :(得分:0)

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

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

如果可以,则说明设置正确,我将删除sslOptions并提供

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

如果这也可行,则问题出在您的getCluster方法中。

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

我已成功将您的代码作为maven项目执行,并将密钥库存储在resources文件夹中并加载了

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