orlandos-nl / MongoKitten

Native MongoDB driver for Swift, written in Swift
MIT License
715 stars 101 forks source link

Can't connect to MongoDB #263

Closed jjabo closed 3 years ago

jjabo commented 3 years ago

The connection to mongodb fails, using vapor 4, the fluent-mongo-driver and the connection string mongodb://UserAdmin:ABCDEF@localhost:27017/admin Following error is produced in MongoKitten: [ ERROR ] Connection to Host(hostname: "localhost", port: 27017) disconnected with error MongoGenericErrorReply(ok: 0, errorMessage: Optional("BSON field \'saslContinue.conversationId\' is the wrong type \'long\', expected type \'int\'"), code: Optional(14), codeName: Optional("TypeMismatch"))

The connection works using e.g. MongoDB Compass. I tried as mechanisms SCRAM-SHA-1, SCRAM-SHA-256 and auto. If I disable the authorization the connection works.

The mongodb user account looks as follows: [ { _id: 'admin.UserAdmin', userId: UUID("5f48a642-221b-4399-bfe5-2524587d7d5a"), user: 'UserAdmin', db: 'admin', roles: [ { role: 'readWriteAnyDatabase', db: 'admin' }, { role: 'userAdminAnyDatabase', db: 'admin' } ], mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ] } ]

Environment:

Joannis commented 3 years ago

Thanks for the heads up! This is a super simple fix, but I'm surprised they changed the SASL mechanism in this way.

Joannis commented 3 years ago

Can you try the latest master branch

Joannis commented 3 years ago

I haven't updated my environments just yet, as I just had a vacation. I'll be able to better evaluate this once I'm all updated.

jjabo commented 3 years ago

I checked it against the master/6.0 branch and it seems fixed! However, the first connection takes about 10-15 seconds - is that expected behavior?

This was the fastest response and fix ever - Thank you!

Joannis commented 3 years ago

@jjabo You're welcome! On DEBUG mode, the password hashing used to authenticate to MongoDB is unfortunately quite slow. Swift is extremely unoptimised in DEBUG compilation. You'll notice a significant performance improvement on RELEASE builds, which is what production must always be compiled on. The credential proof is only computed once, and cached after that. However, inbetween launches this cache is lost. You can, if you want to, save that cache to the disk & load it again the next boot. If you're interested in that, please poke me on Discord :)

jjabo commented 3 years ago

Thanks again. That is super helpful info and advice! :) Looking forward to the next tag/release including the bugfix.

Joannis commented 3 years ago

Tagged!