2

我正在 AWS 中创建新的 documentDb 集群,并尝试通过 MongoDriver 将我的 net.core 应用程序连接到它。具有启用 SSL 的属性的集群。

根据这个问题和答案,我尝试了几种方法来实现我的目标。

  • 将证书链导入本地计算机存储,导入受信任的根证书颁发机构rds-combined-ca-bundle.p7b
  • 阅读pem文件并在 C# 代码中创建证书,或在带有--sslCAFile参数的 mongoShell 中使用它。
var clientSetting = MongoClientSettings.FromUrl("mongodb://<myloging>:<mypassword>@<myclusterendpoint>/?ssl=true&replicaSet=rs0");

var setting = new MongoClientSettings()
{
    Server = clientSetting.Server,
    UseSsl = clientSetting.UseSsl,
    Credential = clientSetting.Credential,

    GuidRepresentation = GuidRepresentation.CSharpLegacy,
    ReadPreference = new ReadPreference(ReadPreferenceMode.Primary),
    VerifySslCertificate = true,
    SslSettings = new SslSettings
    {
        ClientCertificates = new List<X509Certificate2>()
        {
            new X509Certificate2("<path>\\rds-combined-ca-bundle.pem")
        },
        EnabledSslProtocols = System.Security.Authentication.SslProtocols.Default,
        CheckCertificateRevocation = true
    },
    ReplicaSetName = clientSetting.ReplicaSetName

};

setting.SslSettings.ClientCertificateSelectionCallback = (sender, host, certificates, certificate, issuers) => setting.SslSettings.ClientCertificates.ToList()[0];
setting.SslSettings.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

setting.MaxConnectionIdleTime = new TimeSpan(0, 0, 30);

client = new MongoClient(setting);

并这样做:

var filter = new BsonDocument("name", "mycollection");
var collectionCursor = client.GetDatabase("mydatabase").ListCollections(new ListCollectionsOptions { Filter = filter });
if (!collectionCursor.Any())
{
    throw new Exception("Collection not found");
}

我希望这会得到带有名称mycollectionCollection not found异常的集合,但是得到

A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/<myclusterendpoint>" }", EndPoint: "Unspecified/<myclusterendpoint>", State: "Disconnected", Type: "Unknown" }] }.

尝试通过 MongoShell 连接时出现同样的问题。也许问题出在不同的区域。示例:在 us-east-2 中创建的集群,我尝试从乌克兰连接。:)

UPD:假设我应该在一个 VPC 中连接到 DocumentDb 集群。

4

2 回答 2

3

我的问题在于设计对 AWS DocumentDB 的访问。有关从 VPC 访问数据库的更多信息

于 2019-04-16T13:40:45.543 回答
1

我看到一些你可能想看的东西:

于 2019-04-16T00:30:19.397 回答