AWS DocumentDB TLS与Java的连接

箱子

我在TLS / SSL上使用普通Java与我的DocumentDB集群连接时遇到问题

根据AWS文档,我遵循的过程是这样的:

.pem从AWS下载了文件,然后将其复制到我的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的情况下,AWS提供的Java代码(仅在密钥库配置中有所不同)起作用。

PD:我知道关于与AWS DocumentDB进行SSL / TLS连接还有很多其他问题,但是这些问题都没有专门解决我的问题。此外,我还检查了普通的mongodb TLS / SSL连接问题,并且过程有所不同,因此对我无效。

箱子

该问题似乎与使用相同别名捆绑包中导入证书的过程有关

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Python从AWS Lambda连接到DocumentDB

来自分类Dev

ExpressJS和AWS DocumentDB连接身份验证失败

来自分类Dev

从Java程序连接到documentdb

来自分类Dev

mongorestore在AWS DocumentDB上非常慢

来自分类Dev

AWS DocumentDb不支持mongodb 4.0

来自分类Dev

从Android连接到Azure DocumentDB

来自分类Dev

代理后面的DocumentDB Java SDK

来自分类Dev

Amazon DocumentDB是否支持BI连接器?

来自分类Dev

旋转密码时与documentdb的命令行连接

来自分类Dev

DocumentDB 子查询的性能是否足以进行 RDMS 样式连接?

来自分类Dev

AWS SimpleDb与Azure DocumentDb有何不同?两者与ElasticSearch有何不同

来自分类Dev

AWS放大使用documentdb或文档类型nosql数据库的工作

来自分类Dev

AWS Lambda Java,连接到MySQL RDS

来自分类Dev

Redis与DocumentDB?

来自分类Dev

Redis与DocumentDB?

来自分类Dev

Azure 是否支持像 AWS 速度模板一样将负载请求/响应映射到 documentDB?

来自分类Dev

在没有中间件的情况下将DocumentDB与SNS主题连接

来自分类Dev

EC2实例将不会连接到其他AZ中的DocumentDB。为什么?

来自分类Dev

DocumentDB:将TCP连接与客户端库> 1.9.0一起使用

来自分类Dev

是否可以使用JavaScript将documentDB存储连接到我的应用程序?

来自分类Dev

如何使用泛型在 Linq 中为 DocumentDB 创建多个/嵌套的 SelectMany 连接

来自分类Dev

连接AWS-Cyberduck

来自分类Dev

AWS VPN如何连接

来自分类Dev

连接AWS-Cyberduck

来自分类Dev

AWS VPN如何连接

来自分类Dev

Java中MQTT的TLS / SSL连接

来自分类Dev

使用某些JDK重置Java TLS连接

来自分类Dev

使用带有Raspberry Pi的基本AWS示例的“ iot_tls_connect L#143 TCP连接错误”

来自分类Dev

无法使用Java SSH连接到Amazon AWS实例

Related 相关文章

热门标签

归档