User deleted & recreated during active session/connection

I noticed interesting behaviour that is at least common for Java MongoClient and Mongosh in combination with MongoDB server/db provided via Atlas MongoDB. It’s related to user deletion/recreation scenario during active session/connection.
It came out that after db user was accidentally deleted and then recreated manually via Atlas MongoDB console our application that is using Spring Mongo driver (effectively MongoClient from org.mongo.db.mongodb-driver-reactivestreams driver) was not able to recover and needed a restart.

It’s also possible to reproduce it using mongosh via following steps:

  1. Established mongosh session to Atlas Mongo DB where all commands are working fine.
  2. Removed Mongo DB user used by #1via Atlas console.
  3. After a while (time needed for Atlas MongoDB config population) mongosh #1 starts returning errors.
    • first cmd fails with “cannot find user account after reload”
    • every subsequent cmd fails with „user is not allowed to do action [XXX] on [CollectionYYY].
  4. Recreated the MongoDB user with the same password.
  5. Mongosh session #1 is still not able to successfully invoke cmds like “find” etc. and is returning the same errors (Atlas MongoDB config population is completed) but the session seems active in the terminal.
  6. Started another Mongosh session #2 and it can successfully connect to Atlas MongoDB and invoke cmds.
  7. Mongos session #1 is still not able successfully invoke commands but connection/session is still active.

Is it desired and documented behavior? It seems natural behaviour would be to recover from above scenario when user is recreated or at least drop existing connection/session created via Mongosh or Java MongoClient?

1 Like

Attaching image with two Mongsh sessions - on the left it’s a first session created before user deletion and on the right there is session that shows sessions created after user deletion & recreation.

Personally, for security reason, this is the behavior that I want.

Imagine you are connected as User1 that has read only privileges on Database2. For some reasons, malicious or error, User1 is delete and then created with a different set of privileges, for example write access. Unaware User1 may now wreck havoc in Database2.

I think it is better to know as soon as possible that something is wrong. You have the chance to make an assessment of the situation and take corrective measures.