nightroman / Mdbc

MongoDB Cmdlets for PowerShell
Apache License 2.0
141 stars 16 forks source link

Can't connect to mongoDB with TLS enabled #84

Closed LindnerBrewery closed 11 months ago

LindnerBrewery commented 11 months ago

Just set a mongoDB server with a self-signed certificate and TLS enabled.

First I execute: connect-mdbc "mongodb://admin:Test123!@localhost:27017/?tls=true&tlsAllowInvalidCertificates=true" Then when calling Get-MdbcDatabase I get following error:

Get-MdbcDatabase : A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors =
MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange =
00:00:00.0150000 }, OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "1", Type :
"Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27017"
}", EndPoint: "Unspecified/localhost:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: ,
TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception
occurred while opening a connection to the server. ---> System.Security.Authentication.AuthenticationException: The
remote certificate is invalid according to the validation procedure.
   at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest,
Exception exception)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest
asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest
asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest
asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest
asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest
asyncRequest, Boolean renegotiation)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at MongoDB.Driver.Core.Connections.SslStreamFactory.CreateStream(EndPoint endPoint, CancellationToken
cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelper(CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelper(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.Open(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnection(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.ServerMonitor.Heartbeat(CancellationToken cancellationToken)",
LastHeartbeatTimestamp: "2023-11-08T15:47:15.1216746Z", LastUpdateTimestamp: "2023-11-08T15:47:15.1216746Z" }] }.
At line:1 char:1
+ Get-MdbcDatabase
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-MdbcDatabase], TimeoutException
    + FullyQualifiedErrorId : System.TimeoutException,Mdbc.Commands.GetDatabaseCommand

Using the same connection string with mongosh.exe works fine. After turning off TLS, which I don't want to, MDBC can connect without any issues.

I'm using MDBC 6.6.5

LindnerBrewery commented 11 months ago

Solved the issue. &tlsInsecure=true also has to be added to the connection string, which is not needed when using mongosh.exe.

nightroman commented 11 months ago

Solved the issue. &tlsInsecure=true also has to be added to the connection string, which is not needed when using mongosh.exe.

Great! Thank you for reporting the issue and posting the solution.