mongo connection exceeded when using csfle

401 Views Asked by At

Using Mongo Atlas M0 free tier.

I have recently implemented mongo client side field level encryption (csfle). The implementation was successful but problem started after that.

As par documentation, to implement csfle, we have to provide the connection setting when we create new mongoClient instance. The settings include KMS Provider, KeyVaultNamespace and Schemamap.

I wanted to implement encryption for three different collections, so i have to create new mongoclient instance everytime as the settings parameters are different.

I have implemented all this successfully but after deployment, i have started receiving mongo connection error.

Encryption related exception: A timeout occurred after 10000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }, OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "3", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 3, EndPoint : "Unspecified/localhost:27020" }", EndPoint: "Unspecified/localhost:27020", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
 ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (111): Connection refused 127.0.0.1:27020
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.Socket.<>c.<ConnectAsync>b__274_0(IAsyncResult iar)
--- End of stack trace from previous location where exception was thrown ---
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)", LastHeartbeatTimestamp: "2021-11-03T08:46:10.0467929Z", LastUpdateTimestamp: "2021-11-03T08:46:10.0467932Z" }] }.., stacktrace:    at MongoDB.Driver.Encryption.AutoEncryptionLibMongoCryptController.EncryptFieldsAsync(String databaseName, Byte[] unencryptedCommandBytes, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.WireProtocol.CommandMessageFieldEncryptor.EncryptFieldsAsync(String databaseName, CommandRequestMessage unencryptedRequestMessage, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.AutoEncryptFieldsIfNecessaryAsync(CommandRequestMessage unencryptedRequestMessage, IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocolAsync[TResult](IWireProtocol`1 protocol, ICoreSession session, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadOperationExecutor.ExecuteAsync[TResult](IRetryableReadOperation`1 operation, RetryableReadContext context, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.ReadCommandOperation`1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.FindCommandOperation`1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.FindOperation`1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.FindOperation`1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperationAsync[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
   at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)

Also i have started receiving Alerts from mongo atlas about the connections threshold "You are receiving this alert email because connections to your cluster(s) have exceeded 500, and is nearing the connection limit for the M0 cluster "

My question is:

  1. Is there a way we can create singleton instance of mongoClient while using csfle?
  2. Is there a way i can set the connection settings once i create the mongoClient instance. I tried but look like it is a readonly property.

Any help will be appreciated.

1

There are 1 best solutions below

9
On

Is there a way we can create singleton instance of mongoClient while using csfle?

yes, you just need to specify different collection in schemaMap, see how autoEncryptionSettings are set here (add a different collection into schemaMap dictionary)

Is there a way i can set the connection settings once i create the mongoClient instance. I tried but look like it is a readonly property.

no, you can set settings only before starting MongoClient, after that all settings are readonly

"Unspecified/localhost:27020"

the issue you have is related to the fact that mongocryptd (not mongo server) which is internal process for fle logic is unresponsible. I'm not sure why it happens, try changing schemaMap first and see whether it will help