skip to content

In MongoDB, what exactly is a privilege, and how do you write its resource for a collection, a database and the cluster?

level: middleimportance: should knowfreq 42%

answer

  1. what is allowed, and where
  2. a document inside a role
  3. empty strings widen the scope
  4. system collections must be named
  5. cluster true for deployment actions

basics

~20 s

A MongoDB privilege is a resource plus the actions allowed on it. Write the resource as { db, collection } for a collection, with an empty collection for a database's non-system collections, or as { cluster: true } for deployment-wide actions.

solid answer

~40 s

A privilege pairs one resource with a list of actions, and users get privileges only through roles. `{ db: "shop", collection: "orders" }` names one collection. `{ db: "shop", collection: "" }` means every non-system collection in `shop`, so `system.profile` still has to be named. `{ db: "", collection: "orders" }` means every collection called `orders` in every database, `local` and `config` included, while `{ db: "", collection: "" }` means all non-system collections outside `local` and `config`. `{ cluster: true }` is for actions on the deployment, such as `shutdown` or `serverStatus`. The action has to suit the resource: `find` belongs on collections and `shutdown` on the cluster. A misspelled action is rejected when the role is created, but a real action on the wrong resource is generally accepted and then authorizes nothing.

code

javascript · 8 lines
javascript
use admin
db.createRole({
  role: "manageOps",
  privileges: [
    { resource: { cluster: true }, actions: ["killop", "inprog"] }
  ],
  roles: []
})

go deeper

for a junior

Recall the definition: a resource plus the actions allowed on it. Know the three everyday forms, which are a named collection, a database with an empty collection, and cluster true.

for a middle

Explain what empty strings really mean, including that system collections stay out of a database-level resource and that an empty db with a named collection includes local and config.

for a senior

Show the failure that hides: a real action on the wrong resource is generally accepted when the role is created and then authorizes nothing. Verify a new role by running the command, not by reading the role.

for a principal

Choose the smallest resource each action needs, and weigh it against upkeep. Narrow resources limit damage but need changing as collections appear, so decide where that maintenance is worth paying for.

## What a privilege is MongoDB authorization is built from three nouns: - An **action** is one operation the server can permit, such as `find`, `insert`, `createIndex` or `shutdown`. - A **resource** is what the action applies to: a collection, a database, a set of collections, or the cluster. - A **privilege** is one resource together with the list of actions permitted on it. Privileges live inside roles, and users hold roles. There is no command that hands a privilege straight to a user, so a one-off grant still means a role, even a very small one. ## Writing the resource A privilege is written as `{ resource: { ... }, actions: [ ... ] }`. The resource document has these forms: | Resource document | What it covers | |---|---| | `{ db: "shop", collection: "orders" }` | that one collection | | `{ db: "shop", collection: "" }` | every non-system collection in `shop` | | `{ db: "", collection: "orders" }` | every collection named `orders` in every database, including `local` and `config` | | `{ db: "", collection: "" }` | every non-system collection in every database except `local` and `config` | | `{ cluster: true }` | the deployment itself, for actions that affect system state | | `{ anyResource: true }` | everything; documented as internal, and not for normal use | Two of those rows carry traps. An empty `collection` does not mean literally everything in the database, because system collections are left out. An empty `db` with a named collection reaches somewhere the all-collections form never does, because it includes `local` and `config`. ## System collections need naming A collection counts as a system collection when it matches `<database>.system.*` or `local.replset.*`. Those are not covered by a database-level resource, so you name them: - `{ db: "shop", collection: "system.profile" }` for profiler output. - `{ db: "shop", collection: "system.js" }` for stored functions. `local.oplog.rs` matches neither pattern, which makes it an ordinary collection of `local`. That is why `read` on `local` includes the oplog, and why the all-collections form still does not reach it: that form skips `local` entirely. ## Actions must suit the resource Each action is meant for a kind of resource, and the privilege actions reference says which: `shutdown`, `killop`, `inprog` and `serverStatus` go on the cluster, `createUser` and `grantRole` on a database, and `find` or `insert` on database or collection resources. The server treats the two possible mistakes differently: 1. A misspelled action, such as `killOp` for the real `killop`, is an unrecognized action, and `db.createRole` refuses it. 2. A real action on the wrong kind of resource is generally accepted, because the system does not usually prohibit the combination. The privilege then authorizes nothing, since the server checks `shutdown` against the cluster resource and never finds a match. This second mistake is the costly one, because it fails later and quietly. ## How a request is matched When a command needs an action on an ordinary collection such as `shop.orders`, the server looks for that action across every pattern that could cover it: 1. Any resource. 2. All non-system collections, unless the database is `local` or `config`. 3. The database, `{ db: "shop", collection: "" }`. 4. The collection name in any database, `{ db: "", collection: "orders" }`. 5. The exact namespace. A system collection gets a much shorter list, with no database or all-collections patterns. That is the mechanism behind the rule above. A cluster-level action is checked only against the cluster and any-resource patterns, so no database grant can satisfy it. ## A worked example An analytics job on a shared replica set needs to read one collection, list what exists, and report server status. That spans three resources, so it takes three privileges in one role, created in `admin` because it reaches the cluster: ```javascript use admin db.createRole({ role: "analyticsReadout", privileges: [ { resource: { db: "shop", collection: "orders" }, actions: ["find"] }, { resource: { db: "shop", collection: "" }, actions: ["listCollections"] }, { resource: { cluster: true }, actions: ["serverStatus"] } ], roles: [] }) ``` Each line is the smallest resource its action needs. Nothing here allows reading another collection in `shop`, and nothing allows writes.

  • Why can't a user with find on { db: "shop", collection: "" } read shop.system.profile?
    A database-level resource excludes system collections, and the server does not even try that pattern for a namespace like `shop.system.profile`. Only an any-resource grant, the collection-name form or the exact namespace can match. So you add `{ db: "shop", collection: "system.profile" }`, or use a role such as `dbAdmin` that already names it.
  • What does { db: "", collection: "accounts" } reach that { db: "", collection: "" } does not?
    It matches a collection named `accounts` in every database, including `local` and `config`, whereas the all-empty form skips those two databases. It is also narrower in the other direction, because it covers only that one collection name. Teams use it for a collection that exists under the same name in many databases.
  • A role in admin lists shutdown on { db: "shop", collection: "" }. Does its holder gain shutdown?
    No. The server generally accepts that combination at creation, but `shutdown` is checked against the cluster resource, so a database-level entry never matches. The role looks correct in `rolesInfo` and the command is still refused. The fix is `{ resource: { cluster: true }, actions: ["shutdown"] }`.

  • A privilege can be handed directly to a user without any role.
  • An empty collection string covers shop.system.profile as well as the ordinary collections.
  • A resource with both db and collection empty also covers the local and config databases.
  • The cluster resource means every database and collection in the cluster.
  • Granting shutdown on the admin database lets a user shut the server down.