#1 2015-05-16 14:53:10

BBackSoon
Member
Registered: 2014-11-15
Posts: 41

Using official MongoDB Connection String

Hello,

currently the TMongoClient class only features one constructor, defined like this:

constructor Create(const Host: RawUTF8; Port: Integer=MONGODB_DEFAULTPORT;
      const SecondaryHostCSV: RawUTF8=''; const SecondaryPortCSV: RawUTF8=''); overload;

But, even though it's very easy to use, the above is somewhat limited.
Why not adding an overloaded constructor like the following:

constructor Create(const MongoDBConnString: RawUTF8); overload;

That would allow an expert user to create the TMongoClient object by passing a standard MongoDB connection string, as specified here:
http://docs.mongodb.org/manual/referenc … on-string/

You'd be able to do beautiful things like this:

 fClient := TMongoClient.Create('mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test&connectTimeoutMS=300000');

Offline

#2 2015-06-11 22:22:08

BBackSoon
Member
Registered: 2014-11-15
Posts: 41

Re: Using official MongoDB Connection String

No answer to this? I thought it was a good idea...

Offline

#3 2015-06-11 23:11:20

marius maximus
Member
From: Olsztyn
Registered: 2015-06-11
Posts: 30

Re: Using official MongoDB Connection String

In my opinion it is good idea but .....

... sometimes something you need do it yourself
it is open source wink


Lazarus x64 Linux

Offline

#4 2015-06-12 10:44:07

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Using official MongoDB Connection String

Yes, any input is welcome!

Offline

#5 2015-06-12 13:51:04

BBackSoon
Member
Registered: 2014-11-15
Posts: 41

Re: Using official MongoDB Connection String

Marius, I agree. And if you look at the source code you will actually find my contributions.
But this is a "delicate" addition, hence my request to the higher level person.

Offline

#6 2015-06-20 21:25:18

BBackSoon
Member
Registered: 2014-11-15
Posts: 41

Re: Using official MongoDB Connection String

Anyone willing to pick up this proposal?

Offline

#7 2015-07-10 23:13:11

Michael66
Member
Registered: 2015-07-10
Posts: 1

Re: Using official MongoDB Connection String

Hello,

I tested the MongoDB sample and it works just fine with a local MongoDB.

But I would also like to connect to a remote MongoDB on MongoLab in the following format:

"mongodb://<dbuser>:<dbpassword>@ds04542.mongolab.com:44542/mymongodb"

Does somebody have a solution or maybe a workaround for this?!

I tried a static change in Create method but as soon I assign a Database (fDB := fClient.Database['mymongodb'];) I get an exception "Query failure".

Thanks for any help...

Michael

Offline

#8 2015-07-17 10:40:16

zed
Member
From: Belarus
Registered: 2015-02-26
Posts: 105

Re: Using official MongoDB Connection String

Here is implementation of TMongoClient.OpenAuth method:

uses
SynCrypto;
...
function TMongoClient.OpenAuth(const DatabaseName, UserName,
  PassWord: RawUTF8): TMongoDatabase;
var res,bson: variant;
    err,nonce,key: RawUTF8;
begin
  if (self=nil) or (UserName='') or (PassWord='') then
    result := nil else begin
    result := TMongoDatabase(fDatabases.GetObjectByName(DatabaseName));
    if result=nil then begin // not already opened -> try now from primary host
      if not fConnections[0].Opened then begin
        fConnections[0].Open;
        // step 1
        bson := BSONVariant('{getnonce:1}');
        err := fConnections[0].RunCommand(DatabaseName,bson,res);
        if err<>'' then
          raise EMongoException.CreateUTF8('%.OpenAuth("%") step1 error: %',[self,DatabaseName,err]);
        // step 2
        nonce := _Safe(res)^.GetValueOrRaiseException('nonce');
        key := MD5(nonce + UserName + MD5(UserName + ':mongo:' + PassWord));
        bson := BSONVariant('{authenticate:1,user:?,nonce:?,key:?}',[],[UserName,nonce,key]);
        err := fConnections[0].RunCommand(DatabaseName,bson,res);
        if err<>'' then
          raise EMongoException.CreateUTF8('%.OpenAuth("%") step2 error: %',[self,DatabaseName,err]);
      end;
      result := TMongoDatabase.Create(Self,DatabaseName);
      fDatabases.AddObject(DatabaseName,result);
    end;
  end;
  if result=nil then
    raise EMongoException.CreateUTF8('%.Open("%") unknown DB',[self,DatabaseName]);
end;

Tested on localhost with MongoDB 2.6 and Delphi 2007.

Based on MongoDB docs: Implement Authentication in a Driver

Last edited by zed (2015-07-17 10:44:04)

Offline

#9 2015-07-17 12:39:20

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Using official MongoDB Connection String

Included in http://synopse.info/fossil/info/59481a85f9

Thanks a lot zed, for the patch!

Offline

#10 2015-07-17 21:13:54

zed
Member
From: Belarus
Registered: 2015-02-26
Posts: 105

Re: Using official MongoDB Connection String

Bad news: MongoDB 3.x has 5 (!) auth mechanisms, and MONGODB-CR that I implement is deprecated. Now default is SCRAM-SHA-1 and MongoLab use it.

I found implementations of all mechanisms in one place in PyMongo driver: auth.py and SCRAM is terrible.

Last edited by zed (2015-07-17 21:17:08)

Offline

#11 2015-07-18 06:53:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Using official MongoDB Connection String

Sounds not so difficult to implement.

See https://github.com/stijnsanders/TMongoW … oAuth3.pas

Offline

#12 2015-07-18 18:29:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Using official MongoDB Connection String

I've implemented SCRAM-SHA1 authentication.

See http://synopse.info/fossil/info/5e2e99181e

Feedback is welcome!

BTW, our implementation is much easier to follow (and also much faster) than TMongoWire unit (which would not be able to handle password bigger than 64 chars, I'm afraid).
I tried to stick to https://tools.ietf.org/html/rfc5802 official definition.

Offline

#13 2015-07-18 20:04:38

zed
Member
From: Belarus
Registered: 2015-02-26
Posts: 105

Re: Using official MongoDB Connection String

I test it with mongod 2.6, 3.0.4 and with MongoLab and it works! ab, you’re awesome.

Auto-detect authentication mechanism by server version is a good idea, but to be on the safe side, сan you make option to use MONGODB-CR with MongoDB 3.xx?

Offline

#14 2015-07-19 08:36:52

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Using official MongoDB Connection String

Indeed.
I've added a ForceMongoDBCR optional parameter to OpenAuth() method.
See http://synopse.info/fossil/info/f8536c7fba

Offline

#15 2015-07-19 08:51:45

zed
Member
From: Belarus
Registered: 2015-02-26
Posts: 105

Re: Using official MongoDB Connection String

Thanks a lot.

Offline

Board footer

Powered by FluxBB