【问题标题】:AWS DocumentDB TLS connection with JavaAWS DocumentDB TLS 与 Java 的连接
【发布时间】:2020-05-01 11:42:18
【问题描述】:

我在使用基于 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: 异常发送消息

原因:javax.net.ssl.SSLHandshakeException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径

有趣的是,当我使用 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 的连接还有很多其他问题,但没有一个专门解决我的问题。此外,我还检查了普通的 mongodb TLS/SSL 连接问题,并且过程不同,因此它们对我无效。

【问题讨论】:

标签: aws-documentdb


【解决方案1】:

问题似乎与在 bundle 中以相同别名导入证书的过程有关。

所以我不得不停止使用捆绑选项(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下实现与数据库的连接。

【讨论】:

  • 你能解决这个问题真是太好了。这就说得通了。通常,您将只导入根 ca 证书,这样服务器就会显示链并且您信任它的根。
  • 感谢您的回答。我在尝试将我的 EMR Spark 集群连接到 documentdb 时遇到了困难,这解决了它。
  • 我很高兴@FábioJunqueira
  • @Arcones 你能分享完整的解决方案吗?步骤,文件夹结构和代码?我面临同样的问题,无法解决
  • 非常感谢您的回答。我很难找到解决办法。但是,我对这些根证书、中间证书、信任链感到困惑。如果有人有相同的参考下面的链接来掌握这方面的基本想法。 aboutssl.org/root-certificates-vs-intermediate-certificates/….
【解决方案2】:

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

然后你让keytool导入它默认的java keystore密码是改变它:

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

如果可行,请回帖。

【讨论】:

  • 不幸的是,这并没有解决问题。我最终打开了一个 AWS 支持案例,问题与以相同别名在捆绑包中导入证书的过程有关。我将发布帮助我解决它的程序。无论如何感谢您的帮助
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2020-04-21
    • 1970-01-01
    • 2022-06-27
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多