Mongodb – Users and Roles in MongoDB

mongodbpermissionsusers

Following users are created in MongoDB.

First user created with name "user" having read write role for "project" database use project.

db.createUser(
    {
      user: "user",
      pwd: "user123",
      roles: [
         { role: "readWrite", db: "project" },
         { role: "readAnyDatabase", db:"admin" }
      ]
    }
)

Second user created with name "stud" having read write role for "student" database use student.

db.createUser(
    {
      user: "stud",
      pwd: "stud123",
      roles: [
         { role: "readWrite", db: "stud" },
         { role: "readAnyDatabase", db:"admin" }
      ]
    }
)

When I connected to database having name project using "user" I am getting the project as well as the student database and I'm also able to create new collection in the student database. I haven't give any permission to "user" to access the student database.

Still it is possible to access the student database. How can I avoid this?

Best Answer

You've given 'user' the readAnyDatabase permission. This means it has read access for every database in the cluster. Change the permission to readOnly so user will have read permission on admin DB only.