balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.83k stars 1.95k forks source link

Using CosmosDB instead of MongoDB cause Error #6964

Open jnahlovsky opened 4 years ago

jnahlovsky commented 4 years ago

Hi, is there any chance to get CosmosDB working under Waterline ORM and sails-mongo adapter? I just tried to only change the connection string with provided one from MS Azure pointing to CosmosDB instance, but some error shows up. Thanks for any information.

{"message":"A hook (orm`) failed to load!","level":"error","timestamp":"2020-03-27T15:34:04.327Z"} {"level":"error","message":"Could not tear down the ORM hook. Error details:","timestamp":"2020-03-27T15:34:04.329Z"} error: error: Error: Consistency violation: Unexpected error creating db connection manager:


ImplementationError: Internal error occurred while running `createManager`.  Got non-Error: { MongoError: connection 0 to mal-ms-tmdb.documents.azure.com:10255 timed out
    at Function.MongoError.create (/Users/jn/Work/MALL/messaging/mg-ms-tmbe/node_modules/mongodb-core/lib/error.js:29:11)
    at Socket.<anonymous> (/Users/jn/Work/MALL/messaging/mg-ms-tmbe/node_modules/mongodb-core/lib/connection/connection.js:188:20)
    at Object.onceWrapper (events.js:273:13)
    at Socket.emit (events.js:182:13)
    at Socket.EventEmitter.emit (domain.js:442:20)
    at Socket._onTimeout (net.js:449:8)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)
  name: 'MongoError',
  message:
   'connection 0 to mal-ms-tmdb.documents.azure.com:10255 timed out' }`
sailsbot commented 4 years ago

@VonSandberg Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

jnahlovsky commented 4 years ago

If it will be necessary to provide further info (system, versions, etc.), just let me know. Peace

johnabrams7 commented 4 years ago

@VonSandberg Are you entering your DB credentials from the command-line or similar? Sure, more version info could help: what versions of Sails, sails-mongo, and CosmosDB?

jnahlovsky commented 4 years ago

Here is more info (and sorry for the delay).

connection string I used mongodb://<USER>:<PWD>@my-project.documents.azure.com:10255/db-name?ssl=true&replicaSet=globaldb

sails: 1.2.4 sails-mongo: 1.0.1 mongodb: 2.2.25

Azure's CosmoDB was 3.6 I think (their latest supported version of MongoDB).

It is possible that I just missed something and somebody already figure that out.

a1dxn commented 4 years ago

Hi @VonSandberg @johnabrams7,

I'm a newbie to sails.js and cosmosDB and I encountered this error myself... Luckily from ruffling through the sails-mongo code and reading up on the cosmosDB and mongoDB driver docs I've solved it. Hopefully this can solve your issue too as it looks to be the same!

Cosmos DB requires authentication and secure communication via TLS, and the connection string cosmosDB provides is in the format mongodb://username:password@host:port/[database]?ssl=true&replicaSet=globaldb

So I initially setup my configuration as default : { adapter: 'sails-mongo', url: 'the one used above with my info' } Didn't work :(

I could access my db using 3T, so I knew that the string worked, and the error which sails gave me of connection 0 to XXX timed out meant that the problem stemmed from the connection being used by sails... I then found this issue logged elsewhere here, heavily meantioning the SSL statement, made me think about those added url queries... After looking into the sails-mongo code, I saw a file called config-whitelist.constant.js and it had options matching ones listed here in the mongoDB driver documentation including SSL and replicaSet - both of which are included in the url that cosmosDB provided...

TL;DR

I updated my datastore configuration to: default: { adapter : 'sails-mongo', url : 'mongodb://username:password@host:port', database : 'database-name', ssl : true, replicaSet : 'globaldb', autoReconnect : true, } And it worked! Hooray! Fingers crossed this may solve your issue too 👍

Aidan