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

main
ts 531 lines 20.3 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
3 import type { PolicyDocument } from "@alchemy.run/aws/IAM/Policy";
4 import { normalizePolicyDocument } from "@alchemy.run/aws/IAM/Policy";
5 import { Secret } from "@alchemy.run/aws/SecretsManager/Secret";
6 import { expect } from "@alchemy.run/test";
7 import * as Test from "@alchemy.run/test/Alchemy";
8 import * as secretsmanager from "@distilled.cloud/aws/secrets-manager";
9 import * as Provider from "alchemy/Provider";
10 import * as Data from "effect/Data";
11 import * as Effect from "effect/Effect";
12 import * as Redacted from "effect/Redacted";
13 import * as Schedule from "effect/Schedule";
14 import { driftOf, repairDrift, waitForDrift } from "../drift.ts";
15
16 const { test } = Test.make({ providers: AWS.providers() });
17
18 class SecretNotListed extends Data.TaggedError("SecretNotListed") {}
19
20 class ResourcePolicyNotAttached extends Data.TaggedError("ResourcePolicyNotAttached") {}
21
22 class SecretStillExists extends Data.TaggedError("SecretStillExists") {}
23
24 class SecretVersionNotVisible extends Data.TaggedError("SecretVersionNotVisible")<{
25 readonly expectedVersionId: string;
26 readonly observedVersionId: string | undefined;
27 readonly descriptionMatches: boolean;
28 }> {}
29
30 // Read APIs propagate independently and can return older snapshots on later calls.
31 const readCurrentSecret = Effect.fn(function* (
32 secretArn: string,
33 versionId: string,
34 description: string,
35 ) {
36 return yield* Effect.retry(
37 Effect.gen(function* () {
38 const value = yield* secretsmanager.getSecretValue({ SecretId: secretArn });
39 const described = yield* secretsmanager.describeSecret({ SecretId: secretArn });
40 if (
41 value.VersionId !== versionId ||
42 !value.VersionStages?.includes("AWSCURRENT") ||
43 described.Description !== description ||
44 !described.VersionIdsToStages?.[versionId]?.includes("AWSCURRENT")
45 ) {
46 return yield* Effect.fail(
47 new SecretVersionNotVisible({
48 expectedVersionId: versionId,
49 observedVersionId: value.VersionId,
50 descriptionMatches: described.Description === description,
51 }),
52 );
53 }
54 return { value, described };
55 }).pipe(
56 Effect.tapError((error) =>
57 error._tag === "SecretVersionNotVisible"
58 ? Effect.logInfo("Waiting for secret version visibility", error)
59 : Effect.void,
60 ),
61 ),
62 {
63 while: (error) =>
64 error._tag === "SecretVersionNotVisible" || error._tag === "ResourceNotFoundException",
65 schedule: Schedule.fixed("2 seconds"),
66 times: 10,
67 },
68 );
69 });
70
71 // Secrets Manager marks values as sensitive, so the distilled client can hand
72 // them back either raw or wrapped in `Redacted` — unwrap for assertions.
73 const unwrapString = (value: string | Redacted.Redacted<string> | undefined): string | undefined =>
74 value === undefined ? undefined : typeof value === "string" ? value : Redacted.value(value);
75
76 const unwrapBinary = (
77 value: Uint8Array | Redacted.Redacted<Uint8Array> | undefined,
78 ): Uint8Array | undefined =>
79 value === undefined ? undefined : value instanceof Uint8Array ? value : Redacted.value(value);
80
81 // Typed wait-until-gone: the provider deletes with
82 // `ForceDeleteWithoutRecovery`, which completes asynchronously — poll
83 // `describeSecret` (bounded) until it fails with the typed
84 // `ResourceNotFoundException`.
85 const assertSecretDeleted = (secretArn: string) =>
86 secretsmanager.describeSecret({ SecretId: secretArn }).pipe(
87 Effect.flatMap(() => Effect.fail(new SecretStillExists())),
88 Effect.retry({
89 while: (e) => e._tag === "SecretStillExists",
90 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
91 }),
92 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
93 );
94
95 test.provider(
96 "create, update value, destroy",
97 (stack) =>
98 Effect.gen(function* () {
99 yield* stack.destroy();
100
101 const secret = yield* stack.deploy(
102 Effect.gen(function* () {
103 return yield* Secret("LifecycleSecret", {
104 description: "lifecycle v1",
105 secretString: Redacted.make("initial-value"),
106 tags: { Environment: "test" },
107 });
108 }),
109 );
110
111 expect(secret.secretArn).toContain("arn:aws:secretsmanager:");
112 expect(secret.secretName).toBeTruthy();
113 expect(secret.versionId).toBeTruthy();
114
115 // Out-of-band verification via distilled.
116 const { value: v1 } = yield* readCurrentSecret(
117 secret.secretArn,
118 secret.versionId!,
119 "lifecycle v1",
120 );
121 expect(v1.VersionId).toBe(secret.versionId);
122 expect(v1.VersionStages).toContain("AWSCURRENT");
123 expect(unwrapString(v1.SecretString)).toBe("initial-value");
124
125 // Update the value + description in place (no replacement).
126 const updated = yield* stack.deploy(
127 Effect.gen(function* () {
128 return yield* Secret("LifecycleSecret", {
129 description: "lifecycle v2",
130 secretString: Redacted.make("updated-value"),
131 tags: { Environment: "test" },
132 });
133 }),
134 );
135
136 expect(updated.secretArn).toBe(secret.secretArn);
137 expect(updated.versionId).toBeTruthy();
138 expect(updated.versionId).not.toBe(secret.versionId);
139
140 const { value: v2, described } = yield* readCurrentSecret(
141 secret.secretArn,
142 updated.versionId!,
143 "lifecycle v2",
144 );
145 expect(v2.VersionId).toBe(updated.versionId);
146 expect(v2.VersionStages).toContain("AWSCURRENT");
147 expect(unwrapString(v2.SecretString)).toBe("updated-value");
148
149 expect(described.Description).toBe("lifecycle v2");
150 expect(updated.description).toBe(described.Description);
151 expect(described.VersionIdsToStages?.[updated.versionId!]).toContain("AWSCURRENT");
152
153 yield* stack.destroy();
154
155 yield* assertSecretDeleted(secret.secretArn);
156 }),
157 { tags: ["provider:aws", "provider:aws:secretsmanager", "live"] },
158 );
159
160 // `recoveryWindowInDays` opts out of force deletion: removing the secret
161 // schedules it for deletion (still describable, `DeletedDate` set), and
162 // re-adding the same `Secret` restores that secret rather than failing on a
163 // name that is pending deletion. Restoring needs a stable `name` (a re-added
164 // resource otherwise gets a fresh generated name); it is scoped to the test
165 // stage. Cleanup force-deletes out-of-band so the scheduled secret does not
166 // linger for the window.
167 test.provider(
168 "recovery window: removal schedules deletion, re-adding restores",
169 (stack) =>
170 Effect.gen(function* () {
171 yield* stack.destroy();
172
173 const program = (included: boolean) =>
174 Effect.gen(function* () {
175 if (!included) return {};
176 const secret = yield* Secret("RecoverableSecret", {
177 name: `alchemy-test-recoverable-${stack.stage}`,
178 description: "recoverable",
179 secretString: Redacted.make("recoverable-value"),
180 recoveryWindowInDays: 7,
181 });
182 return { secret };
183 });
184
185 // DescribeSecret is eventually consistent after DeleteSecret /
186 // RestoreSecret; poll (bounded) until the deletion state shows.
187 const describeUntil = (secretArn: string, scheduled: boolean) =>
188 secretsmanager.describeSecret({ SecretId: secretArn }).pipe(
189 Effect.repeat({
190 schedule: Schedule.spaced("1 second"),
191 until: (d): boolean => (d.DeletedDate !== undefined) === scheduled,
192 times: 15,
193 }),
194 );
195
196 const created = (yield* stack.deploy(program(true))).secret!;
197
198 // Removal schedules deletion instead of force-deleting.
199 yield* stack.deploy(program(false));
200 const scheduled = yield* describeUntil(created.secretArn, true);
201 expect(scheduled.DeletedDate).toBeDefined();
202
203 // Re-adding restores the same secret.
204 const restored = (yield* stack.deploy(program(true))).secret!;
205 expect(restored.secretArn).toBe(created.secretArn);
206 const live = yield* describeUntil(created.secretArn, false);
207 expect(live.DeletedDate).toBeUndefined();
208 expect(live.Description).toBe("recoverable");
209
210 // Destroy schedules it again; then force-delete so nothing lingers.
211 yield* stack.destroy();
212 const afterDestroy = yield* describeUntil(created.secretArn, true);
213 expect(afterDestroy.DeletedDate).toBeDefined();
214 yield* secretsmanager
215 .deleteSecret({ SecretId: created.secretArn, ForceDeleteWithoutRecovery: true })
216 .pipe(
217 // Briefly unfindable right after being scheduled; retry (bounded).
218 Effect.retry({
219 while: (e) => e._tag === "ResourceNotFoundException",
220 schedule: Schedule.spaced("1 second"),
221 times: 10,
222 }),
223 );
224 yield* assertSecretDeleted(created.secretArn);
225 }),
226 { tags: ["provider:aws", "provider:aws:secretsmanager", "live"] },
227 );
228
229 // The recovery window is validated before any API call: DeleteSecret only
230 // accepts a whole number of days from 7 to 30.
231 test.provider(
232 "recovery window outside 7..30 fails before creating anything",
233 (stack) =>
234 Effect.gen(function* () {
235 yield* stack.destroy();
236 const name = `alchemy-test-bad-window-${stack.stage}`;
237 const failure = yield* stack
238 .deploy(
239 Secret("BadWindowSecret", {
240 name,
241 secretString: Redacted.make("value"),
242 recoveryWindowInDays: 3,
243 }),
244 )
245 .pipe(Effect.flip);
246 expect(failure).toMatchObject({ _tag: "SecretRecoveryWindowOutOfRange" });
247 const absent = yield* secretsmanager.describeSecret({ SecretId: name }).pipe(Effect.flip);
248 expect(absent._tag).toBe("ResourceNotFoundException");
249 yield* stack.destroy();
250 }),
251 { tags: ["provider:aws", "provider:aws:secretsmanager", "live"] },
252 );
253
254 // A pending secret this resource did not create (no ownership tags) is never
255 // restored: the deploy fails with OwnedBySomeoneElse and the secret stays
256 // scheduled for deletion.
257 test.provider(
258 "recovery window never restores a foreign pending secret",
259 (stack) =>
260 Effect.gen(function* () {
261 yield* stack.destroy();
262 const name = `alchemy-test-foreign-pending-${stack.stage}`;
263 const foreign = yield* secretsmanager.createSecret({
264 Name: name,
265 SecretString: "foreign",
266 });
267 yield* secretsmanager.deleteSecret({ SecretId: foreign.ARN!, RecoveryWindowInDays: 7 });
268
269 const failure = yield* stack
270 .deploy(
271 Secret("ForeignNameSecret", {
272 name,
273 secretString: Redacted.make("mine"),
274 recoveryWindowInDays: 7,
275 }),
276 )
277 .pipe(Effect.flip);
278 expect(failure).toMatchObject({ _tag: "OwnedBySomeoneElse" });
279 const still = yield* secretsmanager.describeSecret({ SecretId: foreign.ARN! });
280 expect(still.DeletedDate).toBeDefined();
281
282 yield* stack.destroy();
283 yield* secretsmanager
284 .deleteSecret({ SecretId: foreign.ARN!, ForceDeleteWithoutRecovery: true })
285 .pipe(
286 Effect.retry({
287 while: (e) => e._tag === "ResourceNotFoundException",
288 schedule: Schedule.spaced("1 second"),
289 times: 10,
290 }),
291 );
292 yield* assertSecretDeleted(foreign.ARN!);
293 }),
294 { tags: ["provider:aws", "provider:aws:secretsmanager", "live"] },
295 );
296
297 // Audit: `secretBinary` is declared as `Redacted.Redacted<Uint8Array>` — this
298 // exercises the Redacted conversion end-to-end at deploy time: create with a
299 // binary value, verify the exact bytes on the wire out-of-band via distilled,
300 // rotate the binary value in place, and verify the new bytes.
301 // Deterministic checked-in constants (never generated at test time),
302 // exercising non-UTF8 bytes through the base64 transport.
303 const BINARY_V1 = new Uint8Array([0, 1, 2, 3, 250, 251, 252, 253, 254, 255]);
304 const BINARY_V2 = new Uint8Array([42, 7, 128, 129, 130, 0, 255]);
305
306 test.provider(
307 "binary secret value round-trips (Redacted prop)",
308 (stack) =>
309 Effect.gen(function* () {
310 yield* stack.destroy();
311
312 const secret = yield* stack.deploy(
313 Effect.gen(function* () {
314 return yield* Secret("BinaryLifecycleSecret", {
315 description: "binary lifecycle v1",
316 secretBinary: Redacted.make(BINARY_V1),
317 });
318 }),
319 );
320
321 expect(secret.secretArn).toContain("arn:aws:secretsmanager:");
322 expect(secret.versionId).toBeTruthy();
323
324 // Out-of-band verification via distilled: exact bytes on the wire.
325 const { value: v1 } = yield* readCurrentSecret(
326 secret.secretArn,
327 secret.versionId!,
328 "binary lifecycle v1",
329 );
330 expect(v1.VersionId).toBe(secret.versionId);
331 expect(v1.VersionStages).toContain("AWSCURRENT");
332 expect(Array.from(unwrapBinary(v1.SecretBinary)!)).toEqual(Array.from(BINARY_V1));
333 expect(unwrapString(v1.SecretString)).toBeUndefined();
334
335 // Rotate the binary value in place (no replacement).
336 const updated = yield* stack.deploy(
337 Effect.gen(function* () {
338 return yield* Secret("BinaryLifecycleSecret", {
339 description: "binary lifecycle v2",
340 secretBinary: Redacted.make(BINARY_V2),
341 });
342 }),
343 );
344 expect(updated.secretArn).toBe(secret.secretArn);
345 expect(updated.versionId).toBeTruthy();
346 expect(updated.versionId).not.toBe(secret.versionId);
347
348 const { value: v2, described } = yield* readCurrentSecret(
349 secret.secretArn,
350 updated.versionId!,
351 "binary lifecycle v2",
352 );
353 expect(v2.VersionId).toBe(updated.versionId);
354 expect(v2.VersionStages).toContain("AWSCURRENT");
355 expect(Array.from(unwrapBinary(v2.SecretBinary)!)).toEqual(Array.from(BINARY_V2));
356 expect(unwrapString(v2.SecretString)).toBeUndefined();
357
358 expect(described.Description).toBe("binary lifecycle v2");
359 expect(updated.description).toBe(described.Description);
360 expect(described.VersionIdsToStages?.[updated.versionId!]).toContain("AWSCURRENT");
361
362 yield* stack.destroy();
363 yield* assertSecretDeleted(secret.secretArn);
364 }),
365 { tags: ["provider:aws", "provider:aws:secretsmanager", "live"] },
366 );
367
368 // PolicyDocument adoption: a typed `resourcePolicy` deploys, the attached
369 // policy round-trips (normalized comparison), and re-deploying the identical
370 // document is clean — reconcile diffs `normalizePolicyDocument(observed)`
371 // against `normalizePolicyDocument(desired)` and skips `PutResourcePolicy`
372 // on equivalence. Removing the prop detaches the policy.
373 test.provider(
374 "resource policy deploys and re-deploys clean",
375 (stack) =>
376 Effect.gen(function* () {
377 const { accountId } = yield* AWSEnvironment.current;
378 const resourcePolicy: PolicyDocument = {
379 Version: "2012-10-17",
380 Statement: [
381 {
382 Sid: "AllowAccountRead",
383 Effect: "Allow",
384 Principal: { AWS: `arn:aws:iam::${accountId}:root` },
385 Action: ["secretsmanager:GetSecretValue"],
386 Resource: "*",
387 },
388 ],
389 };
390
391 const secret = yield* stack.deploy(
392 Effect.gen(function* () {
393 return yield* Secret("PolicySecret", {
394 description: "resource policy round-trip",
395 secretString: Redacted.make("policy-value"),
396 resourcePolicy,
397 });
398 }),
399 );
400
401 // Out-of-band verification via distilled: the attached policy is
402 // equivalent to the typed document (bounded retry through propagation).
403 const attached = yield* secretsmanager.getResourcePolicy({ SecretId: secret.secretArn }).pipe(
404 Effect.flatMap((response) =>
405 response.ResourcePolicy === undefined
406 ? Effect.fail(new ResourcePolicyNotAttached())
407 : Effect.succeed(response.ResourcePolicy),
408 ),
409 Effect.retry({
410 while: (e) => e._tag === "ResourcePolicyNotAttached",
411 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(5)]),
412 }),
413 );
414 expect(normalizePolicyDocument(attached)).toBe(normalizePolicyDocument(resourcePolicy));
415
416 // Re-deploy the identical PolicyDocument — must be a clean no-op.
417 const redeployed = yield* stack.deploy(
418 Effect.gen(function* () {
419 return yield* Secret("PolicySecret", {
420 description: "resource policy round-trip",
421 secretString: Redacted.make("policy-value"),
422 resourcePolicy,
423 });
424 }),
425 );
426 expect(redeployed.secretArn).toBe(secret.secretArn);
427
428 const afterRedeploy = yield* secretsmanager.getResourcePolicy({ SecretId: secret.secretArn });
429 expect(afterRedeploy.ResourcePolicy).toBeTruthy();
430 expect(normalizePolicyDocument(afterRedeploy.ResourcePolicy ?? "")).toBe(
431 normalizePolicyDocument(resourcePolicy),
432 );
433
434 // Removing the prop detaches the policy.
435 yield* stack.deploy(
436 Effect.gen(function* () {
437 return yield* Secret("PolicySecret", {
438 description: "resource policy round-trip",
439 secretString: Redacted.make("policy-value"),
440 });
441 }),
442 );
443 const removed = yield* secretsmanager.getResourcePolicy({ SecretId: secret.secretArn });
444 expect(removed.ResourcePolicy).toBeUndefined();
445
446 yield* stack.destroy();
447 yield* assertSecretDeleted(secret.secretArn);
448 }),
449 { tags: ["provider:aws", "provider:aws:iam", "provider:aws:secretsmanager", "live"] },
450 );
451
452 // Canonical `list()` test (AWS account/region-scoped collection): deploy a real
453 // secret, resolve the provider from context via the typed `findProvider`, call
454 // `list()`, and assert the deployed secret appears in the exhaustively-paginated
455 // result. `listSecrets` is eventually consistent, so the assertion retries with
456 // a bounded schedule until the new secret surfaces.
457 test.provider(
458 "list enumerates the deployed secret",
459 (stack) =>
460 Effect.gen(function* () {
461 yield* stack.destroy();
462
463 const secret = yield* stack.deploy(
464 Effect.gen(function* () {
465 return yield* Secret("ListSecret", {
466 description: "list lifecycle op coverage",
467 secretString: Redacted.make("super-secret-value"),
468 tags: { Environment: "test" },
469 });
470 }),
471 );
472
473 const provider = yield* Provider.findProvider(Secret);
474
475 yield* Effect.gen(function* () {
476 const all = yield* provider.list();
477 const found = all.find((s) => s.secretArn === secret.secretArn);
478 if (!found) {
479 return yield* Effect.fail(new SecretNotListed());
480 }
481 // `list` hydrates the exact `read` Attributes shape (no plaintext value).
482 expect(found.secretName).toBe(secret.secretName);
483 expect(found.versionId).toBeUndefined();
484 expect(found.tags.Environment).toBe("test");
485 }).pipe(
486 Effect.retry({
487 while: (e) => e._tag === "SecretNotListed",
488 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
489 }),
490 );
491
492 yield* stack.destroy();
493 }),
494 { tags: ["provider:aws", "provider:aws:secretsmanager", "live"] },
495 );
496
497 // #2688: `read` must observe the live AWSCURRENT version and the resource
498 // policy, or a value written out of band reads as `unchanged`.
499 test.provider(
500 "drift detects and repairs an out-of-band secret value",
501 (stack) =>
502 Effect.gen(function* () {
503 yield* stack.destroy();
504
505 const secret = yield* stack.deploy(
506 Secret("DriftSecret", {
507 secretString: Redacted.make("declared"),
508 tags: { team: "declared" },
509 }),
510 );
511 expect((yield* driftOf(stack, "DriftSecret"))?.action).toBe("unchanged");
512
513 yield* secretsmanager.putSecretValue({
514 SecretId: secret.secretArn,
515 SecretString: "out-of-band",
516 });
517
518 const drifted = yield* waitForDrift(stack, "DriftSecret", "drifted");
519 expect(drifted?.attr?.versionId).not.toBe(secret.versionId);
520
521 const repaired = yield* repairDrift(stack);
522 expect(repaired.resources.DriftSecret?.action).toBe("repaired");
523 expect((yield* waitForDrift(stack, "DriftSecret", "unchanged"))?.action).toBe("unchanged");
524 const current = yield* secretsmanager.getSecretValue({ SecretId: secret.secretArn });
525 expect(unwrapString(current.SecretString)).toBe("declared");
526
527 yield* stack.destroy();
528 yield* assertSecretDeleted(secret.secretArn);
529 }),
530 { tags: ["provider:aws", "provider:aws:secretsmanager", "live"], timeout: 180_000 },
531 );