@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, 03:37 UTC

main
ts 2,400 lines 90.5 KB
Raw
1 import { createHash } from "node:crypto";
2 import { Region } from "@distilled.cloud/aws/Region";
3 import type { BucketLocationConstraint } from "@distilled.cloud/aws/s3";
4 import * as s3 from "@distilled.cloud/aws/s3";
5 import { deepEqual, isResolved } from "alchemy/Diff";
6 import { createPhysicalName } from "alchemy/PhysicalName";
7 import * as Provider from "alchemy/Provider";
8 import type { ScopedPlanStatusSession } from "alchemy/Report";
9 import { Resource, type ResourceBinding } from "alchemy/Resource";
10 import { Stack } from "alchemy/Stack";
11 import { isActionState, State } from "alchemy/State/State";
12 import { diffTags } from "alchemy/Tags";
13 import * as Arr from "effect/Array";
14 import * as Data from "effect/Data";
15 import type * as Duration from "effect/Duration";
16 import * as Effect from "effect/Effect";
17 import * as Order from "effect/Order";
18 import * as Redacted from "effect/Redacted";
19 import * as Schedule from "effect/Schedule";
20 import * as Stream from "effect/Stream";
21 import { AWSEnvironment, type AccountID } from "../Environment.ts";
22 import { durationToDays } from "../IAM/common.ts";
23 import type { PolicyStatement } from "../IAM/Policy.ts";
24 import type { Providers } from "../Providers.ts";
25 import type { RegionID } from "../Region.ts";
26
27 export type BucketName = string;
28 export type BucketArn = `arn:aws:s3:::${BucketName}`;
29
30 /**
31 * Server-side encryption configuration for a bucket.
32 */
33 export interface BucketEncryption {
34 /**
35 * Server-side encryption algorithm to use for the default encryption.
36 */
37 sseAlgorithm: "AES256" | "aws:kms" | "aws:kms:dsse";
38 /**
39 * KMS key id (or ARN) to use when `sseAlgorithm` is `aws:kms` or
40 * `aws:kms:dsse`. Ignored for `AES256`.
41 */
42 kmsMasterKeyId?: string;
43 /**
44 * Whether to use an S3 Bucket Key for SSE-KMS to reduce KMS request costs.
45 * @default false
46 */
47 bucketKeyEnabled?: boolean;
48 /**
49 * Encryption types to block for new object writes. Currently supports SSE-C
50 * (server-side encryption with customer-provided keys).
51 * Omitted or `[]` blocks no encryption types and permits SSE-C, sending
52 * AWS's `NONE` value. Set to `["SSE-C"]` to block customer-provided keys.
53 * Removing this property resets the blocklist to its default.
54 * @default []
55 */
56 blockedEncryptionTypes?: "SSE-C"[];
57 }
58
59 /**
60 * Public access block settings for a bucket. Each flag defaults to `false`
61 * (i.e. the corresponding public access is allowed) when omitted.
62 */
63 export interface BucketPublicAccessBlock {
64 /** Block new public ACLs and uploading public objects. */
65 blockPublicAcls?: boolean;
66 /** Ignore all public ACLs on the bucket and its objects. */
67 ignorePublicAcls?: boolean;
68 /** Block new bucket policies that grant public access. */
69 blockPublicPolicy?: boolean;
70 /** Restrict access granted by public bucket policies to AWS principals. */
71 restrictPublicBuckets?: boolean;
72 }
73
74 /**
75 * Access-logging configuration for a bucket.
76 */
77 export interface BucketLogging {
78 /** Bucket that receives the access logs. */
79 targetBucket: string;
80 /** Key prefix applied to log object names. */
81 targetPrefix: string;
82 /** Optional grants giving accounts access to the log objects. */
83 targetGrants?: s3.TargetGrant[];
84 /** Optional log object key format (simple or partitioned prefix). */
85 targetObjectKeyFormat?: s3.TargetObjectKeyFormat;
86 }
87
88 /**
89 * Static-website hosting configuration for a bucket.
90 */
91 export interface BucketWebsite {
92 /** Index document served for directory-style requests. */
93 indexDocument?: { suffix: string };
94 /** Document served for 4XX errors. */
95 errorDocument?: { key: string };
96 /** Redirect every request to another host instead of serving objects. */
97 redirectAllRequestsTo?: { hostName: string; protocol?: "http" | "https" };
98 /** Routing rules for conditional redirects. */
99 routingRules?: s3.RoutingRule[];
100 }
101
102 /**
103 * Cross-region (or same-region) replication configuration for a bucket.
104 * Requires `versioning: "Enabled"` on the source bucket and an IAM role
105 * that S3 can assume to perform the replication.
106 */
107 export interface BucketReplication {
108 /** ARN of the IAM role S3 assumes to replicate objects. */
109 role: string;
110 /** Replication rules describing what to replicate and where. */
111 rules: s3.ReplicationRule[];
112 }
113
114 /**
115 * Default object-lock retention applied to objects placed in a bucket that
116 * was created with `objectLockEnabled: true`.
117 */
118 export interface BucketObjectLockConfiguration {
119 /** Retention mode. */
120 mode: "GOVERNANCE" | "COMPLIANCE";
121 /**
122 * Retention period (e.g. `"30 days"` or `Duration.days(30)`; a bare number
123 * is milliseconds). Rounded to whole days on the wire. Mutually exclusive
124 * with `years`.
125 */
126 days?: Duration.Input;
127 /** Retention period in years (mutually exclusive with `days`). */
128 years?: number;
129 }
130
131 export interface BucketProps {
132 /**
133 * Name of the bucket. If omitted, a unique name will be generated.
134 * Must be lowercase and between 3-63 characters.
135 */
136 bucketName?: string;
137 /**
138 * Indicates whether this bucket has Object Lock enabled.
139 * Once enabled, cannot be disabled.
140 */
141 objectLockEnabled?: boolean;
142 /**
143 * Whether to delete all objects when the bucket is destroyed.
144 * @default false
145 */
146 forceDestroy?: boolean;
147 /**
148 * Tags to apply to the bucket.
149 */
150 tags?: Record<string, string>;
151 /**
152 * Object versioning status. `"Enabled"` keeps every version of an object;
153 * `"Suspended"` stops accruing new versions (existing versions are kept).
154 */
155 versioning?: "Enabled" | "Suspended";
156 /**
157 * MFA-delete status. Rarely used — enabling it requires an MFA serial and
158 * the root account, so it cannot be toggled through normal credentials.
159 */
160 mfaDelete?: "Enabled" | "Disabled";
161 /**
162 * Default server-side encryption for new objects. Omission restores AES256,
163 * no KMS key, bucket keys disabled, and no encryption types blocked.
164 * Existing objects are not re-encrypted. External changes are repaired on deploy.
165 */
166 encryption?: BucketEncryption;
167 /**
168 * Block-public-access settings. Applied before any ACL or policy that
169 * grants public access.
170 */
171 publicAccessBlock?: BucketPublicAccessBlock;
172 /**
173 * Cross-origin resource sharing (CORS) rules.
174 */
175 cors?: s3.CORSRule[];
176 /**
177 * Object lifecycle rules (expiration, transition, abort-incomplete-MPU…).
178 */
179 lifecycleRules?: s3.LifecycleRule[];
180 /**
181 * Object ownership control. `"BucketOwnerEnforced"` disables ACLs entirely.
182 */
183 objectOwnership?: "BucketOwnerPreferred" | "ObjectWriter" | "BucketOwnerEnforced";
184 /**
185 * Canned ACL to apply. Only valid when object ownership is not
186 * `BucketOwnerEnforced`.
187 */
188 acl?: s3.BucketCannedACL;
189 /**
190 * Access-logging configuration.
191 */
192 logging?: BucketLogging;
193 /**
194 * S3 Transfer Acceleration status.
195 */
196 transferAcceleration?: "Enabled" | "Suspended";
197 /**
198 * Who pays for requests and data transfer. `"Requester"` enables
199 * requester-pays.
200 */
201 requestPayer?: "BucketOwner" | "Requester";
202 /**
203 * Static-website hosting configuration.
204 */
205 website?: BucketWebsite;
206 /**
207 * Replication configuration. Requires `versioning: "Enabled"` and an
208 * IAM role.
209 */
210 replication?: BucketReplication;
211 /**
212 * S3 Intelligent-Tiering configurations (id-keyed).
213 */
214 intelligentTiering?: s3.IntelligentTieringConfiguration[];
215 /**
216 * Default object-lock retention. Requires `objectLockEnabled: true`.
217 */
218 objectLockConfiguration?: BucketObjectLockConfiguration;
219 /**
220 * Explicit bucket policy as policy statements. Merged with any
221 * policy statements contributed via bindings.
222 */
223 policy?: PolicyStatement[];
224 }
225
226 export interface Bucket extends Resource<
227 "AWS.S3.Bucket",
228 BucketProps,
229 {
230 /**
231 * Name of the bucket.
232 */
233 bucketName: BucketName;
234 /**
235 * ARN of the bucket.
236 */
237 bucketArn: BucketArn;
238 /**
239 * Domain name of the bucket (e.g., bucket-name.s3.amazonaws.com).
240 */
241 bucketDomainName: `${BucketName}.s3.amazonaws.com`;
242 /**
243 * Regional domain name of the bucket.
244 */
245 bucketRegionalDomainName: `${BucketName}.s3.${RegionID}.amazonaws.com`;
246 /**
247 * AWS region where the bucket is located.
248 */
249 region: RegionID;
250 /**
251 * AWS account ID that owns the bucket.
252 */
253 accountId: AccountID;
254 /**
255 * Notification configuration requested through bindings, retained for drift
256 * detection when binding outputs are unresolved during planning.
257 */
258 managedNotificationConfiguration?: s3.NotificationConfiguration;
259 // The attributes below record the bucket's configuration as observed on
260 // S3, so `alchemy drift` detects out-of-band changes. Settings whose prop
261 // is unset are not managed and are not observed.
262 /** Tags on the bucket, as observed. */
263 tags?: Record<string, string>;
264 /** Bucket policy document (explicit statements plus bindings), as observed. */
265 policy?: unknown;
266 /** Default encryption, as observed. */
267 encryption?: {
268 sseAlgorithm?: string;
269 kmsMasterKeyId?: string;
270 bucketKeyEnabled: boolean;
271 blockedEncryptionTypes: string[];
272 };
273 /**
274 * Event notification targets Alchemy owns (`<id>:<target>` keys), and
275 * whether it owns EventBridge delivery, as observed.
276 */
277 notifications?: {
278 lambdaFunctions: string[];
279 queues: string[];
280 topics: string[];
281 eventBridge: boolean;
282 };
283 /** Versioning status, observed when `versioning` is set. */
284 versioning?: string;
285 /** MFA-delete status, observed when `mfaDelete` is set. */
286 mfaDelete?: string;
287 /** Block-public-access settings, observed when `publicAccessBlock` is set. */
288 publicAccessBlock?: Required<BucketPublicAccessBlock>;
289 /** CORS rules, observed when `cors` is set. */
290 cors?: s3.CORSRule[];
291 /** Lifecycle rules (sorted by id), observed when `lifecycleRules` is set. */
292 lifecycleRules?: s3.LifecycleRule[];
293 /** Object ownership, observed when `objectOwnership` is set. */
294 objectOwnership?: string;
295 /**
296 * Non-owner ACL grants (`<grantee>:<permission>`), observed when `acl`
297 * is set.
298 */
299 aclGrants?: string[];
300 /** Access logging, observed when `logging` is set. */
301 logging?: {
302 targetBucket?: string;
303 targetPrefix?: string;
304 targetGrants?: s3.TargetGrant[];
305 };
306 /** Transfer acceleration status, observed when `transferAcceleration` is set. */
307 transferAcceleration?: string;
308 /** Request payer, observed when `requestPayer` is set. */
309 requestPayer?: string;
310 /** Website hosting configuration, observed when `website` is set. */
311 website?: {
312 indexDocument?: s3.IndexDocument;
313 errorDocument?: s3.ErrorDocument;
314 redirectAllRequestsTo?: s3.RedirectAllRequestsTo;
315 routingRules?: s3.RoutingRule[];
316 };
317 /** Replication configuration, observed when `replication` is set. */
318 replication?: {
319 role?: string;
320 rules: s3.ReplicationRule[];
321 };
322 /**
323 * The declared Intelligent-Tiering configurations (sorted by id;
324 * `undefined` for one that no longer exists), observed when
325 * `intelligentTiering` is set.
326 */
327 intelligentTiering?: (s3.IntelligentTieringConfiguration | undefined)[];
328 /**
329 * Default Object Lock retention, observed when `objectLockConfiguration`
330 * is set.
331 */
332 objectLockConfiguration?: {
333 mode?: string;
334 days?: number;
335 years?: number;
336 };
337 },
338 {
339 /**
340 * Notification configuration for the bucket. Alchemy assigns stable target
341 * IDs and reconciles Lambda, queue, topic, and EventBridge bindings.
342 */
343 notificationConfiguration?: s3.NotificationConfiguration;
344 /**
345 * Policy statements for the bucket.
346 */
347 policyStatements?: PolicyStatement[];
348 },
349 Providers
350 > {}
351
352 /**
353 * An S3 bucket for storing objects in AWS.
354 *
355 * A bucket name is auto-generated from the app, stage, and logical ID unless
356 * you provide one explicitly via `bucketName`. Enable `forceDestroy` to allow
357 * Alchemy to empty the bucket before deleting it.
358 * ### Creating a Bucket
359 * **Example:** Basic Bucket
360 * ```typescript
361 * import * as S3 from "@alchemy.run/aws/S3";
362 *
363 * const bucket = yield* S3.Bucket("my-bucket", {});
364 * ```
365 *
366 * **Example:** Bucket with a custom name
367 * ```typescript
368 * const bucket = yield* S3.Bucket("my-bucket", {
369 * bucketName: "my-company-assets",
370 * });
371 * ```
372 *
373 * **Example:** Bucket with force destroy
374 * ```typescript
375 * const bucket = yield* S3.Bucket("my-bucket", {
376 * forceDestroy: true,
377 * });
378 * ```
379 *
380 * ### Configuring a Bucket
381 * **Example:** Versioning and encryption
382 * ```typescript
383 * const bucket = yield* S3.Bucket("my-bucket", {
384 * versioning: "Enabled",
385 * encryption: { sseAlgorithm: "AES256" },
386 * });
387 * ```
388 *
389 * **Example:** Block all public access
390 * ```typescript
391 * const bucket = yield* S3.Bucket("my-bucket", {
392 * publicAccessBlock: {
393 * blockPublicAcls: true,
394 * ignorePublicAcls: true,
395 * blockPublicPolicy: true,
396 * restrictPublicBuckets: true,
397 * },
398 * });
399 * ```
400 *
401 * **Example:** CORS and lifecycle rules
402 * ```typescript
403 * const bucket = yield* S3.Bucket("my-bucket", {
404 * cors: [
405 * {
406 * AllowedMethods: ["GET"],
407 * AllowedOrigins: ["*"],
408 * AllowedHeaders: ["*"],
409 * MaxAgeSeconds: 3000,
410 * },
411 * ],
412 * lifecycleRules: [
413 * {
414 * ID: "expire-old",
415 * Status: "Enabled",
416 * Filter: { Prefix: "logs/" },
417 * Expiration: { Days: 30 },
418 * },
419 * ],
420 * });
421 * ```
422 *
423 * **Example:** Static website hosting
424 * ```typescript
425 * const bucket = yield* S3.Bucket("my-bucket", {
426 * objectOwnership: "BucketOwnerPreferred",
427 * website: {
428 * indexDocument: { suffix: "index.html" },
429 * errorDocument: { key: "error.html" },
430 * },
431 * });
432 * ```
433 *
434 * ### Default Bucket Encryption
435 * **Example:** Use the default encryption settings
436 * ```typescript
437 * const bucket = yield* S3.Bucket("my-bucket", {});
438 * ```
439 *
440 * The default is AES256 encryption with S3-managed keys, bucket keys disabled,
441 * and no encryption types blocked. Requests may explicitly supply their own
442 * encryption keys (SSE-C); permitting SSE-C does not change the default
443 * encryption used by requests without those keys.
444 *
445 * ### Blocking Customer-Provided Encryption Keys
446 * **Example:** Reject new SSE-C writes
447 * ```typescript
448 * const bucket = yield* S3.Bucket("my-bucket", {
449 * encryption: {
450 * sseAlgorithm: "AES256",
451 * blockedEncryptionTypes: ["SSE-C"],
452 * },
453 * });
454 * ```
455 *
456 * `blockedEncryptionTypes` lists encryption types to reject. This blocks new
457 * writes using customer-provided keys while retaining AES256 default encryption.
458 * Existing encrypted objects are unchanged.
459 *
460 * ### Resetting Encryption Restrictions
461 * **Example:** Remove the block to restore the default
462 * ```diff
463 * const bucket = yield* S3.Bucket("my-bucket", {
464 * encryption: {
465 * sseAlgorithm: "AES256",
466 * - blockedEncryptionTypes: ["SSE-C"],
467 * },
468 * });
469 * ```
470 *
471 * Redeploy the same logical resource after removing the property. Alchemy
472 * resets the blocklist to its default, `[]`, so SSE-C writes are permitted.
473 * It updates AWS rather than preserving the previously configured block.
474 *
475 * **Example:** Set the default blocklist explicitly
476 * ```typescript
477 * const bucket = yield* S3.Bucket("my-bucket", {
478 * encryption: {
479 * sseAlgorithm: "AES256",
480 * blockedEncryptionTypes: [],
481 * },
482 * });
483 * ```
484 *
485 * `[]` and omission have the same desired state: no encryption types blocked.
486 * The provider sends AWS's `NONE` value when it needs to reset a restriction.
487 * Redeploying unchanged code also repairs externally modified restrictions.
488 *
489 * ### Resetting All Encryption Settings
490 * **Example:** Remove the entire encryption configuration
491 * ```diff
492 * const bucket = yield* S3.Bucket("my-bucket", {
493 * - encryption: {
494 * - sseAlgorithm: "aws:kms",
495 * - kmsMasterKeyId: "arn:aws:kms:us-west-2:123456789012:key/12345678-1234-1234-1234-123456789012",
496 * - bucketKeyEnabled: true,
497 * - blockedEncryptionTypes: ["SSE-C"],
498 * - },
499 * });
500 * ```
501 *
502 * Omitting `encryption` resets every setting to the defaults: AES256, no custom
503 * KMS key, bucket keys disabled, and an empty blocklist. Previously configured
504 * KMS encryption is also reset; existing objects are not re-encrypted.
505 *
506 * ### Runtime Operations
507 * Bind S3 operations in the init phase and use them in runtime
508 * handlers. Bindings inject the bucket name and grant scoped IAM
509 * permissions automatically.
510 *
511 * **Example:** Read and write objects
512 * ```typescript
513 * // init
514 * const getObject = yield* S3.GetObject(bucket);
515 * const putObject = yield* S3.PutObject(bucket);
516 *
517 * return {
518 * fetch: Effect.gen(function* () {
519 * // runtime
520 * yield* putObject({
521 * Key: "hello.txt",
522 * Body: "Hello, World!",
523 * ContentType: "text/plain",
524 * });
525 * const response = yield* getObject({ Key: "hello.txt" });
526 * return HttpServerResponse.text("OK");
527 * }),
528 * };
529 * ```
530 *
531 * **Example:** Delete an object
532 * ```typescript
533 * // init
534 * const deleteObject = yield* S3.DeleteObject(bucket);
535 * ```
536 *
537 * ### Event Notifications
538 * Subscribe to bucket events from the init phase. The runtime binding creates
539 * the delivery permissions: Lambda invokes directly, while servers consume an SQS queue.
540 *
541 * Notification bindings reconcile Lambda, SQS, SNS, and EventBridge destinations.
542 * Removing a binding removes the targets Alchemy manages while preserving external
543 * configurations, including identical pre-existing targets. EventBridge ownership
544 * is recorded in a reserved bucket tag so interrupted deployments can recover.
545 *
546 * **Example:** Process object creation events
547 * ```typescript
548 * // init
549 * yield* S3.consumeBucketEvents(bucket, {
550 * events: ["s3:ObjectCreated:*"],
551 * }, (stream) =>
552 * stream.pipe(
553 * Stream.runForEach((event) =>
554 * Effect.log(`New object: ${event.key}`),
555 * ),
556 * ),
557 * );
558 * ```
559 *
560 * @resource
561 */
562 export const Bucket = Resource<Bucket>("AWS.S3.Bucket");
563
564 export const BucketProvider = () =>
565 Provider.effect(
566 Bucket,
567 Effect.gen(function* () {
568 const createBucketName = (id: string, props: { bucketName?: string | undefined }) =>
569 Effect.gen(function* () {
570 if (props.bucketName) {
571 return props.bucketName;
572 }
573 return yield* createPhysicalName({
574 id,
575 maxLength: 63,
576 lowercase: true,
577 });
578 });
579
580 const deleteAllObjects = Effect.fn(function* (bucketName: string) {
581 yield* Effect.logInfo(`S3 Bucket delete: deleting all objects from ${bucketName}`);
582 // Delete every object VERSION and DELETE MARKER, by Key+VersionId.
583 // `DeleteBucket` fails with BucketNotEmpty until all of them are
584 // gone — deleting only current objects (listObjectsV2) leaves
585 // versions/markers behind in versioned buckets. listObjectVersions
586 // also covers unversioned buckets (plain objects surface as
587 // Versions with VersionId "null"), so this single loop empties
588 // both. Pagination is gated on IsTruncated — never on NextKeyMarker,
589 // which S3 may omit even mid-listing.
590 let isTruncated = true;
591 let keyMarker: string | undefined;
592 let versionIdMarker: string | undefined;
593 while (isTruncated) {
594 const page = yield* s3.listObjectVersions({
595 Bucket: bucketName,
596 KeyMarker: keyMarker,
597 VersionIdMarker: versionIdMarker,
598 });
599
600 const objectsToDelete = [
601 ...(page.Versions ?? []).map((v) => ({
602 Key: v.Key!,
603 VersionId: v.VersionId,
604 })),
605 ...(page.DeleteMarkers ?? []).map((dm) => ({
606 Key: dm.Key!,
607 VersionId: dm.VersionId,
608 })),
609 ];
610
611 if (objectsToDelete.length > 0) {
612 yield* Effect.logInfo(
613 `S3 Bucket delete: deleting ${objectsToDelete.length} object version(s)/delete marker(s) from ${bucketName}`,
614 );
615 yield* s3.deleteObjects({
616 Bucket: bucketName,
617 Delete: {
618 Objects: objectsToDelete,
619 Quiet: true,
620 },
621 });
622 }
623
624 isTruncated = page.IsTruncated === true;
625 keyMarker = page.NextKeyMarker;
626 versionIdMarker = page.NextVersionIdMarker;
627 }
628
629 // Abort any in-progress multipart uploads — their stored parts also
630 // keep DeleteBucket failing with BucketNotEmpty.
631 let uploadsTruncated = true;
632 let uploadKeyMarker: string | undefined;
633 let uploadIdMarker: string | undefined;
634 while (uploadsTruncated) {
635 const uploads = yield* s3.listMultipartUploads({
636 Bucket: bucketName,
637 KeyMarker: uploadKeyMarker,
638 UploadIdMarker: uploadIdMarker,
639 });
640 const inProgress = (uploads.Uploads ?? []).filter(
641 (u) => u.Key != null && u.UploadId != null,
642 );
643 if (inProgress.length > 0) {
644 yield* Effect.logInfo(
645 `S3 Bucket delete: aborting ${inProgress.length} in-progress multipart upload(s) in ${bucketName}`,
646 );
647 yield* Effect.forEach(
648 inProgress,
649 (u) =>
650 s3
651 .abortMultipartUpload({
652 Bucket: bucketName,
653 Key: u.Key!,
654 UploadId: u.UploadId!,
655 })
656 // Already aborted/completed concurrently — a race, not a
657 // failure.
658 .pipe(Effect.catchTag("NoSuchUpload", () => Effect.void)),
659 { concurrency: 8 },
660 );
661 }
662 uploadsTruncated = uploads.IsTruncated === true;
663 uploadKeyMarker = uploads.NextKeyMarker;
664 uploadIdMarker = uploads.NextUploadIdMarker;
665 }
666 });
667
668 const ensureBucketExists = Effect.fn(function* ({
669 id,
670 news = {},
671 }: {
672 id: string;
673 news: BucketProps;
674 }) {
675 const { region } = yield* AWSEnvironment.current;
676 const { accountId } = yield* AWSEnvironment.current;
677 const bucketName = yield* createBucketName(id, news);
678
679 yield* Effect.logInfo(`S3 Bucket create: bucket=${bucketName} region=${region} `);
680
681 // For us-east-1, BucketAlreadyOwnedByYou is not thrown, so we need to
682 // pre-emptively check if the bucket exists for idempotency
683 if (region === "us-east-1") {
684 const exists = yield* s3
685 .getBucketLocation({
686 Bucket: bucketName,
687 ExpectedBucketOwner: accountId,
688 })
689 .pipe(
690 Effect.map(() => true),
691 Effect.catchTag("NoSuchBucket", () => Effect.succeed(false)),
692 );
693
694 yield* Effect.logInfo(
695 `S3 Bucket create: us-east-1 existence check for ${bucketName} -> ${exists}`,
696 );
697
698 if (!exists) {
699 yield* Effect.logInfo(`S3 Bucket create: creating bucket ${bucketName} in us-east-1`);
700 yield* s3
701 .createBucket({
702 Bucket: bucketName,
703 ObjectLockEnabledForBucket: news.objectLockEnabled ?? false,
704 })
705 .pipe(
706 Effect.retry({
707 while: (e) => e._tag === "OperationAborted" || e._tag === "ServiceUnavailable",
708 schedule: Schedule.exponential(100),
709 }),
710 );
711 }
712 } else {
713 // For non-us-east-1 regions, we can rely on BucketAlreadyOwnedByYou
714 yield* Effect.logInfo(`S3 Bucket create: creating bucket ${bucketName} in ${region}`);
715 yield* s3
716 .createBucket({
717 Bucket: bucketName,
718 CreateBucketConfiguration: {
719 LocationConstraint: region as BucketLocationConstraint,
720 },
721 ObjectLockEnabledForBucket: news.objectLockEnabled,
722 })
723 .pipe(
724 Effect.catchTag("BucketAlreadyOwnedByYou", () => Effect.void),
725 Effect.retry({
726 while: (e) => e._tag === "OperationAborted" || e._tag === "ServiceUnavailable",
727 schedule: Schedule.exponential(100),
728 }),
729 );
730 }
731
732 // Wait for bucket to exist (eventual consistency)
733 yield* s3
734 .getBucketLocation({
735 Bucket: bucketName,
736 ExpectedBucketOwner: accountId,
737 })
738 .pipe(
739 Effect.retry({
740 while: (error) => error._tag === "NoSuchBucket",
741 schedule: Schedule.exponential(100),
742 times: 8,
743 }),
744 );
745 yield* Effect.logInfo(`S3 Bucket create: bucket is available ${bucketName}`);
746
747 return {
748 bucketName,
749 bucketArn: `arn:aws:s3:::${bucketName}` as const,
750 bucketDomainName: `${bucketName}.s3.amazonaws.com` as const,
751 bucketRegionalDomainName: `${bucketName}.s3.${region}.amazonaws.com` as const,
752 region,
753 accountId,
754 };
755 });
756
757 const fetchBucketTags = (bucketName: string) =>
758 s3.getBucketTagging({ Bucket: bucketName }).pipe(
759 Effect.map((r) => Object.fromEntries((r.TagSet ?? []).map((t) => [t.Key!, t.Value!]))),
760 Effect.catchTag("NoSuchTagSet", () => Effect.succeed<Record<string, string>>({})),
761 );
762
763 const eventBridgeOwnerTag = "alchemy:notifications:eventbridge";
764 // The tags `alchemy drift` compares: the EventBridge ownership marker
765 // is notification bookkeeping, not a user tag.
766 const withoutOwnerTag = (tags: Record<string, string>) =>
767 Object.fromEntries(Object.entries(tags).filter(([key]) => key !== eventBridgeOwnerTag));
768 const readBucketTags = (bucketName: string) =>
769 fetchBucketTags(bucketName).pipe(Effect.map(withoutOwnerTag));
770 const notificationOwner = (bucketName: string) =>
771 Effect.sync(
772 () =>
773 `alchemy-notification-${createHash("sha256").update(bucketName).digest("hex").slice(0, 16)}-`,
774 );
775
776 const syncBucketTags = Effect.fn(function* ({
777 bucketName,
778 oldTags,
779 newTags,
780 session,
781 operation,
782 previous,
783 }: {
784 bucketName: string;
785 oldTags?: Record<string, string>;
786 newTags?: Record<string, string>;
787 session: ScopedPlanStatusSession;
788 operation: "create" | "update";
789 /** The last observed projection (`output`); a divergence forces the write. */
790 previous?: unknown;
791 }) {
792 // Compare against the cloud's actual tags so drift surfaces
793 // correctly even after a cold-start adoption (where olds.tags
794 // equals news.tags and would otherwise look like a no-op).
795 const previousTags = oldTags ?? (yield* fetchBucketTags(bucketName));
796 const desiredTags = { ...newTags };
797 delete desiredTags[eventBridgeOwnerTag];
798 if (previousTags[eventBridgeOwnerTag]) {
799 desiredTags[eventBridgeOwnerTag] = previousTags[eventBridgeOwnerTag];
800 }
801 const { removed, upsert } = diffTags(previousTags, desiredTags);
802 const expected = withoutOwnerTag(desiredTags);
803
804 yield* Effect.logInfo(
805 `S3 Bucket ${operation}: bucket=${bucketName} removedTags=${removed.length} upsertTags=${Object.keys(upsert).length}`,
806 );
807
808 if (
809 removed.length === 0 &&
810 Object.keys(upsert).length === 0 &&
811 settled(previous, withoutOwnerTag(previousTags))
812 ) {
813 return withoutOwnerTag(previousTags);
814 }
815 const written = settle(readBucketTags(bucketName), (observed) =>
816 deepEqual(observed, expected),
817 );
818
819 if (Object.keys(desiredTags).length > 0) {
820 yield* Effect.logInfo(
821 `S3 Bucket ${operation}: writing ${Object.keys(desiredTags).length} total tag(s) to ${bucketName}`,
822 );
823 yield* s3.putBucketTagging({
824 Bucket: bucketName,
825 Tagging: {
826 TagSet: Object.entries(desiredTags).map(([Key, Value]) => ({
827 Key,
828 Value,
829 })),
830 },
831 });
832 yield* session.note(`Updated bucket tags: ${bucketName}`);
833 return yield* written;
834 }
835
836 yield* Effect.logInfo(`S3 Bucket ${operation}: removing all tags from ${bucketName}`);
837 yield* s3.deleteBucketTagging({
838 Bucket: bucketName,
839 });
840 yield* session.note(`Removed all tags from bucket: ${bucketName}`);
841 return yield* written;
842 });
843
844 const parsePolicy = (policy: string): unknown => {
845 try {
846 return JSON.parse(policy);
847 } catch {
848 return policy;
849 }
850 };
851 const readBucketPolicy = (bucketName: string) =>
852 s3.getBucketPolicy({ Bucket: bucketName }).pipe(
853 Effect.map((r): unknown => (r.Policy === undefined ? undefined : parsePolicy(r.Policy))),
854 Effect.catchTag("NoSuchBucketPolicy", () => Effect.undefined),
855 );
856
857 const syncBucketPolicy = Effect.fn(function* ({
858 bucketName,
859 bindings,
860 explicitStatements,
861 session,
862 operation,
863 previous,
864 }: {
865 bucketName: string;
866 session: ScopedPlanStatusSession;
867 bindings: ResourceBinding<Bucket["Binding"]>[];
868 explicitStatements?: PolicyStatement[];
869 operation: "create" | "update";
870 /** The last observed projection (`output`); a divergence forces the write. */
871 previous?: unknown;
872 }) {
873 const policyStatements = [
874 ...(explicitStatements ?? []),
875 ...bindings.flatMap((binding) => binding.data.policyStatements ?? []),
876 ];
877 const desiredPolicy =
878 policyStatements.length > 0
879 ? JSON.stringify({
880 Version: "2012-10-17",
881 Statement: policyStatements,
882 })
883 : undefined;
884 const expected = desiredPolicy === undefined ? undefined : parsePolicy(desiredPolicy);
885 const read = readBucketPolicy(bucketName);
886 const matches = (observed: unknown) => deepEqual(observed, expected);
887 const existingPolicy = yield* read;
888
889 yield* Effect.logInfo(
890 `S3 Bucket ${operation}: bucket=${bucketName} policyStatements=${policyStatements.length}`,
891 );
892
893 if (matches(existingPolicy) && settled(previous, existingPolicy)) {
894 return existingPolicy;
895 }
896
897 if (desiredPolicy) {
898 yield* Effect.logInfo(
899 `S3 Bucket ${operation}: applying ${policyStatements.length} policy statement(s) to ${bucketName}`,
900 );
901 yield* s3.putBucketPolicy({
902 Bucket: bucketName,
903 Policy: desiredPolicy,
904 });
905 yield* session.note(`Updated bucket policy: ${bucketName}`);
906 return yield* settle(read, matches);
907 }
908
909 yield* Effect.logInfo(`S3 Bucket ${operation}: deleting bucket policy for ${bucketName}`);
910 yield* s3.deleteBucketPolicy({ Bucket: bucketName });
911 yield* session.note(`Removed bucket policy: ${bucketName}`);
912 return yield* settle(read, matches);
913 });
914
915 type NotificationTarget =
916 | s3.LambdaFunctionConfiguration
917 | s3.QueueConfiguration
918 | s3.TopicConfiguration;
919 const notificationTargetKey = (target: NotificationTarget) =>
920 JSON.stringify({
921 arn:
922 "LambdaFunctionArn" in target
923 ? target.LambdaFunctionArn
924 : "QueueArn" in target
925 ? target.QueueArn
926 : target.TopicArn,
927 events: [...(target.Events ?? [])].sort(),
928 filter: (target.Filter?.Key?.FilterRules ?? [])
929 .map((rule) => [rule.Name?.toLowerCase(), rule.Value])
930 .sort(([a, av], [b, bv]) => `${a}:${av}`.localeCompare(`${b}:${bv}`)),
931 });
932 const readManagedNotifications = Effect.fn(function* (
933 fqn: string,
934 instanceId: string,
935 output: Bucket["Attributes"] | undefined,
936 ) {
937 if (!output || output.managedNotificationConfiguration !== undefined) {
938 return output?.managedNotificationConfiguration;
939 }
940 const stack = yield* Stack;
941 const state = yield* yield* State;
942 const row = yield* state.get({
943 stack: stack.name,
944 stage: stack.stage,
945 fqn,
946 });
947 if (
948 !row ||
949 isActionState(row) ||
950 row.resourceType !== "AWS.S3.Bucket" ||
951 row.fqn !== fqn ||
952 row.instanceId !== instanceId ||
953 row.attr?.bucketName !== output.bucketName ||
954 (row.status !== "created" &&
955 row.status !== "updated" &&
956 row.status !== "replaced" &&
957 row.status !== "updating") ||
958 (row.status === "updating" && row.adopting)
959 ) {
960 return undefined;
961 }
962 // An updating row contains the new bindings; only its old snapshot proves ownership.
963 const previous = row.status === "updating" ? row.old : row;
964 if (
965 previous.attr?.bucketName !== output.bucketName ||
966 previous.attr?.bucketArn !== output.bucketArn ||
967 previous.attr?.accountId !== output.accountId ||
968 previous.attr?.region !== output.region
969 ) {
970 return undefined;
971 }
972 const managed: s3.NotificationConfiguration | undefined =
973 previous.attr.managedNotificationConfiguration;
974 if (managed !== undefined) return managed;
975 const bindings: ResourceBinding<Bucket["Binding"]>[] = previous.bindings ?? [];
976 if (!isResolved(bindings)) return undefined;
977 const legacy = bindings.flatMap(
978 ({ data }) => data.notificationConfiguration?.LambdaFunctionConfigurations ?? [],
979 );
980 if (legacy.length === 0) return undefined;
981 const observed = yield* s3.getBucketNotificationConfiguration({
982 Bucket: output.bucketName,
983 });
984 return {
985 LambdaFunctionConfigurations: (observed.LambdaFunctionConfigurations ?? []).filter(
986 (target) =>
987 legacy.some(
988 (old) =>
989 (old.Id === undefined || old.Id === target.Id) &&
990 notificationTargetKey(old) === notificationTargetKey(target),
991 ),
992 ),
993 } satisfies s3.NotificationConfiguration;
994 });
995 const notificationKey = (config: s3.NotificationConfiguration) => {
996 const targets = (values: NotificationTarget[] = []) =>
997 values.map((target) => `${target.Id ?? ""}:${notificationTargetKey(target)}`).sort();
998 return JSON.stringify({
999 lambda: targets(config.LambdaFunctionConfigurations),
1000 queue: targets(config.QueueConfigurations),
1001 topic: targets(config.TopicConfigurations),
1002 eventBridge: config.EventBridgeConfiguration !== undefined,
1003 });
1004 };
1005 const desiredNotifications = (
1006 bindings: ResourceBinding<Bucket["Binding"]>[],
1007 owner: string,
1008 ) =>
1009 Effect.sync(() => {
1010 const desired: s3.NotificationConfiguration = {
1011 LambdaFunctionConfigurations: [],
1012 QueueConfigurations: [],
1013 TopicConfigurations: [],
1014 };
1015 for (const { sid, data } of bindings) {
1016 const config = data.notificationConfiguration;
1017 if (!config) continue;
1018 const target = <T extends NotificationTarget>(value: T): T => ({
1019 ...value,
1020 Id: `${owner}${createHash("sha256")
1021 .update(JSON.stringify([sid, value.Id ?? notificationTargetKey(value)]))
1022 .digest("hex")}`,
1023 });
1024 desired.LambdaFunctionConfigurations!.push(
1025 ...(config.LambdaFunctionConfigurations ?? []).map(target),
1026 );
1027 desired.QueueConfigurations!.push(...(config.QueueConfigurations ?? []).map(target));
1028 desired.TopicConfigurations!.push(...(config.TopicConfigurations ?? []).map(target));
1029 if (config.EventBridgeConfiguration !== undefined) {
1030 desired.EventBridgeConfiguration = {};
1031 }
1032 }
1033 return desired;
1034 });
1035 const mergeNotifications = (
1036 existing: s3.NotificationConfiguration,
1037 desired: s3.NotificationConfiguration,
1038 previous: s3.NotificationConfiguration | undefined,
1039 owner: string,
1040 ownsEventBridge: boolean,
1041 ): s3.NotificationConfiguration => {
1042 const targets = <T extends NotificationTarget>(
1043 observed: T[] = [],
1044 wanted: T[] = [],
1045 managed: T[] = [],
1046 ): T[] => {
1047 const managedIds = new Set(managed.flatMap((target) => (target.Id ? [target.Id] : [])));
1048 const external = observed.filter(
1049 (target) =>
1050 !(
1051 target.Id?.startsWith(owner) && /^[0-9a-f]{64}$/.test(target.Id.slice(owner.length))
1052 ) && !managedIds.has(target.Id ?? ""),
1053 );
1054 const satisfied = new Set(external.map(notificationTargetKey));
1055 return [
1056 ...external,
1057 ...[...wanted]
1058 .sort((a, b) => (a.Id ?? "").localeCompare(b.Id ?? ""))
1059 .filter((target) => {
1060 const key = notificationTargetKey(target);
1061 if (satisfied.has(key)) return false;
1062 satisfied.add(key);
1063 return true;
1064 }),
1065 ];
1066 };
1067 return {
1068 LambdaFunctionConfigurations: targets(
1069 existing.LambdaFunctionConfigurations,
1070 desired.LambdaFunctionConfigurations,
1071 previous?.LambdaFunctionConfigurations,
1072 ),
1073 QueueConfigurations: targets(
1074 existing.QueueConfigurations,
1075 desired.QueueConfigurations,
1076 previous?.QueueConfigurations,
1077 ),
1078 TopicConfigurations: targets(
1079 existing.TopicConfigurations,
1080 desired.TopicConfigurations,
1081 previous?.TopicConfigurations,
1082 ),
1083 ...(desired.EventBridgeConfiguration !== undefined ||
1084 (existing.EventBridgeConfiguration !== undefined && !ownsEventBridge)
1085 ? { EventBridgeConfiguration: {} }
1086 : {}),
1087 };
1088 };
1089 /**
1090 * The notification targets `alchemy drift` compares: only the targets
1091 * Alchemy owns (stable owner-prefixed ids) and whether it owns the
1092 * EventBridge switch. External targets are not managed.
1093 */
1094 const projectNotifications = (
1095 config: s3.NotificationConfiguration,
1096 owner: string,
1097 ownsEventBridge: boolean,
1098 ) => {
1099 const owned = (targets: NotificationTarget[] = []) =>
1100 targets
1101 .filter(
1102 (target) =>
1103 target.Id?.startsWith(owner) &&
1104 /^[0-9a-f]{64}$/.test(target.Id.slice(owner.length)),
1105 )
1106 .map((target) => `${target.Id}:${notificationTargetKey(target)}`)
1107 .sort();
1108 const projected = {
1109 lambdaFunctions: owned(config.LambdaFunctionConfigurations),
1110 queues: owned(config.QueueConfigurations),
1111 topics: owned(config.TopicConfigurations),
1112 eventBridge: ownsEventBridge && config.EventBridgeConfiguration !== undefined,
1113 };
1114 return projected.lambdaFunctions.length === 0 &&
1115 projected.queues.length === 0 &&
1116 projected.topics.length === 0 &&
1117 !projected.eventBridge
1118 ? undefined
1119 : projected;
1120 };
1121 const readBucketNotifications = Effect.fn(function* (bucketName: string, owner: string) {
1122 const [config, tags] = yield* Effect.all(
1123 [
1124 s3.getBucketNotificationConfiguration({ Bucket: bucketName }),
1125 fetchBucketTags(bucketName),
1126 ],
1127 { concurrency: "unbounded" },
1128 );
1129 return projectNotifications(config, owner, tags[eventBridgeOwnerTag] === owner);
1130 });
1131
1132 const syncBucketNotifications = Effect.fn(function* ({
1133 bucketName,
1134 bindings,
1135 previous,
1136 session,
1137 operation,
1138 }: {
1139 bucketName: string;
1140 session: ScopedPlanStatusSession;
1141 bindings: ResourceBinding<Bucket["Binding"]>[];
1142 previous: s3.NotificationConfiguration | undefined;
1143 operation: "create" | "update";
1144 }) {
1145 const owner = yield* notificationOwner(bucketName);
1146 const desired = yield* desiredNotifications(bindings, owner);
1147 const existing = yield* s3.getBucketNotificationConfiguration({
1148 Bucket: bucketName,
1149 });
1150 const tags = yield* fetchBucketTags(bucketName);
1151 let ownsEventBridge = tags[eventBridgeOwnerTag] === owner;
1152 if (
1153 desired.EventBridgeConfiguration !== undefined &&
1154 existing.EventBridgeConfiguration === undefined &&
1155 !ownsEventBridge
1156 ) {
1157 // Record ownership before enabling so interrupted writes remain recoverable.
1158 yield* s3.putBucketTagging({
1159 Bucket: bucketName,
1160 Tagging: {
1161 TagSet: Object.entries({
1162 ...tags,
1163 [eventBridgeOwnerTag]: owner,
1164 }).map(([Key, Value]) => ({ Key, Value })),
1165 },
1166 });
1167 ownsEventBridge = true;
1168 }
1169 const next = mergeNotifications(existing, desired, previous, owner, ownsEventBridge);
1170 if (notificationKey(existing) !== notificationKey(next)) {
1171 yield* Effect.logInfo(
1172 `S3 Bucket ${operation}: applying notification configuration to ${bucketName}`,
1173 );
1174 yield* s3.putBucketNotificationConfiguration({
1175 Bucket: bucketName,
1176 NotificationConfiguration: next,
1177 // Destination policies can reconcile later in the same binding cycle.
1178 SkipDestinationValidation: true,
1179 });
1180 yield* session.note(`Updated bucket notifications: ${bucketName}`);
1181 }
1182 if (desired.EventBridgeConfiguration === undefined && ownsEventBridge) {
1183 delete tags[eventBridgeOwnerTag];
1184 if (Object.keys(tags).length > 0) {
1185 yield* s3.putBucketTagging({
1186 Bucket: bucketName,
1187 Tagging: {
1188 TagSet: Object.entries(tags).map(([Key, Value]) => ({
1189 Key,
1190 Value,
1191 })),
1192 },
1193 });
1194 } else {
1195 yield* s3.deleteBucketTagging({ Bucket: bucketName });
1196 }
1197 }
1198 const expected = projectNotifications(
1199 next,
1200 owner,
1201 desired.EventBridgeConfiguration !== undefined,
1202 );
1203 const observed = yield* settle(readBucketNotifications(bucketName, owner), (observed) =>
1204 deepEqual(observed, expected),
1205 );
1206 return { managed: desired, observed };
1207 });
1208
1209 // ---- Bucket configuration sync helpers ------------------------------
1210 // Each aspect has a read (the live cloud state), a projection (the
1211 // canonical shape `alchemy drift` compares) and a sync helper that
1212 // diffs observed against desired, applies only the delta, and returns
1213 // the projection it converged to. `read` reuses the same read and
1214 // projection, so a converged bucket reads unchanged. An aspect whose
1215 // prop is unset is unmanaged: it is neither synced nor observed.
1216 // The "not configured" read error for each aspect is already a typed
1217 // tag in distilled (see processes/AWS/catalog/S3.md), so we
1218 // `Effect.catchTag` it rather than inspecting status codes.
1219
1220 /**
1221 * Re-read after a write until S3 reports the desired state: bucket
1222 * configuration is eventually consistent. Returns the last observation
1223 * either way, so attributes record what S3 reports.
1224 */
1225 const settle = <A, E, R>(read: Effect.Effect<A, E, R>, matches: (observed: A) => boolean) =>
1226 read.pipe(
1227 Effect.repeat({
1228 until: matches,
1229 schedule: Schedule.spaced("1 second"),
1230 times: 8,
1231 }),
1232 );
1233
1234 /**
1235 * S3 can serve a stale read, so a sync may only skip its write when the
1236 * last observation (`output`, which drift fills with what it just read)
1237 * agrees with the current read. `undefined` (no observation yet, or a
1238 * row from before the attribute existed) is no evidence either way.
1239 */
1240 const settled = (previous: unknown, current: unknown) =>
1241 previous === undefined || deepEqual(previous, current);
1242
1243 const projectVersioning = (
1244 current: s3.GetBucketVersioningOutput,
1245 versioning: BucketProps["versioning"],
1246 mfaDelete: BucketProps["mfaDelete"],
1247 ) => ({
1248 versioning: versioning === undefined ? undefined : current.Status,
1249 mfaDelete: mfaDelete === undefined ? undefined : (current.MFADelete ?? "Disabled"),
1250 });
1251
1252 const syncBucketVersioning = Effect.fn(function* ({
1253 bucketName,
1254 versioning,
1255 mfaDelete,
1256 session,
1257 previous,
1258 }: {
1259 bucketName: string;
1260 versioning?: "Enabled" | "Suspended";
1261 mfaDelete?: "Enabled" | "Disabled";
1262 session: ScopedPlanStatusSession;
1263 /** The last observed projection (`output`); a divergence forces the write. */
1264 previous?: unknown;
1265 }) {
1266 if (versioning === undefined && mfaDelete === undefined) {
1267 return projectVersioning({}, versioning, mfaDelete);
1268 }
1269 const read = s3.getBucketVersioning({ Bucket: bucketName });
1270 const matches = (current: s3.GetBucketVersioningOutput) =>
1271 deepEqual(projectVersioning(current, versioning, mfaDelete), { versioning, mfaDelete });
1272 const current = yield* read;
1273 if (
1274 matches(current) &&
1275 settled(previous, projectVersioning(current, versioning, mfaDelete))
1276 ) {
1277 return projectVersioning(current, versioning, mfaDelete);
1278 }
1279 yield* s3.putBucketVersioning({
1280 Bucket: bucketName,
1281 VersioningConfiguration: {
1282 Status: versioning,
1283 MFADelete: mfaDelete,
1284 },
1285 });
1286 yield* session.note(`Updated bucket versioning: ${bucketName}`);
1287 return projectVersioning(yield* settle(read, matches), versioning, mfaDelete);
1288 });
1289
1290 const readPublicAccessBlock = (bucketName: string) =>
1291 s3.getPublicAccessBlock({ Bucket: bucketName }).pipe(
1292 Effect.map((r) => r.PublicAccessBlockConfiguration),
1293 Effect.catchTag("NoSuchPublicAccessBlockConfiguration", () => Effect.undefined),
1294 );
1295
1296 const projectPublicAccessBlock = (
1297 c: s3.PublicAccessBlockConfiguration | undefined,
1298 ): Required<BucketPublicAccessBlock> => ({
1299 blockPublicAcls: c?.BlockPublicAcls ?? false,
1300 ignorePublicAcls: c?.IgnorePublicAcls ?? false,
1301 blockPublicPolicy: c?.BlockPublicPolicy ?? false,
1302 restrictPublicBuckets: c?.RestrictPublicBuckets ?? false,
1303 });
1304
1305 const syncPublicAccessBlock = Effect.fn(function* ({
1306 bucketName,
1307 publicAccessBlock,
1308 session,
1309 previous,
1310 }: {
1311 bucketName: string;
1312 publicAccessBlock?: BucketPublicAccessBlock;
1313 session: ScopedPlanStatusSession;
1314 /** The last observed projection (`output`); a divergence forces the write. */
1315 previous?: unknown;
1316 }) {
1317 if (publicAccessBlock === undefined) return undefined;
1318 const desired: s3.PublicAccessBlockConfiguration = {
1319 BlockPublicAcls: publicAccessBlock.blockPublicAcls ?? false,
1320 IgnorePublicAcls: publicAccessBlock.ignorePublicAcls ?? false,
1321 BlockPublicPolicy: publicAccessBlock.blockPublicPolicy ?? false,
1322 RestrictPublicBuckets: publicAccessBlock.restrictPublicBuckets ?? false,
1323 };
1324 const read = readPublicAccessBlock(bucketName).pipe(Effect.map(projectPublicAccessBlock));
1325 const matches = (current: Required<BucketPublicAccessBlock>) =>
1326 deepEqual(current, projectPublicAccessBlock(desired));
1327 const current = yield* read;
1328 if (matches(current) && settled(previous, current)) return current;
1329 yield* s3.putPublicAccessBlock({
1330 Bucket: bucketName,
1331 PublicAccessBlockConfiguration: desired,
1332 });
1333 yield* session.note(`Updated public access block: ${bucketName}`);
1334 return yield* settle(read, matches);
1335 });
1336
1337 // Drop S3-assigned `ID`s and sort member arrays so re-ordering
1338 // doesn't read as drift.
1339 const projectCors = (rules: readonly s3.CORSRule[]) =>
1340 Arr.map(rules, (r) => ({
1341 AllowedHeaders: Arr.sort(r.AllowedHeaders ?? [], Order.String),
1342 AllowedMethods: Arr.sort(r.AllowedMethods ?? [], Order.String),
1343 AllowedOrigins: Arr.sort(r.AllowedOrigins ?? [], Order.String),
1344 ExposeHeaders: Arr.sort(r.ExposeHeaders ?? [], Order.String),
1345 MaxAgeSeconds: r.MaxAgeSeconds,
1346 }));
1347
1348 const readBucketCors = (bucketName: string) =>
1349 s3.getBucketCors({ Bucket: bucketName }).pipe(
1350 Effect.map((r) => projectCors(r.CORSRules ?? [])),
1351 Effect.catchTag("NoSuchCORSConfiguration", () =>
1352 Effect.succeed<ReturnType<typeof projectCors>>([]),
1353 ),
1354 );
1355
1356 const syncBucketCors = Effect.fn(function* ({
1357 bucketName,
1358 cors,
1359 session,
1360 previous,
1361 }: {
1362 bucketName: string;
1363 cors?: s3.CORSRule[];
1364 session: ScopedPlanStatusSession;
1365 /** The last observed projection (`output`); a divergence forces the write. */
1366 previous?: unknown;
1367 }) {
1368 if (cors === undefined) return undefined;
1369 const read = readBucketCors(bucketName);
1370 const matches = (current: ReturnType<typeof projectCors>) =>
1371 deepEqual(current, projectCors(cors));
1372 const current = yield* read;
1373 if (matches(current) && settled(previous, current)) return current;
1374 if (cors.length === 0) {
1375 yield* s3.deleteBucketCors({ Bucket: bucketName });
1376 yield* session.note(`Removed bucket CORS: ${bucketName}`);
1377 } else {
1378 yield* s3.putBucketCors({
1379 Bucket: bucketName,
1380 CORSConfiguration: { CORSRules: cors },
1381 });
1382 yield* session.note(`Updated bucket CORS: ${bucketName}`);
1383 }
1384 return yield* settle(read, matches);
1385 });
1386
1387 const projectLifecycle = (rules: readonly s3.LifecycleRule[]) =>
1388 Arr.sort(
1389 Arr.map(rules, (r) => ({ ...r })),
1390 Order.mapInput(Order.String, (r: s3.LifecycleRule) => r.ID ?? ""),
1391 );
1392
1393 const readBucketLifecycle = (bucketName: string) =>
1394 s3.getBucketLifecycleConfiguration({ Bucket: bucketName }).pipe(
1395 Effect.map((r) => projectLifecycle(r.Rules ?? [])),
1396 Effect.catchTag("NoSuchLifecycleConfiguration", () =>
1397 Effect.succeed<s3.LifecycleRule[]>([]),
1398 ),
1399 );
1400
1401 const syncBucketLifecycle = Effect.fn(function* ({
1402 bucketName,
1403 lifecycleRules,
1404 session,
1405 previous,
1406 }: {
1407 bucketName: string;
1408 lifecycleRules?: s3.LifecycleRule[];
1409 session: ScopedPlanStatusSession;
1410 /** The last observed projection (`output`); a divergence forces the write. */
1411 previous?: unknown;
1412 }) {
1413 if (lifecycleRules === undefined) return undefined;
1414 const read = readBucketLifecycle(bucketName);
1415 const matches = (current: s3.LifecycleRule[]) =>
1416 deepEqual(current, projectLifecycle(lifecycleRules));
1417 const current = yield* read;
1418 if (matches(current) && settled(previous, current)) return current;
1419 if (lifecycleRules.length === 0) {
1420 yield* s3.deleteBucketLifecycle({ Bucket: bucketName });
1421 yield* session.note(`Removed bucket lifecycle: ${bucketName}`);
1422 } else {
1423 yield* s3.putBucketLifecycleConfiguration({
1424 Bucket: bucketName,
1425 LifecycleConfiguration: { Rules: lifecycleRules },
1426 });
1427 yield* session.note(`Updated bucket lifecycle: ${bucketName}`);
1428 }
1429 return yield* settle(read, matches);
1430 });
1431
1432 const readBucketOwnershipControls = (bucketName: string) =>
1433 s3.getBucketOwnershipControls({ Bucket: bucketName }).pipe(
1434 Effect.map((r) => r.OwnershipControls?.Rules?.[0]?.ObjectOwnership),
1435 Effect.catchTag("OwnershipControlsNotFoundError", () => Effect.undefined),
1436 );
1437
1438 const syncBucketOwnershipControls = Effect.fn(function* ({
1439 bucketName,
1440 objectOwnership,
1441 session,
1442 previous,
1443 }: {
1444 bucketName: string;
1445 objectOwnership?: "BucketOwnerPreferred" | "ObjectWriter" | "BucketOwnerEnforced";
1446 session: ScopedPlanStatusSession;
1447 /** The last observed projection (`output`); a divergence forces the write. */
1448 previous?: unknown;
1449 }) {
1450 if (objectOwnership === undefined) return undefined;
1451 const read = readBucketOwnershipControls(bucketName);
1452 const current = yield* read;
1453 if (current === objectOwnership && settled(previous, current)) return current;
1454 yield* s3.putBucketOwnershipControls({
1455 Bucket: bucketName,
1456 OwnershipControls: {
1457 Rules: [{ ObjectOwnership: objectOwnership }],
1458 },
1459 });
1460 yield* session.note(`Updated object ownership: ${bucketName}`);
1461 return yield* settle(read, (observed) => observed === objectOwnership);
1462 });
1463
1464 // S3 has no canned-ACL read: compare the grants a canned ACL expands
1465 // to (owner grants excluded) with the observed grants.
1466 const projectAclGrants = (acl: s3.GetBucketAclOutput) =>
1467 Arr.sort(
1468 (acl.Grants ?? []).flatMap(({ Grantee: grantee, Permission: permission }) => {
1469 if (grantee?.ID !== undefined && grantee.ID === acl.Owner?.ID) return [];
1470 const who =
1471 grantee?.URI?.split("/").pop() ??
1472 (grantee?.ID ? `id:${grantee.ID}` : `email:${grantee?.EmailAddress ?? ""}`);
1473 return [`${who}:${permission}`];
1474 }),
1475 Order.String,
1476 );
1477 const cannedAclGrants: Partial<Record<s3.BucketCannedACL, string[]>> = {
1478 private: [],
1479 "public-read": ["AllUsers:READ"],
1480 "public-read-write": ["AllUsers:READ", "AllUsers:WRITE"],
1481 "authenticated-read": ["AuthenticatedUsers:READ"],
1482 };
1483
1484 const syncBucketAcl = Effect.fn(function* ({
1485 bucketName,
1486 acl,
1487 session,
1488 previous,
1489 }: {
1490 bucketName: string;
1491 acl?: s3.BucketCannedACL;
1492 session: ScopedPlanStatusSession;
1493 /** The last observed projection (`output`); a divergence forces the write. */
1494 previous?: unknown;
1495 }) {
1496 if (acl === undefined) return undefined;
1497 const read = s3.getBucketAcl({ Bucket: bucketName }).pipe(Effect.map(projectAclGrants));
1498 const expected = cannedAclGrants[acl];
1499 const matches = (grants: string[]) => expected !== undefined && deepEqual(grants, expected);
1500 const current = yield* read;
1501 if (matches(current) && settled(previous, current)) return current;
1502 // putBucketAcl is idempotent for canned ACLs; an ACL whose grants we
1503 // can't predict is applied on every reconcile.
1504 yield* s3.putBucketAcl({ Bucket: bucketName, ACL: acl });
1505 yield* session.note(`Updated bucket ACL: ${bucketName}`);
1506 return expected === undefined ? yield* read : yield* settle(read, matches);
1507 });
1508
1509 const projectLogging = (logging: s3.LoggingEnabled | undefined) =>
1510 logging === undefined
1511 ? undefined
1512 : {
1513 targetBucket: logging.TargetBucket,
1514 targetPrefix: logging.TargetPrefix,
1515 targetGrants: logging.TargetGrants?.length ? logging.TargetGrants : undefined,
1516 };
1517
1518 const readBucketLogging = (bucketName: string) =>
1519 s3
1520 .getBucketLogging({ Bucket: bucketName })
1521 .pipe(Effect.map((r) => projectLogging(r.LoggingEnabled)));
1522
1523 const syncBucketLogging = Effect.fn(function* ({
1524 bucketName,
1525 logging,
1526 session,
1527 previous,
1528 }: {
1529 bucketName: string;
1530 logging?: BucketLogging;
1531 session: ScopedPlanStatusSession;
1532 /** The last observed projection (`output`); a divergence forces the write. */
1533 previous?: unknown;
1534 }) {
1535 if (logging === undefined) return undefined;
1536 const desired: s3.LoggingEnabled = {
1537 TargetBucket: logging.targetBucket,
1538 TargetPrefix: logging.targetPrefix,
1539 TargetGrants: logging.targetGrants,
1540 TargetObjectKeyFormat: logging.targetObjectKeyFormat,
1541 };
1542 const read = readBucketLogging(bucketName);
1543 const matches = (current: ReturnType<typeof projectLogging>) =>
1544 deepEqual(current, projectLogging(desired));
1545 const current = yield* read;
1546 if (matches(current) && settled(previous, current)) return current;
1547 yield* s3.putBucketLogging({
1548 Bucket: bucketName,
1549 BucketLoggingStatus: { LoggingEnabled: desired },
1550 });
1551 yield* session.note(`Updated bucket logging: ${bucketName}`);
1552 return yield* settle(read, matches);
1553 });
1554
1555 const readTransferAcceleration = (bucketName: string) =>
1556 s3
1557 .getBucketAccelerateConfiguration({ Bucket: bucketName })
1558 .pipe(Effect.map((r) => r.Status));
1559
1560 const syncTransferAcceleration = Effect.fn(function* ({
1561 bucketName,
1562 transferAcceleration,
1563 session,
1564 previous,
1565 }: {
1566 bucketName: string;
1567 transferAcceleration?: "Enabled" | "Suspended";
1568 session: ScopedPlanStatusSession;
1569 /** The last observed projection (`output`); a divergence forces the write. */
1570 previous?: unknown;
1571 }) {
1572 if (transferAcceleration === undefined) return undefined;
1573 const read = readTransferAcceleration(bucketName);
1574 const current = yield* read;
1575 if (current === transferAcceleration && settled(previous, current)) return current;
1576 yield* s3.putBucketAccelerateConfiguration({
1577 Bucket: bucketName,
1578 AccelerateConfiguration: { Status: transferAcceleration },
1579 });
1580 yield* session.note(`Updated transfer acceleration: ${bucketName}`);
1581 return yield* settle(read, (observed) => observed === transferAcceleration);
1582 });
1583
1584 const readRequestPayment = (bucketName: string) =>
1585 s3.getBucketRequestPayment({ Bucket: bucketName }).pipe(Effect.map((r) => r.Payer));
1586
1587 const syncRequestPayment = Effect.fn(function* ({
1588 bucketName,
1589 requestPayer,
1590 session,
1591 previous,
1592 }: {
1593 bucketName: string;
1594 requestPayer?: "BucketOwner" | "Requester";
1595 session: ScopedPlanStatusSession;
1596 /** The last observed projection (`output`); a divergence forces the write. */
1597 previous?: unknown;
1598 }) {
1599 if (requestPayer === undefined) return undefined;
1600 const read = readRequestPayment(bucketName);
1601 const current = yield* read;
1602 if (current === requestPayer && settled(previous, current)) return current;
1603 yield* s3.putBucketRequestPayment({
1604 Bucket: bucketName,
1605 RequestPaymentConfiguration: { Payer: requestPayer },
1606 });
1607 yield* session.note(`Updated request payment: ${bucketName}`);
1608 return yield* settle(read, (observed) => observed === requestPayer);
1609 });
1610
1611 const projectWebsite = (
1612 w: s3.GetBucketWebsiteOutput | s3.WebsiteConfiguration | undefined,
1613 ) => ({
1614 indexDocument: w?.IndexDocument,
1615 errorDocument: w?.ErrorDocument,
1616 redirectAllRequestsTo: w?.RedirectAllRequestsTo,
1617 routingRules: w?.RoutingRules,
1618 });
1619
1620 const readBucketWebsite = (bucketName: string) =>
1621 s3.getBucketWebsite({ Bucket: bucketName }).pipe(
1622 Effect.map(projectWebsite),
1623 Effect.catchTag("NoSuchWebsiteConfiguration", () =>
1624 Effect.succeed(projectWebsite(undefined)),
1625 ),
1626 );
1627
1628 const syncBucketWebsite = Effect.fn(function* ({
1629 bucketName,
1630 website,
1631 session,
1632 previous,
1633 }: {
1634 bucketName: string;
1635 website?: BucketWebsite;
1636 session: ScopedPlanStatusSession;
1637 /** The last observed projection (`output`); a divergence forces the write. */
1638 previous?: unknown;
1639 }) {
1640 if (website === undefined) return undefined;
1641 const desired: s3.WebsiteConfiguration = {
1642 IndexDocument: website.indexDocument
1643 ? { Suffix: website.indexDocument.suffix }
1644 : undefined,
1645 ErrorDocument: website.errorDocument ? { Key: website.errorDocument.key } : undefined,
1646 RedirectAllRequestsTo: website.redirectAllRequestsTo
1647 ? {
1648 HostName: website.redirectAllRequestsTo.hostName,
1649 Protocol: website.redirectAllRequestsTo.protocol,
1650 }
1651 : undefined,
1652 RoutingRules: website.routingRules,
1653 };
1654 const read = readBucketWebsite(bucketName);
1655 const matches = (current: ReturnType<typeof projectWebsite>) =>
1656 deepEqual(current, projectWebsite(desired));
1657 const current = yield* read;
1658 if (matches(current) && settled(previous, current)) return current;
1659 yield* s3.putBucketWebsite({
1660 Bucket: bucketName,
1661 WebsiteConfiguration: desired,
1662 });
1663 yield* session.note(`Updated bucket website: ${bucketName}`);
1664 return yield* settle(read, matches);
1665 });
1666
1667 const projectReplication = (
1668 cfg: { Role?: string; Rules?: readonly s3.ReplicationRule[] } | undefined,
1669 ) =>
1670 cfg === undefined
1671 ? undefined
1672 : {
1673 role: cfg.Role,
1674 rules: Arr.sort(
1675 Arr.map(cfg.Rules ?? [], (r) => ({ ...r })),
1676 Order.mapInput(Order.String, (r: s3.ReplicationRule) => r.ID ?? ""),
1677 ),
1678 };
1679
1680 const readBucketReplication = (bucketName: string) =>
1681 s3.getBucketReplication({ Bucket: bucketName }).pipe(
1682 Effect.map((r) => projectReplication(r.ReplicationConfiguration)),
1683 Effect.catchTag("ReplicationConfigurationNotFoundError", () =>
1684 Effect.succeed(projectReplication(undefined)),
1685 ),
1686 );
1687
1688 const syncBucketReplication = Effect.fn(function* ({
1689 bucketName,
1690 replication,
1691 session,
1692 previous,
1693 }: {
1694 bucketName: string;
1695 replication?: BucketReplication;
1696 session: ScopedPlanStatusSession;
1697 /** The last observed projection (`output`); a divergence forces the write. */
1698 previous?: unknown;
1699 }) {
1700 if (replication === undefined) return undefined;
1701 const desired: s3.ReplicationConfiguration = {
1702 Role: replication.role,
1703 Rules: replication.rules,
1704 };
1705 const read = readBucketReplication(bucketName);
1706 const matches = (current: ReturnType<typeof projectReplication>) =>
1707 deepEqual(current, projectReplication(desired));
1708 const current = yield* read;
1709 if (matches(current) && settled(previous, current)) return current;
1710 yield* s3.putBucketReplication({
1711 Bucket: bucketName,
1712 ReplicationConfiguration: desired,
1713 });
1714 yield* session.note(`Updated bucket replication: ${bucketName}`);
1715 return yield* settle(read, matches);
1716 });
1717
1718 const readIntelligentTiering = (bucketName: string, id: string) =>
1719 s3
1720 .getBucketIntelligentTieringConfiguration({
1721 Bucket: bucketName,
1722 Id: id,
1723 })
1724 .pipe(
1725 Effect.map((r) => r.IntelligentTieringConfiguration),
1726 Effect.catchTag("NoSuchConfiguration", () => Effect.undefined),
1727 );
1728
1729 // The declared configurations, observed per id (the per-id read is
1730 // read-after-write consistent; `list` is not).
1731 const observeIntelligentTiering = (
1732 bucketName: string,
1733 intelligentTiering: s3.IntelligentTieringConfiguration[],
1734 ) =>
1735 Effect.forEach(
1736 Arr.sort(
1737 Arr.map(intelligentTiering, (c) => c.Id),
1738 Order.String,
1739 ),
1740 (id) => readIntelligentTiering(bucketName, id),
1741 );
1742
1743 const syncIntelligentTiering = Effect.fn(function* ({
1744 bucketName,
1745 intelligentTiering,
1746 oldIntelligentTiering,
1747 session,
1748 }: {
1749 bucketName: string;
1750 intelligentTiering?: s3.IntelligentTieringConfiguration[];
1751 oldIntelligentTiering?: s3.IntelligentTieringConfiguration[];
1752 session: ScopedPlanStatusSession;
1753 }) {
1754 if (intelligentTiering === undefined) return undefined;
1755 const desiredById = new Map(Arr.map(intelligentTiering, (c) => [c.Id, c] as const));
1756 // Reconcile each desired id: put when missing or changed.
1757 for (const [id, desired] of desiredById) {
1758 const current = yield* readIntelligentTiering(bucketName, id);
1759 if (deepEqual(current, desired)) continue;
1760 yield* s3.putBucketIntelligentTieringConfiguration({
1761 Bucket: bucketName,
1762 Id: id,
1763 IntelligentTieringConfiguration: desired,
1764 });
1765 yield* session.note(`Updated intelligent-tiering ${id}: ${bucketName}`);
1766 }
1767 // Remove ids that were previously declared but are no longer desired.
1768 // `list` is eventually-consistent, so diff against the prior props
1769 // (when available) and fall back to listing for adoption. Each delete
1770 // tolerates a config that is already gone.
1771 const removedIds = new Set<string>();
1772 for (const old of oldIntelligentTiering ?? []) {
1773 if (old.Id && !desiredById.has(old.Id)) removedIds.add(old.Id);
1774 }
1775 if (oldIntelligentTiering === undefined) {
1776 const observed = yield* s3
1777 .listBucketIntelligentTieringConfigurations({ Bucket: bucketName })
1778 .pipe(Effect.map((r) => r.IntelligentTieringConfigurationList ?? []));
1779 for (const cfg of observed) {
1780 if (cfg.Id && !desiredById.has(cfg.Id)) removedIds.add(cfg.Id);
1781 }
1782 }
1783 for (const id of removedIds) {
1784 // delete is idempotent server-side (no NoSuchConfiguration in the
1785 // typed error union), so a missing id is simply a no-op.
1786 yield* s3.deleteBucketIntelligentTieringConfiguration({
1787 Bucket: bucketName,
1788 Id: id,
1789 });
1790 yield* session.note(`Removed intelligent-tiering ${id}: ${bucketName}`);
1791 }
1792 return yield* observeIntelligentTiering(bucketName, intelligentTiering);
1793 });
1794
1795 const projectObjectLockRetention = (r: s3.DefaultRetention | undefined) => ({
1796 mode: r?.Mode,
1797 days: r?.Days,
1798 years: r?.Years,
1799 });
1800
1801 const readObjectLockRetention = (bucketName: string) =>
1802 s3.getObjectLockConfiguration({ Bucket: bucketName }).pipe(
1803 Effect.map((r) =>
1804 projectObjectLockRetention(r.ObjectLockConfiguration?.Rule?.DefaultRetention),
1805 ),
1806 Effect.catchTag("ObjectLockConfigurationNotFoundError", () =>
1807 Effect.succeed(projectObjectLockRetention(undefined)),
1808 ),
1809 );
1810
1811 const syncObjectLockRetention = Effect.fn(function* ({
1812 bucketName,
1813 objectLockConfiguration,
1814 session,
1815 previous,
1816 }: {
1817 bucketName: string;
1818 objectLockConfiguration?: BucketObjectLockConfiguration;
1819 session: ScopedPlanStatusSession;
1820 /** The last observed projection (`output`); a divergence forces the write. */
1821 previous?: unknown;
1822 }) {
1823 if (objectLockConfiguration === undefined) return undefined;
1824 const desired: s3.DefaultRetention = {
1825 Mode: objectLockConfiguration.mode,
1826 Days: durationToDays(objectLockConfiguration.days),
1827 Years: objectLockConfiguration.years,
1828 };
1829 const read = readObjectLockRetention(bucketName);
1830 const matches = (current: ReturnType<typeof projectObjectLockRetention>) =>
1831 deepEqual(current, projectObjectLockRetention(desired));
1832 const current = yield* read;
1833 if (matches(current) && settled(previous, current)) return current;
1834 yield* s3.putObjectLockConfiguration({
1835 Bucket: bucketName,
1836 ObjectLockConfiguration: {
1837 ObjectLockEnabled: "Enabled",
1838 Rule: { DefaultRetention: desired },
1839 },
1840 });
1841 yield* session.note(`Updated object-lock retention: ${bucketName}`);
1842 return yield* settle(read, matches);
1843 });
1844
1845 /**
1846 * Observe every managed aspect of a bucket as `read` reports it: the
1847 * same projections the sync helpers return from `reconcile`.
1848 */
1849 const observeBucketConfiguration = Effect.fn(function* (
1850 bucketName: string,
1851 props: BucketProps,
1852 ) {
1853 const owner = yield* notificationOwner(bucketName);
1854 const managed = <A, E, R>(isManaged: boolean, read: () => Effect.Effect<A, E, R>) =>
1855 isManaged ? read() : Effect.undefined;
1856 return yield* Effect.all(
1857 {
1858 tags: readBucketTags(bucketName),
1859 policy: readBucketPolicy(bucketName),
1860 encryption: readBucketEncryption(bucketName).pipe(Effect.map(projectEncryption)),
1861 notifications: readBucketNotifications(bucketName, owner),
1862 versioning: managed(
1863 props.versioning !== undefined || props.mfaDelete !== undefined,
1864 () =>
1865 s3
1866 .getBucketVersioning({ Bucket: bucketName })
1867 .pipe(
1868 Effect.map((current) =>
1869 projectVersioning(current, props.versioning, props.mfaDelete),
1870 ),
1871 ),
1872 ),
1873 publicAccessBlock: managed(props.publicAccessBlock !== undefined, () =>
1874 readPublicAccessBlock(bucketName).pipe(Effect.map(projectPublicAccessBlock)),
1875 ),
1876 cors: managed(props.cors !== undefined, () => readBucketCors(bucketName)),
1877 lifecycleRules: managed(props.lifecycleRules !== undefined, () =>
1878 readBucketLifecycle(bucketName),
1879 ),
1880 objectOwnership: managed(props.objectOwnership !== undefined, () =>
1881 readBucketOwnershipControls(bucketName),
1882 ),
1883 aclGrants: managed(props.acl !== undefined, () =>
1884 s3.getBucketAcl({ Bucket: bucketName }).pipe(Effect.map(projectAclGrants)),
1885 ),
1886 logging: managed(props.logging !== undefined, () => readBucketLogging(bucketName)),
1887 transferAcceleration: managed(props.transferAcceleration !== undefined, () =>
1888 readTransferAcceleration(bucketName),
1889 ),
1890 requestPayer: managed(props.requestPayer !== undefined, () =>
1891 readRequestPayment(bucketName),
1892 ),
1893 website: managed(props.website !== undefined, () => readBucketWebsite(bucketName)),
1894 replication: managed(props.replication !== undefined, () =>
1895 readBucketReplication(bucketName),
1896 ),
1897 intelligentTiering: managed(props.intelligentTiering !== undefined, () =>
1898 observeIntelligentTiering(bucketName, props.intelligentTiering ?? []),
1899 ),
1900 objectLockConfiguration: managed(props.objectLockConfiguration !== undefined, () =>
1901 readObjectLockRetention(bucketName),
1902 ),
1903 },
1904 { concurrency: 8 },
1905 ).pipe(
1906 Effect.map(({ versioning, ...observed }) => ({
1907 ...observed,
1908 versioning: versioning?.versioning,
1909 mfaDelete: versioning?.mfaDelete,
1910 })),
1911 );
1912 });
1913
1914 return {
1915 stables: ["bucketName", "bucketArn", "region", "accountId"],
1916 // ListBuckets enumerates this account. Read verifies the same ownership
1917 // with ExpectedBucketOwner; cross-account access alone is not ownership.
1918 // GetBucketLocation needs configuration access, not HeadBucket's
1919 // s3:ListBucket permission to enumerate the bucket's objects.
1920 list: () =>
1921 Effect.gen(function* () {
1922 const { accountId, region } = yield* AWSEnvironment.current;
1923 // S3 only includes `BucketRegion` in the response when the request
1924 // carries at least one parameter — hence the explicit MaxBuckets.
1925 return yield* s3.listBuckets.pages({ MaxBuckets: 1000 }).pipe(
1926 Stream.runCollect,
1927 Effect.map((chunk) =>
1928 Array.from(chunk).flatMap((page) =>
1929 (page.Buckets ?? [])
1930 .filter((b): b is s3.Bucket & { Name: string } => b.Name != null)
1931 .map((b) => {
1932 // ListBuckets is global — record each bucket's actual
1933 // region so later operations (delete) can target it.
1934 const bucketRegion = (b.BucketRegion ?? region) as RegionID;
1935 return {
1936 bucketName: b.Name,
1937 bucketArn: `arn:aws:s3:::${b.Name}` as const,
1938 bucketDomainName: `${b.Name}.s3.amazonaws.com` as const,
1939 bucketRegionalDomainName:
1940 `${b.Name}.s3.${bucketRegion}.amazonaws.com` as const,
1941 region: bucketRegion,
1942 accountId,
1943 };
1944 }),
1945 ),
1946 ),
1947 );
1948 }),
1949 read: Effect.fn(function* ({ id, fqn, instanceId, olds, output }) {
1950 const bucketName = output?.bucketName ?? (yield* createBucketName(id, olds ?? {}));
1951 const { accountId, region } = yield* AWSEnvironment.current;
1952 const exists = yield* s3
1953 .getBucketLocation({
1954 Bucket: bucketName,
1955 ExpectedBucketOwner: accountId,
1956 })
1957 .pipe(
1958 Effect.map(() => true),
1959 Effect.catchTag("NoSuchBucket", () => Effect.succeed(false)),
1960 );
1961 if (!exists) return undefined;
1962 return {
1963 bucketName,
1964 bucketArn: `arn:aws:s3:::${bucketName}` as const,
1965 bucketDomainName: `${bucketName}.s3.amazonaws.com` as const,
1966 bucketRegionalDomainName: `${bucketName}.s3.${region}.amazonaws.com` as const,
1967 region,
1968 accountId,
1969 managedNotificationConfiguration: yield* readManagedNotifications(
1970 fqn,
1971 instanceId,
1972 output,
1973 ),
1974 ...(yield* observeBucketConfiguration(bucketName, olds ?? {})),
1975 };
1976 }),
1977 diff: Effect.fn(function* ({
1978 id,
1979 fqn,
1980 instanceId,
1981 news = {},
1982 olds = {},
1983 output,
1984 newBindings,
1985 }) {
1986 if (!isResolved(news)) return undefined;
1987 const oldBucketName = yield* createBucketName(id, olds);
1988 const newBucketName = yield* createBucketName(id, news);
1989 yield* Effect.logInfo(
1990 `S3 Bucket diff: old=${oldBucketName} new=${newBucketName} oldObjectLock=${olds.objectLockEnabled ?? false} newObjectLock=${news.objectLockEnabled ?? false}`,
1991 );
1992 if (oldBucketName !== newBucketName) {
1993 yield* Effect.logInfo(
1994 `S3 Bucket diff: replacing bucket because name changed from ${oldBucketName} to ${newBucketName}`,
1995 );
1996 return { action: "replace" } as const;
1997 }
1998 // Object lock can only be enabled at creation time
1999 if ((olds.objectLockEnabled ?? false) !== (news.objectLockEnabled ?? false)) {
2000 yield* Effect.logInfo(
2001 `S3 Bucket diff: replacing bucket because object lock changed for ${newBucketName}`,
2002 );
2003 return { action: "replace" } as const;
2004 }
2005 if (output) {
2006 const observed = yield* readBucketEncryption(output.bucketName).pipe(
2007 Effect.catchTag("NoSuchBucket", () => Effect.undefined),
2008 );
2009 if (
2010 encryptionFingerprint(observed) !==
2011 encryptionFingerprint(desiredEncryptionRule(news.encryption))
2012 ) {
2013 return { action: "update" } as const;
2014 }
2015 const previous = yield* readManagedNotifications(fqn, instanceId, output);
2016 if (output.managedNotificationConfiguration === undefined && previous !== undefined) {
2017 return { action: "update" } as const;
2018 }
2019 const owner = yield* notificationOwner(output.bucketName);
2020 const desired = isResolved(newBindings)
2021 ? yield* desiredNotifications(newBindings, owner)
2022 : previous;
2023 if (desired) {
2024 const notifications = yield* s3
2025 .getBucketNotificationConfiguration({
2026 Bucket: output.bucketName,
2027 })
2028 .pipe(Effect.catchTag("NoSuchBucket", () => Effect.undefined));
2029 const tags: Record<string, string> = notifications
2030 ? yield* fetchBucketTags(output.bucketName)
2031 : {};
2032 if (
2033 notifications &&
2034 ((desired.EventBridgeConfiguration === undefined &&
2035 tags[eventBridgeOwnerTag] === owner) ||
2036 notificationKey(notifications) !==
2037 notificationKey(
2038 mergeNotifications(
2039 notifications,
2040 desired,
2041 previous,
2042 owner,
2043 tags[eventBridgeOwnerTag] === owner,
2044 ),
2045 ))
2046 ) {
2047 return { action: "update" } as const;
2048 }
2049 }
2050 }
2051 }),
2052 precreate: (props) => ensureBucketExists(props),
2053 reconcile: Effect.fn(function* ({
2054 id,
2055 fqn,
2056 instanceId,
2057 news = {},
2058 olds,
2059 output,
2060 session,
2061 bindings,
2062 }) {
2063 const operation = output === undefined ? "create" : "update";
2064 const resolved = output ?? (yield* ensureBucketExists({ id, news }));
2065
2066 const tags = yield* syncBucketTags({
2067 bucketName: resolved.bucketName,
2068 previous: output?.tags,
2069 // Omit `oldTags` so syncBucketTags fetches the cloud's actual
2070 // current tags. This makes drift detection correct even after
2071 // a cold-start adoption where olds.tags would equal news.tags.
2072 newTags: news.tags,
2073 session,
2074 operation,
2075 });
2076
2077 // Ownership + public-access-block must precede any ACL/policy that
2078 // grants public access, else those puts can fail with AccessDenied.
2079 const objectOwnership = yield* syncBucketOwnershipControls({
2080 bucketName: resolved.bucketName,
2081 previous: output?.objectOwnership,
2082 objectOwnership: news.objectOwnership,
2083 session,
2084 });
2085
2086 const publicAccessBlock = yield* syncPublicAccessBlock({
2087 bucketName: resolved.bucketName,
2088 previous: output?.publicAccessBlock,
2089 publicAccessBlock: news.publicAccessBlock,
2090 session,
2091 });
2092
2093 // Versioning before replication (replication requires it enabled).
2094 const versioning = yield* syncBucketVersioning({
2095 bucketName: resolved.bucketName,
2096 previous:
2097 output?.versioning === undefined && output?.mfaDelete === undefined
2098 ? undefined
2099 : { versioning: output.versioning, mfaDelete: output.mfaDelete },
2100 versioning: news.versioning,
2101 mfaDelete: news.mfaDelete,
2102 session,
2103 });
2104
2105 if (
2106 yield* syncBucketEncryption(resolved.bucketName, news.encryption, output?.encryption)
2107 ) {
2108 yield* session.note(`Updated bucket encryption: ${resolved.bucketName}`);
2109 }
2110 const encryption = projectEncryption(yield* readBucketEncryption(resolved.bucketName));
2111
2112 const cors = yield* syncBucketCors({
2113 bucketName: resolved.bucketName,
2114 previous: output?.cors,
2115 cors: news.cors,
2116 session,
2117 });
2118
2119 const lifecycleRules = yield* syncBucketLifecycle({
2120 bucketName: resolved.bucketName,
2121 previous: output?.lifecycleRules,
2122 lifecycleRules: news.lifecycleRules,
2123 session,
2124 });
2125
2126 const logging = yield* syncBucketLogging({
2127 bucketName: resolved.bucketName,
2128 previous: output?.logging,
2129 logging: news.logging,
2130 session,
2131 });
2132
2133 const transferAcceleration = yield* syncTransferAcceleration({
2134 bucketName: resolved.bucketName,
2135 previous: output?.transferAcceleration,
2136 transferAcceleration: news.transferAcceleration,
2137 session,
2138 });
2139
2140 const requestPayer = yield* syncRequestPayment({
2141 bucketName: resolved.bucketName,
2142 previous: output?.requestPayer,
2143 requestPayer: news.requestPayer,
2144 session,
2145 });
2146
2147 const website = yield* syncBucketWebsite({
2148 bucketName: resolved.bucketName,
2149 previous: output?.website,
2150 website: news.website,
2151 session,
2152 });
2153
2154 const replication = yield* syncBucketReplication({
2155 bucketName: resolved.bucketName,
2156 previous: output?.replication,
2157 replication: news.replication,
2158 session,
2159 });
2160
2161 const intelligentTiering = yield* syncIntelligentTiering({
2162 bucketName: resolved.bucketName,
2163 intelligentTiering: news.intelligentTiering,
2164 oldIntelligentTiering: olds?.intelligentTiering,
2165 session,
2166 });
2167
2168 const objectLockConfiguration = yield* syncObjectLockRetention({
2169 bucketName: resolved.bucketName,
2170 previous: output?.objectLockConfiguration,
2171 objectLockConfiguration: news.objectLockConfiguration,
2172 session,
2173 });
2174
2175 // ACL after ownership/public-access-block.
2176 const aclGrants = yield* syncBucketAcl({
2177 bucketName: resolved.bucketName,
2178 previous: output?.aclGrants,
2179 acl: news.acl,
2180 session,
2181 });
2182
2183 const policy = yield* syncBucketPolicy({
2184 bucketName: resolved.bucketName,
2185 previous: output?.policy,
2186 bindings,
2187 explicitStatements: news.policy,
2188 session,
2189 operation,
2190 });
2191
2192 const notifications = yield* syncBucketNotifications({
2193 bucketName: resolved.bucketName,
2194 bindings,
2195 previous: yield* readManagedNotifications(fqn, instanceId, output),
2196 session,
2197 operation,
2198 });
2199
2200 if (operation === "create") {
2201 yield* session.note(`Ensured bucket: ${resolved.bucketName}`);
2202 }
2203
2204 return {
2205 ...resolved,
2206 managedNotificationConfiguration: notifications.managed,
2207 tags,
2208 policy,
2209 encryption,
2210 notifications: notifications.observed,
2211 versioning: versioning.versioning,
2212 mfaDelete: versioning.mfaDelete,
2213 publicAccessBlock,
2214 cors,
2215 lifecycleRules,
2216 objectOwnership,
2217 aclGrants,
2218 logging,
2219 transferAcceleration,
2220 requestPayer,
2221 website,
2222 replication,
2223 intelligentTiering,
2224 objectLockConfiguration,
2225 };
2226 }),
2227 delete: Effect.fn(function* ({ olds = {}, output, session, force }) {
2228 // Whether we are allowed to destroy the bucket's contents.
2229 // - `olds.forceDestroy` — the user opted in on the resource props.
2230 // - `force` — set only by `alchemy unsafe nuke`, an explicit
2231 // operator-confirmed account teardown. Nuke enumerates buckets
2232 // straight from the cloud (its `olds` is Attributes, not Props),
2233 // so `forceDestroy` is never present there. S3 ownership is
2234 // account-level: list enumerates this account and read verifies
2235 // ExpectedBucketOwner. This provider deliberately does not stamp
2236 // alchemy tags on buckets, so every bucket nuke hands us is one
2237 // this account owns.
2238 // A normal destroy without `forceDestroy` must NOT empty the
2239 // bucket — a non-empty bucket fails with BucketNotEmpty, which is
2240 // the data-protection behavior users rely on.
2241 const mayEmpty = olds.forceDestroy === true || force === true;
2242 yield* Effect.logInfo(
2243 `S3 Bucket delete: bucket=${output.bucketName} forceDestroy=${olds.forceDestroy ?? false} force=${force ?? false} region=${output.region}`,
2244 );
2245 const run = Effect.gen(function* () {
2246 // If we may destroy contents, delete all object versions and
2247 // delete markers first. The bucket may already be gone (deleted
2248 // out-of-band, or a previous destroy partially succeeded) —
2249 // treat NoSuchBucket as a no-op so the overall delete still
2250 // converges.
2251 if (mayEmpty) {
2252 yield* session.note(
2253 `Force destroying bucket: ${output.bucketName} - deleting all objects...`,
2254 );
2255 yield* deleteAllObjects(output.bucketName).pipe(
2256 Effect.catchTag("NoSuchBucket", () => Effect.void),
2257 );
2258 }
2259
2260 yield* s3
2261 .deleteBucket({
2262 Bucket: output.bucketName,
2263 })
2264 .pipe(
2265 Effect.catchTags({
2266 NoSuchBucket: () => Effect.void,
2267 // BucketNotEmpty after an empty is eventual consistency (or
2268 // a concurrent writer landing new versions between the empty
2269 // and the DeleteBucket): re-empty so the bounded retry below
2270 // has a chance to succeed instead of spinning on a bucket
2271 // that still has content.
2272 BucketNotEmpty: (e) =>
2273 Effect.gen(function* () {
2274 if (!mayEmpty) return yield* e;
2275 yield* deleteAllObjects(output.bucketName).pipe(
2276 Effect.catchTag("NoSuchBucket", () => Effect.void),
2277 );
2278 // Re-fail with the original error so the retry policy
2279 // drives the next DeleteBucket attempt.
2280 return yield* e;
2281 }),
2282 }),
2283 Effect.retry({
2284 // BucketHasAccessPointsAttached: S3's access-point
2285 // attachment view is eventually consistent — deleting a
2286 // bucket immediately after its access points were deleted
2287 // can transiently report attachments. Retry until the view
2288 // converges (bounded).
2289 while: (e): boolean =>
2290 e._tag === "BucketNotEmpty" || e._tag === "BucketHasAccessPointsAttached",
2291 schedule: Schedule.max([Schedule.exponential(250), Schedule.recurs(7)]),
2292 }),
2293 );
2294 });
2295
2296 // The bucket may live in a different region than the ambient client
2297 // (list enumerates buckets from every region) — target the bucket's
2298 // own region to avoid PermanentRedirect. If we still get redirected
2299 // (e.g. a stale/missing region attribute), retry once in the region
2300 // the redirect reports.
2301 yield* (
2302 output.region
2303 ? run.pipe(Effect.provideService(Region, Effect.succeed(output.region)))
2304 : run
2305 ).pipe(
2306 Effect.tapError(Effect.logInfo),
2307 Effect.catchTag("PermanentRedirect", (e) =>
2308 e.BucketRegion
2309 ? run.pipe(
2310 Effect.provideService(Region, Effect.succeed(e.BucketRegion as RegionID)),
2311 )
2312 : Effect.fail(e),
2313 ),
2314 );
2315
2316 yield* session.note(`Deleted bucket: ${output.bucketName}`);
2317 }),
2318 };
2319 }),
2320 );
2321
2322 class BucketEncryptionNotConverged extends Data.TaggedError("BucketEncryptionNotConverged")<{
2323 bucket: string;
2324 }> {}
2325
2326 const desiredEncryptionRule = (encryption?: BucketEncryption): s3.ServerSideEncryptionRule => {
2327 const algorithm = encryption?.sseAlgorithm ?? "AES256";
2328 const blocked = encryption?.blockedEncryptionTypes ?? [];
2329 return {
2330 ApplyServerSideEncryptionByDefault: {
2331 SSEAlgorithm: algorithm,
2332 KMSMasterKeyID: algorithm === "AES256" ? undefined : encryption?.kmsMasterKeyId,
2333 },
2334 BucketKeyEnabled: encryption?.bucketKeyEnabled ?? false,
2335 BlockedEncryptionTypes: {
2336 EncryptionType: blocked.length ? [...new Set(blocked)] : ["NONE"],
2337 },
2338 };
2339 };
2340
2341 /** The default-encryption settings `alchemy drift` compares. */
2342 const projectEncryption = (rule: s3.ServerSideEncryptionRule | undefined) => {
2343 const key = rule?.ApplyServerSideEncryptionByDefault?.KMSMasterKeyID;
2344 return {
2345 sseAlgorithm: rule?.ApplyServerSideEncryptionByDefault?.SSEAlgorithm,
2346 kmsMasterKeyId: (Redacted.isRedacted(key) ? Redacted.value(key) : key) ?? undefined,
2347 bucketKeyEnabled: rule?.BucketKeyEnabled ?? false,
2348 blockedEncryptionTypes: [
2349 ...new Set(
2350 rule?.BlockedEncryptionTypes?.EncryptionType?.filter((type) => type !== "NONE") ?? [],
2351 ),
2352 ].sort(),
2353 };
2354 };
2355
2356 const encryptionFingerprint = (rule: s3.ServerSideEncryptionRule | undefined) =>
2357 JSON.stringify(projectEncryption(rule));
2358
2359 const readBucketEncryption = (bucket: string) =>
2360 s3
2361 .getBucketEncryption({ Bucket: bucket })
2362 .pipe(Effect.map((result) => result.ServerSideEncryptionConfiguration?.Rules?.[0]));
2363
2364 export const syncBucketEncryption = Effect.fn(function* (
2365 bucket: string,
2366 encryption?: BucketEncryption,
2367 /**
2368 * The last observed projection; when it disagrees with the current read
2369 * (S3 served a stale read), the write is forced.
2370 */
2371 previous?: Bucket["Attributes"]["encryption"],
2372 ) {
2373 const desired = desiredEncryptionRule(encryption);
2374 const matches = (rule: s3.ServerSideEncryptionRule | undefined) =>
2375 encryptionFingerprint(rule) === encryptionFingerprint(desired);
2376 const current = yield* readBucketEncryption(bucket);
2377 if (
2378 matches(current) &&
2379 (previous === undefined || deepEqual(previous, projectEncryption(current)))
2380 ) {
2381 return false;
2382 }
2383 yield* s3.putBucketEncryption({
2384 Bucket: bucket,
2385 ServerSideEncryptionConfiguration: { Rules: [desired] },
2386 });
2387 // Encryption reads stay stale for well over 8s after a write, notably on a
2388 // re-created bucket name or right after a bucket-policy change.
2389 const observed = yield* readBucketEncryption(bucket).pipe(
2390 Effect.repeat({
2391 until: matches,
2392 schedule: Schedule.spaced("2 seconds"),
2393 times: 15,
2394 }),
2395 );
2396 if (!matches(observed)) {
2397 return yield* new BucketEncryptionNotConverged({ bucket });
2398 }
2399 return true;
2400 });