AWS文档声明要连接到我的DocumentDB集群,我需要使用一个像?ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0
那样结束的查询字符串。我的客户应该验证的It is a root certificate chain。我不应该需要一个Client Certificate。
使用MongoDB C#驱动程序和此特定查询,在同一目录中使用.pem
文件,我无法建立连接。如果我使用相同的.pem
文件和Mongo Shell中的查询字符串,我可以正确连接到我的数据库。它只能在我的.net核心应用程序中运行,该应用程序也可以在AWS上运行。
通过从群集中删除TLS并从查询中删除ssl_ca_certs
选项,我可以正确连接到我的群集。
我以为我可以使用.pem
将我的.pfx
文件转换为openssl
,但我必须给.pfx
一个密码和MongoDB documentation states that
在使用密码加载证书时,必须使用PrivateKey属性为空。如果该属性为null,则表示您的证书不包含私钥,并且不会传递给服务器。
如何使用C#MongoDB驱动程序使用the .pem
file provided by Amazon AWS连接到我的数据库?
尝试将RDS CA文件添加到C#trust store中。
X509Store store = new X509Store(StoreName.Root);
X509Certificate2 ca = new X509Certificate2(<path_to_rds-combined-ca-bundle.pem>);
try {
store.Open(OpenFlags.ReadWrite);
store.Add(ca);
} catch (Exception ex) {
Console.WriteLine("Root certificate import failed: " + ex.Message);
throw;
} finally {
store.Close();
}
- >首先,通过将参数tls设置为“enabled”,在Document DB集群上启用SSL。确保重新引导群集的writer节点以重新引导整个群集,以便应用参数组更改。默认情况下,如果启动了新的Doc DB群集,则会启用TLS。
- >在您的环境中设置SSL证书:
1)从以下链接下载源Windows机器上的PKCS#7 SSL证书:
https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.p7b
2)单击“开始”菜单,单击“运行”,然后键入mmc
3)在MMC中,文件 - >添加/删除管理单元。
4)从管理单元列表中选择“证书”,然后单击“添加”。
5)受信任的CA证书应该进入本地计算机商店,因此选择“计算机帐户”单选按钮,单击“下一步”,然后选择“本地计算机”。单击Next,然后单击Finish。
6)现在从左侧窗格(在控制台根目录下,您将看到'证书'选项。单击它。
7)将出现一个列表,右键单击“受信任的根证书颁发机构”,然后选择“所有任务” - >“导入”
8)在打开的窗口中,单击“下一步”,浏览在步骤1中下载的证书(.p7b)文件(如果找不到,请从文件类型下拉列表中选择“所有文件”),然后继续单击Next,最后单击Finish。然后保存配置。
- >然后写下面的代码:
---------------------------------------------------
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace FirstDocDB
{
public class Program
{
public static void Main(string[] args)
{
var connectionString = "mongodb://pulkit:password@ClusterID:27017/?ssl=true&sslVerifyCertificate=true&replicaSet=rs0";
var client = new MongoClient(connectionString);
var database = client.GetDatabase("test");
var collection = database.GetCollection("stuff");
var document = collection.Find(new BsonDocument()).FirstOrDefault();
Console.WriteLine(document.ToString());
}
}
}
---------------------------------------------------
- >在构建和运行之后,我成功地将名为“stuff”的集合中的文档作为输出:{“_ id”:ObjectId(“5c5a63b10cf861158c1d241c”),“hello”:“world”}
因此,按照上述步骤后,我成功地使用Mongo驱动程序连接到文档数据库.Net。
我有类似的问题,用AWS打开了一张票,它的解决方法与Pulkit Agarwal的回答类似。
主要更改是连接字符串,即使将证书添加到本地存储后,我仍然使用查询字符串作为“?ssl_ca_certs = rds-combined-ca-bundle.pem&replicaSet = rs0”,需要将其更改为“?ssl = true&sslVerifyCertificate = true&replicaSet = RS0"
这是另一种方式。但是我发现通过使用SSL与C#Mongo驱动程序不进行连接池并为每个调用打开一个新连接。您可以通过包含MaxConnectionIdleTime来减少活动连接,但如果您的应用程序创建了大量连接,它仍然不理想。
var connectionString = "username:password@cluster_endpoint:27017/?replicaSet=rs0";
var clientSettings = MongoClientSettings.FromUrl(new MongoUrl("mongodb://" + connectionString));
var certificatePath = "ssl\rds-combined-ca-bundle.pem";
var pem = System.IO.File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + certificatePath);
byte[] certBuffer = GetBytesFromPEM(pem, "CERTIFICATE");
clientSettings.UseSsl = true;
clientSettings.SslSettings = new SslSettings()
{
ClientCertificates = new List<X509Certificate2>()
{
new X509Certificate2(certBuffer)
},
EnabledSslProtocols = System.Security.Authentication.SslProtocols.Default,
CheckCertificateRevocation = true
};
clientSettings.VerifySslCertificate = true;
clientSettings.SslSettings.ClientCertificateSelectionCallback = (sender, host, certificates, certificate, issuers) => clientSettings.SslSettings.ClientCertificates.ToList()[0];
clientSettings.SslSettings.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
clientSettings.MaxConnectionIdleTime = new TimeSpan(0, 0, 30);
_client = new MongoClient(clientSettings);
_database = _client.GetDatabase(db.ToString());
以下是如何使用C#(和其他驱动程序)以编程方式连接到Amazon DocumentDB并同时启用/禁用TLS的示例。
https://docs.aws.amazon.com/documentdb/latest/developerguide/connect.html