skip to content

How do you move a live MongoDB replica set from keyfile to X.509 member authentication without downtime, and what do the `clusterAuthMode` transition values do?

level: seniorimportance: should knowfreq 30%

answer

  1. what a member sends against what it accepts
  2. four values, one direction
  3. accept both before sending the new one
  4. setParameter does not survive a restart

basics

~20 s

Roll members through clusterAuthMode values: sendKeyFile sends the keyfile and accepts both, sendX509 sends the certificate and accepts both, x509 accepts only certificates. Finish each step on every member first, then record the final mode in the configuration files.

solid answer

~40 s

Members authenticate to each other as `local.__system`, by keyfile or X.509. The trick is to separate what a member sends from what it accepts. With TLS already on, I do a rolling restart with `security.clusterAuthMode: sendKeyFile` and `net.tls.clusterFile`: each member still sends its keyfile but now accepts either. Once every member is there, I run `setParameter` to `sendX509` on each one, so they send certificates and still accept either. Then `setParameter` to `x509` on each, so only certificates are accepted. Finally I write `x509` into the configuration files, since `setParameter` does not survive a restart. Order matters: a member in `x509` refuses a peer still sending a keyfile. The transitions only run forward, so `x509` cannot go back at runtime. A cluster without TLS first goes through `allowTLS` and `preferTLS`.

code

yaml · 9 lines
yaml
net:
  tls:
    mode: requireTLS
    certificateKeyFile: /etc/ssl/mongodb.pem
    CAFile: /etc/ssl/ca.pem
    clusterFile: /etc/ssl/member.pem
security:
  clusterAuthMode: sendKeyFile
  keyFile: /etc/mongodb/keyfile

go deeper

for a junior

Know that replica set members authenticate to each other separately from clients, using either a shared keyfile or X.509 certificates, chosen by clusterAuthMode.

for a middle

Explain what each of the four values sends and accepts, and why a keyfile must have owner-only permissions and how it can carry an old and a new key.

for a senior

Run the rollout as all-members-then-next, record the end state in configuration, and know the failure signatures: a disallowed keyfile from a peer and the illegal transition error.

for a principal

Weigh running a CA and certificate renewal for members against guarding one shared secret, and set a policy that a transitional value such as sendX509 is never the resting state.

## What member authentication is The members of a replica set or sharded cluster must prove to each other that they belong. This **internal** or **member authentication** is separate from client logins: every member authenticates as the same internal user, `local.__system`. There are two methods, and `clusterAuthMode` selects between them. - **Keyfile.** All members share one secret stored in a file, and they use it as a password with SCRAM-SHA-256. - **X.509.** Each member presents a certificate, and peers recognise it from shared attributes, by default `O`, `OU` and `DC`, or from `net.tls.clusterAuthX509.attributes` or `extensionValue` if set. The manual marks `x509` as recommended, since no single shared secret opens the whole cluster. Both turn on access control for clients too: setting `security.keyFile` or `security.clusterAuthMode` enables it unless `transitionToAuth` is in use. ## The four values The design separates sending from accepting, which lets a mixed cluster keep working during the change: | Value | Sends | Accepts | |---|---|---| | `keyFile` | Keyfile | Keyfile only | | `sendKeyFile` | Keyfile | Keyfile or X.509 | | `sendX509` | X.509 | Keyfile or X.509 | | `x509` | X.509 | X.509 only | The manual lists `keyFile` as the default. In the source at r9.0.4 that default is only applied when `security.keyFile` is set: with neither setting, the mode stays undefined and there is no internal authentication at all. ## The procedure for a cluster already on TLS 1. **Rolling restart into `sendKeyFile`.** On each member, add `security.clusterAuthMode: sendKeyFile` and `net.tls.clusterFile`, keep `security.keyFile`, and restart. Finish every member first. 2. **Runtime change to `sendX509`.** On each member, run `db.adminCommand({ setParameter: 1, clusterAuthMode: "sendX509" })`. Every member accepts both, so the mix is safe. Finish every member first. 3. **Runtime change to `x509`.** Run the same command with `"x509"`. The manual calls this step optional but recommended, because `sendX509` still accepts a keyfile, so whoever holds the old key can still join. 4. **Make it permanent.** Set `security.clusterAuthMode: x509` in each configuration file, and remove `security.keyFile`. A `setParameter` value does not survive a restart, and a member restarted on an old file would come back in `sendKeyFile`, sending a keyfile that peers in `x509` refuse. Between stages, check that `rs.status()` shows every member healthy and that each member's log is free of authentication failures, because a peer that has been refused is the earliest sign of a stage rushed. A sharded cluster does this on every `mongod` and `mongos`. A cluster with no TLS adds a rolling start: `net.tls.mode: allowTLS` alongside `sendKeyFile`, then `tlsMode: preferTLS` alongside `sendX509`, and later `requireTLS`. ## What goes wrong - **Skipping ahead.** A member in `x509` refuses a peer that still sends a keyfile, with the internal reason `SCRAM-SHA-256 is disallowed for cluster authentication`. That peer cannot authenticate to it until it too sends a certificate. - **Going backwards.** The source's transition check allows one step at a time, from `keyFile` to `sendKeyFile`, to `sendX509`, to `x509`, and nothing after that. Any other change to a running member fails with `Illegal state transition for clusterAuthMode`. To undo a change you restart with an older configuration. - **Wrong certificates.** Member certificates must match on the membership attributes. If one file serves both client and member roles, it must omit `extendedKeyUsage` or include both `serverAuth` and `clientAuth`. - **A client certificate that matches.** Once X.509 is allowed, a client certificate with the members' attributes is treated as a member, so audit issued certificates before the change. ## Keyfile details worth knowing If you stay on keyfiles, these apply: - **Format.** A key must be 6 to 1024 characters from the base64 set. Whitespace is stripped. - **Permissions.** On Unix, `mongod` refuses to start if the file has group or other permissions, with `permissions on <file> are too open`. Windows does not check. - **Rotation.** A file can carry a YAML sequence of keys for a rolling change. The manual speaks of multiple keys; the source allows at most two, and more than that stops startup with `Only two keys are supported in the security key file`. A member tries its keys in file order when connecting out, and accepts either when a peer connects in. - **Moving from no authentication.** `security.transitionToAuth` handles that rolling change, and it only works with `keyFile` or `x509`, not with the two `send` values.

  • Why does the MongoDB procedure need `sendKeyFile` at all instead of going straight to `sendX509`?
    A member can only send a certificate safely once its peers accept one. `sendKeyFile` gets every member accepting certificates while still sending what the others expect. If one member sent a certificate while a peer was in plain `keyFile`, that peer would refuse it.
  • How do you rotate a MongoDB keyfile without downtime?
    Put the old and new keys in each member's file as a YAML sequence and do a rolling restart, primary last after a step-down, so all members accept both. Then replace the contents with the new key only and do a second rolling restart. The source caps the file at two keys.
  • What does `keyFile` mode offer that X.509 does not, if X.509 is recommended?
    Simplicity: no certificate authority, no expiry to watch and no TLS prerequisite beyond what you already run. The cost is one shared secret that every member holds and that lets any holder join as a member. That trade fits small or short-lived deployments better than long-lived production ones.

  • You can set clusterAuthMode to x509 on one member first and let the others follow later.
  • A setParameter change to clusterAuthMode persists across restarts.
  • A keyfile can hold as many keys as you want for a rotation.
  • A cluster stuck on sendX509 is fully hardened, since members send certificates.
  • After x509 a member can step back to sendX509 with setParameter if something goes wrong.

Assumes MongoDB 9.0: the two-key cap on a keyfile and the one-way transition check were read at r9.0.4, and the manual's wording on multiple keys is looser than the source.