@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 953 lines 36.6 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { normalizePolicyDocument } from "@alchemy.run/aws/IAM/Policy";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Lambda from "@distilled.cloud/aws/lambda";
6 import * as S3 from "@distilled.cloud/aws/s3";
7 import * as SNS from "@distilled.cloud/aws/sns";
8 import * as SQS from "@distilled.cloud/aws/sqs";
9 import * as Drift from "alchemy/Drift";
10 import * as Output from "alchemy/Output";
11 import { isResourceState, State } from "alchemy/State";
12 import * as Effect from "effect/Effect";
13 import * as Schedule from "effect/Schedule";
14
15 const { test } = Test.make({ providers: AWS.providers() });
16 const waitPolicy = {
17 until: (gone: boolean) => gone,
18 schedule: Schedule.spaced("2 seconds"),
19 times: 9,
20 };
21
22 const filter = (
23 prefix: string,
24 suffix = ".json",
25 reversed = false,
26 ): S3.NotificationConfigurationFilter => {
27 const rules: S3.FilterRule[] = [
28 { Name: "prefix", Value: prefix },
29 { Name: "suffix", Value: suffix },
30 ];
31 return { Key: { FilterRules: reversed ? rules.reverse() : rules } };
32 };
33
34 const canonicalTargets = <
35 T extends {
36 Id?: string;
37 Events: S3.Event[];
38 Filter?: S3.NotificationConfigurationFilter;
39 },
40 >(
41 targets: T[] = [],
42 ) =>
43 targets
44 .map((target) => ({
45 ...target,
46 Events: [...target.Events].sort(),
47 Filter: target.Filter
48 ? {
49 Key: {
50 FilterRules: (target.Filter.Key?.FilterRules ?? [])
51 .map((rule) => ({ ...rule, Name: rule.Name?.toLowerCase() }))
52 .sort((a, b) => (a.Name ?? "").localeCompare(b.Name ?? "")),
53 },
54 }
55 : undefined,
56 }))
57 .sort((a, b) => (a.Id ?? "").localeCompare(b.Id ?? ""));
58
59 const canonical = (configuration: S3.NotificationConfiguration) => ({
60 QueueConfigurations: canonicalTargets(configuration.QueueConfigurations),
61 TopicConfigurations: canonicalTargets(configuration.TopicConfigurations),
62 LambdaFunctionConfigurations: canonicalTargets(configuration.LambdaFunctionConfigurations),
63 EventBridgeConfiguration: configuration.EventBridgeConfiguration,
64 });
65
66 const readNotifications = (bucketName: string) =>
67 S3.getBucketNotificationConfiguration({ Bucket: bucketName });
68
69 const readEventBridgeOwner = (bucketName: string) =>
70 S3.getBucketTagging({ Bucket: bucketName }).pipe(
71 Effect.map(
72 (response) =>
73 response.TagSet.find((tag) => tag.Key === "alchemy:notifications:eventbridge")?.Value,
74 ),
75 Effect.catchTag("NoSuchTagSet", () => Effect.succeed(undefined)),
76 );
77
78 const notificationQueue = Effect.fn(function* (id: string, bucket: AWS.S3.Bucket) {
79 const queue = yield* AWS.SQS.Queue(id);
80 // Keep both the self-reference and bucket dependency in the binding graph.
81 yield* queue.bind("AllowBucketNotifications", {
82 policyStatements: [
83 {
84 Sid: "AllowBucketNotifications",
85 Effect: "Allow",
86 Principal: { Service: "s3.amazonaws.com" },
87 Action: ["sqs:SendMessage"],
88 Resource: [queue.queueArn],
89 Condition: { ArnEquals: { "aws:SourceArn": bucket.bucketArn } },
90 },
91 ],
92 });
93 return queue;
94 });
95
96 const assertBucketDeleted = Effect.fn(function* (bucketName: string) {
97 const gone = yield* readNotifications(bucketName).pipe(
98 Effect.as(false),
99 Effect.catchTag("NoSuchBucket", () => Effect.succeed(true)),
100 Effect.repeat(waitPolicy),
101 );
102 expect(gone).toBe(true);
103 });
104
105 const assertQueueDeleted = Effect.fn(function* (queueUrl: string) {
106 const gone = yield* SQS.getQueueAttributes({
107 QueueUrl: queueUrl,
108 AttributeNames: ["QueueArn"],
109 }).pipe(
110 Effect.as(false),
111 Effect.catchTag("QueueDoesNotExist", () => Effect.succeed(true)),
112 Effect.repeat(waitPolicy),
113 );
114 expect(gone).toBe(true);
115 });
116
117 test.provider(
118 "queue-only notification bindings create, update filters and events, switch destinations, and remove",
119 (stack) =>
120 Effect.gen(function* () {
121 yield* stack.destroy();
122 const definition = (
123 updated = false,
124 destination: "first" | "second" = "first",
125 enabled = true,
126 ) =>
127 Effect.gen(function* () {
128 const bucket = yield* AWS.S3.Bucket("Bucket", {});
129 const first = yield* notificationQueue("FirstQueue", bucket);
130 const second = yield* notificationQueue("SecondQueue", bucket);
131 if (enabled) {
132 yield* bucket.bind("QueueNotifications", {
133 notificationConfiguration: {
134 QueueConfigurations: [
135 {
136 QueueArn: destination === "first" ? first.queueArn : second.queueArn,
137 Events: [updated ? "s3:ObjectRemoved:Delete" : "s3:ObjectCreated:Put"],
138 Filter: updated ? filter("updated/") : filter("initial/", ".txt"),
139 },
140 ],
141 },
142 });
143 }
144 return { bucket, first, second };
145 });
146 const { bucket, first, second } = yield* stack.deploy(definition());
147 const initial = yield* readNotifications(bucket.bucketName);
148 expect(canonical(initial)).toEqual(
149 canonical({
150 QueueConfigurations: [
151 {
152 Id: expect.any(String),
153 QueueArn: first.queueArn,
154 Events: ["s3:ObjectCreated:Put"],
155 Filter: filter("initial/", ".txt"),
156 },
157 ],
158 }),
159 );
160 const expectedPolicy = normalizePolicyDocument({
161 Version: "2012-10-17",
162 Statement: [
163 {
164 Sid: "AllowBucketNotifications",
165 Effect: "Allow",
166 Principal: { Service: "s3.amazonaws.com" },
167 Action: ["sqs:SendMessage"],
168 Resource: [first.queueArn],
169 Condition: { ArnEquals: { "aws:SourceArn": bucket.bucketArn } },
170 },
171 ],
172 });
173 const observedPolicy = yield* SQS.getQueueAttributes({
174 QueueUrl: first.queueUrl,
175 AttributeNames: ["Policy"],
176 }).pipe(
177 Effect.map((response) => normalizePolicyDocument(response.Attributes?.Policy ?? "")),
178 Effect.repeat({
179 until: (policy) => policy === expectedPolicy,
180 schedule: Schedule.spaced("2 seconds"),
181 times: 9,
182 }),
183 );
184 expect(observedPolicy).toEqual(expectedPolicy);
185
186 yield* stack.deploy(definition(true));
187 const updated = yield* readNotifications(bucket.bucketName);
188 expect(canonical(updated)).toEqual(
189 canonical({
190 QueueConfigurations: [
191 {
192 Id: expect.any(String),
193 QueueArn: first.queueArn,
194 Events: ["s3:ObjectRemoved:Delete"],
195 Filter: filter("updated/"),
196 },
197 ],
198 }),
199 );
200
201 // Retain both destinations while switching the notification reference.
202 yield* stack.deploy(definition(true, "second"));
203 const switched = yield* readNotifications(bucket.bucketName);
204 expect(canonical(switched)).toEqual(
205 canonical({
206 QueueConfigurations: [
207 {
208 Id: expect.any(String),
209 QueueArn: second.queueArn,
210 Events: ["s3:ObjectRemoved:Delete"],
211 Filter: filter("updated/"),
212 },
213 ],
214 }),
215 );
216 yield* S3.putBucketNotificationConfiguration({
217 Bucket: bucket.bucketName,
218 NotificationConfiguration: {},
219 });
220 yield* stack.deploy(definition(true, "second"));
221 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical(switched));
222 yield* stack.deploy(definition(true, "second"));
223 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical(switched));
224 yield* stack.deploy(definition(true, "second", false));
225 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical({}));
226
227 yield* stack.destroy();
228 yield* Effect.all(
229 [
230 assertBucketDeleted(bucket.bucketName),
231 assertQueueDeleted(first.queueUrl),
232 assertQueueDeleted(second.queueUrl),
233 ],
234 { concurrency: 3 },
235 );
236 }),
237 {
238 tags: ["provider:aws", "provider:aws:iam", "provider:aws:s3", "provider:aws:sqs", "live"],
239 timeout: 120_000,
240 },
241 );
242
243 test.provider(
244 "mixed notification bindings keep stable IDs across reordering and preserve foreign configurations on removal",
245 (stack) =>
246 Effect.gen(function* () {
247 yield* stack.destroy();
248 const definition = (
249 mode: "all" | "without-queue" | "none" = "all",
250 reversed = false,
251 revision = "initial",
252 ) =>
253 Effect.gen(function* () {
254 const bucket = yield* AWS.S3.Bucket("Bucket", { tags: { revision } });
255 const queue = yield* notificationQueue("Queue", bucket);
256 const topic = yield* AWS.SNS.Topic("Topic", {
257 attributes: {
258 Policy: bucket.bucketArn.pipe(
259 Output.mapEffect((bucketArn) =>
260 Effect.sync(() =>
261 JSON.stringify({
262 Version: "2012-10-17",
263 Statement: [
264 {
265 Effect: "Allow",
266 Principal: { Service: "s3.amazonaws.com" },
267 Action: "sns:Publish",
268 Resource: "*",
269 Condition: {
270 ArnEquals: { "aws:SourceArn": bucketArn },
271 },
272 },
273 ],
274 }),
275 ),
276 ),
277 ),
278 },
279 });
280 const fn = yield* AWS.Lambda.Function("Target", {
281 main: new URL("./fixtures/notification-target.ts", import.meta.url).pathname,
282 isExternal: true,
283 handler: "handler",
284 functionUrl: false,
285 });
286 yield* AWS.Lambda.Permission("AllowBucket", {
287 action: "lambda:InvokeFunction",
288 functionName: fn.functionName,
289 principal: "s3.amazonaws.com",
290 sourceArn: bucket.bucketArn,
291 });
292 const events: S3.Event[] = ["s3:ObjectCreated:Put", "s3:ObjectCreated:Post"];
293 if (reversed) events.reverse();
294 const bindings = [
295 ...(mode === "all"
296 ? [
297 bucket.bind("QueueNotifications", {
298 notificationConfiguration: {
299 QueueConfigurations: (reversed ? ["b", "a"] : ["a", "b"]).map((key) => ({
300 QueueArn: queue.queueArn,
301 Events: events,
302 Filter: filter(`owned/queue-${key}/`, ".json", reversed),
303 })),
304 },
305 }),
306 ]
307 : []),
308 ...(mode !== "none"
309 ? [
310 bucket.bind("TopicNotifications", {
311 notificationConfiguration: {
312 TopicConfigurations: [
313 {
314 TopicArn: topic.topicArn,
315 Events: events,
316 Filter: filter("owned/topic/", ".json", reversed),
317 },
318 ],
319 },
320 }),
321 bucket.bind("LambdaNotifications", {
322 notificationConfiguration: {
323 LambdaFunctionConfigurations: [
324 {
325 LambdaFunctionArn: fn.functionArn,
326 Events: events,
327 Filter: filter("owned/lambda/", ".json", reversed),
328 },
329 ],
330 },
331 }),
332 ]
333 : []),
334 ];
335 yield* Effect.all(reversed ? bindings.reverse() : bindings);
336 return { bucket, queue, topic, fn };
337 });
338 const { bucket, queue, topic, fn } = yield* stack.deploy(definition());
339 const owned = yield* readNotifications(bucket.bucketName);
340 expect(owned.QueueConfigurations).toHaveLength(2);
341 expect(owned.TopicConfigurations).toHaveLength(1);
342 expect(owned.LambdaFunctionConfigurations).toHaveLength(1);
343 const ids = [
344 ...owned.QueueConfigurations!,
345 ...owned.TopicConfigurations!,
346 ...owned.LambdaFunctionConfigurations!,
347 ].map((target) => target.Id);
348 expect(ids.every((id) => typeof id === "string" && id.length > 0)).toBe(true);
349 expect(new Set(ids).size).toBe(4);
350 const actual = canonical(owned);
351 const events = ["s3:ObjectCreated:Post", "s3:ObjectCreated:Put"];
352 expect(actual.QueueConfigurations).toEqual(
353 expect.arrayContaining(
354 ["a", "b"].map((key) => ({
355 Id: expect.any(String),
356 QueueArn: queue.queueArn,
357 Events: events,
358 Filter: filter(`owned/queue-${key}/`),
359 })),
360 ),
361 );
362 expect(actual.TopicConfigurations).toEqual([
363 {
364 Id: expect.any(String),
365 TopicArn: topic.topicArn,
366 Events: events,
367 Filter: filter("owned/topic/"),
368 },
369 ]);
370 expect(actual.LambdaFunctionConfigurations).toEqual([
371 {
372 Id: expect.any(String),
373 LambdaFunctionArn: fn.functionArn,
374 Events: events,
375 Filter: filter("owned/lambda/"),
376 },
377 ]);
378 expect(actual.EventBridgeConfiguration).toBeUndefined();
379 const foreign: S3.NotificationConfiguration = {
380 QueueConfigurations: [
381 {
382 Id: "external-queue",
383 QueueArn: queue.queueArn,
384 Events: ["s3:ObjectCreated:Put"],
385 Filter: filter("external/queue/"),
386 },
387 ],
388 TopicConfigurations: [
389 {
390 Id: "external-topic",
391 TopicArn: topic.topicArn,
392 Events: ["s3:ObjectCreated:Put"],
393 Filter: filter("external/topic/"),
394 },
395 ],
396 LambdaFunctionConfigurations: [
397 {
398 Id: "external-lambda",
399 LambdaFunctionArn: fn.functionArn,
400 Events: ["s3:ObjectCreated:Put"],
401 Filter: filter("external/lambda/"),
402 },
403 ],
404 EventBridgeConfiguration: {},
405 };
406 const combined: S3.NotificationConfiguration = {
407 QueueConfigurations: [...owned.QueueConfigurations!, ...foreign.QueueConfigurations!],
408 TopicConfigurations: [...owned.TopicConfigurations!, ...foreign.TopicConfigurations!],
409 LambdaFunctionConfigurations: [
410 ...owned.LambdaFunctionConfigurations!,
411 ...foreign.LambdaFunctionConfigurations!,
412 ],
413 EventBridgeConfiguration: {},
414 };
415 // Add foreign entries only after deploy; AWS validates all real destinations.
416 yield* S3.putBucketNotificationConfiguration({
417 Bucket: bucket.bucketName,
418 NotificationConfiguration: combined,
419 });
420 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical(combined));
421
422 yield* stack.deploy(definition("all", true));
423 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical(combined));
424 // Exercise the provider's no-change notification sync, not only an engine noop.
425 yield* stack.deploy(definition("all", true, "noop"));
426 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical(combined));
427 yield* stack.deploy(definition("without-queue", true, "noop"));
428 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(
429 canonical({
430 ...combined,
431 QueueConfigurations: foreign.QueueConfigurations,
432 }),
433 );
434 yield* stack.deploy(definition("none", true, "noop"));
435 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical(foreign));
436
437 yield* stack.destroy();
438 yield* Effect.all(
439 [
440 assertBucketDeleted(bucket.bucketName),
441 assertQueueDeleted(queue.queueUrl),
442 SNS.getTopicAttributes({ TopicArn: topic.topicArn }).pipe(
443 Effect.as(false),
444 Effect.catchTag("NotFoundException", () => Effect.succeed(true)),
445 Effect.repeat(waitPolicy),
446 Effect.tap((gone) => Effect.sync(() => expect(gone).toBe(true))),
447 ),
448 Lambda.getFunction({ FunctionName: fn.functionName }).pipe(
449 Effect.as(false),
450 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
451 Effect.repeat(waitPolicy),
452 Effect.tap((gone) => Effect.sync(() => expect(gone).toBe(true))),
453 ),
454 ],
455 { concurrency: 4 },
456 );
457 }),
458 {
459 tags: [
460 "provider:aws",
461 "provider:aws:lambda",
462 "provider:aws:s3",
463 "provider:aws:sns",
464 "provider:aws:sqs",
465 "live",
466 ],
467 timeout: 120_000,
468 },
469 );
470
471 test.provider(
472 "managed EventBridge remains until its final binding is removed after a no-change reconcile",
473 (stack) =>
474 Effect.gen(function* () {
475 yield* stack.destroy();
476 const definition = (bindings: string[], revision = "initial") =>
477 Effect.gen(function* () {
478 const bucket = yield* AWS.S3.Bucket("Bucket", { tags: { revision } });
479 for (const id of bindings) {
480 yield* bucket.bind(id, {
481 notificationConfiguration: { EventBridgeConfiguration: {} },
482 });
483 }
484 return bucket;
485 });
486 const bucket = yield* stack.deploy(definition(["First", "Second"]));
487 const enabled = canonical({ EventBridgeConfiguration: {} });
488 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(enabled);
489 const owner = yield* readEventBridgeOwner(bucket.bucketName);
490 expect(owner).toBeTruthy();
491 yield* stack.deploy(definition(["Second"]));
492 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(enabled);
493 // Force tag sync while keeping the remaining notification binding unchanged.
494 yield* stack.deploy(definition(["Second"], "tag-sync"));
495 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(enabled);
496 expect(yield* readEventBridgeOwner(bucket.bucketName)).toBe(owner);
497 yield* stack.deploy(definition([], "tag-sync"));
498 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical({}));
499 expect(yield* readEventBridgeOwner(bucket.bucketName)).toBeUndefined();
500 yield* stack.destroy();
501 yield* assertBucketDeleted(bucket.bucketName);
502 }),
503 { tags: ["provider:aws", "provider:aws:s3", "live"], timeout: 120_000 },
504 );
505
506 test.provider(
507 "adding and removing identical bindings does not claim a foreign queue target or EventBridge",
508 (stack) =>
509 Effect.gen(function* () {
510 yield* stack.destroy();
511 const definition = (enabled: boolean) =>
512 Effect.gen(function* () {
513 const bucket = yield* AWS.S3.Bucket("Bucket", {});
514 const queue = yield* notificationQueue("Queue", bucket);
515 if (enabled) {
516 yield* bucket.bind("MatchingNotifications", {
517 notificationConfiguration: {
518 QueueConfigurations: [
519 {
520 QueueArn: queue.queueArn,
521 Events: ["s3:ObjectCreated:Put"],
522 Filter: filter("shared/"),
523 },
524 ],
525 EventBridgeConfiguration: {},
526 },
527 });
528 }
529 return { bucket, queue };
530 });
531 const { bucket, queue } = yield* stack.deploy(definition(false));
532 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical({}));
533 const foreign: S3.NotificationConfiguration = {
534 QueueConfigurations: [
535 {
536 Id: "external-identical-target",
537 QueueArn: queue.queueArn,
538 Events: ["s3:ObjectCreated:Put"],
539 Filter: filter("shared/"),
540 },
541 ],
542 EventBridgeConfiguration: {},
543 };
544 yield* S3.putBucketNotificationConfiguration({
545 Bucket: bucket.bucketName,
546 NotificationConfiguration: foreign,
547 });
548 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical(foreign));
549 expect(yield* readEventBridgeOwner(bucket.bucketName)).toBeUndefined();
550 yield* stack.deploy(definition(true));
551 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical(foreign));
552 expect(yield* readEventBridgeOwner(bucket.bucketName)).toBeUndefined();
553 yield* stack.deploy(definition(false));
554 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical(foreign));
555 expect(yield* readEventBridgeOwner(bucket.bucketName)).toBeUndefined();
556 yield* stack.destroy();
557 yield* Effect.all(
558 [assertBucketDeleted(bucket.bucketName), assertQueueDeleted(queue.queueUrl)],
559 { concurrency: 2 },
560 );
561 }),
562 {
563 tags: ["provider:aws", "provider:aws:s3", "provider:aws:sqs", "live"],
564 timeout: 120_000,
565 },
566 );
567
568 test.provider(
569 "legacy Lambda notifications migrate from persisted bindings without claiming new identical external bindings",
570 (stack) =>
571 Effect.gen(function* () {
572 yield* stack.destroy();
573 const definition = (mode: "legacy" | "updated" | "none") =>
574 Effect.gen(function* () {
575 const bucket = yield* AWS.S3.Bucket("Bucket", {});
576 const fn = yield* AWS.Lambda.Function("Target", {
577 main: new URL("./fixtures/notification-target.ts", import.meta.url).pathname,
578 isExternal: true,
579 handler: "handler",
580 functionUrl: false,
581 });
582 yield* AWS.Lambda.Permission("AllowBucket", {
583 action: "lambda:InvokeFunction",
584 functionName: fn.functionName,
585 principal: "s3.amazonaws.com",
586 sourceArn: bucket.bucketArn,
587 });
588 if (mode !== "none") {
589 yield* bucket.bind("ChangingNotifications", {
590 notificationConfiguration: {
591 LambdaFunctionConfigurations: [
592 {
593 LambdaFunctionArn: fn.functionArn,
594 Events: ["s3:ObjectCreated:Put"],
595 Filter: filter(mode === "legacy" ? "owned/original/" : "owned/updated/"),
596 },
597 ],
598 },
599 });
600 yield* bucket.bind(
601 mode === "legacy" ? "RemovedNotifications" : "ExternalNotifications",
602 {
603 notificationConfiguration: {
604 LambdaFunctionConfigurations: [
605 {
606 LambdaFunctionArn: fn.functionArn,
607 Events: ["s3:ObjectCreated:Put"],
608 Filter: filter(mode === "legacy" ? "owned/removed/" : "external/"),
609 },
610 ],
611 },
612 },
613 );
614 }
615 return { bucket, fn };
616 });
617 const { bucket, fn } = yield* stack.deploy(definition("legacy"));
618 const legacyTargets: S3.LambdaFunctionConfiguration[] = [
619 "owned/original/",
620 "owned/removed/",
621 ].map((prefix) => ({
622 LambdaFunctionArn: fn.functionArn,
623 Events: ["s3:ObjectCreated:Put"],
624 Filter: filter(prefix),
625 }));
626 const foreign: S3.LambdaFunctionConfiguration = {
627 Id: "external-lambda",
628 LambdaFunctionArn: fn.functionArn,
629 Events: ["s3:ObjectCreated:Put"],
630 Filter: filter("external/"),
631 };
632 // The legacy provider omitted IDs; S3 assigned them outside Alchemy's namespace.
633 yield* S3.putBucketNotificationConfiguration({
634 Bucket: bucket.bucketName,
635 NotificationConfiguration: {
636 LambdaFunctionConfigurations: [...legacyTargets, foreign],
637 },
638 });
639 const legacy = yield* readNotifications(bucket.bucketName);
640 expect(legacy.LambdaFunctionConfigurations).toHaveLength(3);
641 const legacyIds = legacy
642 .LambdaFunctionConfigurations!.filter((target) => target.Id !== foreign.Id)
643 .map((target) => target.Id!);
644 expect(legacyIds).toHaveLength(2);
645 for (const id of legacyIds) {
646 expect(id).toBeTruthy();
647 expect(id.startsWith("alchemy-notification-")).toBe(false);
648 }
649 yield* Effect.gen(function* () {
650 const state = yield* yield* State;
651 const key = { stack: stack.name, stage: stack.stage, fqn: "Bucket" };
652 const row = yield* state.get(key);
653 if (!row || !isResourceState(row) || !row.attr) {
654 return yield* Effect.fail(new Error("Expected persisted bucket attributes"));
655 }
656 const bindings = row.bindings.map((binding) => ({
657 ...binding,
658 data: {
659 notificationConfiguration: {
660 LambdaFunctionConfigurations:
661 binding.sid === "ChangingNotifications" ? [legacyTargets[0]!] : [legacyTargets[1]!],
662 },
663 },
664 }));
665 expect(bindings).toHaveLength(2);
666 expect(bindings.map((binding) => binding.sid).sort()).toEqual([
667 "ChangingNotifications",
668 "RemovedNotifications",
669 ]);
670 const attr = { ...row.attr };
671 delete attr.managedNotificationConfiguration;
672 const persisted = yield* state.set({
673 ...key,
674 value: { ...row, attr, bindings },
675 });
676 expect(persisted.attr.managedNotificationConfiguration).toBeUndefined();
677 }).pipe(Effect.provide(stack.state));
678
679 yield* stack.deploy(definition("updated"));
680 const updated = yield* readNotifications(bucket.bucketName);
681 const managedId = updated.LambdaFunctionConfigurations?.find(
682 (target) => target.Id !== foreign.Id,
683 )?.Id;
684 expect(managedId).toMatch(/^alchemy-notification-/);
685 expect(canonical(updated)).toEqual(
686 canonical({
687 LambdaFunctionConfigurations: [
688 {
689 Id: managedId,
690 LambdaFunctionArn: fn.functionArn,
691 Events: ["s3:ObjectCreated:Put"],
692 Filter: filter("owned/updated/"),
693 },
694 foreign,
695 ],
696 }),
697 );
698 for (const target of updated.LambdaFunctionConfigurations!) {
699 expect(legacyIds).not.toContain(target.Id);
700 }
701 yield* stack.deploy(definition("updated"));
702 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical(updated));
703 yield* stack.deploy(definition("none"));
704 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(
705 canonical({ LambdaFunctionConfigurations: [foreign] }),
706 );
707 yield* stack.destroy();
708 yield* assertBucketDeleted(bucket.bucketName);
709 const gone = yield* Lambda.getFunction({
710 FunctionName: fn.functionName,
711 }).pipe(
712 Effect.as(false),
713 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
714 Effect.repeat(waitPolicy),
715 );
716 expect(gone).toBe(true);
717 }),
718 {
719 tags: ["provider:aws", "provider:aws:lambda", "provider:aws:s3", "live"],
720 timeout: 120_000,
721 },
722 );
723
724 test.provider(
725 "clears a stale EventBridge ownership marker when notifications and withdrawn bindings already match",
726 (stack) =>
727 Effect.gen(function* () {
728 yield* stack.destroy();
729 const definition = (enabled: boolean) =>
730 Effect.gen(function* () {
731 const bucket = yield* AWS.S3.Bucket("Bucket", {
732 tags: { purpose: "eventbridge-marker-recovery" },
733 });
734 if (enabled) {
735 yield* bucket.bind("EventBridge", {
736 notificationConfiguration: { EventBridgeConfiguration: {} },
737 });
738 }
739 return bucket;
740 });
741 const bucket = yield* stack.deploy(definition(true));
742 const owner = yield* readEventBridgeOwner(bucket.bucketName);
743 expect(owner).toBeTruthy();
744 yield* S3.putBucketNotificationConfiguration({
745 Bucket: bucket.bucketName,
746 NotificationConfiguration: {},
747 });
748 // Model withdrawn bindings with a lost snapshot and an uncleared cloud marker.
749 yield* Effect.gen(function* () {
750 const state = yield* yield* State;
751 const key = { stack: stack.name, stage: stack.stage, fqn: "Bucket" };
752 const row = yield* state.get(key);
753 if (!row || !isResourceState(row) || !row.attr) {
754 return yield* Effect.fail(new Error("Expected persisted bucket attributes"));
755 }
756 const attr = { ...row.attr };
757 delete attr.managedNotificationConfiguration;
758 yield* state.set({ ...key, value: { ...row, attr, bindings: [] } });
759 }).pipe(Effect.provide(stack.state));
760 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical({}));
761 expect(yield* readEventBridgeOwner(bucket.bucketName)).toBe(owner);
762 const retained = yield* stack.deploy(definition(false));
763 expect(retained.bucketName).toBe(bucket.bucketName);
764 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical({}));
765 expect(yield* readEventBridgeOwner(bucket.bucketName)).toBeUndefined();
766 expect((yield* S3.getBucketTagging({ Bucket: bucket.bucketName })).TagSet).toContainEqual({
767 Key: "purpose",
768 Value: "eventbridge-marker-recovery",
769 });
770 yield* stack.destroy();
771 yield* assertBucketDeleted(bucket.bucketName);
772 }),
773 { tags: ["provider:aws", "provider:aws:s3", "live"], timeout: 120_000 },
774 );
775
776 test.provider(
777 "EventBridge binding removal recovers ownership after losing the committed notification snapshot",
778 (stack) =>
779 Effect.gen(function* () {
780 yield* stack.destroy();
781 const definition = (enabled: boolean) =>
782 Effect.gen(function* () {
783 const bucket = yield* AWS.S3.Bucket("Bucket", {
784 tags: { purpose: "notifications-crash-recovery" },
785 });
786 if (enabled) {
787 yield* bucket.bind("EventBridge", {
788 notificationConfiguration: { EventBridgeConfiguration: {} },
789 });
790 }
791 return bucket;
792 });
793 const bucket = yield* stack.deploy(definition(true));
794 const enabled = canonical({ EventBridgeConfiguration: {} });
795 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(enabled);
796 const owner = yield* readEventBridgeOwner(bucket.bucketName);
797 expect(owner).toBeTruthy();
798
799 // Model a lost attributes commit without deleting the live bucket or its state row.
800 yield* Effect.gen(function* () {
801 const state = yield* yield* State;
802 const key = { stack: stack.name, stage: stack.stage, fqn: "Bucket" };
803 const row = yield* state.get(key);
804 if (!row || !isResourceState(row) || !row.attr) {
805 return yield* Effect.fail(new Error("Expected persisted bucket attributes"));
806 }
807 expect(row.attr.managedNotificationConfiguration?.EventBridgeConfiguration).toEqual({});
808 const attr = { ...row.attr };
809 delete attr.managedNotificationConfiguration;
810 const persisted = yield* state.set({ ...key, value: { ...row, attr } });
811 expect(persisted.attr.managedNotificationConfiguration).toBeUndefined();
812 expect(persisted.attr.bucketName).toBe(bucket.bucketName);
813 }).pipe(Effect.provide(stack.state));
814 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(enabled);
815 expect(yield* readEventBridgeOwner(bucket.bucketName)).toBe(owner);
816
817 const retained = yield* stack.deploy(definition(false));
818 expect(retained.bucketName).toBe(bucket.bucketName);
819 expect(canonical(yield* readNotifications(bucket.bucketName))).toEqual(canonical({}));
820 expect(yield* readEventBridgeOwner(bucket.bucketName)).toBeUndefined();
821 expect((yield* S3.getBucketTagging({ Bucket: bucket.bucketName })).TagSet).toContainEqual({
822 Key: "purpose",
823 Value: "notifications-crash-recovery",
824 });
825 yield* stack.destroy();
826 yield* assertBucketDeleted(bucket.bucketName);
827 }),
828 { tags: ["provider:aws", "provider:aws:s3", "live"], timeout: 120_000 },
829 );
830
831 // #1536: state outlives every physical resource of an upload pipeline
832 // (bucket -> topic -> queue subscription). `drift --repair` must recreate
833 // them in dependency order so the bucket's topic notification and the
834 // subscription land on live targets, and the next deploy hands downstream
835 // consumers the recreated subscription ARN.
836 test.provider(
837 "drift repair recreates a deleted bucket -> topic -> queue pipeline in dependency order",
838 (stack) =>
839 Effect.gen(function* () {
840 yield* stack.destroy();
841 const pipeline = Effect.gen(function* () {
842 const bucket = yield* AWS.S3.Bucket("Uploads", { forceDestroy: true });
843 const topic = yield* AWS.SNS.Topic("Events", {
844 attributes: {
845 Policy: bucket.bucketArn.pipe(
846 Output.mapEffect((bucketArn) =>
847 Effect.sync(() =>
848 JSON.stringify({
849 Version: "2012-10-17",
850 Statement: [
851 {
852 Effect: "Allow",
853 Principal: { Service: "s3.amazonaws.com" },
854 Action: "sns:Publish",
855 Resource: "*",
856 Condition: { ArnEquals: { "aws:SourceArn": bucketArn } },
857 },
858 ],
859 }),
860 ),
861 ),
862 ),
863 },
864 });
865 const queue = yield* AWS.SQS.Queue("Processing");
866 yield* queue.bind("AllowTopic", {
867 policyStatements: [
868 {
869 Sid: "AllowTopic",
870 Effect: "Allow",
871 Principal: { Service: "sns.amazonaws.com" },
872 Action: ["sqs:SendMessage"],
873 Resource: [queue.queueArn],
874 Condition: { ArnEquals: { "aws:SourceArn": topic.topicArn } },
875 },
876 ],
877 });
878 const subscription = yield* AWS.SNS.Subscription("Subscription", {
879 topicArn: topic.topicArn,
880 protocol: "sqs",
881 endpoint: queue.queueArn,
882 returnSubscriptionArn: true,
883 });
884 yield* bucket.bind("TopicNotifications", {
885 notificationConfiguration: {
886 TopicConfigurations: [{ TopicArn: topic.topicArn, Events: ["s3:ObjectCreated:*"] }],
887 },
888 });
889 return {
890 bucketName: bucket.bucketName,
891 topicArn: topic.topicArn,
892 queueUrl: queue.queueUrl,
893 subscriptionArn: subscription.subscriptionArn,
894 };
895 });
896
897 const before = yield* stack.deploy(pipeline);
898
899 yield* SNS.unsubscribe({ SubscriptionArn: before.subscriptionArn });
900 yield* SQS.deleteQueue({ QueueUrl: before.queueUrl });
901 yield* SNS.deleteTopic({ TopicArn: before.topicArn });
902 yield* S3.deleteBucket({ Bucket: before.bucketName });
903 yield* assertBucketDeleted(before.bucketName);
904 yield* assertQueueDeleted(before.queueUrl);
905
906 const target = { name: stack.name, stage: stack.stage };
907 const detected = yield* Drift.detect(target).pipe(Effect.provide(stack.state));
908 for (const fqn of ["Uploads", "Events", "Processing", "Subscription"]) {
909 expect(detected.resources[fqn]?.action).toBe("missing");
910 }
911
912 const repaired = yield* Drift.repair(target).pipe(Effect.provide(stack.state));
913 for (const fqn of ["Uploads", "Events", "Processing", "Subscription"]) {
914 expect(repaired.resources[fqn]?.action).toBe("recreated");
915 }
916
917 // The next unchanged deploy reports the recreated subscription.
918 const after = yield* stack.deploy(pipeline);
919 expect(after.bucketName).toBe(before.bucketName);
920 expect(after.topicArn).toBe(before.topicArn);
921 expect(after.subscriptionArn).not.toBe(before.subscriptionArn);
922 const subscription = yield* SNS.getSubscriptionAttributes({
923 SubscriptionArn: after.subscriptionArn,
924 });
925 expect(subscription.Attributes?.TopicArn).toBe(after.topicArn);
926 expect(canonical(yield* readNotifications(after.bucketName)).TopicConfigurations).toEqual([
927 expect.objectContaining({ TopicArn: after.topicArn }),
928 ]);
929
930 // An upload flows end to end: S3 -> SNS -> SQS.
931 yield* S3.putObject({ Bucket: after.bucketName, Key: "recovered.txt", Body: "recovered" });
932 const bodies = yield* SQS.receiveMessage({
933 QueueUrl: after.queueUrl,
934 WaitTimeSeconds: 5,
935 MaxNumberOfMessages: 10,
936 }).pipe(
937 Effect.map((response) => (response.Messages ?? []).map((message) => message.Body ?? "")),
938 Effect.repeat({
939 until: (bodies) => bodies.some((body) => body.includes("recovered.txt")),
940 times: 8,
941 }),
942 );
943 expect(bodies.some((body) => body.includes("recovered.txt"))).toBe(true);
944
945 yield* stack.destroy();
946 yield* assertBucketDeleted(after.bucketName);
947 yield* assertQueueDeleted(after.queueUrl);
948 }),
949 {
950 tags: ["provider:aws", "provider:aws:s3", "provider:aws:sns", "provider:aws:sqs", "live"],
951 timeout: 180_000,
952 },
953 );