Database keys

Creating a database key, its permissions, rotating it with an overlap window and revoking it.

A database key authenticates your backend to one database. It is sent to the database's cluster as a bearer token:

Authorization: Bearer mk_…

A database key reaches the whole database, so it belongs on your servers and in your CI's secrets, never in a browser or a mobile app. Give those document tokens instead.

Permissions

Each key has one permission. Each includes the ones before it.

PermissionAllows
readReading documents and collections
writeAlso changing documents
adminAlso managing collections, migrations, keys and token keys in the database itself

Give each service the least it needs. A backend that only reads and writes documents does not need an admin key; keep the admin key for the job that changes collections.

Creating a key

On the database's Keys page, choose Create key, then give it a name of up to 64 characters and a permission. The database's Connect page creates one the same way as its first step.

The key's secret is shown once, in a dialog. Copy it into your secret store before closing it. The service does not store the secret and cannot show it again.

Keys can be created once the database is ready, and while it is suspended. Any member of the organization can create one.

From the management API, it is POST /v1/databases/{databaseId}/keys: see database keys in the API.

The key list

ColumnMeaning
NameThe name you gave it
PermissionRead, Write or Admin
Secretmk_… and the secret's last four characters
CreatedWhen it was created
StatusActive, Rotating, Revoked, or Revoked in database when it was revoked or removed in the database itself

Rotating a key

Rotating replaces a key's secret without downtime. It creates a successor with the same name and permission, shows the successor's secret once, and keeps the old key working for an overlap window that you choose.

Rotate

Choose Rotate key on the key and pick how long the old key keeps working:

ChoiceOverlap
Revoke the old key nowNone
Keep the old key for 1 hour1 hour
Keep the old key for 24 hours24 hours
Keep the old key for 7 days7 days

The management API takes any number of seconds from 0 to 604800 (7 days) as overlapSeconds.

Deploy the new secret

Copy the successor's secret and deploy it everywhere the old one is used. During the overlap both keys work, so instances that still hold the old secret keep running.

The old key is revoked

When the overlap ends, the old key is revoked for you. Until then it is listed as Rotating, with the time it will be revoked.

A few rules:

  • A key that is already being rotated cannot be rotated again. Its successor can.
  • Rotation is an operation on the database. It cannot start while the database is being created, suspended, resumed, deleted or restored. A rotation that is waiting out its overlap does not hold up those operations.
  • If revoking the old key fails when the overlap ends, the database's page says so and offers a retry. The database keeps working either way. The advisor flags a rotated key that still works an hour after its overlap ended.

Revoking a key

Choose Revoke key. Requests with that key fail from then on, and it cannot be undone. The key stays in the list as Revoked.

To replace a key that may have leaked, revoke it rather than rotate it, or rotate it with Revoke the old key now.

Keys not made here

An admin key can create, revoke and remove keys in the database itself, beside this dashboard and the management API. About every 15 minutes the service compares the database's keys with its own list:

  • A key made here that was revoked or removed in the database is shown as Revoked in database. It no longer works.
  • A key the database holds that was not made here is listed under Not made here on the Keys page, with its name, permission, its ID in the database and its status. That is usually one created in the database with an admin key, or one issued between when a backup was taken and when this service was restored from it. Its secret is never shown. The service never revokes one on its own. You can: Revoke key on its row revokes it in the database, and what it minted. The management API lists them at GET /v1/databases/{databaseId}/unmanaged-credentials and revokes one at DELETE /v1/databases/{databaseId}/unmanaged-credentials/database-keys/{keyIdInDatabase}.

When a database is deleted

When a deleted database's grace period ends, all of its keys made here are revoked. The service does not revoke keys made in the database itself: they end when the database is removed from its cluster. They are not listed while the database is being deleted. If the database is restored during the grace period, its keys work again as before, and keys not made here are listed again once the service has compared the database again.

A database restored from a backup has no keys: credentials are never restored. Create new ones for it.

If creating a key fails

Creating or rotating a key can fail with a message asking you to try again, for example if the cluster's answer was lost. After a lost answer the request can take a minute or two before it says so. The service revokes any key whose secret was not delivered to you: at once when it can, and otherwise in the background shortly afterwards. Nobody holds its secret. Try again to get a new one.

Rarely, when several answers are lost in a row, the database can hold more than one key of the name the service gave the issuance (a name starting dbk_) and the service cannot tell which is its own. It then revokes none of them: they are listed under Not made here, with nobody holding their secrets. Revoke them with an admin key; they also end when the database is deleted.