Rotate a secret key
Rotate a secret key on a schedule, and whenever it may have leaked. Secret keys describes the values of status.
Before you start
- The
apihelper that sends each request, from Platform quickstart. - Your credential,
token, as an owner or admin of the workspace that holds the project, from Start a workspace. - A project,
project, with adevsecret key namedserver, from Create a project and issue a key.
This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.
1. Find the key
List the project’s keys and pick the active dev secret key named server. If more than one matches, use the ID of the key your server uses.
type Key = {
id: string;
name: string;
status: string;
kind: string;
environment: string;
};
const { keys } = await api('GET', `/v1/projects/${project.id}/keys`, token);
const server: Key = keys.find(
(k: Key) =>
k.name === 'server' &&
k.status === 'active' &&
k.kind === 'secret' &&
k.environment === 'dev',
);
2. Rotate the key
This example rotates server.id and keeps the old key working for one hour.
const {
key,
record: replacement,
previous,
} = await api(
'POST',
`/v1/projects/${project.id}/keys/${server.id}/rotate`,
token,
{ grace: '1h' },
);
console.log(key.slice(0, 8), previous.status); // sk_test_ grace
- Store
key, the new secret key, where the server reads it. - Deploy the server with it.
- The old key keeps working until
previous.graceEndsAt. Secret keys says how long the user tokens it minted keep working.
Send grace as a duration string from "0s" to "7d". If you leave it out, the API answers invalid_request. A grace of "0s" stops the old key at once. With it, send the key’s name in confirm, as in { grace: '0s', confirm: 'server' }, or the API answers confirmation_required. When a rotation answers conflict, for a reason Secret keys gives, rotate the key its replacedBy names, or issue a new key if it has no replacedBy.
3. Revoke the old key
When the old key leaked, revoke it once the server runs with the new key, even if its grace has ended. Revoking stops the key and every user token it minted at once, and can’t be undone. For a routine rotation, you can let grace finish and skip this step.
const { record } = await api(
'DELETE',
`/v1/projects/${project.id}/keys/${previous.id}`,
token,
);
console.log(record.status); // revoked
Revoking may answer not_found.
Check it worked
List the project’s keys again and check the two keys by ID:
const after = await api('GET', `/v1/projects/${project.id}/keys`, token);
const servers = after.keys.filter(
(k: Key) => k.id === previous.id || k.id === replacement.id,
);
console.log(servers.map((k: Key) => k.status)); // [ 'revoked', 'active' ]
The list is oldest first. The old key is revoked and the new one is active. If you skipped step 3, the old key shows grace or expired, as Secret keys describes.