I am trying to implement some restrictions on my MongoDB server:
Two databases on my server should be restricted regarding delete/drop operations - only a special user account should be allowed to do so. All the other database should be totally unrestricted (of course excluding the admin database):
I tried to model this situation using two users:
| database A & B | all the other databases |
---------------------------------------------------------
user a | read & write | read & write |
user b | read-only | read & write |
Making everybody read all databases is easy using the readAnyDatabase
role.
However modelling that user b can only read database A & B but has read & write access to all the other databases (including those databases that are created later on) gives me a headache.
How can this security model be implemented in MongoDB?
It is not possible.
You can combine multiple roles and inherit them from multiple databases, but:
-
You can find these paragraphs in mongodb authorization doc.
In order to give read write on all future databases, you need to set
readWriteAnyDatabase
role to userb. That means, you can't downgrade toread
role, for the A and B databases.I am afraid you need to set the roles manually for the new dbs.
First of all enable authentication in your
mongodb.conf
fileCreate a database
perm
for holding user permissions that we are going to create below.then create
userb
with read-only permissions forDatabaseA
andDatabaseB
userb
will only be allowed to readDatabaseA
andDatabaseB
rest all databases access to userb will beread-write
Now
userb
can login with below commandYou should use
instead of
auth = true
, as for the rest Rohit answer did the job.See also : https://stackoverflow.com/a/33325891/1814774