@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, 04:45 UTC

main
ts 905 lines 32.8 KB
Raw
1 import { createHash } from "node:crypto";
2 import * as AWS from "@alchemy.run/aws";
3 import { makeS3State } from "@alchemy.run/aws";
4 import { createStateBucketName } from "@alchemy.run/aws/StateStore/State";
5 import { describe, expect } from "@alchemy.run/test";
6 import * as Test from "@alchemy.run/test/Alchemy";
7 import * as Core from "@alchemy.run/test/Core";
8 import * as kms from "@distilled.cloud/aws/kms";
9 import * as s3 from "@distilled.cloud/aws/s3";
10 import * as sqs from "@distilled.cloud/aws/sqs";
11 import * as Alchemy from "alchemy";
12 import { StateStoreError, type ResourceState, type StateService } from "alchemy/State";
13 import * as Effect from "effect/Effect";
14 import * as Logger from "effect/Logger";
15 import * as Redacted from "effect/Redacted";
16 import * as References from "effect/References";
17 import * as Schedule from "effect/Schedule";
18 import * as Stream from "effect/Stream";
19
20 const { test } = Test.make({ providers: AWS.providers() });
21
22 test.provider(
23 "PR1587 fresh state services skip matching KMS encryption writes",
24 (stack) =>
25 Effect.gen(function* () {
26 yield* stack.destroy();
27 const { bucket, first, second } = yield* stack.deploy(
28 Effect.gen(function* () {
29 const first = yield* AWS.KMS.Key("FirstStateKey", {
30 deletionWindow: "7 days",
31 });
32 const second = yield* AWS.KMS.Key("SecondStateKey", {
33 deletionWindow: "7 days",
34 });
35 const bucket = yield* AWS.S3.Bucket("IdentityStateBucket", {
36 encryption: {
37 sseAlgorithm: "aws:kms",
38 kmsMasterKeyId: first.keyArn,
39 },
40 });
41 return { bucket, first, second };
42 }),
43 );
44 const initialize = (key: string, bucketKeyEnabled?: boolean) =>
45 Effect.gen(function* () {
46 const state = yield* makeS3State({
47 bucketName: bucket.bucketName,
48 prefix: "pr1587",
49 encryption: {
50 sseAlgorithm: "aws:kms",
51 kmsMasterKeyId: key,
52 bucketKeyEnabled,
53 },
54 });
55 return yield* state.listStacks();
56 });
57 expect(yield* initialize(first.keyArn)).toEqual([]);
58 const observed = yield* s3.getBucketEncryption({
59 Bucket: bucket.bucketName,
60 });
61 const decoded =
62 observed.ServerSideEncryptionConfiguration!.Rules[0]!.ApplyServerSideEncryptionByDefault!
63 .KMSMasterKeyID;
64 expect(Redacted.isRedacted(decoded)).toBe(true);
65 expect(Redacted.isRedacted(decoded) ? Redacted.value(decoded) : decoded).toBe(first.keyArn);
66 const write = s3.putBucketEncryption({
67 Bucket: bucket.bucketName,
68 ServerSideEncryptionConfiguration: observed.ServerSideEncryptionConfiguration!,
69 });
70 const deniedWrite = write.pipe(
71 Effect.as(false),
72 Effect.catchTag("AccessDeniedException", () => Effect.succeed(true)),
73 );
74 yield* Effect.gen(function* () {
75 yield* s3.putBucketPolicy({
76 Bucket: bucket.bucketName,
77 Policy: JSON.stringify({
78 Version: "2012-10-17",
79 Statement: [
80 {
81 Effect: "Deny",
82 Principal: "*",
83 Action: "s3:PutEncryptionConfiguration",
84 Resource: bucket.bucketArn,
85 },
86 ],
87 }),
88 });
89 expect(
90 yield* deniedWrite.pipe(
91 Effect.repeat({
92 until: Boolean,
93 schedule: Schedule.spaced("1 second"),
94 times: 8,
95 }),
96 ),
97 ).toBe(true);
98 // Each call constructs a fresh service, bypassing the per-service initialization cache.
99 expect(yield* initialize(first.keyArn)).toEqual([]);
100 expect(yield* initialize(first.keyArn, false)).toEqual([]);
101 expect(yield* deniedWrite).toBe(true);
102 }).pipe(
103 Effect.ensuring(s3.deleteBucketPolicy({ Bucket: bucket.bucketName }).pipe(Effect.orDie)),
104 );
105 yield* write.pipe(
106 Effect.retry({
107 while: (error) => error._tag === "AccessDeniedException",
108 schedule: Schedule.spaced("1 second"),
109 times: 8,
110 }),
111 );
112
113 yield* initialize(second.keyArn);
114 const changed = (yield* s3.getBucketEncryption({
115 Bucket: bucket.bucketName,
116 })).ServerSideEncryptionConfiguration!.Rules[0]!.ApplyServerSideEncryptionByDefault!
117 .KMSMasterKeyID;
118 expect(Redacted.isRedacted(changed) ? Redacted.value(changed) : changed).toBe(second.keyArn);
119 yield* initialize(second.keyArn, true);
120 expect(
121 (yield* s3.getBucketEncryption({ Bucket: bucket.bucketName }))
122 .ServerSideEncryptionConfiguration?.Rules[0]?.BucketKeyEnabled,
123 ).toBe(true);
124 const defaultState = yield* makeS3State({
125 bucketName: bucket.bucketName,
126 prefix: "pr1587",
127 });
128 expect(yield* defaultState.listStacks()).toEqual([]);
129 const defaults = (yield* s3.getBucketEncryption({
130 Bucket: bucket.bucketName,
131 })).ServerSideEncryptionConfiguration!.Rules[0]!;
132 expect(defaults.ApplyServerSideEncryptionByDefault?.SSEAlgorithm).toBe("AES256");
133 expect(defaults.ApplyServerSideEncryptionByDefault?.KMSMasterKeyID).toBeUndefined();
134 expect(defaults.BucketKeyEnabled ?? false).toBe(false);
135 expect(defaults.BlockedEncryptionTypes?.EncryptionType).toEqual(["NONE"]);
136 yield* stack.destroy();
137 const absent = yield* s3.getBucketLocation({ Bucket: bucket.bucketName }).pipe(
138 Effect.as(false),
139 Effect.catchTag("NoSuchBucket", () => Effect.succeed(true)),
140 Effect.repeat({
141 until: Boolean,
142 schedule: Schedule.spaced("1 second"),
143 times: 8,
144 }),
145 );
146 expect(absent).toBe(true);
147 for (const key of [first, second]) {
148 expect((yield* kms.describeKey({ KeyId: key.keyId })).KeyMetadata?.KeyState).toBe(
149 "PendingDeletion",
150 );
151 }
152 }),
153 {
154 tags: [
155 "provider:aws",
156 "provider:aws:kms",
157 "provider:aws:s3",
158 "provider:aws:statestore",
159 "live",
160 ],
161 timeout: 120_000,
162 },
163 );
164
165 for (const blocked of ["SSE-C", "NONE"] as const) {
166 test.provider(
167 `state initialization restores encryption defaults after external ${blocked} settings`,
168 (stack) =>
169 Effect.gen(function* () {
170 yield* stack.destroy();
171 const bucket = yield* stack.deploy(AWS.S3.Bucket("EncryptionBlocksStateBucket", {}));
172 const initialize = Effect.gen(function* () {
173 const state = yield* makeS3State({
174 bucketName: bucket.bucketName,
175 prefix: "pr1588",
176 });
177 return yield* state.listStacks();
178 });
179 expect(yield* initialize).toEqual([]);
180 yield* s3.putBucketEncryption({
181 Bucket: bucket.bucketName,
182 ServerSideEncryptionConfiguration: {
183 Rules: [
184 {
185 ApplyServerSideEncryptionByDefault: { SSEAlgorithm: "aws:kms" },
186 BlockedEncryptionTypes: { EncryptionType: [blocked] },
187 },
188 ],
189 },
190 });
191 const before = (yield* s3.getBucketEncryption({
192 Bucket: bucket.bucketName,
193 })).ServerSideEncryptionConfiguration!.Rules[0]!;
194 expect(before.BlockedEncryptionTypes?.EncryptionType).toEqual([blocked]);
195 expect(before.ApplyServerSideEncryptionByDefault?.SSEAlgorithm).toBe("aws:kms");
196 // Re-running the generator constructs a fresh state service and rechecks cloud configuration.
197 expect(yield* initialize).toEqual([]);
198 const after = (yield* s3.getBucketEncryption({
199 Bucket: bucket.bucketName,
200 })).ServerSideEncryptionConfiguration!.Rules[0]!;
201 expect(after.ApplyServerSideEncryptionByDefault?.SSEAlgorithm).toBe("AES256");
202 expect(after.ApplyServerSideEncryptionByDefault?.KMSMasterKeyID).toBeUndefined();
203 expect(after.BucketKeyEnabled ?? false).toBe(false);
204 expect(after.BlockedEncryptionTypes?.EncryptionType).toEqual(["NONE"]);
205 yield* stack.destroy();
206 const absent = yield* s3.getBucketLocation({ Bucket: bucket.bucketName }).pipe(
207 Effect.as(false),
208 Effect.catchTag("NoSuchBucket", () => Effect.succeed(true)),
209 Effect.repeat({
210 until: Boolean,
211 schedule: Schedule.spaced("1 second"),
212 times: 8,
213 }),
214 );
215 expect(absent).toBe(true);
216 }),
217 {
218 tags: ["provider:aws", "provider:aws:s3", "provider:aws:statestore", "live"],
219 timeout: 120_000,
220 },
221 );
222 }
223
224 test.provider(
225 "state services manage encryption blocks and restore defaults on removal",
226 (stack) =>
227 Effect.gen(function* () {
228 yield* stack.destroy();
229 const { bucket, blockedBucket } = yield* stack.deploy(
230 Effect.gen(function* () {
231 const bucket = yield* AWS.S3.Bucket("ManagedStateEncryptionBlocks", {});
232 const blockedBucket = yield* AWS.S3.Bucket("BlockedNoOpStateBucket", {});
233 return { bucket, blockedBucket };
234 }),
235 );
236 const initialize = (
237 blockedEncryptionTypes: AWS.S3.BucketEncryption["blockedEncryptionTypes"],
238 bucketName = bucket.bucketName,
239 ) =>
240 Effect.gen(function* () {
241 const state = yield* makeS3State({
242 bucketName,
243 prefix: "pr1588-managed",
244 encryption: { sseAlgorithm: "AES256", blockedEncryptionTypes },
245 });
246 return yield* state.listStacks();
247 });
248 const readRule = s3
249 .getBucketEncryption({ Bucket: bucket.bucketName })
250 .pipe(Effect.map((result) => result.ServerSideEncryptionConfiguration!.Rules[0]!));
251 const block = s3.putBucketEncryption({
252 Bucket: bucket.bucketName,
253 ServerSideEncryptionConfiguration: {
254 Rules: [
255 {
256 ApplyServerSideEncryptionByDefault: { SSEAlgorithm: "AES256" },
257 BlockedEncryptionTypes: { EncryptionType: ["SSE-C"] },
258 },
259 ],
260 },
261 });
262 yield* block;
263 expect(yield* initialize([])).toEqual([]);
264 expect((yield* readRule).BlockedEncryptionTypes?.EncryptionType).toEqual(["NONE"]);
265 yield* block;
266 expect(yield* initialize([])).toEqual([]);
267 expect((yield* readRule).BlockedEncryptionTypes?.EncryptionType).toEqual(["NONE"]);
268 expect(yield* initialize(["SSE-C"])).toEqual([]);
269 expect((yield* readRule).BlockedEncryptionTypes?.EncryptionType).toEqual(["SSE-C"]);
270 expect(yield* initialize(undefined)).toEqual([]);
271 expect((yield* readRule).BlockedEncryptionTypes?.EncryptionType).toEqual(["NONE"]);
272
273 // Keep each deny policy until bucket deletion; policy removal is eventually consistent.
274 const settings: "SSE-C"[][] = [[], ["SSE-C"]];
275 for (const types of settings) {
276 const target = types.length ? blockedBucket : bucket;
277 expect(yield* initialize(types, target.bucketName)).toEqual([]);
278 const rule = (yield* s3.getBucketEncryption({
279 Bucket: target.bucketName,
280 })).ServerSideEncryptionConfiguration!.Rules[0]!;
281 expect(rule.BlockedEncryptionTypes?.EncryptionType).toEqual(
282 types.length ? ["SSE-C"] : ["NONE"],
283 );
284 const probeWrite = s3
285 .putBucketEncryption({
286 Bucket: target.bucketName,
287 ServerSideEncryptionConfiguration: {
288 Rules: [
289 {
290 ApplyServerSideEncryptionByDefault: {
291 SSEAlgorithm: "AES256",
292 },
293 BucketKeyEnabled: false,
294 BlockedEncryptionTypes: {
295 EncryptionType: types.length ? types : ["NONE"],
296 },
297 },
298 ],
299 },
300 })
301 .pipe(
302 Effect.as(false),
303 Effect.catchTag("AccessDeniedException", () => Effect.succeed(true)),
304 );
305 yield* s3.putBucketPolicy({
306 Bucket: target.bucketName,
307 Policy: JSON.stringify({
308 Version: "2012-10-17",
309 Statement: [
310 {
311 Effect: "Deny",
312 Principal: "*",
313 Action: "s3:PutEncryptionConfiguration",
314 Resource: target.bucketArn,
315 },
316 ],
317 }),
318 });
319 expect(
320 yield* probeWrite.pipe(
321 Effect.repeat({
322 until: Boolean,
323 schedule: Schedule.spaced("1 second"),
324 times: 8,
325 }),
326 ),
327 ).toBe(true);
328 expect(yield* initialize([...types, ...types], target.bucketName)).toEqual([]);
329 if (!types.length) {
330 expect(yield* initialize(undefined, target.bucketName)).toEqual([]);
331 }
332 expect(yield* probeWrite).toBe(true);
333 }
334 yield* stack.destroy();
335 for (const target of [bucket, blockedBucket]) {
336 const absent = yield* s3.getBucketLocation({ Bucket: target.bucketName }).pipe(
337 Effect.as(false),
338 Effect.catchTag("NoSuchBucket", () => Effect.succeed(true)),
339 Effect.repeat({
340 until: Boolean,
341 schedule: Schedule.spaced("1 second"),
342 times: 8,
343 }),
344 );
345 expect(absent).toBe(true);
346 }
347 }),
348 {
349 tags: ["provider:aws", "provider:aws:s3", "provider:aws:statestore", "live"],
350 timeout: 120_000,
351 },
352 );
353
354 const STACK = "S3StateStoreTestStack";
355
356 /**
357 * Guaranteed out-of-band cleanup: `deleteStack` is idempotent (list +
358 * batched delete), and `Effect.orDie` collapses the finalizer's error
359 * channel to `never` so it is a valid `Effect.ensuring` finalizer.
360 * Every test pre-cleans AND finalizes with this so a failing assertion
361 * can never leave state objects behind in the bucket.
362 */
363 const cleanStage = (state: StateService, stage: string) =>
364 state.deleteStack({ stack: STACK, stage }).pipe(Effect.orDie);
365
366 const resource = (fqn: string, attr: Record<string, unknown>): ResourceState =>
367 ({
368 resourceType: "test:resource",
369 namespace: undefined,
370 fqn,
371 logicalId: fqn,
372 instanceId: `instance-${fqn}`,
373 providerVersion: 1,
374 status: "created",
375 downstream: [],
376 bindings: [],
377 props: {},
378 attr,
379 }) as ResourceState;
380
381 test.provider(
382 "set/get/list/delete round-trips state through S3",
383 (stack) =>
384 Effect.gen(function* () {
385 yield* stack.destroy();
386 const bucket = yield* stack.deploy(AWS.S3.Bucket("StateBucket", { forceDestroy: true }));
387 const state = yield* makeS3State({
388 bucketName: bucket.bucketName,
389 prefix: "test-state",
390 });
391 const stage = "round-trip";
392
393 // start from a clean slate (idempotent)
394 yield* state.deleteStack({ stack: STACK, stage });
395
396 yield* Effect.gen(function* () {
397 const a = resource("Parent/ResourceA", { value: "a" });
398 const b = resource("ResourceB", { value: "b" });
399
400 yield* state.set({ stack: STACK, stage, fqn: a.fqn, value: a });
401 yield* state.set({ stack: STACK, stage, fqn: b.fqn, value: b });
402
403 expect(yield* state.get({ stack: STACK, stage, fqn: a.fqn })).toEqual(a);
404 expect(yield* state.get({ stack: STACK, stage, fqn: "does-not-exist" })).toBeUndefined();
405
406 const fqns = yield* state.list({ stack: STACK, stage });
407 expect([...fqns].sort()).toEqual(["Parent/ResourceA", "ResourceB"]);
408
409 expect(yield* state.listStacks()).toContain(STACK);
410 expect(yield* state.listStages(STACK)).toContain(stage);
411
412 yield* state.delete({ stack: STACK, stage, fqn: a.fqn });
413 expect(yield* state.get({ stack: STACK, stage, fqn: a.fqn })).toBeUndefined();
414 // deleting a missing resource is a no-op
415 yield* state.delete({ stack: STACK, stage, fqn: a.fqn });
416
417 yield* state.deleteStack({ stack: STACK, stage });
418 expect(yield* state.list({ stack: STACK, stage })).toEqual([]);
419 }).pipe(Effect.ensuring(cleanStage(state, stage)));
420 yield* stack.destroy();
421 }),
422 {
423 tags: ["provider:aws", "provider:aws:s3", "provider:aws:statestore", "live"],
424 timeout: 120_000,
425 },
426 );
427
428 // State objects hold generated secrets, and the AWS client logs request
429 // payloads and parsed responses at Debug. A Debug floor where the store is
430 // built (the CLI's run log sets one) must not put state contents in the logs.
431 test.provider(
432 "state contents stay out of Debug logs",
433 (stack) =>
434 Effect.gen(function* () {
435 yield* stack.destroy();
436 const bucket = yield* stack.deploy(
437 AWS.S3.Bucket("LoggingStateBucket", { forceDestroy: true }),
438 );
439 const secret = "state-logging-secret-0123456789";
440 const stage = "logging";
441
442 // Record every log line emitted at a Debug floor.
443 const atDebug = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
444 Effect.gen(function* () {
445 const lines: string[] = [];
446 const logger = Logger.make<unknown, void>((options) => {
447 lines.push(
448 JSON.stringify(options.message, (_, v) => (typeof v === "bigint" ? `${v}` : v)),
449 );
450 });
451 const result = yield* effect.pipe(
452 Effect.provide(Logger.layer([logger])),
453 Effect.provideService(References.MinimumLogLevel, "Debug"),
454 );
455 return { result, logs: lines.join("\n") };
456 });
457
458 const { logs } = yield* atDebug(
459 Effect.gen(function* () {
460 const state = yield* makeS3State({ bucketName: bucket.bucketName, prefix: "logging" });
461 const value = resource("SecretHolder", { token: secret });
462 yield* state.set({ stack: STACK, stage, fqn: value.fqn, value });
463 expect(yield* state.get({ stack: STACK, stage, fqn: value.fqn })).toEqual(value);
464 yield* state.deleteStack({ stack: STACK, stage });
465 }),
466 );
467 expect(logs).not.toContain(secret);
468
469 // Control: the same payload sent directly is logged at Debug, so the
470 // capture above is real.
471 const control = yield* atDebug(
472 s3.putObject({ Bucket: bucket.bucketName, Key: "control", Body: secret }),
473 );
474 expect(control.logs).toContain(secret);
475
476 yield* stack.destroy();
477 }),
478 {
479 tags: ["provider:aws", "provider:aws:s3", "provider:aws:statestore", "live"],
480 timeout: 120_000,
481 },
482 );
483
484 test.provider(
485 "existing state buckets converge to secure defaults",
486 (stack) =>
487 Effect.gen(function* () {
488 const { accountId, region } = yield* AWS.AWSEnvironment.current;
489 const suffix = yield* Effect.sync(() =>
490 createHash("sha256").update(stack.stage).digest("hex").slice(0, 8),
491 );
492 const bucketName = createStateBucketName(`security-${suffix}-${accountId}`, region);
493 const deleteBucket = s3.deleteBucket({ Bucket: bucketName }).pipe(
494 Effect.catchTag("NoSuchBucket", () => Effect.void),
495 Effect.orDie,
496 );
497
498 yield* s3
499 .deleteBucket({ Bucket: bucketName })
500 .pipe(Effect.catchTag("NoSuchBucket", () => Effect.void));
501
502 yield* Effect.gen(function* () {
503 // First create the bucket, then simulate out-of-band configuration
504 // drift. The second state service targets the SAME existing bucket and
505 // must repair every setting from observed cloud state.
506 const initial = yield* makeS3State({
507 bucketName,
508 prefix: "security-test",
509 });
510 yield* initial.listStacks();
511
512 yield* s3.putBucketVersioning({
513 Bucket: bucketName,
514 VersioningConfiguration: { Status: "Suspended" },
515 });
516 yield* s3.putBucketEncryption({
517 Bucket: bucketName,
518 ServerSideEncryptionConfiguration: {
519 Rules: [
520 {
521 ApplyServerSideEncryptionByDefault: {
522 SSEAlgorithm: "aws:kms",
523 },
524 },
525 ],
526 },
527 });
528 yield* s3.putPublicAccessBlock({
529 Bucket: bucketName,
530 PublicAccessBlockConfiguration: {
531 BlockPublicAcls: false,
532 IgnorePublicAcls: false,
533 BlockPublicPolicy: false,
534 RestrictPublicBuckets: false,
535 },
536 });
537 yield* s3.putBucketOwnershipControls({
538 Bucket: bucketName,
539 OwnershipControls: {
540 Rules: [{ ObjectOwnership: "ObjectWriter" }],
541 },
542 });
543
544 // Bucket configuration reads are eventually consistent. Observe the
545 // injected drift before reconciliation, or a stale AES256 read can
546 // incorrectly make the provider conclude that no repair is needed.
547 const driftedEncryption = yield* s3.getBucketEncryption({ Bucket: bucketName }).pipe(
548 Effect.map(
549 (result) =>
550 result.ServerSideEncryptionConfiguration?.Rules?.[0]
551 ?.ApplyServerSideEncryptionByDefault?.SSEAlgorithm,
552 ),
553 Effect.repeat({
554 until: (algorithm) => algorithm === "aws:kms",
555 schedule: Schedule.spaced("1 second"),
556 times: 8,
557 }),
558 );
559 expect(driftedEncryption).toBe("aws:kms");
560
561 const secured = yield* makeS3State({
562 bucketName,
563 prefix: "security-test",
564 });
565 yield* secured.listStacks();
566
567 const versioning = yield* s3.getBucketVersioning({
568 Bucket: bucketName,
569 });
570 expect(versioning.Status).toBe("Enabled");
571
572 const encryption = yield* s3.getBucketEncryption({
573 Bucket: bucketName,
574 });
575 expect(
576 encryption.ServerSideEncryptionConfiguration?.Rules?.[0]
577 ?.ApplyServerSideEncryptionByDefault?.SSEAlgorithm,
578 ).toBe("AES256");
579
580 const publicAccess = yield* s3.getPublicAccessBlock({
581 Bucket: bucketName,
582 });
583 expect(publicAccess.PublicAccessBlockConfiguration).toEqual({
584 BlockPublicAcls: true,
585 IgnorePublicAcls: true,
586 BlockPublicPolicy: true,
587 RestrictPublicBuckets: true,
588 });
589
590 const ownership = yield* s3.getBucketOwnershipControls({
591 Bucket: bucketName,
592 });
593 expect(ownership.OwnershipControls?.Rules?.[0]?.ObjectOwnership).toBe(
594 "BucketOwnerEnforced",
595 );
596 }).pipe(Effect.ensuring(deleteBucket));
597 }),
598 {
599 tags: ["provider:aws", "provider:aws:s3", "provider:aws:statestore", "live"],
600 timeout: 120_000,
601 },
602 );
603
604 test.provider(
605 "stack outputs are stored separately from resources",
606 (stack) =>
607 Effect.gen(function* () {
608 yield* stack.destroy();
609 const bucket = yield* stack.deploy(AWS.S3.Bucket("StateBucket", { forceDestroy: true }));
610 const state = yield* makeS3State({
611 bucketName: bucket.bucketName,
612 prefix: "test-state",
613 });
614 const stage = "outputs";
615
616 yield* state.deleteStack({ stack: STACK, stage });
617
618 yield* Effect.gen(function* () {
619 expect(yield* state.getOutput({ stack: STACK, stage })).toBeUndefined();
620
621 yield* state.setOutput({
622 stack: STACK,
623 stage,
624 value: { url: "https://example.com" },
625 });
626 expect(yield* state.getOutput({ stack: STACK, stage })).toEqual({
627 url: "https://example.com",
628 });
629
630 // the output bookkeeping object must not leak into list()
631 expect(yield* state.list({ stack: STACK, stage })).toEqual([]);
632
633 yield* state.deleteStack({ stack: STACK, stage });
634 expect(yield* state.getOutput({ stack: STACK, stage })).toBeUndefined();
635 }).pipe(Effect.ensuring(cleanStage(state, stage)));
636 yield* stack.destroy();
637 }),
638 {
639 tags: ["provider:aws", "provider:aws:s3", "provider:aws:statestore", "live"],
640 timeout: 120_000,
641 },
642 );
643
644 test.provider(
645 "getReplacedResources returns only replaced resources",
646 (stack) =>
647 Effect.gen(function* () {
648 yield* stack.destroy();
649 const bucket = yield* stack.deploy(AWS.S3.Bucket("StateBucket", { forceDestroy: true }));
650 const state = yield* makeS3State({
651 bucketName: bucket.bucketName,
652 prefix: "test-state",
653 });
654 const stage = "replaced";
655
656 yield* state.deleteStack({ stack: STACK, stage });
657
658 yield* Effect.gen(function* () {
659 const created = resource("Created", { value: "created" });
660 const replaced = {
661 ...resource("Replaced", { value: "replaced" }),
662 status: "replaced",
663 } as ResourceState;
664
665 yield* state.set({
666 stack: STACK,
667 stage,
668 fqn: created.fqn,
669 value: created,
670 });
671 yield* state.set({
672 stack: STACK,
673 stage,
674 fqn: replaced.fqn,
675 value: replaced,
676 });
677
678 const result = yield* state.getReplacedResources({
679 stack: STACK,
680 stage,
681 });
682 expect(result.map((r) => r.fqn)).toEqual(["Replaced"]);
683 }).pipe(Effect.ensuring(cleanStage(state, stage)));
684 yield* stack.destroy();
685 }),
686 {
687 tags: ["provider:aws", "provider:aws:s3", "provider:aws:statestore", "live"],
688 timeout: 120_000,
689 },
690 );
691
692 test.provider(
693 "a second S3 state store cannot write a stage another deploy holds",
694 (stack) =>
695 Effect.gen(function* () {
696 yield* stack.destroy();
697 const bucket = yield* stack.deploy(AWS.S3.Bucket("StateBucket", { forceDestroy: true }));
698 const options = { bucketName: bucket.bucketName, prefix: "lease" };
699 const stage = "race";
700 const created = resource("Vpc", { vpcId: "vpc-aaaa" });
701 const request = { stack: STACK, stage, fqn: created.fqn, value: created };
702
703 yield* Effect.scoped(
704 Effect.gen(function* () {
705 const held = yield* makeS3State(options);
706 yield* held.set(request);
707 const other = yield* makeS3State(options);
708 const error = yield* other
709 .set({ ...request, value: { ...created, attr: { vpcId: "vpc-bbbb" } } })
710 .pipe(Effect.flip);
711 expect(error).toBeInstanceOf(StateStoreError);
712 expect(error.message).toContain("another deploy holds the S3 state lock");
713 expect(yield* held.get({ stack: STACK, stage, fqn: created.fqn })).toMatchObject({
714 attr: { vpcId: "vpc-aaaa" },
715 });
716 // The lease object is not a resource row.
717 expect(yield* held.list({ stack: STACK, stage })).toEqual([created.fqn]);
718 }),
719 );
720
721 // The first store's scope has closed, so the lease is expired and the
722 // next deploy of this stage can write.
723 yield* Effect.scoped(
724 Effect.gen(function* () {
725 const next = yield* makeS3State(options);
726 yield* next.set({ ...request, value: { ...created, attr: { vpcId: "vpc-cccc" } } });
727 expect(yield* next.get({ stack: STACK, stage, fqn: created.fqn })).toMatchObject({
728 attr: { vpcId: "vpc-cccc" },
729 });
730 yield* next.deleteStack({ stack: STACK, stage });
731 }),
732 );
733
734 // Closing the store deletes its lease, so a destroyed stage is no
735 // longer listed.
736 const after = yield* makeS3State(options);
737 expect(yield* after.listStages(STACK)).not.toContain(stage);
738 yield* stack.destroy();
739 }),
740 {
741 tags: ["provider:aws", "provider:aws:s3", "provider:aws:statestore", "live"],
742 timeout: 120_000,
743 },
744 );
745
746 test.provider(
747 "an expired S3 state lease can be taken and a live one cannot",
748 (stack) =>
749 Effect.gen(function* () {
750 yield* stack.destroy();
751 const bucket = yield* stack.deploy(AWS.S3.Bucket("StateBucket", { forceDestroy: true }));
752 const prefix = "lease-expiry";
753 const state = yield* makeS3State({ bucketName: bucket.bucketName, prefix });
754 const expiredStage = "expired";
755 const heldStage = "held";
756 const leaseKey = (stage: string) => `${prefix}/${STACK}/${stage}/__lease__.json`;
757 const putLease = (stage: string, expiresAt: number) =>
758 s3.putObject({
759 Bucket: bucket.bucketName,
760 Key: leaseKey(stage),
761 Body: JSON.stringify({ token: "foreign", expiresAt }),
762 ContentType: "application/json",
763 });
764
765 yield* putLease(expiredStage, 0);
766 const created = resource("Group", { groupId: "sg-aaaa" });
767 yield* state.set({
768 stack: STACK,
769 stage: expiredStage,
770 fqn: created.fqn,
771 value: created,
772 });
773 expect(yield* state.list({ stack: STACK, stage: expiredStage })).toEqual([created.fqn]);
774
775 yield* putLease(heldStage, Date.now() + 60_000);
776 const blocked = yield* state
777 .set({
778 stack: STACK,
779 stage: heldStage,
780 fqn: created.fqn,
781 value: created,
782 })
783 .pipe(Effect.flip);
784 expect(blocked).toBeInstanceOf(StateStoreError);
785 expect(blocked.message).toContain("another deploy holds the S3 state lock");
786
787 yield* s3.deleteObject({ Bucket: bucket.bucketName, Key: leaseKey(heldStage) });
788 yield* state.deleteStack({ stack: STACK, stage: expiredStage });
789 yield* state.deleteStack({ stack: STACK, stage: heldStage });
790 yield* stack.destroy();
791 }),
792 {
793 tags: ["provider:aws", "provider:aws:s3", "provider:aws:statestore", "live"],
794 timeout: 120_000,
795 },
796 );
797
798 // A large deploy can queue its lease refresh behind thousands of state
799 // writes until the lease lapses. Nobody else took it, so the deploy keeps
800 // going; a deploy that did take it wins.
801 test.provider(
802 "a lease that lapsed while still held is taken back unless another deploy took it",
803 (stack) =>
804 Effect.gen(function* () {
805 yield* stack.destroy();
806 const bucket = yield* stack.deploy(AWS.S3.Bucket("StateBucket", { forceDestroy: true }));
807 const options = { bucketName: bucket.bucketName, prefix: "lease-lapse" };
808 const stage = "lapse";
809 const key = `lease-lapse/${STACK}/${stage}/__lease__.json`;
810 const created = resource("Queue", { queueUrl: "https://example.com/a" });
811 const request = { stack: STACK, stage, fqn: created.fqn, value: created };
812 const lapse = Effect.gen(function* () {
813 const object = yield* s3.getObject({ Bucket: bucket.bucketName, Key: key });
814 const lease = JSON.parse(yield* Stream.mkString(Stream.decodeText(object.Body!)));
815 yield* s3.putObject({
816 Bucket: bucket.bucketName,
817 Key: key,
818 Body: JSON.stringify({ ...lease, expiresAt: 0 }),
819 ContentType: "application/json",
820 });
821 });
822 // Past the store's 5s trust in its last lease check.
823 const pastCheck = Effect.sleep("6 seconds");
824
825 yield* Effect.scoped(
826 Effect.gen(function* () {
827 const held = yield* makeS3State(options);
828 yield* held.set(request);
829 yield* lapse;
830 yield* pastCheck;
831 yield* held.set({ ...request, value: { ...created, attr: { queueUrl: "b" } } });
832 expect(yield* held.get({ stack: STACK, stage, fqn: created.fqn })).toMatchObject({
833 attr: { queueUrl: "b" },
834 });
835
836 // Lapsed again, and this time another deploy takes it.
837 yield* lapse;
838 yield* Effect.scoped(
839 Effect.gen(function* () {
840 const other = yield* makeS3State(options);
841 yield* other.list({ stack: STACK, stage });
842 yield* pastCheck;
843 const lost = yield* held.set(request).pipe(Effect.flip);
844 expect(lost.message).toContain("was lost mid-run");
845 yield* other.deleteStack({ stack: STACK, stage });
846 }),
847 );
848 }),
849 );
850 yield* stack.destroy();
851 }),
852 {
853 tags: ["provider:aws", "provider:aws:s3", "provider:aws:statestore", "live"],
854 timeout: 120_000,
855 },
856 );
857
858 // Each deploy/destroy of a Stack declaring the S3 state store releases its
859 // lease when it finishes, so a file can redeploy and destroy that Stack.
860 const s3Stack = { providers: AWS.providers(), state: AWS.state({ backend: "s3" }) };
861 const s3Harness = Test.make(s3Stack);
862
863 const S3StateStack = Alchemy.Stack(
864 "S3StateHarness",
865 s3Stack,
866 Effect.gen(function* () {
867 const queue = yield* AWS.SQS.Queue("HarnessQueue");
868 return { queueUrl: queue.queueUrl };
869 }),
870 );
871
872 describe(
873 "a Stack with the S3 state store",
874 { tags: ["provider:aws", "provider:aws:sqs", "provider:aws:statestore", "live"] },
875 () => {
876 const deployed = s3Harness.beforeAll(s3Harness.deploy(S3StateStack), { timeout: 120_000 });
877
878 s3Harness.afterAll.skipIf(!!process.env.NO_DESTROY)(
879 Effect.gen(function* () {
880 const { queueUrl } = yield* deployed;
881 yield* s3Harness.destroy(S3StateStack);
882 const gone = yield* Core.withProviders(
883 sqs.getQueueAttributes({ QueueUrl: queueUrl, AttributeNames: ["QueueArn"] }).pipe(
884 Effect.as(false),
885 Effect.catchTag("QueueDoesNotExist", () => Effect.succeed(true)),
886 ),
887 s3Stack,
888 "S3StateHarness",
889 );
890 expect(gone).toBe(true);
891 }),
892 { timeout: 120_000 },
893 );
894
895 s3Harness.test(
896 "redeploys after the file's first deploy released its lease",
897 Effect.gen(function* () {
898 const { queueUrl } = yield* deployed;
899 const redeployed = yield* s3Harness.deploy(S3StateStack);
900 expect(redeployed.queueUrl).toBe(queueUrl);
901 }),
902 { timeout: 120_000 },
903 );
904 },
905 );