nightroman / Mdbc

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

DNS Resolution Failure to MongoDb Atlas #69

Closed twilcomgistix closed 2 years ago

twilcomgistix commented 2 years ago

When connecting to my Atlas instance with Mdbc Powershell module, I get the following error response:

I've redacted part of the hostname

PSMessageDetails      :
Exception             : DnsClient.DnsResponseException: Query 27847 => [REDACTED].mongodb.net IN TXT on
                        10.0.8.228:53 failed with an error. ---> System.IO.FileNotFoundException: Could not load file
                        or assembly 'System.Buffers, Version=4.0.2.0, Culture=neutral,
                        PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file
                        specified.
                           at DnsClient.Internal.PooledBytes..ctor(Int32 length)
                           at DnsClient.DnsDatagramWriter..ctor()
                           at DnsClient.DnsUdpMessageHandler.Query(IPEndPoint server, DnsRequestMessage request,
                        TimeSpan timeout)
                           at DnsClient.LookupClient.ResolveQuery(IReadOnlyList`1 servers, DnsQuerySettings settings,
                        DnsMessageHandler handler, DnsRequestMessage request, LookupClientAudit audit)
                           --- End of inner exception stack trace ---
                           at DnsClient.LookupClient.ResolveQuery(IReadOnlyList`1 servers, DnsQuerySettings settings,
                        DnsMessageHandler handler, DnsRequestMessage request, LookupClientAudit audit)
                           at DnsClient.LookupClient.QueryInternal(DnsQuestion question, DnsQuerySettings
                        queryOptions, IReadOnlyCollection`1 servers)
                           at MongoDB.Driver.Core.Misc.DnsClientWrapper.ResolveTxtRecords(String domainName,
                        CancellationToken cancellationToken)
                           at MongoDB.Driver.Core.Configuration.ConnectionString.Resolve(Boolean resolveHosts,
                        CancellationToken cancellationToken)
                           at MongoDB.Driver.MongoUrl.Resolve(Boolean resolveHosts, CancellationToken
                        cancellationToken)
                           at MongoDB.Driver.MongoClientSettings.FromUrl(MongoUrl url)
                           at Mdbc.Commands.ConnectCommand.BeginProcessing()
                           at System.Management.Automation.Cmdlet.DoBeginProcessing()
                           at System.Management.Automation.CommandProcessorBase.DoBegin()
TargetObject          :
CategoryInfo          : NotSpecified: (:) [Connect-Mdbc], DnsResponseException
FullyQualifiedErrorId : DnsClient.DnsResponseException,Mdbc.Commands.ConnectCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, K:\DattoSpy\Powershell\MongoDb-Atlas\Connect-MongoDbAtlas.ps1: line 5
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

Past issues that appear to show similar error responses appeared to be resolved by a module version update but I'm on the latest:

    Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands                                                                                                                                      
---------- -------    ----                                ----------------                                                                                                                                      
Binary     6.5.11     Mdbc                                {Add-MdbcCollection, Add-MdbcData, Connect-Mdbc, Export-MdbcData...}                                                                                  

Here is the source for the script I'm running with redacted username, password, and part of hostname:

Import-Module Mdbc

$uri = "mongodb+srv://[username]:[password]@[hostname].mongodb.net/bdrSpy?retryWrites=true&w=majority"
Try {
    Connect-Mdbc -ConnectionString $uri -DatabaseName "bdrSpy" -CollectionName "tests"
} Catch {
    $_ | Select *
}

Get-MdbcData -As PS | Format-Table

I'm able to connect to this database without issue using MongoDB Compass in Windows.

I'm not super new to Powershell and Windows but I certainly have a greater comfort with nix-based environments. Not sure which part of the error response I could key in on to find the fix.

nightroman commented 2 years ago

DLL hell...

Anyways, I've just pushed Mdbc v6.5.12 (updated C# driver to the latest 2.13.2). Try again. But System.Buffers in the package is still the same:

System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51

Currently I have no idea why this happens. I do not know why Version=4.0.2.0 is required in your scenario.

What is the PowerShell version with the problem? See $PSVersionTable.

twilcomgistix commented 2 years ago

Here's my $PSVersionTable


Name                           Value                                                                                                                                                                            
----                           -----                                                                                                                                                                            
PSVersion                      5.1.22000.282                                                                                                                                                                    
PSEdition                      Desktop                                                                                                                                                                          
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                          
BuildVersion                   10.0.22000.282                                                                                                                                                                   
CLRVersion                     4.0.30319.42000                                                                                                                                                                  
WSManStackVersion              3.0                                                                                                                                                                              
PSRemotingProtocolVersion      2.3                                                                                                                                                                              
SerializationVersion           1.1.0.1                                                                                                                                                                          
PSMessageDetails      : 
Exception             : DnsClient.DnsResponseException: Query 40781 =>[REDACTED].mongodb.net IN TXT on 
                        10.10.10.11:53 failed with an error. ---> System.IO.FileNotFoundException: Could not load file 
                        or assembly 'System.Buffers, Version=4.0.2.0, Culture=neutral, 
                        PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file 
                        specified.
                           at DnsClient.Internal.PooledBytes..ctor(Int32 length)
                           at DnsClient.DnsDatagramWriter..ctor()
                           at DnsClient.DnsUdpMessageHandler.Query(IPEndPoint server, DnsRequestMessage request, 
                        TimeSpan timeout)
                           at DnsClient.LookupClient.ResolveQuery(IReadOnlyList`1 servers, DnsQuerySettings settings, 
                        DnsMessageHandler handler, DnsRequestMessage request, LookupClientAudit audit)
                           --- End of inner exception stack trace ---
                           at DnsClient.LookupClient.ResolveQuery(IReadOnlyList`1 servers, DnsQuerySettings settings, 
                        DnsMessageHandler handler, DnsRequestMessage request, LookupClientAudit audit)
                           at DnsClient.LookupClient.QueryInternal(DnsQuestion question, DnsQuerySettings 
                        queryOptions, IReadOnlyCollection`1 servers)
                           at MongoDB.Driver.Core.Misc.DnsClientWrapper.ResolveTxtRecords(String domainName, 
                        CancellationToken cancellationToken)
                           at MongoDB.Driver.Core.Configuration.ConnectionString.Resolve(Boolean resolveHosts, 
                        CancellationToken cancellationToken)
                           at MongoDB.Driver.MongoUrl.Resolve(Boolean resolveHosts, CancellationToken 
                        cancellationToken)
                           at MongoDB.Driver.MongoClientSettings.FromUrl(MongoUrl url)
                           at Mdbc.Commands.ConnectCommand.BeginProcessing()
                           at System.Management.Automation.Cmdlet.DoBeginProcessing()
                           at System.Management.Automation.CommandProcessorBase.DoBegin()
TargetObject          : 
CategoryInfo          : NotSpecified: (:) [Connect-Mdbc], DnsResponseException
FullyQualifiedErrorId : DnsClient.DnsResponseException,Mdbc.Commands.ConnectCommand
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, K:\DattoSpy\Powershell\MongoDb-Atlas\Connect-MongoDbAtlas.ps1: line 5
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

I haven't yet tried this in Windows 10, or the container environment where my final application will execute. Could there be some component in my Windows environment I'm lacking? I don't do dotnet/C++ dev so I don't have full Visual Studio or any of the other components typical to those development environments.

Edit The DNS server address in the error above is different because I was VPN connected. Disconnecting that VPN didn't help. Just wanted to make sure I ruled that out.

nightroman commented 2 years ago

FYI https://stackoverflow.com/questions/63019110/could-not-load-file-or-assembly-system-buffers-version-4-0-2-0

twilcomgistix commented 2 years ago

Oh man....this is gonna be a rabbit hole. I'm gonna close this one. This seems to be out of scope for Mdbc.

Thanks for your time!

wci-lrjackson commented 1 year ago

I ran into this same issue and poked at it until I found something that worked (though I'm not sure if it's a best practice). On Atlas, if you change your connection driver to C# / .NET and select 2.4 or later, it gives you a mongodb:// protocol connection string with all the shards. This seems to get around the issues caused by the newer mongodb+srv:// protocol.

nightroman commented 1 year ago

@wci-lrjackson Thank you. The latest Mdbc uses C# driver 2.19.0, i.e. definitely "2.4 or later". I am not sure if it's a best practice either, but I think this tip makes sense.