与 Java 的 AWS DocumentDB TLS 连接

AWS DocumentDB TLS connection with Java

我在 TLS/SSL

上使用纯 Java 连接我的 DocumentDB 集群时遇到问题

我按照 AWS docs 遵循的程序是这样的:

我从 AWS 下载了 .pem 文件并复制到我的 java 项目的根目录中,从那里我执行了:

keytool -importcert -trustcacerts -file rds-combined-ca-bundle.pem -alias certAlias -keystore rds-ca-certs -storepass keyStorePassword

这已经在我的项目的根目录创建了 rds-ca-certs,现在看起来像这样:

而 Main.java 中的 java 代码是:

package com.example.documentdb;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ServerAddress;
import com.mongodb.MongoException;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
import org.bson.Document;


public final class Main {
    private Main() {
    }
    public static void main(String[] args) {

        String template = "mongodb://%s:%s@%s/test?ssl=true&replicaSet=rs0&readpreference=%s";
        String username = "myUsernameInCluster";
        String password = "myPasswordInCluster";
        String clusterEndpoint = "mycluster.node.us-east-1.docdb.amazonaws.com:27017";
        String readPreference = "secondaryPreferred";
        String connectionString = String.format(template, username, password, clusterEndpoint, readPreference);

        String keystore = "rds-ca-certs";
        String keystorePassword = "keyStorePassword";

        System.setProperty("javax.net.ssl.trustStore", keystore);
        System.setProperty("javax.net.ssl.trustStorePassword", keystorePassword);

        MongoClientURI clientURI = new MongoClientURI(connectionString);
        MongoClient mongoClient = new MongoClient(clientURI);

        MongoDatabase testDB = mongoClient.getDatabase("test");
        MongoCollection<Document> numbersCollection = testDB.getCollection("numbers");

        Document doc = new Document("name", "pi").append("value", 3.14159);
        numbersCollection.insertOne(doc);

        MongoCursor<Document> cursor = numbersCollection.find().iterator();
        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next().toJson());
            }
        } finally {
            cursor.close();
        }

    }
}

它给了我这个丑陋的错误:

com.mongodb.MongoSocketWriteException: Exception sending message

Caused by: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

有趣的是,当我使用 mongo CLI 通过 SSL/TLS 连接时,如下所示:

mongo --ssl --host mycluster.node.eu-central-1.docdb.amazonaws.com:27017 --sslCAFile rds-combined-ca-bundle.pem --username myUsernameInCluster --password myPasswordInCluster

它工作得很好,所以我排除了网络问题,我认为问题出在 Java 代码本身或执行的 keytool 命令中。

此外,在集群中禁用 TLS 后,此 java code provided by AWS(仅在密钥库配置中有所不同)有效。

PD:我知道还有很多关于 SSL/TLS 与 AWS DocumentDB 连接的其他问题,但其中 none 专门解决了我的问题。此外,我还检查了简单的 mongodb TLS/SSL 连接问题和过程不同,因此它们对我无效。

您可以将 AWS 提供的证书添加到 java 的信任库,然后 java 将默认信任对 AWS 服务的请求。
您必须找到 java cacerts 文件。根据您的 OS 和 java 版本,它应该在 ..lib/security/cacerts.
我正在使用 OSX,它在
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/security/cacerts

然后让 keytool 导入它默认的 java 密钥库密码是更改它:

keytool -import -trustcacerts -keystore /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/security/cacerts -storepass changeit -noprompt -alias aws-rds -file rds-combined-ca-bundle.pem

要检查证书是否已导入,您可以使用:
keytool -list -keystore /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/security/cacerts | grep aws

Post 如果有效,请返回。

此问题似乎与在相同别名下的 捆绑包 中导入证书的过程有关。

所以我不得不停止使用捆绑选项(rds-combined-ca-bundle.pem)并开始使用这个rds-ca-2019-root.pem

使用以下命令导入密钥库后:

keytool -importcert -trustcacerts -file rds-ca-2019-root.pem -alias rds19 -keystore rds-ca-certs -storepass keyStorePassword

已实现在 TLS 下与数据库的连接。

https://github.com/hungbang/spring-boot-aws-docdb 我创建了一个示例,用于从启用 SSL 的 spring 启动连接到 amazon docdb。