- Security >
- Security Reference >
- User Privilege Roles in MongoDB
User Privilege Roles in MongoDB¶
On this page
New in version 2.4: In version 2.4, MongoDB adds support for the following user roles:
Roles¶
Changed in version 2.4.
Roles in MongoDB provide users with a set of specific privileges, on
specific logical databases. Users may have multiple roles and may have
different roles on different logical database. Roles only grant
privileges and never limit access: if a user has read
and readWriteAnyDatabase permissions on
the records database, that user will be able to
write data to the records database.
Note
By default, MongoDB 2.4 is backwards-compatible with the MongoDB 2.2
access control roles. You can explicitly disable this
backwards-compatibility by setting the
supportCompatibilityFormPrivilegeDocuments option to
0 during startup, as in the following command-line invocation of
MongoDB:
In general, you should set this option if your deployment does not need to support legacy user documents. Typically legacy user documents are only useful during the upgrade process and while you migrate applications to the updated privilege document form.
See privilege documents and Delegated Credentials for MongoDB Authentication for more information about permissions and authentication in MongoDB.
Database User Roles¶
-
read¶ Provides users with the ability to read data from any collection within a specific logical database. This includes
find()and the following database commands:
-
readWrite¶ Provides users with the ability to read from or write to any collection within a specific logical database. Users with
readWritehave access to all of the operations available toreadusers, as well as the following basic write operations:insert(),remove(), andupdate().Additionally, users with the
readWritehave access to the following database commands:cloneCollection(as the target database.)convertToCappedcreate(and to create collections implicitly.)drop()dropIndexesemptycappedensureIndex()findAndModifymapReduce(output to a collection.)renameCollection(within the same database.)
Database Administration Roles¶
-
dbAdmin¶ Provides the ability to perform the following set of administrative operations within the scope of this logical database.
cleancollModcollStatscompactconvertToCappedcreatedb.createCollection()dbStatsdrop()dropIndexesensureIndex()indexStatsprofilereIndexrenameCollection(within a single database.)validate
Furthermore, only
dbAdminhas the ability to read thesystem.profilecollection.
-
userAdmin¶ Allows users to read and write data to the
system.userscollection of the user’s database. Users with this role will be able to modify permissions for existing users and create new users.userAdmindoes not restrict the permissions that a user can grant, and auserAdminuser can grant privileges to themselves or other users in excess of theuserAdminusers’ current privileges.Important
userAdminis effectively the superuser role for a specific database. Users withuserAdmincan grant themselves all privileges. However,userAdmindoes not explicitly authorize a user for any privileges beyond user administration.Note
The
userAdminrole is a database-specific privilege, and only grants a user the ability to administer users on a single database. However, for theadmindatabase,userAdminallows a user the ability to gainuserAdminAnyDatabase. Thus, for theadmindatabase only, these roles are effectively the same.
Administrative Roles¶
-
clusterAdmin¶ clusterAdmingrants access to several administration operations that affect or present information about the whole system, rather than just a single database. These privileges include but are not limited to replica set and sharded cluster administrative functions.clusterAdminis only applicable on theadmindatabase, and does not confer any access to thelocalorconfigdatabases.Specifically, users with the
clusterAdminrole have access to the following operations:addShardcloseAllDatabasesconnPoolStatsconnPoolSync_cpuProfilerStart_cpuProfilerStopcursorInfodiagLoggingdropDatabaseenableShardingflushRouterConfigfsyncdb.fsyncUnlock()getCmdLineOptsgetLoggetParametergetShardMapgetShardVersionhostInfodb.currentOp()db.killOp()listDatabaseslistShardslogRotatemoveChunkmovePrimarynetstatremoveShardrepairDatabasereplSetFreezereplSetGetStatusreplSetInitiatereplSetMaintenancereplSetReconfigreplSetStepDownreplSetSyncFromresyncserverStatussetParametersetShardVersionshardCollectionshardingStateshutdownsplitChunksplitVectorsplittoptouchunsetSharding
For some cluster administration operations, MongoDB requires read and write access to the
localorconfigdatabases. You must specify this access separately fromclusterAdmin. See the Combined Access section for more information.
Any Database Roles¶
Note
You must specify the following “any” database roles on the
admin databases. These roles apply to all databases in a
mongod instance and are roughly equivalent to their
single-database equivalents.
If you add any of these roles to a user privilege document outside of the admin
database, the privilege will have no effect. However, only the
specification of the roles must occur in the admin database,
with delegated authentication credentials, users can gain these privileges by
authenticating to another database.
-
readAnyDatabase¶ readAnyDatabaseprovides users with the same read-only permissions asread, except it applies to all logical databases in the MongoDB environment.
-
readWriteAnyDatabase¶ readWriteAnyDatabaseprovides users with the same read and write permissions asreadWrite, except it applies to all logical databases in the MongoDB environment.
-
userAdminAnyDatabase¶ userAdminAnyDatabaseprovides users with the same access to user administration operations asuserAdmin, except it applies to all logical databases in the MongoDB environment.Important
Because users with
userAdminAnyDatabaseanduserAdminhave the ability to create and modify permissions in addition to their own level of access, this role is effectively the MongoDB system superuser. However,userAdminAnyDatabaseanduserAdmindo not explicitly authorize a user for any privileges beyond user administration.
-
dbAdminAnyDatabase¶ dbAdminAnyDatabaseprovides users with the same access to database administration operations asdbAdmin, except it applies to all logical databases in the MongoDB environment.
Combined Access¶
Some operations are only available to users that have multiple roles. Consider the following:
sh.status()- Requires
clusterAdminandreadaccess to theconfigdatabase. applyOps,eval[1]- Requires
readWriteAnyDatabase,userAdminAnyDatabase,dbAdminAnyDatabaseandclusterAdmin(on theadmindatabase.)
Some operations related to cluster administration are not available to
users who only have the clusterAdmin role:
rs.conf()- Requires
readon thelocaldatabase. sh.addShard()- Requires
readWriteon theconfigdatabase.
| [1] | The mongo shell provides
db.eval() as a helper for the eval command.
As a wrapper, db.eval() requires the same privileges. |