Connect to AWS DocumentDb in .Net Core through SSH Tunnel

Viewed 940

I've managed to connect both MongoDb Compass as well as Studio3T to my DocumentDb Cluster on AWS through an SSH tunnel. So theres no configuration or security issue on the VM or cluster.

However, when trying to connect using .NET core I keep getting timeouts.

I'm setting up the SSH tunnel as follows:

ssh -i "VMKey.pem" -L 27015:<db-name>.<cluster-name>.eu-central-1.docdb.amazonaws.com:27015 <user>@<vm-name>.eu-central-1.compute.amazonaws.com -N

And here is the code to connect:

string template = "mongodb://{0}:{1}@{2}/test?ssl=true&replicaSet=rs0&readpreference={3}";
string readPreference = "secondaryPreferred";
string connectionString = String.Format(template, username, password, "localhost:27015", readPreference);

var settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
settings.AllowInsecureTls = true;
var client = new MongoClient(settings);

var database = client.GetDatabase("logs-database");
_collection = database.GetCollection<BsonDocument>("logs-collection");

After trying to insert a record into the database, this is the error I am receiving:

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/<logs-db>.ckwpv9kdyp3i.eu-central-1.docdb.amazonaws.com:27015" }", EndPoint: "Unspecified/<logs-db>.ckwpv9kdyp3i.eu-central-1.docdb.amazonaws.com:27015", 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 (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. 172.31.41.178:27015
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)
   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.SslStreamFactory.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-01-28T15:11:45.2629877Z", LastUpdateTimestamp: "2021-01-28T15:11:45.2629880Z" }] }.

Note: TLS is disabled on the cluster

1 Answers

Please change the following and retry

  1. ssl=false
  2. remove replicaSet=rs0 : You will not be able to connect to Amazon DocumentDB as a replica set, using a SSH tunnel. Refer this link for details.

Here is the updated string

"mongodb://{0}:{1}@{2}/test?ssl=false&readpreference={3}"
Related