具有自己的证书颁发机构的 Cassandra SSL

Cassandra SSL with own Certificate Authority

我想设置我自己的 CA 以用于 cassandra 集群,这样我就不必在每次添加新节点时都复制所有证书。我已经阅读了一些关于 Cassandra 和 SSL 的教程,但它们都与复制证书有关。我在CA过程中有点迷茫

这就是我认为我需要做的

现在:

优点:不再需要在节点之间复制 ssl 证书。每个节点只需一个即可。

编辑:

好的,我就是这样做的。如果我犯了任何错误,请告诉我。我遗漏了诸如 JCE 文件和适当的 cassandra.yaml 配置之类的东西。这些需要出现在服务器上!

openssl genrsa -out clusterCA.key 2048
openssl req -x509 -new -key clusterCA.key -days <DAYS> -out clusterCA.pem

keytool -importcert -alias clusterCA -file clusterCA.pem -keystore clustertruststore -storepass <PASS>

#on each cassandra host for clients. for client replace nodename with clientname
keytool -genkeypair -alias <NODENAME> -keyalg RSA -keysize 2048 -dname "CN=<NODENAME>,OU=<UNITNAME>,O=<ORGANISATION>" -keypass <PASS> -keystore <NODENAME>.keystore -storepass <PASS> -validity <DAYS>

keytool -keystore <NODENAME>.keystore -alias <NODENAME> -certreq -file <NODENAME>.cert -storepass <PASS> -keypass <PASS>


# sign it with CA

openssl x509 -req -CA clusterCA.pem -CAkey clusterCa.key -in <NODENAME>.cert -out <NODENAME>.signed -days <DAYS> -CAcreateserial

# add rootCA to host

keytool -keystore <NODENAME>.keystore -storepass <PASS> -alias clusterCA -import -file clusterCA.pem -noprompt

keytool -keystore <NODENAME>.keystore -storepass <PASS> -alias <NODENAME> -import -file <NODENAME>.signed -keypass <PASS>

## use <NODENAME>.keystore as truststore and keystore for cassandra node / client trust/keystore
## No need to copy keystores around. You only need it on your host


## create CQLSH pem
keytool -importkeystore -srckeystore <NODENAME>.keystore -destkeystore <NODENAME>_user1.p12 -deststoretype PKCS12
openssl pkcs12 -in <NODENAME>_user1.p12 -out <NODENAME>_user1.pem -nodes

##  use <NODENAME>_user1.pem as certfile for cqlsh

你的策略非常合理,我也会这样做。您希望拥有自己的证书颁发机构,然后为每个节点创建一个 CSR。这比单独信任节点证书更容易管理。

  • 每个节点都有自己的密钥库来存储它的证书。
  • 您将希望每个节点在其信任库中都有 CA public 证书。这仅在您将 'require_client_auth' 设置为 true 时才会发生。我建议这样做,因为它设置起来并不太困难,并且添加了一个额外的识别层,这应该被认为很重要。

区分internode encryption and client encryption也很重要。 Cassandra 对每个都有不同的设置(记录在上面的链接中)。如果使用客户端到节点加密,您还需要有一个用于客户端证书的信任库。您可以使用相同的信任库并向客户端颁发证书。

在客户端到节点端 here's an example 来自 java 驱动程序测试如何使用您的密钥和信任库设置您的 SSLContext:

/**
 * @param keyStorePath Path to keystore, if absent is not used.
 * @param trustStorePath Path to truststore, if absent is not used.
 * @return {@link com.datastax.driver.core.SSLOptions} with the given keystore and truststore path's for
 * server certificate validation and client certificate authentication.
 */
public SSLOptions getSSLOptions(Optional<String> keyStorePath, Optional<String> trustStorePath) throws Exception {

    TrustManagerFactory tmf = null;
    if(trustStorePath.isPresent()) {
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(this.getClass().getResourceAsStream(trustStorePath.get()), DEFAULT_CLIENT_TRUSTSTORE_PASSWORD.toCharArray());

        tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ks);
    }

    KeyManagerFactory kmf = null;
    if(keyStorePath.isPresent()) {
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(this.getClass().getResourceAsStream(keyStorePath.get()), DEFAULT_CLIENT_KEYSTORE_PASSWORD.toCharArray());

        kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, DEFAULT_CLIENT_KEYSTORE_PASSWORD.toCharArray());
    }

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(kmf != null ? kmf.getKeyManagers() : null, tmf != null ? tmf.getTrustManagers() : null, new SecureRandom());

    return new SSLOptions(sslContext, SSLOptions.DEFAULT_SSL_CIPHER_SUITES);
}

一旦你有了一个 SSLOptions 对象,你就可以简单地将它传递到你的 Cluster Builder 中,即:

cluster = Cluster.builder()
    .addContactPoint(host)
    .withSSL(sslOptions))
    .build();

CQLSH 通过 cqlshrc 文件支持 SSL。您可以找到如何设置的示例 here.

对不起,我有 0 个代表,所以无法发表评论。我正在回应我刚才遵循的优秀说明的这一部分:

## create CQLSH pem
keytool -importkeystore -srckeystore <NODENAME>.keystore -destkeystore <NODENAME>_user1.p12 -deststoretype PKCS12
openssl pkcs12 -in <NODENAME>_user1.p12 -out <NODENAME>_user1.pem -nodes

IMO 如果您要将此 pem 提供给用户或应用程序,他们不需要将放置在 pem 中的私钥,除非您添加: -nokeys

希望对您有所帮助。自从我为 SO 做出贡献以来已经有 10 年了。 :)