@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 143 lines 6.12 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Alias, Key } from "@alchemy.run/aws/PaymentCryptography";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as paymentcryptography from "@distilled.cloud/aws/payment-cryptography";
6 import * as Effect from "effect/Effect";
7 import { reapLeakedKeys } from "./reapKeys.ts";
8
9 const { test } = Test.make({ providers: AWS.providers() });
10
11 // Keys cannot be hard-deleted (DeleteKey only schedules deletion after a
12 // mandatory >=3 day window) and the scratch stacks keep state in memory, so
13 // a crashed gated run leaves ACTIVE keys nothing can reclaim. This ungated
14 // sweep runs in every CI pass and schedules deletion for any alchemy-tagged
15 // test key that is not already DELETE_PENDING. It is skipped when the gate
16 // is ON so it can never race an actively-running gated suite (which reaps
17 // its own keys via `Effect.ensuring` instead).
18 test.provider.skipIf(!!process.env.AWS_TEST_PAYMENTCRYPTO)(
19 "reap: schedule deletion for keys leaked by crashed gated runs",
20 () => reapLeakedKeys(),
21 { tags: ["provider:aws", "provider:aws:paymentcryptography", "live"] },
22 );
23
24 // Ungated typed-error probes: prove the distilled error unions carry the
25 // not-found tag the provider's read/delete paths depend on. These run in
26 // every CI pass at near-zero cost, unlike the gated lifecycle below.
27 test.provider(
28 "getKey on a nonexistent identifier fails with ResourceNotFoundException",
29 () =>
30 Effect.gen(function* () {
31 const error = yield* Effect.flip(
32 paymentcryptography.getKey({
33 KeyIdentifier: "alias/alchemy-nonexistent-payment-key-probe",
34 }),
35 );
36 expect(error._tag).toBe("ResourceNotFoundException");
37 }),
38 { tags: ["provider:aws", "provider:aws:paymentcryptography", "live"] },
39 );
40
41 test.provider(
42 "getAlias on a nonexistent alias fails with ResourceNotFoundException",
43 () =>
44 Effect.gen(function* () {
45 const error = yield* Effect.flip(
46 paymentcryptography.getAlias({
47 AliasName: "alias/alchemy-nonexistent-payment-alias-probe",
48 }),
49 );
50 expect(error._tag).toBe("ResourceNotFoundException");
51 }),
52 { tags: ["provider:aws", "provider:aws:paymentcryptography", "live"] },
53 );
54
55 const dataKeyAttributes = {
56 keyAlgorithm: "AES_128",
57 keyClass: "SYMMETRIC_KEY",
58 keyUsage: "TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY",
59 keyModesOfUse: { encrypt: true, decrypt: true, wrap: true, unwrap: true },
60 } as const;
61
62 // Keys bill monthly while they exist, so the live lifecycle is gated behind
63 // AWS_TEST_PAYMENTCRYPTO=1 and always destroys what it created (with the
64 // minimum 3-day deletion window).
65 test.provider.skipIf(!process.env.AWS_TEST_PAYMENTCRYPTO)(
66 "create key + alias, toggle enabled, destroy",
67 (stack) =>
68 Effect.gen(function* () {
69 yield* stack.destroy();
70
71 const deployed = yield* stack.deploy(
72 Effect.gen(function* () {
73 const key = yield* Key("DataKey", {
74 keyAttributes: dataKeyAttributes,
75 tags: { fixture: "payment-cryptography-key" },
76 });
77 const alias = yield* Alias("DataKeyAlias", { keyArn: key.keyArn });
78 return { key, alias };
79 }),
80 );
81
82 expect(deployed.key.keyArn).toContain(":key/");
83 expect(deployed.key.keyState).toBe("CREATE_COMPLETE");
84 expect(deployed.key.enabled).toBe(true);
85 expect(deployed.key.exportable).toBe(false);
86 expect(deployed.key.keyCheckValue).toBeTruthy();
87 expect(deployed.alias.aliasName).toMatch(/^alias\//);
88 expect(deployed.alias.keyArn).toBe(deployed.key.keyArn);
89
90 // Out-of-band verification via distilled.
91 const observed = yield* paymentcryptography.getKey({ KeyIdentifier: deployed.key.keyArn });
92 expect(observed.Key.KeyState).toBe("CREATE_COMPLETE");
93 expect(observed.Key.Enabled).toBe(true);
94 expect(observed.Key.KeyAttributes.KeyAlgorithm).toBe("AES_128");
95 expect(observed.Key.KeyAttributes.KeyUsage).toBe("TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
96 const observedAlias = yield* paymentcryptography.getAlias({
97 AliasName: deployed.alias.aliasName,
98 });
99 expect(observedAlias.Alias.KeyArn).toBe(deployed.key.keyArn);
100
101 // Update in place: disable the key (StopKeyUsage) — same ARN.
102 const updated = yield* stack.deploy(
103 Effect.gen(function* () {
104 const key = yield* Key("DataKey", {
105 keyAttributes: dataKeyAttributes,
106 enabled: false,
107 tags: { fixture: "payment-cryptography-key", phase: "two" },
108 });
109 const alias = yield* Alias("DataKeyAlias", { keyArn: key.keyArn });
110 return { key, alias };
111 }),
112 );
113 expect(updated.key.keyArn).toBe(deployed.key.keyArn);
114 expect(updated.key.enabled).toBe(false);
115 const observedDisabled = yield* paymentcryptography.getKey({
116 KeyIdentifier: deployed.key.keyArn,
117 });
118 expect(observedDisabled.Key.Enabled).toBe(false);
119
120 // Destroy — the key schedules deletion (DELETE_PENDING) and the alias
121 // is removed immediately.
122 yield* stack.destroy();
123 const stateAfter = yield* paymentcryptography
124 .getKey({ KeyIdentifier: deployed.key.keyArn })
125 .pipe(
126 Effect.map((r) => r.Key.KeyState),
127 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)),
128 );
129 expect(["DELETE_PENDING", "DELETE_COMPLETE", "gone"]).toContain(stateAfter);
130 const aliasError = yield* Effect.flip(
131 paymentcryptography.getAlias({ AliasName: deployed.alias.aliasName }),
132 );
133 expect(aliasError._tag).toBe("ResourceNotFoundException");
134 }).pipe(
135 // Belt-and-braces teardown: even if the body fails before its trailing
136 // destroy (or the engine's destroy fails), schedule deletion for any
137 // key this stack created so a failed run still leaves the key at
138 // worst DELETE_PENDING (the best achievable state — the deletion
139 // window is mandatory). Idempotent: a no-op when destroy already ran.
140 Effect.ensuring(reapLeakedKeys([stack.name])),
141 ),
142 { tags: ["provider:aws", "provider:aws:paymentcryptography", "live"], timeout: 120_000 },
143 );