As a platform lead, how would you model MongoDB roles across many services on one deployment, and who may hold userAdmin?
answer
- contain by where roles live
- granularity against upkeep
- grant power equals total power
- more roles than userAdmin escalate
- definitions in version control
basics
~20 sGive each service its own user and a role defined in its own database, keep shared roles in admin, and confine user administration to a few guarded accounts, because userAdmin can grant its holder anything on its database, or deployment-wide on admin.
solid answer
~50 sI would give each service one user holding a role defined in that service's database, because the server then refuses to let that role reach anywhere else. Cross-cutting needs such as on-call and analytics get a small family of roles in `admin`. The real choice is granularity: built-ins are cheap but `readWrite` can drop collections, and collection-level roles are tight but every new collection means a role change. My default is a database-level custom role with data actions only, tightening the sensitive services further. `userAdmin` decides the rest. On a database, its holder can grant themselves anything there; on `admin`, like `userAdminAnyDatabase`, it is effectively superuser. So no service or on-call login holds it, only a few guarded accounts. `dbOwner` and `restore` get the same care, because they carry those actions. Role definitions live in version control and get compared against `rolesInfo`.
code
javascript · 8 linesuse shop
db.createRole({
role: "checkoutData",
privileges: [
{ resource: { db: "shop", collection: "" }, actions: ["find", "insert", "update", "remove"] }
],
roles: []
})go deeper
Recall that userAdmin can create users and grant roles on its database, which is why it is handed out rarely.
Explain that a role outside admin cannot leave its own database, and that this containment is why service roles belong in their service's database.
Identify every role that can escalate, including dbOwner, userAdmin on admin, userAdminAnyDatabase and restore, and check that no service or daily login holds one.
Own the tradeoff between tightness and upkeep, and say where you would accept each. Decide who holds grant power, how it is reviewed, and what you detect without auditing, then name what would change your mind.
## What is being decided Moving a shared replica set off one admin login is the easy part. The standing decision is how roles scale as services multiply, and who keeps the power to change them. There is no single right answer, because tighter roles cost upkeep, and that cost lands on whichever team owns them. ## Choosing the granularity | Model | Gain | Cost | |---|---|---| | Built-in `readWrite` per service database | No upkeep, and every engineer knows it | The service can `dropCollection` and `dropIndex` | | Database-level custom role, data actions only | New collections just work, and structural actions are gone | Structural changes need another identity | | Collection-level custom role | Smallest damage, clearest record of intent | Every new collection is a role change, tied to a release | | Shared family in `admin` | One definition for many users | Wide blast radius when a definition changes | I would default to the second row and move a few sensitive services, such as payments, to the third. Most services do not repay collection-level upkeep. Two server rules make this cheaper than it looks: - **Where a role lives is itself a control.** A role defined in `shop` cannot target another database or the cluster, and cannot inherit from elsewhere. A role in `admin` can do all of that. So keep service roles in their own databases, and treat `admin` as the place for the few cross-cutting roles. - **Inheritance works by reference.** A change to a shared base role reaches every role built on it, which is useful for a family and dangerous without review. For people, on-call inspects through `clusterMonitor` plus `killop` rather than a broad `clusterAdmin`. Remember too that `clusterMonitor` is not blind to data: its `read` on `local` includes the oplog. ## Who holds the power to grant The action that matters is `grantRole`, because whoever holds it can hand themselves anything it reaches. The server does not limit grants to what the granter already has. | Holding | Effective reach | |---|---| | `userAdmin` or `dbOwner` on `shop` | Anything on `shop`, and grants of `shop` roles to any user from any database | | `userAdmin` or `dbOwner` on `admin` | Effectively the whole deployment | | `userAdminAnyDatabase` | Effectively superuser, as the manual states | | `restore` | `createUser` and `grantRole` on ordinary databases, including `admin`, plus direct writes to `system.users` | | `root` | Everything directly | The `restore` row is the one teams miss. It is not on the manual's superuser list, but the action list and the role file both give it those actions. Treat a backup operator who holds `restore` as an administrator, and note that `backup` can read `system.users`, so it sees stored credential material. ## Standing rules I would set 1. No service account holds `userAdmin`, `dbOwner`, `restore` or `root`. A compromised service should not be able to widen its own access. 2. A guarded break-glass account holds `userAdminAnyDatabase`, with its credential held away from daily use and each login reviewed afterwards. 3. Automation that provisions roles uses an identity limited to what it manages, and is itself a high-value secret. If it holds `userAdmin` on `admin`, it is a superuser. 4. A team gets `userAdmin` on its own database only if it is already trusted with every document there, because that is what the role amounts to. 5. Definitions live in version control. `db.updateRole` replaces whole arrays, which suits declarative definitions, since the desired state overwrites whatever is there. 6. Administrative roles carry `authenticationRestrictions`, so holders can connect only from the addresses you expect. ## Detecting drift Declaring roles is only half of it. Someone with grant power can change them, so compare reality against the repository on a schedule: `usersInfo: { forAllDBs: true }` for users, and `rolesInfo` with `showPrivileges` on each database for roles. That shows state, not who changed it. Recording the act needs auditing, which the manual describes as a MongoDB Enterprise facility, so without it the comparison and the login record are what you have. ## When I would revisit this - **Service count outgrows review.** If role changes queue behind one team, delegate `userAdmin` per database, with the containment the scope rule gives. - **Tenants arrive.** Per-tenant databases change the arithmetic, since a role per tenant is a lot of definitions to maintain. - **Identity moves outside the database.** External authorization maps groups to roles in `admin`, which shifts who controls grants away from `grantRole` holders.
- Would you let a product team self-serve with userAdmin on its own database?Often yes, when the team is already trusted with every document in that database, since it can grant itself anything there. The blast radius is contained: `userAdmin` on `shop` cannot grant `admin` roles, and a role defined in `shop` cannot leave it. I would never extend this to `admin`, and I would still compare results against version control to catch accidental grants.
- How would you notice that someone with grant power had quietly widened a role?Compare a regular snapshot of `usersInfo` with `forAllDBs` and of `rolesInfo` with `showPrivileges` against the definitions in version control, and alert on any difference. That detects the changed state, not who did it. Attributing the act takes auditing, which the manual describes as a MongoDB Enterprise facility, or else a tightly limited set of grant holders whose logins you review.
- Why does restore need the same care as userAdmin, when it sounds like a backup operator's role?`restore` holds `createUser`, `grantRole` and the other user administration actions across ordinary databases, and the server lets that reach `admin`. It also has `insert` and `update` on `system.users`. So its holder can grant themselves `root`, although the manual's superuser list leaves it out. I would grant it only for the duration of a restore, to a guarded identity.
- userAdmin is safe to hand out because it cannot read any data itself.
- A holder of grantRole can only grant privileges it already has.
- userAdmin on a service database gives its holder control of the whole deployment.
- restore is a low-risk role because it only writes data back from a dump.
- Collection-level roles for every service are always the right default.