Cannot Connect to AWS Database using TLS with Server CA Validation

前端 未结 6 1850
余生分开走
余生分开走 2021-01-18 05:06

AWS documentation states that to connect to my DocumentDB Cluster, I need to use a query string that ends like so ?ssl_ca_certs=rds-combined-ca-bundle.pem&replicaS

6条回答
  •  遇见更好的自我
    2021-01-18 05:54

    Here is another way. However I found that by using SSL with the C# Mongo Driver doesn't do connection Pooling and opened a new connection for each call. You can reduce the active connections by including MaxConnectionIdleTime but it's still not ideal if your application creates a lot of connections.

        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()
            {
                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());
    
    

提交回复
热议问题