skip to content

For MongoDB logins, what should a team standardise on for humans and for services, and how do you rotate service passwords without an outage?

level: principalimportance: should knowfreq 31%

answer

  1. edition limits the menu
  2. who owns the identity
  3. one credential per user per mechanism
  4. two users overlap, one user cannot
  5. what 9.0 says about LDAP

basics

~20 s

Edition sets the menu: Community has SCRAM and X.509, and Enterprise adds OIDC, Kerberos and deprecated LDAP. Prefer central identity for humans and certificates or workload tokens for services. Rotate passwords with a second user, because one user cannot hold two passwords.

solid answer

~50 s

It starts with edition: Community offers SCRAM and X.509, while Enterprise adds Kerberos, LDAP proxy and OIDC. LDAP has been deprecated since 8.0, with removal promised in a future release, so I would not build anything new on it. For humans I want identity owned by the company's directory, so offboarding is one action: OIDC Workforce Identity Federation on Enterprise, otherwise named SCRAM users. For services I prefer no long-lived password: Workload Identity Federation or X.509, with SCRAM as the plain baseline. To rotate a SCRAM password, remember that one user holds one password, so changing it in place breaks any instance still holding the old value at its next new connection, and the driver then clears its pool. So I create a second user with the same roles, deploy it everywhere, confirm the old one is idle and only then drop it.

code

javascript · 9 lines
javascript
use admin
db.createUser({
  user: "orders_svc_b",
  pwd: passwordPrompt(),
  roles: [ { role: "readWrite", db: "orders" } ]
})

// after every instance runs on orders_svc_b and the old name stops logging in
db.dropUser("orders_svc_a")

go deeper

for a junior

Know which mechanisms exist and that SCRAM is the default, and that Kerberos, LDAP and OIDC need Enterprise rather than Community.

for a middle

Explain why changing a password in place fails new connections from instances holding the old one, and describe the two-user rotation in order.

for a senior

Drive a rotation safely: confirm from server logs that the old user is idle before dropping it, since a drop ends sessions still open, and recognise a cleared driver pool as the symptom of a botched in-place change.

for a principal

Own the policy: which identities come from the directory, which services get certificates or tokens, what stays as break-glass, and how to retire LDAP before a release removes it, given what each option costs to run.

## The menu depends on the edition This is a standing decision with no single right answer, and the first constraint is what your deployment can run at all. | Mechanism | Editions | Who holds the proof | Main running cost | |---|---|---|---| | SCRAM | Community, Enterprise | MongoDB, as salted keys | Distributing and rotating passwords | | X.509 (`MONGODB-X509`) | Community, Enterprise | The certificate and its CA | Issuing, renewing and watching expiry | | Kerberos (`GSSAPI`) | Enterprise | A Kerberos KDC | Service principals and keytabs on each node | | LDAP proxy (`PLAIN`) | Enterprise | A directory server | Deprecated, and the password passes through `mongod` | | OIDC (`MONGODB-OIDC`) | Enterprise, Linux only | An identity provider | Provider configuration and token lifetimes | The last three keep their users in `$external`, or map them from token claims. OIDC needs MongoDB Enterprise 7.0.11 or later, configured with `oidcIdentityProviders`. ## What each one is for - **SCRAM** is the default and needs nothing else to exist, which is why almost every deployment has it somewhere. Its weakness is organisational: a password is a long-lived secret that gets copied. - **X.509** suits services well, since the certificate is issued to the workload and there is no shared string to leak. It also needs a working issuance and renewal process, or expiries become outages. - **Kerberos** fits an organisation that already runs a KDC. Each server needs a service principal such as `mongodb/[email protected]`, and users are added to `$external` by principal name. - **LDAP proxy** forwards a username and password to a directory. The manual says LDAP authentication and authorization have been deprecated since 8.0, continued in 9.0, and will be removed in a future release, and it advises planning a migration. - **OIDC** has two faces. **Workforce Identity Federation** logs in humans through an identity provider, and **Workload Identity Federation** gives applications access tokens. MongoDB never receives the secret, and tokens are short-lived, typically an hour. ## A defensible standard For LDAP migrations, the manual recommends Workforce federation for humans, Workload federation for programmatic users, and X.509 where Workload federation is not possible. That makes a good default shape more generally: - **Humans:** individual identities from a central directory, so that leaving the company removes access in one place. On Community, that means named SCRAM users with a written offboarding step, and never a shared login. - **Services:** one identity per service, using X.509 or Workload federation where you can, and SCRAM with passwords held in a secret manager where you cannot. - **Break-glass:** keep one locally held administrator. The manual's OIDC examples enable `MONGODB-OIDC,SCRAM-SHA-256` together, and an identity provider outage should not lock out the people repairing it. - **Mechanism policy:** `authSource` always explicit, and SCRAM-SHA-1 removed where drivers allow. The trade-offs are real. Federation moves a dependency to the provider. Certificates add an issuing system. Passwords add a rotation process. Choose the cost your team can actually carry, and a small team on Community can reasonably stay on SCRAM with discipline. An existing LDAP estate does not have to move all at once. MongoDB 9.0's `useInternalAuthzForX509` lets X.509 clients take their roles from `$external` even while LDAP authorization stays configured for others. ## Rotating a SCRAM password The constraint is that a user holds one credential per mechanism. `db.changeUserPassword` replaces it, and there is no overlap period. What follows in a running service is subtle: - **Existing connections keep working.** Per the 9.0 source, a changed user stays authenticated on open connections, since its user ID is unchanged. - **New connections fail.** Any instance still holding the old password fails its next handshake. The driver authentication specification then requires marking that server Unknown and clearing its pool, so one failed connection costs the instance its pooled connections to that server, and their replacements fail the same way. So in-place rotation looks fine at first, then fails when a pool grows or an instance restarts. The usual fix uses two users, since identity is cheap: 1. Create `orders_svc_b` with the same roles as `orders_svc_a`. 2. Deploy every instance with the new credential. Restarted instances then hold no connection as the old user. 3. Watch the server's `Successfully authenticated` log lines, which name the user, to confirm nothing still logs in as the old one. 4. Drop `orders_svc_a`. Dropping removes the user from any session still open, so step 3 matters. Recreating a dropped name does not help, because its new user ID does not match those sessions. The next rotation reverses the names. ## Rotation for the other mechanisms - **X.509:** renewing with the same subject changes nothing in MongoDB. A new subject needs a new `$external` user first. - **OIDC:** there is no database password. When a token expires, the server returns `ReauthenticationRequired` (391) and the driver reauthenticates. The specification says OIDC is currently the only server mechanism that supports this. - **Keyfile:** this is member authentication, not a login, and it has its own rolling rotation.

  • When would you keep SCRAM for MongoDB services instead of moving to certificates or tokens?
    When you run Community and have no certificate issuance to lean on, or when the team cannot yet own renewals. An expired certificate causes an outage just as a bad rotation does. SCRAM-SHA-256 with one user per service, secrets in a manager and a practised two-user rotation is a sound position, if it is actually followed.
  • A team runs LDAP proxy on Enterprise today. What do you tell them after reading MongoDB 9.0's docs?
    It still works in 9.0, but it has been deprecated since 8.0 and is due for removal in a future release, with migration details still to come. So stop adding new dependants, name a target such as Workforce federation for people and Workload federation or X.509 for services, and move in stages.
  • Why not rotate a MongoDB service password in place during a quiet period?
    Quiet does not remove the race. From the moment the password changes until the last instance has the new value, every new connection from an instance holding the old one fails, and its driver clears the pool. With a second user there is no such window, so nothing depends on timing.

  • Changing a user's password with db.changeUserPassword gives a grace period for the old one.
  • LDAP proxy is the safe enterprise default for new MongoDB deployments on 9.0.
  • One shared admin login is fine for humans as long as its password is strong.
  • OIDC and Kerberos are available on MongoDB Community if you configure them.
  • Changing a user's password immediately closes all of that user's open connections.

Assumes MongoDB 9.0: LDAP has been deprecated since 8.0 and stays available in 9.0, while useInternalAuthzForX509 is new in 9.0, so teams on 8.0 share the deprecation but not that parameter.