@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 55 lines 2.56 KB
Raw
1 import * as paymentcryptography from "@distilled.cloud/aws/payment-cryptography";
2 import * as Effect from "effect/Effect";
3 import * as Stream from "effect/Stream";
4
5 /**
6 * Out-of-band reaper for leaked Payment Cryptography keys.
7 *
8 * Payment Cryptography keys cannot be hard-deleted: `DeleteKey` only
9 * *schedules* deletion after a mandatory waiting window (minimum 3 days),
10 * during which the key stays visible in `ListKeys` as `DELETE_PENDING`.
11 * The scratch stacks these suites deploy through keep state in memory, so a
12 * crashed run (vitest timeout kill, OOM) leaves its keys ACTIVE with no
13 * state pointing at them — no later run can reclaim them through the engine.
14 *
15 * This sweep lists every key, skips those already pending/complete deletion,
16 * and idempotently schedules deletion (minimum window) for any key branded
17 * with the internal alchemy tags at stage `test` — optionally narrowed to
18 * specific stack names so a gated suite only ever reaps its own keys.
19 *
20 * `DELETE_PENDING` is the best achievable end state; the service purges the
21 * key itself when the window elapses. Errors are defects (`orDie`) so this
22 * is a valid `Effect.ensuring` finalizer.
23 */
24 export const reapLeakedKeys = (stackNames?: ReadonlyArray<string>) =>
25 Effect.gen(function* () {
26 const keys = yield* paymentcryptography.listKeys.items({}).pipe(
27 Stream.runCollect,
28 Effect.map((chunk) => Array.from(chunk)),
29 );
30 for (const key of keys) {
31 if (key.KeyState === "DELETE_PENDING" || key.KeyState === "DELETE_COMPLETE") {
32 continue; // already handled — the service purges it after the window
33 }
34 const tags: Record<string, string> = yield* paymentcryptography.listTagsForResource
35 .items({ ResourceArn: key.KeyArn })
36 .pipe(
37 Stream.runCollect,
38 Effect.map((chunk) =>
39 Object.fromEntries(Array.from(chunk).map((t) => [t.Key, t.Value ?? ""])),
40 ),
41 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed({})),
42 );
43 const stack = tags["alchemy::stack"];
44 const owned =
45 tags["alchemy::stage"] === "test" &&
46 stack !== undefined &&
47 (stackNames === undefined || stackNames.includes(stack));
48 if (!owned) continue;
49 yield* paymentcryptography.deleteKey({ KeyIdentifier: key.KeyArn, DeleteKeyInDays: 3 }).pipe(
50 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
51 // Raced into DELETE_PENDING between observe and delete — done.
52 Effect.catchTag("ConflictException", () => Effect.void),
53 );
54 }
55 }).pipe(Effect.orDie);