nightroman / Mdbc

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

Connection via X509 not possible #73

Closed PixaGitHub closed 1 year ago

PixaGitHub commented 1 year ago

Hello together,

we are using the Mdbc package to connect to our MongoDB by using SCRAM authentication. To make this way more secured we wanted to switch to a certificate based x.509 authentication. Now the Mdbc package is not connecting anymore with the new connection string. The connetion via the MongoShell or MongoCompass with the same connection string is working fine. My questions:

KR Philip

nightroman commented 1 year ago

I have just released Mdbc v6.6.2 with the updated C# driver 2.18.0. I do not think it will help but try the latest module, just in case.

Is the x509 authentication not supported by your package?

Honestly, I do not know. Mdbc does not support anything itself, Mdbc uses the C# driver and exposes some of its basic methods in PowerShell friendly way.

Some functionality might be available in the included C# driver. But not all of it is exposed by Mdbc, this was never a goal.

NB I am not a MongoDB guru, especially remote. I use MongoDB 100% locally.

Do we need to follow exactly this connection string format? mongodb://[username:password@]hostname[:port][/[database][?options]]

I think currently it's the only way, if you use Connect-Mdbc.

Below is the alternative possible way using MongoClientSettings and MongoClient directly. Try it, in theory MongoClientSettings should give you the best control possible.

NB I will eventually add this way to Connect-Mdbc, more likely.

Import-Module Mdbc

### Use C# driver MongoClientSettings, "inconvenient" but full control

$set = [MongoDB.Driver.MongoClientSettings]::new()
$set.Server = [MongoDB.Driver.MongoServerAddress]::new('localhost', 27017)

# show available settings, set some
# $set | Format-List | Out-String
# $set.Xyz = ...

# connect and get database "manually"
$client = [MongoDB.Driver.MongoClient]::new($set)
$database = $client.GetDatabase('test')

### Now use Mdbc

# get collection by Mdbc
$collection = Get-MdbcCollection test -NewCollection

# add and get data by Mdbd
@{_id=1; name='John'} | Add-MdbcData
Get-MdbcData
nightroman commented 1 year ago

Any progress and luck with the suggested way? It looks promising. Here is API docs https://mongodb.github.io/mongo-csharp-driver/2.18/apidocs/html/T_MongoDB_Driver_MongoClientSettings.htm

PixaGitHub commented 1 year ago

Hi Roman,

Thank you for your reply and suggestion on my question. Yeah I already went through the documentation and tried to set up a new script with the c# driver methods as you proposed. But I am still on it. The way how the certificates are passed there is different. I first have to figure that out.

nightroman commented 1 year ago

Another possible way is using MongoUrl and related MongoUrlBuilder for creating the client, similar to using settings. https://mongodb.github.io/mongo-csharp-driver/2.18/apidocs/html/T_MongoDB_Driver_MongoUrl.htm https://mongodb.github.io/mongo-csharp-driver/2.18/apidocs/html/T_MongoDB_Driver_MongoUrlBuilder.htm

nightroman commented 1 year ago

I have just released Mdbc v6.6.3 with the new parameters Settings and Url added to Connect-Mdbc. This should make connecting the client easier with all options available.

I think this is all that Mdbc is practically designed for. I'm closing the issue.