03e4d5fe31
## Summary
Regenerating a RuntimeEnvironment API key no longer immediately
invalidates the previous one. Rotation is now overlap-based: the old key
keeps working for 24 hours so customers can roll it out in their env
vars without downtime, then stops working.
## Design
- **New `RevokedApiKey` table** (one row per revocation). Holds the
archived `apiKey`, a FK to the env, an `expiresAt`, and a `createdAt`.
Indexed on `apiKey` (high-cardinality equality — single-row hits) and on
`runtimeEnvironmentId`.
- **`regenerateApiKey` wraps both writes in a single `$transaction`:**
insert a `RevokedApiKey` with `expiresAt = now + 24h`, update the env
with the new `apiKey`/`pkApiKey`.
- **`findEnvironmentByApiKey` does a two-step lookup:** primary
unique-index hit on `RuntimeEnvironment.apiKey` first; on miss,
`RevokedApiKey.findFirst({ apiKey, expiresAt: { gt: now } })` with an
`include: { runtimeEnvironment }`. Two-step (not `OR`-join) keeps the
hot path identical to today and puts the fallback cost only on invalid
keys. Both lookups use `$replica`.
- **Admin endpoint** `POST /admin/api/v1/revoked-api-keys/:id` accepts
`{ expiresAt }` and updates the row. Setting to `now` ends the grace
window immediately; setting to the future extends it.
- **Modal copy** on the regenerate dialog updated — previously warned of
downtime, now explains the 24h overlap.
## Why a separate table instead of columns on `RuntimeEnvironment`
- Keeps the hot auth path's primary lookup unchanged — no
OR/nullable-apiKey semantics to reason about.
- Naturally supports multiple in-flight grace windows (regenerate twice
in a day → two old keys valid until their independent expiries).
- FK + cascade cleans up correctly when an env is deleted; nothing to
backfill.
## Test plan
Verified locally against hello-world with dev and prod env keys:
- [x] baseline — current key authenticates (`GET /api/v1/runs`) → `200`
- [x] regenerate via UI — DB shows old key in `RevokedApiKey` with
`expiresAt ≈ now+24h`, env has new key
- [x] grace window — both old and new keys → `200`; bogus key → `401`
- [x] admin endpoint: `expiresAt = now` → old key `401`
- [x] admin endpoint: `expiresAt = +1h` (after early-expire) → old key
`200` again
- [x] admin endpoint: `expiresAt = past` → old key `401`
- [x] admin 400 (invalid body), 404 (unknown id), 401 (missing/non-admin
PAT)
- [x] same flow exercised end-to-end on a PROD-typed env — behavior
identical
- [x] `pnpm run typecheck --filter webapp` passes
123 lines
3.1 KiB
TypeScript
123 lines
3.1 KiB
TypeScript
import type { RuntimeEnvironment } from "@trigger.dev/database";
|
|
import { prisma } from "~/db.server";
|
|
import { customAlphabet } from "nanoid";
|
|
import { RuntimeEnvironmentType } from "~/database-types";
|
|
|
|
const apiKeyId = customAlphabet(
|
|
"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
12
|
|
);
|
|
|
|
const REVOKED_API_KEY_GRACE_PERIOD_MS = 24 * 60 * 60 * 1000;
|
|
|
|
type RegenerateAPIKeyInput = {
|
|
userId: string;
|
|
environmentId: string;
|
|
};
|
|
|
|
export async function regenerateApiKey({ userId, environmentId }: RegenerateAPIKeyInput) {
|
|
const environment = await prisma.runtimeEnvironment.findUnique({
|
|
where: {
|
|
id: environmentId,
|
|
},
|
|
include: {
|
|
organization: true,
|
|
project: true,
|
|
},
|
|
});
|
|
|
|
if (!environment) {
|
|
throw new Error("Environment does not exist");
|
|
}
|
|
|
|
// check if the user is part of the org
|
|
const organization = await prisma.organization.findFirst({
|
|
where: {
|
|
id: environment.organization.id,
|
|
members: { some: { userId } },
|
|
},
|
|
});
|
|
|
|
if (!organization) {
|
|
throw new Error("User does not have permission to regenerate API key");
|
|
}
|
|
|
|
// check if it is the user's dev environment
|
|
if (environment.type === RuntimeEnvironmentType.DEVELOPMENT) {
|
|
if (!environment.orgMemberId) {
|
|
throw new Error("User does not have permission to regenerate API key");
|
|
}
|
|
|
|
const orgMember = await prisma.orgMember.findFirst({
|
|
where: {
|
|
organizationId: organization.id,
|
|
userId: userId,
|
|
id: environment.orgMemberId,
|
|
},
|
|
});
|
|
|
|
if (!orgMember) {
|
|
throw new Error("User does not have permission to regenerate API key");
|
|
}
|
|
}
|
|
|
|
// generate and store new keys
|
|
const newApiKey = createApiKeyForEnv(environment.type);
|
|
const newPkApiKey = createPkApiKeyForEnv(environment.type);
|
|
|
|
const revokedApiKeyExpiresAt = new Date(Date.now() + REVOKED_API_KEY_GRACE_PERIOD_MS);
|
|
|
|
const updatedEnviroment = await prisma.$transaction(async (tx) => {
|
|
await tx.revokedApiKey.create({
|
|
data: {
|
|
apiKey: environment.apiKey,
|
|
runtimeEnvironmentId: environment.id,
|
|
expiresAt: revokedApiKeyExpiresAt,
|
|
},
|
|
});
|
|
|
|
return tx.runtimeEnvironment.update({
|
|
data: {
|
|
apiKey: newApiKey,
|
|
pkApiKey: newPkApiKey,
|
|
},
|
|
where: {
|
|
id: environmentId,
|
|
},
|
|
});
|
|
});
|
|
|
|
return updatedEnviroment;
|
|
}
|
|
|
|
export function createApiKeyForEnv(envType: RuntimeEnvironment["type"]) {
|
|
return `tr_${envSlug(envType)}_${apiKeyId(20)}`;
|
|
}
|
|
|
|
export function createPkApiKeyForEnv(envType: RuntimeEnvironment["type"]) {
|
|
return `pk_${envSlug(envType)}_${apiKeyId(20)}`;
|
|
}
|
|
|
|
export type EnvSlug = "dev" | "stg" | "prod" | "preview";
|
|
|
|
export function envSlug(environmentType: RuntimeEnvironment["type"]): EnvSlug {
|
|
switch (environmentType) {
|
|
case "DEVELOPMENT": {
|
|
return "dev";
|
|
}
|
|
case "PRODUCTION": {
|
|
return "prod";
|
|
}
|
|
case "STAGING": {
|
|
return "stg";
|
|
}
|
|
case "PREVIEW": {
|
|
return "preview";
|
|
}
|
|
}
|
|
}
|
|
|
|
export function isEnvSlug(maybeSlug: string): maybeSlug is EnvSlug {
|
|
return ["dev", "stg", "prod", "preview"].includes(maybeSlug);
|
|
}
|