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
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { AWSEnvironment } from "@alchemy.run/aws/Environment"; |
| 3 | import { Role } from "@alchemy.run/aws/IAM"; |
| 4 | import { Bucket } from "@alchemy.run/aws/S3"; |
| 5 | import { BucketProvider } from "@alchemy.run/aws/S3/Bucket"; |
| 6 | import { describe, expect, it } from "@alchemy.run/test"; |
| 7 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 8 | import { Credentials, fromCredentials } from "@distilled.cloud/aws/Credentials"; |
| 9 | import * as KMS from "@distilled.cloud/aws/kms"; |
| 10 | import { Region } from "@distilled.cloud/aws/Region"; |
| 11 | import * as S3 from "@distilled.cloud/aws/s3"; |
| 12 | import { NodeServices } from "@effect/platform-node"; |
| 13 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 14 | import { ArtifactStore, createArtifactStore } from "alchemy/Artifacts"; |
| 15 | import * as Drift from "alchemy/Drift"; |
| 16 | import * as Output from "alchemy/Output"; |
| 17 | import * as Provider from "alchemy/Provider"; |
| 18 | import { ResourceContext } from "alchemy/ResourceContext"; |
| 19 | import { Stack, type StackSpec } from "alchemy/Stack"; |
| 20 | import { Stage } from "alchemy/Stage"; |
| 21 | import { State } from "alchemy/State"; |
| 22 | import { inMemoryState } from "alchemy/State"; |
| 23 | import * as Data from "effect/Data"; |
| 24 | import * as Effect from "effect/Effect"; |
| 25 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 26 | import * as HttpClient from "effect/http/HttpClient"; |
| 27 | import * as Layer from "effect/Layer"; |
| 28 | import * as Redacted from "effect/Redacted"; |
| 29 | import * as Result from "effect/Result"; |
| 30 | import * as Schedule from "effect/Schedule"; |
| 31 | import { resourceContext } from "../../Util/ResourceContext.ts"; |
| 32 | |
| 33 | const { test } = Test.make({ providers: AWS.providers() }); |
| 34 | |
| 35 | test.provider( |
| 36 | "PR1585 discovers an owned bucket without ListBucket and rejects denied location reads", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | yield* stack.destroy(); |
| 40 | const bucket = yield* stack.deploy(Bucket("OwnershipBucket", {})); |
| 41 | const { accountId } = yield* AWSEnvironment.current; |
| 42 | const definition = (value: string) => |
| 43 | Bucket("OwnershipBucket", { tags: { revision: value } }); |
| 44 | const discovery = Effect.gen(function* () { |
| 45 | yield* definition("updated"); |
| 46 | return yield* Bucket("DiscoveryProbe", { bucketName: bucket.bucketName }); |
| 47 | }); |
| 48 | |
| 49 | const deny = (action: string) => |
| 50 | S3.putBucketPolicy({ |
| 51 | Bucket: bucket.bucketName, |
| 52 | Policy: JSON.stringify({ |
| 53 | Version: "2012-10-17", |
| 54 | Statement: [ |
| 55 | { Effect: "Deny", Principal: "*", Action: action, Resource: bucket.bucketArn }, |
| 56 | ], |
| 57 | }), |
| 58 | }); |
| 59 | const restorePolicy = S3.deleteBucketPolicy({ Bucket: bucket.bucketName }); |
| 60 | |
| 61 | yield* Effect.gen(function* () { |
| 62 | yield* deny("s3:ListBucket"); |
| 63 | const listDenied = yield* S3.listObjectsV2({ Bucket: bucket.bucketName }).pipe( |
| 64 | Effect.as(false), |
| 65 | Effect.catchTag("AccessDeniedException", () => Effect.succeed(true)), |
| 66 | Effect.repeat({ until: Boolean, schedule: Schedule.spaced("1 second"), times: 8 }), |
| 67 | ); |
| 68 | expect(listDenied).toBe(true); |
| 69 | yield* S3.getBucketLocation({ Bucket: bucket.bucketName, ExpectedBucketOwner: accountId }); |
| 70 | |
| 71 | // The probe is planned, not applied: the original resource retains ownership. |
| 72 | const plan = yield* stack.plan(discovery); |
| 73 | expect(plan.resources.DiscoveryProbe?.action).toBe("adopted"); |
| 74 | yield* stack.deploy(definition("updated")); |
| 75 | expect((yield* S3.getBucketTagging({ Bucket: bucket.bucketName })).TagSet).toContainEqual({ |
| 76 | Key: "revision", |
| 77 | Value: "updated", |
| 78 | }); |
| 79 | |
| 80 | const wrongOwner = `${accountId.slice(0, -1)}${accountId.endsWith("0") ? "1" : "0"}`; |
| 81 | const mismatch = yield* S3.getBucketLocation({ |
| 82 | Bucket: bucket.bucketName, |
| 83 | ExpectedBucketOwner: wrongOwner, |
| 84 | }).pipe(Effect.flip); |
| 85 | expect(mismatch._tag).toBe("AccessDeniedException"); |
| 86 | |
| 87 | yield* deny("s3:GetBucketLocation"); |
| 88 | const locationDenied = yield* S3.getBucketLocation({ |
| 89 | Bucket: bucket.bucketName, |
| 90 | ExpectedBucketOwner: accountId, |
| 91 | }).pipe( |
| 92 | Effect.as(false), |
| 93 | Effect.catchTag("AccessDeniedException", () => Effect.succeed(true)), |
| 94 | Effect.repeat({ until: Boolean, schedule: Schedule.spaced("2 seconds"), times: 10 }), |
| 95 | ); |
| 96 | expect(locationDenied).toBe(true); |
| 97 | // HeadBucket remains allowed; treating this rejection as absence would plan a create. |
| 98 | yield* S3.listObjectsV2({ Bucket: bucket.bucketName }).pipe( |
| 99 | Effect.retry({ |
| 100 | while: (error) => error._tag === "AccessDeniedException", |
| 101 | schedule: Schedule.spaced("1 second"), |
| 102 | times: 8, |
| 103 | }), |
| 104 | ); |
| 105 | const result = yield* stack.plan(discovery).pipe( |
| 106 | Effect.result, |
| 107 | Effect.repeat({ |
| 108 | until: Result.isFailure, |
| 109 | schedule: Schedule.spaced("2 seconds"), |
| 110 | times: 10, |
| 111 | }), |
| 112 | ); |
| 113 | expect(Result.isFailure(result)).toBe(true); |
| 114 | if (Result.isFailure(result)) { |
| 115 | expect(result.failure._tag).toBe("AccessDeniedException"); |
| 116 | } |
| 117 | }).pipe(Effect.ensuring(restorePolicy.pipe(Effect.orDie))); |
| 118 | |
| 119 | yield* S3.getBucketLocation({ |
| 120 | Bucket: bucket.bucketName, |
| 121 | ExpectedBucketOwner: accountId, |
| 122 | }).pipe( |
| 123 | Effect.retry({ |
| 124 | while: (error) => error._tag === "AccessDeniedException", |
| 125 | schedule: Schedule.spaced("1 second"), |
| 126 | times: 8, |
| 127 | }), |
| 128 | ); |
| 129 | yield* stack.destroy(); |
| 130 | yield* assertBucketDeleted(bucket.bucketName); |
| 131 | const absent = yield* S3.getBucketLocation({ Bucket: bucket.bucketName }).pipe( |
| 132 | Effect.as(false), |
| 133 | Effect.catchTag("NoSuchBucket", () => Effect.succeed(true)), |
| 134 | Effect.repeat({ until: Boolean, schedule: Schedule.spaced("1 second"), times: 8 }), |
| 135 | ); |
| 136 | expect(absent).toBe(true); |
| 137 | }), |
| 138 | { tags: ["provider:aws", "provider:aws:s3", "live"], timeout: 120_000 }, |
| 139 | ); |
| 140 | |
| 141 | for (const aspect of ["tagging", "encryption"] as const) { |
| 142 | test.provider( |
| 143 | `PR1586 preserves ${aspect} when its read is denied but writes are allowed`, |
| 144 | (stack) => |
| 145 | Effect.gen(function* () { |
| 146 | yield* stack.destroy(); |
| 147 | const initialProps = { |
| 148 | tags: { revision: "before" }, |
| 149 | encryption: { sseAlgorithm: "AES256" as const }, |
| 150 | }; |
| 151 | const bucket = yield* stack.deploy(Bucket("ReadFailureBucket", initialProps)); |
| 152 | const tagging = yield* S3.getBucketTagging({ Bucket: bucket.bucketName }); |
| 153 | const encryption = yield* S3.getBucketEncryption({ Bucket: bucket.bucketName }); |
| 154 | const restorePolicy = S3.deleteBucketPolicy({ Bucket: bucket.bucketName }); |
| 155 | const read = Effect.gen(function* () { |
| 156 | if (aspect === "tagging") { |
| 157 | yield* S3.getBucketTagging({ Bucket: bucket.bucketName }); |
| 158 | } else { |
| 159 | yield* S3.getBucketEncryption({ Bucket: bucket.bucketName }); |
| 160 | } |
| 161 | }); |
| 162 | const desired = Bucket("ReadFailureBucket", { |
| 163 | tags: { revision: "after" }, |
| 164 | encryption: { sseAlgorithm: aspect === "encryption" ? "aws:kms" : "AES256" }, |
| 165 | }); |
| 166 | |
| 167 | yield* Effect.gen(function* () { |
| 168 | yield* S3.putBucketPolicy({ |
| 169 | Bucket: bucket.bucketName, |
| 170 | Policy: JSON.stringify({ |
| 171 | Version: "2012-10-17", |
| 172 | Statement: [ |
| 173 | { |
| 174 | Effect: "Deny", |
| 175 | Principal: "*", |
| 176 | Action: |
| 177 | aspect === "tagging" ? "s3:GetBucketTagging" : "s3:GetEncryptionConfiguration", |
| 178 | Resource: bucket.bucketArn, |
| 179 | }, |
| 180 | ], |
| 181 | }), |
| 182 | }); |
| 183 | const denied = yield* read.pipe( |
| 184 | Effect.as(false), |
| 185 | Effect.catchTag("AccessDeniedException", () => Effect.succeed(true)), |
| 186 | Effect.repeat({ until: Boolean, schedule: Schedule.spaced("1 second"), times: 8 }), |
| 187 | ); |
| 188 | expect(denied).toBe(true); |
| 189 | // Reapply the observed configuration to prove the write permission remains usable. |
| 190 | if (aspect === "tagging") { |
| 191 | yield* S3.putBucketTagging({ |
| 192 | Bucket: bucket.bucketName, |
| 193 | Tagging: { TagSet: tagging.TagSet }, |
| 194 | }); |
| 195 | } else { |
| 196 | yield* S3.putBucketEncryption({ |
| 197 | Bucket: bucket.bucketName, |
| 198 | ServerSideEncryptionConfiguration: encryption.ServerSideEncryptionConfiguration!, |
| 199 | }); |
| 200 | } |
| 201 | const planningFailure = yield* stack.plan(desired).pipe(Effect.flip); |
| 202 | expect(planningFailure._tag).toBe("AccessDeniedException"); |
| 203 | const failure = yield* stack.deploy(desired).pipe(Effect.flip); |
| 204 | expect(failure._tag).toBe("AccessDeniedException"); |
| 205 | }).pipe(Effect.ensuring(restorePolicy.pipe(Effect.orDie))); |
| 206 | |
| 207 | yield* read.pipe( |
| 208 | Effect.retry({ |
| 209 | while: (error) => error._tag === "AccessDeniedException", |
| 210 | schedule: Schedule.spaced("1 second"), |
| 211 | times: 8, |
| 212 | }), |
| 213 | ); |
| 214 | if (aspect === "tagging") { |
| 215 | expect((yield* S3.getBucketTagging({ Bucket: bucket.bucketName })).TagSet).toEqual( |
| 216 | tagging.TagSet, |
| 217 | ); |
| 218 | } else { |
| 219 | expect( |
| 220 | (yield* S3.getBucketEncryption({ Bucket: bucket.bucketName })) |
| 221 | .ServerSideEncryptionConfiguration, |
| 222 | ).toEqual(encryption.ServerSideEncryptionConfiguration); |
| 223 | } |
| 224 | yield* stack.deploy(desired); |
| 225 | if (aspect === "tagging") { |
| 226 | expect((yield* S3.getBucketTagging({ Bucket: bucket.bucketName })).TagSet).toContainEqual( |
| 227 | { Key: "revision", Value: "after" }, |
| 228 | ); |
| 229 | } else { |
| 230 | expect( |
| 231 | (yield* S3.getBucketEncryption({ Bucket: bucket.bucketName })) |
| 232 | .ServerSideEncryptionConfiguration?.Rules[0]?.ApplyServerSideEncryptionByDefault |
| 233 | ?.SSEAlgorithm, |
| 234 | ).toBe("aws:kms"); |
| 235 | } |
| 236 | yield* stack.destroy(); |
| 237 | yield* assertBucketDeleted(bucket.bucketName); |
| 238 | }), |
| 239 | { tags: ["provider:aws", "provider:aws:s3", "live"], timeout: 120_000 }, |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | test.provider( |
| 244 | "PR1586 initializes tags after a real NoSuchTagSet response", |
| 245 | (stack) => |
| 246 | Effect.gen(function* () { |
| 247 | yield* stack.destroy(); |
| 248 | const bucket = yield* stack.deploy(Bucket("UntaggedBucket", {})); |
| 249 | const absent = yield* S3.getBucketTagging({ Bucket: bucket.bucketName }).pipe(Effect.flip); |
| 250 | expect(absent._tag).toBe("NoSuchTagSet"); |
| 251 | yield* stack.deploy(Bucket("UntaggedBucket", { tags: { initialized: "yes" } })); |
| 252 | expect((yield* S3.getBucketTagging({ Bucket: bucket.bucketName })).TagSet).toEqual([ |
| 253 | { Key: "initialized", Value: "yes" }, |
| 254 | ]); |
| 255 | yield* stack.destroy(); |
| 256 | yield* assertBucketDeleted(bucket.bucketName); |
| 257 | }), |
| 258 | { tags: ["provider:aws", "provider:aws:s3", "live"], timeout: 120_000 }, |
| 259 | ); |
| 260 | |
| 261 | test.provider( |
| 262 | "PR1587 skips encryption writes for a decoded KMS ARN and still updates keys", |
| 263 | (stack) => |
| 264 | Effect.gen(function* () { |
| 265 | yield* stack.destroy(); |
| 266 | const definition = ( |
| 267 | revision: string, |
| 268 | key: "first" | "second" | undefined, |
| 269 | bucketKeyEnabled = false, |
| 270 | policy?: AWS.IAM.PolicyStatement[], |
| 271 | ) => |
| 272 | Effect.gen(function* () { |
| 273 | const first = yield* AWS.KMS.Key("FirstKey", { deletionWindow: "7 days" }); |
| 274 | const second = yield* AWS.KMS.Key("SecondKey", { deletionWindow: "7 days" }); |
| 275 | const bucket = yield* Bucket("KmsIdentityBucket", { |
| 276 | tags: { revision }, |
| 277 | policy, |
| 278 | encryption: |
| 279 | key === undefined |
| 280 | ? undefined |
| 281 | : { |
| 282 | sseAlgorithm: "aws:kms", |
| 283 | kmsMasterKeyId: key === "first" ? first.keyArn : second.keyArn, |
| 284 | bucketKeyEnabled, |
| 285 | }, |
| 286 | }); |
| 287 | return { bucket, first, second }; |
| 288 | }); |
| 289 | const initial = yield* stack.deploy(definition("before", "first")); |
| 290 | const { bucket, first, second } = initial; |
| 291 | const observed = yield* S3.getBucketEncryption({ Bucket: bucket.bucketName }); |
| 292 | const decoded = |
| 293 | observed.ServerSideEncryptionConfiguration!.Rules[0]!.ApplyServerSideEncryptionByDefault! |
| 294 | .KMSMasterKeyID; |
| 295 | expect(Redacted.isRedacted(decoded)).toBe(true); |
| 296 | expect(Redacted.isRedacted(decoded) ? Redacted.value(decoded) : decoded).toBe(first.keyArn); |
| 297 | const probeWrite = S3.putBucketEncryption({ |
| 298 | Bucket: bucket.bucketName, |
| 299 | ServerSideEncryptionConfiguration: observed.ServerSideEncryptionConfiguration!, |
| 300 | }).pipe( |
| 301 | Effect.as(false), |
| 302 | Effect.catchTag("AccessDeniedException", () => Effect.succeed(true)), |
| 303 | ); |
| 304 | |
| 305 | const deny: AWS.IAM.PolicyStatement[] = [ |
| 306 | { |
| 307 | Effect: "Deny", |
| 308 | Principal: { AWS: "*" }, |
| 309 | Action: ["s3:PutEncryptionConfiguration"], |
| 310 | Resource: bucket.bucketArn, |
| 311 | }, |
| 312 | ]; |
| 313 | yield* Effect.gen(function* () { |
| 314 | yield* S3.putBucketPolicy({ |
| 315 | Bucket: bucket.bucketName, |
| 316 | Policy: JSON.stringify({ Version: "2012-10-17", Statement: deny }), |
| 317 | }); |
| 318 | expect( |
| 319 | yield* probeWrite.pipe( |
| 320 | Effect.repeat({ until: Boolean, schedule: Schedule.spaced("1 second"), times: 8 }), |
| 321 | ), |
| 322 | ).toBe(true); |
| 323 | const plan = yield* stack.plan(definition("after", "first", false, deny)); |
| 324 | expect(plan.resources.KmsIdentityBucket?.action).toBe("update"); |
| 325 | // Tags force reconcile; an encryption PUT would fail under the proven deny. |
| 326 | yield* stack.deploy(definition("after", "first", false, deny)); |
| 327 | expect((yield* S3.getBucketTagging({ Bucket: bucket.bucketName })).TagSet).toContainEqual({ |
| 328 | Key: "revision", |
| 329 | Value: "after", |
| 330 | }); |
| 331 | expect(yield* probeWrite).toBe(true); |
| 332 | }).pipe( |
| 333 | Effect.ensuring(S3.deleteBucketPolicy({ Bucket: bucket.bucketName }).pipe(Effect.orDie)), |
| 334 | ); |
| 335 | |
| 336 | // Wait for removal using the original configuration, never the desired next key. |
| 337 | yield* S3.putBucketEncryption({ |
| 338 | Bucket: bucket.bucketName, |
| 339 | ServerSideEncryptionConfiguration: observed.ServerSideEncryptionConfiguration!, |
| 340 | }).pipe( |
| 341 | Effect.retry({ |
| 342 | while: (error) => error._tag === "AccessDeniedException", |
| 343 | schedule: Schedule.spaced("1 second"), |
| 344 | times: 8, |
| 345 | }), |
| 346 | ); |
| 347 | yield* stack.deploy(definition("different-key", "second")); |
| 348 | const changed = (yield* S3.getBucketEncryption({ Bucket: bucket.bucketName })) |
| 349 | .ServerSideEncryptionConfiguration!.Rules[0]!.ApplyServerSideEncryptionByDefault! |
| 350 | .KMSMasterKeyID; |
| 351 | expect(Redacted.isRedacted(changed) ? Redacted.value(changed) : changed).toBe(second.keyArn); |
| 352 | yield* stack.deploy(definition("bucket-key", "second", true)); |
| 353 | expect( |
| 354 | (yield* S3.getBucketEncryption({ Bucket: bucket.bucketName })) |
| 355 | .ServerSideEncryptionConfiguration?.Rules[0]?.BucketKeyEnabled, |
| 356 | ).toBe(true); |
| 357 | const defaultsProgram = definition("bucket-key", undefined); |
| 358 | yield* stack.deploy(defaultsProgram); |
| 359 | const defaults = (yield* S3.getBucketEncryption({ Bucket: bucket.bucketName })) |
| 360 | .ServerSideEncryptionConfiguration!.Rules[0]!; |
| 361 | expect(defaults.ApplyServerSideEncryptionByDefault?.SSEAlgorithm).toBe("AES256"); |
| 362 | expect(defaults.ApplyServerSideEncryptionByDefault?.KMSMasterKeyID).toBeUndefined(); |
| 363 | expect(defaults.BucketKeyEnabled ?? false).toBe(false); |
| 364 | expect(defaults.BlockedEncryptionTypes?.EncryptionType).toEqual(["NONE"]); |
| 365 | expect((yield* stack.plan(defaultsProgram)).resources.KmsIdentityBucket?.action).toBe("noop"); |
| 366 | yield* stack.destroy(); |
| 367 | yield* assertBucketDeleted(bucket.bucketName); |
| 368 | for (const key of [first, second]) { |
| 369 | expect((yield* KMS.describeKey({ KeyId: key.keyId })).KeyMetadata?.KeyState).toBe( |
| 370 | "PendingDeletion", |
| 371 | ); |
| 372 | } |
| 373 | }), |
| 374 | { tags: ["provider:aws", "provider:aws:kms", "provider:aws:s3", "live"], timeout: 120_000 }, |
| 375 | ); |
| 376 | |
| 377 | for (const blocked of ["SSE-C", "NONE"] as const) { |
| 378 | test.provider( |
| 379 | `repairs external ${blocked} encryption settings with unchanged default inputs`, |
| 380 | (stack) => |
| 381 | Effect.gen(function* () { |
| 382 | yield* stack.destroy(); |
| 383 | const desired = Bucket("EncryptionBlocksBucket", {}); |
| 384 | const bucket = yield* stack.deploy(desired); |
| 385 | expect( |
| 386 | (yield* S3.getBucketEncryption({ Bucket: bucket.bucketName })) |
| 387 | .ServerSideEncryptionConfiguration?.Rules[0]?.BlockedEncryptionTypes?.EncryptionType, |
| 388 | ).toEqual(["NONE"]); |
| 389 | yield* S3.putBucketEncryption({ |
| 390 | Bucket: bucket.bucketName, |
| 391 | ServerSideEncryptionConfiguration: { |
| 392 | Rules: [ |
| 393 | { |
| 394 | ApplyServerSideEncryptionByDefault: { SSEAlgorithm: "aws:kms" }, |
| 395 | BlockedEncryptionTypes: { EncryptionType: [blocked] }, |
| 396 | }, |
| 397 | ], |
| 398 | }, |
| 399 | }); |
| 400 | const before = (yield* S3.getBucketEncryption({ Bucket: bucket.bucketName })) |
| 401 | .ServerSideEncryptionConfiguration!.Rules[0]!; |
| 402 | expect(before.BlockedEncryptionTypes?.EncryptionType).toEqual([blocked]); |
| 403 | expect(before.ApplyServerSideEncryptionByDefault?.SSEAlgorithm).toBe("aws:kms"); |
| 404 | const plan = yield* stack.plan(desired); |
| 405 | expect(plan.resources.EncryptionBlocksBucket?.action).toBe("update"); |
| 406 | yield* stack.deploy(desired); |
| 407 | const after = (yield* S3.getBucketEncryption({ Bucket: bucket.bucketName })) |
| 408 | .ServerSideEncryptionConfiguration!.Rules[0]!; |
| 409 | expect(after.ApplyServerSideEncryptionByDefault?.SSEAlgorithm).toBe("AES256"); |
| 410 | expect(after.ApplyServerSideEncryptionByDefault?.KMSMasterKeyID).toBeUndefined(); |
| 411 | expect(after.BucketKeyEnabled ?? false).toBe(false); |
| 412 | expect(after.BlockedEncryptionTypes?.EncryptionType).toEqual(["NONE"]); |
| 413 | expect((yield* stack.plan(desired)).resources.EncryptionBlocksBucket?.action).toBe("noop"); |
| 414 | yield* stack.destroy(); |
| 415 | yield* assertBucketDeleted(bucket.bucketName); |
| 416 | }), |
| 417 | { tags: ["provider:aws", "provider:aws:s3", "live"], timeout: 120_000 }, |
| 418 | ); |
| 419 | } |
| 420 | |
| 421 | test.provider( |
| 422 | "manages encryption blocks and restores defaults on property removal", |
| 423 | (stack) => |
| 424 | Effect.gen(function* () { |
| 425 | yield* stack.destroy(); |
| 426 | const program = ( |
| 427 | blockedEncryptionTypes: AWS.S3.BucketEncryption["blockedEncryptionTypes"], |
| 428 | revision: string, |
| 429 | policy?: AWS.IAM.PolicyStatement[], |
| 430 | ) => |
| 431 | Bucket("ManagedEncryptionBlocks", { |
| 432 | encryption: { sseAlgorithm: "AES256", blockedEncryptionTypes }, |
| 433 | tags: { revision }, |
| 434 | policy, |
| 435 | }); |
| 436 | const bucket = yield* stack.deploy(program([], "managed")); |
| 437 | // S3 serves stale encryption reads for a while after a write (worst on a |
| 438 | // re-created bucket name), so poll until the expected value shows up. |
| 439 | const blockedTypes = (expected: ReadonlyArray<string>) => |
| 440 | S3.getBucketEncryption({ Bucket: bucket.bucketName }).pipe( |
| 441 | Effect.map( |
| 442 | (result) => |
| 443 | result.ServerSideEncryptionConfiguration!.Rules[0]!.BlockedEncryptionTypes |
| 444 | ?.EncryptionType, |
| 445 | ), |
| 446 | Effect.repeat({ |
| 447 | until: (types) => JSON.stringify(types) === JSON.stringify(expected), |
| 448 | schedule: Schedule.spaced("2 seconds"), |
| 449 | times: 15, |
| 450 | }), |
| 451 | ); |
| 452 | expect(yield* blockedTypes(["NONE"])).toEqual(["NONE"]); |
| 453 | const deny: AWS.IAM.PolicyStatement[] = [ |
| 454 | { |
| 455 | Effect: "Deny", |
| 456 | Principal: { AWS: "*" }, |
| 457 | Action: ["s3:PutEncryptionConfiguration"], |
| 458 | Resource: bucket.bucketArn, |
| 459 | }, |
| 460 | ]; |
| 461 | const settings: "SSE-C"[][] = [["SSE-C"], []]; |
| 462 | for (const types of settings) { |
| 463 | const probeWrite = S3.putBucketEncryption({ |
| 464 | Bucket: bucket.bucketName, |
| 465 | ServerSideEncryptionConfiguration: { |
| 466 | Rules: [ |
| 467 | { |
| 468 | ApplyServerSideEncryptionByDefault: { SSEAlgorithm: "AES256" }, |
| 469 | BucketKeyEnabled: false, |
| 470 | BlockedEncryptionTypes: { EncryptionType: types.length ? types : ["NONE"] }, |
| 471 | }, |
| 472 | ], |
| 473 | }, |
| 474 | }).pipe( |
| 475 | Effect.as(false), |
| 476 | Effect.catchTag("AccessDeniedException", () => Effect.succeed(true)), |
| 477 | ); |
| 478 | const desired = program(types, "managed"); |
| 479 | expect((yield* stack.plan(desired)).resources.ManagedEncryptionBlocks?.action).toBe( |
| 480 | "update", |
| 481 | ); |
| 482 | const updated = yield* stack.deploy(desired); |
| 483 | expect(updated.bucketName).toBe(bucket.bucketName); |
| 484 | const expected = types.length ? ["SSE-C"] : ["NONE"]; |
| 485 | expect(yield* blockedTypes(expected)).toEqual(expected); |
| 486 | yield* Effect.gen(function* () { |
| 487 | yield* S3.putBucketPolicy({ |
| 488 | Bucket: bucket.bucketName, |
| 489 | Policy: JSON.stringify({ Version: "2012-10-17", Statement: deny }), |
| 490 | }); |
| 491 | expect( |
| 492 | yield* probeWrite.pipe( |
| 493 | Effect.repeat({ until: Boolean, schedule: Schedule.spaced("1 second"), times: 8 }), |
| 494 | ), |
| 495 | ).toBe(true); |
| 496 | // Tags force reconcile while equivalent restrictions must skip the denied PUT. |
| 497 | yield* stack.deploy(program([...types, ...types], "unchanged", deny)); |
| 498 | if (!types.length) { |
| 499 | yield* stack.deploy(program(undefined, "omitted", deny)); |
| 500 | } |
| 501 | expect(yield* probeWrite).toBe(true); |
| 502 | expect(yield* blockedTypes(expected)).toEqual(expected); |
| 503 | }).pipe( |
| 504 | Effect.ensuring(S3.deleteBucketPolicy({ Bucket: bucket.bucketName }).pipe(Effect.orDie)), |
| 505 | ); |
| 506 | expect( |
| 507 | yield* probeWrite.pipe( |
| 508 | Effect.repeat({ |
| 509 | until: (denied) => !denied, |
| 510 | schedule: Schedule.spaced("1 second"), |
| 511 | times: 8, |
| 512 | }), |
| 513 | ), |
| 514 | ).toBe(false); |
| 515 | yield* stack.deploy(program(undefined, "default")); |
| 516 | expect(yield* blockedTypes(["NONE"])).toEqual(["NONE"]); |
| 517 | } |
| 518 | const desired = program([], "repair"); |
| 519 | yield* stack.deploy(desired); |
| 520 | yield* S3.putBucketEncryption({ |
| 521 | Bucket: bucket.bucketName, |
| 522 | ServerSideEncryptionConfiguration: { |
| 523 | Rules: [ |
| 524 | { |
| 525 | ApplyServerSideEncryptionByDefault: { SSEAlgorithm: "AES256" }, |
| 526 | BlockedEncryptionTypes: { EncryptionType: ["SSE-C"] }, |
| 527 | }, |
| 528 | ], |
| 529 | }, |
| 530 | }); |
| 531 | expect((yield* stack.plan(desired)).resources.ManagedEncryptionBlocks?.action).toBe("update"); |
| 532 | yield* stack.deploy(desired); |
| 533 | expect(yield* blockedTypes(["NONE"])).toEqual(["NONE"]); |
| 534 | expect( |
| 535 | (yield* stack.plan(program([], "repair"))).resources.ManagedEncryptionBlocks?.action, |
| 536 | ).toBe("noop"); |
| 537 | yield* stack.destroy(); |
| 538 | const absent = yield* S3.getBucketLocation({ Bucket: bucket.bucketName }).pipe( |
| 539 | Effect.as(false), |
| 540 | Effect.catchTag("NoSuchBucket", () => Effect.succeed(true)), |
| 541 | Effect.repeat({ until: Boolean, schedule: Schedule.spaced("1 second"), times: 8 }), |
| 542 | ); |
| 543 | expect(absent).toBe(true); |
| 544 | }), |
| 545 | { tags: ["provider:aws", "provider:aws:s3", "live"], timeout: 120_000 }, |
| 546 | ); |
| 547 | |
| 548 | test.provider( |
| 549 | "create and delete bucket with default props", |
| 550 | (stack) => |
| 551 | Effect.gen(function* () { |
| 552 | yield* stack.destroy(); |
| 553 | |
| 554 | const bucket = yield* stack.deploy( |
| 555 | Effect.gen(function* () { |
| 556 | return yield* Bucket("DefaultBucket"); |
| 557 | }), |
| 558 | ); |
| 559 | |
| 560 | expect(bucket.bucketName).toBeDefined(); |
| 561 | expect(bucket.bucketArn).toBeDefined(); |
| 562 | expect(bucket.region).toBeDefined(); |
| 563 | |
| 564 | yield* S3.headBucket({ Bucket: bucket.bucketName }); |
| 565 | |
| 566 | yield* stack.destroy(); |
| 567 | |
| 568 | yield* assertBucketDeleted(bucket.bucketName); |
| 569 | }), |
| 570 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 571 | ); |
| 572 | |
| 573 | test.provider( |
| 574 | "create, update, delete bucket", |
| 575 | (stack) => |
| 576 | Effect.gen(function* () { |
| 577 | yield* stack.destroy(); |
| 578 | |
| 579 | const bucket = yield* stack.deploy( |
| 580 | Effect.gen(function* () { |
| 581 | return yield* Bucket("TestBucket", { |
| 582 | bucketName: "alchemy-test-bucket-crud", |
| 583 | tags: { Environment: "test" }, |
| 584 | forceDestroy: true, |
| 585 | }); |
| 586 | }), |
| 587 | ); |
| 588 | |
| 589 | yield* S3.headBucket({ Bucket: bucket.bucketName }); |
| 590 | |
| 591 | const tagging = yield* S3.getBucketTagging({ Bucket: bucket.bucketName }); |
| 592 | expect(tagging.TagSet).toContainEqual({ Key: "Environment", Value: "test" }); |
| 593 | |
| 594 | yield* stack.deploy( |
| 595 | Effect.gen(function* () { |
| 596 | return yield* Bucket("TestBucket", { |
| 597 | bucketName: "alchemy-test-bucket-crud", |
| 598 | tags: { Environment: "production", Team: "platform" }, |
| 599 | forceDestroy: true, |
| 600 | }); |
| 601 | }), |
| 602 | ); |
| 603 | |
| 604 | const updatedTagging = yield* S3.getBucketTagging({ Bucket: bucket.bucketName }); |
| 605 | expect(updatedTagging.TagSet).toContainEqual({ Key: "Environment", Value: "production" }); |
| 606 | expect(updatedTagging.TagSet).toContainEqual({ Key: "Team", Value: "platform" }); |
| 607 | |
| 608 | yield* stack.destroy(); |
| 609 | |
| 610 | yield* assertBucketDeleted(bucket.bucketName); |
| 611 | }), |
| 612 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 613 | ); |
| 614 | |
| 615 | test.provider( |
| 616 | "create bucket with custom name", |
| 617 | (stack) => |
| 618 | Effect.gen(function* () { |
| 619 | yield* stack.destroy(); |
| 620 | |
| 621 | const bucket = yield* stack.deploy( |
| 622 | Effect.gen(function* () { |
| 623 | return yield* Bucket("CustomNameBucket", { |
| 624 | bucketName: "alchemy-test-bucket-custom-name", |
| 625 | forceDestroy: true, |
| 626 | }); |
| 627 | }), |
| 628 | ); |
| 629 | |
| 630 | expect(bucket.bucketName).toEqual("alchemy-test-bucket-custom-name"); |
| 631 | expect(bucket.bucketArn).toEqual("arn:aws:s3:::alchemy-test-bucket-custom-name"); |
| 632 | |
| 633 | yield* S3.headBucket({ Bucket: bucket.bucketName }); |
| 634 | |
| 635 | yield* stack.destroy(); |
| 636 | |
| 637 | yield* assertBucketDeleted(bucket.bucketName); |
| 638 | }), |
| 639 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 640 | ); |
| 641 | |
| 642 | test.provider( |
| 643 | "create bucket with forceDestroy", |
| 644 | (stack) => |
| 645 | Effect.gen(function* () { |
| 646 | yield* stack.destroy(); |
| 647 | |
| 648 | const bucket = yield* stack.deploy( |
| 649 | Effect.gen(function* () { |
| 650 | return yield* Bucket("ForceDestroyBucket", { |
| 651 | bucketName: "alchemy-test-bucket-force-destroy", |
| 652 | forceDestroy: true, |
| 653 | }); |
| 654 | }), |
| 655 | ); |
| 656 | |
| 657 | yield* S3.putObject({ |
| 658 | Bucket: bucket.bucketName, |
| 659 | Key: "test-object.txt", |
| 660 | Body: "Hello, World!", |
| 661 | }); |
| 662 | |
| 663 | // A heavily parallel account sweep can briefly observe the successful |
| 664 | // PutObject before the object is readable through a different S3 |
| 665 | // endpoint. Retry only the typed visibility miss, with a hard bound; |
| 666 | // authorization and request-shape errors still fail immediately. |
| 667 | yield* S3.headObject({ Bucket: bucket.bucketName, Key: "test-object.txt" }).pipe( |
| 668 | Effect.retry({ |
| 669 | while: (error) => error._tag === "NotFound", |
| 670 | schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(6)]), |
| 671 | }), |
| 672 | ); |
| 673 | |
| 674 | yield* stack.destroy(); |
| 675 | |
| 676 | yield* assertBucketDeleted(bucket.bucketName); |
| 677 | }), |
| 678 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 679 | ); |
| 680 | |
| 681 | test.provider( |
| 682 | "idempotent create - bucket already exists", |
| 683 | (stack) => |
| 684 | Effect.gen(function* () { |
| 685 | yield* stack.destroy(); |
| 686 | |
| 687 | const bucket1 = yield* stack.deploy( |
| 688 | Effect.gen(function* () { |
| 689 | return yield* Bucket("IdempotentBucket", { |
| 690 | bucketName: "alchemy-test-bucket-idempotent", |
| 691 | forceDestroy: true, |
| 692 | }); |
| 693 | }), |
| 694 | ); |
| 695 | const bucketName = bucket1.bucketName; |
| 696 | |
| 697 | const bucket2 = yield* stack.deploy( |
| 698 | Effect.gen(function* () { |
| 699 | return yield* Bucket("IdempotentBucket", { |
| 700 | bucketName: "alchemy-test-bucket-idempotent", |
| 701 | forceDestroy: true, |
| 702 | }); |
| 703 | }), |
| 704 | ); |
| 705 | expect(bucket2.bucketName).toEqual(bucketName); |
| 706 | |
| 707 | yield* stack.destroy(); |
| 708 | |
| 709 | yield* assertBucketDeleted(bucketName); |
| 710 | }), |
| 711 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 712 | ); |
| 713 | |
| 714 | test.provider( |
| 715 | "create bucket with objectLockEnabled", |
| 716 | (stack) => |
| 717 | Effect.gen(function* () { |
| 718 | yield* stack.destroy(); |
| 719 | |
| 720 | const bucket = yield* stack.deploy( |
| 721 | Effect.gen(function* () { |
| 722 | return yield* Bucket("ObjectLockBucket", { |
| 723 | bucketName: "alchemy-test-bucket-object-lock", |
| 724 | objectLockEnabled: true, |
| 725 | forceDestroy: true, |
| 726 | }); |
| 727 | }), |
| 728 | ); |
| 729 | |
| 730 | const objectLockConfig = yield* S3.getObjectLockConfiguration({ Bucket: bucket.bucketName }); |
| 731 | expect(objectLockConfig.ObjectLockConfiguration?.ObjectLockEnabled).toEqual("Enabled"); |
| 732 | |
| 733 | yield* stack.destroy(); |
| 734 | |
| 735 | yield* assertBucketDeleted(bucket.bucketName); |
| 736 | }), |
| 737 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 738 | ); |
| 739 | |
| 740 | test.provider( |
| 741 | "remove all tags from bucket", |
| 742 | (stack) => |
| 743 | Effect.gen(function* () { |
| 744 | yield* stack.destroy(); |
| 745 | |
| 746 | const bucket = yield* stack.deploy( |
| 747 | Effect.gen(function* () { |
| 748 | return yield* Bucket("TagRemovalBucket", { |
| 749 | bucketName: "alchemy-test-bucket-tag-removal", |
| 750 | tags: { Environment: "test", Team: "platform" }, |
| 751 | forceDestroy: true, |
| 752 | }); |
| 753 | }), |
| 754 | ); |
| 755 | const bucketName = bucket.bucketName; |
| 756 | |
| 757 | const tagging = yield* S3.getBucketTagging({ Bucket: bucketName }); |
| 758 | expect(tagging.TagSet).toHaveLength(2); |
| 759 | |
| 760 | yield* stack.deploy( |
| 761 | Effect.gen(function* () { |
| 762 | return yield* Bucket("TagRemovalBucket", { |
| 763 | bucketName: "alchemy-test-bucket-tag-removal", |
| 764 | forceDestroy: true, |
| 765 | }); |
| 766 | }), |
| 767 | ); |
| 768 | |
| 769 | const result = yield* S3.getBucketTagging({ Bucket: bucketName }).pipe( |
| 770 | Effect.map(() => "has-tags" as const), |
| 771 | Effect.catchTag("NoSuchTagSet", () => Effect.succeed("no-tags" as const)), |
| 772 | ); |
| 773 | expect(result).toEqual("no-tags"); |
| 774 | |
| 775 | yield* stack.destroy(); |
| 776 | |
| 777 | yield* assertBucketDeleted(bucketName); |
| 778 | }), |
| 779 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 780 | ); |
| 781 | |
| 782 | test.provider( |
| 783 | "create and remove bucket policy from bindings", |
| 784 | (stack) => |
| 785 | Effect.gen(function* () { |
| 786 | yield* stack.destroy(); |
| 787 | |
| 788 | const distributionArn = "arn:aws:cloudfront::123456789012:distribution/TESTDIST"; |
| 789 | const bucketArn = "arn:aws:s3:::alchemy-test-bucket-policy-bindings"; |
| 790 | |
| 791 | const bucket = yield* stack.deploy( |
| 792 | Effect.gen(function* () { |
| 793 | const bucket = yield* Bucket("PolicyBucket", { |
| 794 | bucketName: "alchemy-test-bucket-policy-bindings", |
| 795 | forceDestroy: true, |
| 796 | }); |
| 797 | |
| 798 | yield* bucket.bind("AWS.S3.Policy(TestDistribution, PolicyBucket)", { |
| 799 | policyStatements: [ |
| 800 | { |
| 801 | Effect: "Allow", |
| 802 | Principal: { Service: "cloudfront.amazonaws.com" }, |
| 803 | Action: ["s3:GetObject"], |
| 804 | Resource: [`${bucketArn}/*`], |
| 805 | Condition: { StringEquals: { "AWS:SourceArn": distributionArn } }, |
| 806 | }, |
| 807 | ], |
| 808 | }); |
| 809 | |
| 810 | return bucket; |
| 811 | }), |
| 812 | ); |
| 813 | |
| 814 | const bucketPolicy = yield* S3.getBucketPolicy({ Bucket: bucket.bucketName }).pipe( |
| 815 | Effect.map((response) => JSON.parse(response.Policy!)), |
| 816 | ); |
| 817 | const statement = bucketPolicy.Statement[0]; |
| 818 | |
| 819 | expect(bucketPolicy.Version).toEqual("2012-10-17"); |
| 820 | expect(statement.Effect).toEqual("Allow"); |
| 821 | expect(statement.Principal).toEqual({ Service: "cloudfront.amazonaws.com" }); |
| 822 | expect(statement.Action).toEqual("s3:GetObject"); |
| 823 | expect(statement.Resource).toEqual(`${bucketArn}/*`); |
| 824 | expect(statement.Condition).toEqual({ StringEquals: { "AWS:SourceArn": distributionArn } }); |
| 825 | |
| 826 | yield* stack.deploy( |
| 827 | Effect.gen(function* () { |
| 828 | return yield* Bucket("PolicyBucket", { |
| 829 | bucketName: "alchemy-test-bucket-policy-bindings", |
| 830 | forceDestroy: true, |
| 831 | }); |
| 832 | }), |
| 833 | ); |
| 834 | |
| 835 | const policyAfterRemoval = yield* S3.getBucketPolicy({ Bucket: bucket.bucketName }).pipe( |
| 836 | Effect.map(() => "has-policy" as const), |
| 837 | Effect.catchTag("NoSuchBucketPolicy", () => Effect.succeed("no-policy" as const)), |
| 838 | ); |
| 839 | |
| 840 | expect(policyAfterRemoval).toEqual("no-policy"); |
| 841 | |
| 842 | yield* stack.destroy(); |
| 843 | |
| 844 | yield* assertBucketDeleted(bucket.bucketName); |
| 845 | }), |
| 846 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 847 | ); |
| 848 | |
| 849 | // Engine-level adoption: S3 has no per-stack ownership signal (we don't |
| 850 | // stamp alchemy tags on buckets — the canonical existence check is |
| 851 | // `headBucket`, which only succeeds when the bucket is owned by *this AWS |
| 852 | // account*). So a name match means we own it at the account level — silent |
| 853 | // adoption is correct for the cold-start case. |
| 854 | test.provider( |
| 855 | "owned bucket (account-level) is silently adopted without --adopt", |
| 856 | (stack) => |
| 857 | Effect.gen(function* () { |
| 858 | yield* stack.destroy(); |
| 859 | |
| 860 | // Deterministic name: the state wipe below intentionally makes the |
| 861 | // bucket invisible to the scratch stack's auto-destroy for a window, so |
| 862 | // the name must be stable across runs — a re-run then reclaims any |
| 863 | // orphan left by a previously-killed run instead of minting a new one. |
| 864 | const bucketName = "alchemy-test-s3-adopt-bucket"; |
| 865 | |
| 866 | // Idempotent pre-clean: reclaim a leftover from a prior interrupted run. |
| 867 | yield* deleteBucketIfExists(bucketName); |
| 868 | |
| 869 | yield* Effect.gen(function* () { |
| 870 | const initial = yield* stack.deploy( |
| 871 | Effect.gen(function* () { |
| 872 | return yield* Bucket("AdoptableBucket", { bucketName, forceDestroy: true }); |
| 873 | }), |
| 874 | ); |
| 875 | expect(initial.bucketName).toEqual(bucketName); |
| 876 | yield* S3.putBucketEncryption({ |
| 877 | Bucket: bucketName, |
| 878 | ServerSideEncryptionConfiguration: { |
| 879 | Rules: [ |
| 880 | { |
| 881 | ApplyServerSideEncryptionByDefault: { SSEAlgorithm: "aws:kms" }, |
| 882 | BucketKeyEnabled: true, |
| 883 | BlockedEncryptionTypes: { EncryptionType: ["NONE"] }, |
| 884 | }, |
| 885 | ], |
| 886 | }, |
| 887 | }); |
| 888 | |
| 889 | // Wipe state — bucket stays in S3. |
| 890 | yield* Effect.gen(function* () { |
| 891 | const state = yield* yield* State; |
| 892 | yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "AdoptableBucket" }); |
| 893 | }).pipe(Effect.provide(stack.state)); |
| 894 | |
| 895 | const adopted = yield* stack.deploy( |
| 896 | Effect.gen(function* () { |
| 897 | return yield* Bucket("AdoptableBucket", { bucketName, forceDestroy: true }); |
| 898 | }), |
| 899 | ); |
| 900 | |
| 901 | expect(adopted.bucketArn).toEqual(initial.bucketArn); |
| 902 | const defaults = (yield* S3.getBucketEncryption({ Bucket: bucketName })) |
| 903 | .ServerSideEncryptionConfiguration!.Rules[0]!; |
| 904 | expect(defaults.ApplyServerSideEncryptionByDefault?.SSEAlgorithm).toBe("AES256"); |
| 905 | expect(defaults.ApplyServerSideEncryptionByDefault?.KMSMasterKeyID).toBeUndefined(); |
| 906 | expect(defaults.BucketKeyEnabled ?? false).toBe(false); |
| 907 | expect(defaults.BlockedEncryptionTypes?.EncryptionType).toEqual(["NONE"]); |
| 908 | |
| 909 | yield* stack.destroy(); |
| 910 | yield* assertBucketDeleted(bucketName); |
| 911 | }).pipe( |
| 912 | // Guaranteed out-of-band cleanup: while state is wiped the bucket is |
| 913 | // outside the scratch stack, so the framework's ensuring-destroy can't |
| 914 | // see it. This finalizer runs on success, failure, AND interruption; |
| 915 | // it's a no-op when stack.destroy already deleted the bucket. |
| 916 | Effect.ensuring(deleteBucketIfExists(bucketName).pipe(Effect.ignore)), |
| 917 | ); |
| 918 | }), |
| 919 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 920 | ); |
| 921 | |
| 922 | // Canonical `list()` test (AWS account/region-scoped collection): deploy a |
| 923 | // real bucket, resolve the provider from context via `findProviderByType`, |
| 924 | // call `list()`, and assert the deployed bucket appears in the |
| 925 | // exhaustively-paginated result. |
| 926 | test.provider( |
| 927 | "list enumerates the deployed bucket", |
| 928 | (stack) => |
| 929 | Effect.gen(function* () { |
| 930 | yield* stack.destroy(); |
| 931 | |
| 932 | const bucket = yield* stack.deploy( |
| 933 | Effect.gen(function* () { |
| 934 | return yield* Bucket("ListBucket", { |
| 935 | bucketName: "alchemy-test-bucket-list", |
| 936 | forceDestroy: true, |
| 937 | }); |
| 938 | }), |
| 939 | ); |
| 940 | |
| 941 | const provider = yield* Provider.findProvider(Bucket); |
| 942 | const all = yield* provider.list(); |
| 943 | |
| 944 | expect(all.some((b) => b.bucketName === bucket.bucketName)).toBe(true); |
| 945 | |
| 946 | yield* stack.destroy(); |
| 947 | |
| 948 | yield* assertBucketDeleted(bucket.bucketName); |
| 949 | }), |
| 950 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 951 | ); |
| 952 | |
| 953 | test.provider( |
| 954 | "versioning enable then suspend", |
| 955 | (stack) => |
| 956 | Effect.gen(function* () { |
| 957 | yield* stack.destroy(); |
| 958 | |
| 959 | const name = "alchemy-test-bucket-versioning"; |
| 960 | const bucket = yield* stack.deploy( |
| 961 | Bucket("VersioningBucket", { bucketName: name, versioning: "Enabled", forceDestroy: true }), |
| 962 | ); |
| 963 | |
| 964 | const v1 = yield* S3.getBucketVersioning({ Bucket: bucket.bucketName }); |
| 965 | expect(v1.Status).toEqual("Enabled"); |
| 966 | |
| 967 | yield* stack.deploy( |
| 968 | Bucket("VersioningBucket", { |
| 969 | bucketName: name, |
| 970 | versioning: "Suspended", |
| 971 | forceDestroy: true, |
| 972 | }), |
| 973 | ); |
| 974 | |
| 975 | const v2 = yield* S3.getBucketVersioning({ Bucket: bucket.bucketName }); |
| 976 | expect(v2.Status).toEqual("Suspended"); |
| 977 | |
| 978 | yield* stack.destroy(); |
| 979 | yield* assertBucketDeleted(bucket.bucketName); |
| 980 | }), |
| 981 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 982 | ); |
| 983 | |
| 984 | test.provider( |
| 985 | "encryption SSE-S3 set and update bucketKey", |
| 986 | (stack) => |
| 987 | Effect.gen(function* () { |
| 988 | yield* stack.destroy(); |
| 989 | |
| 990 | const name = "alchemy-test-bucket-encryption"; |
| 991 | const bucket = yield* stack.deploy( |
| 992 | Bucket("EncryptionBucket", { |
| 993 | bucketName: name, |
| 994 | encryption: { sseAlgorithm: "AES256" }, |
| 995 | forceDestroy: true, |
| 996 | }), |
| 997 | ); |
| 998 | |
| 999 | const e1 = yield* S3.getBucketEncryption({ Bucket: bucket.bucketName }); |
| 1000 | expect( |
| 1001 | e1.ServerSideEncryptionConfiguration?.Rules?.[0]?.ApplyServerSideEncryptionByDefault |
| 1002 | ?.SSEAlgorithm, |
| 1003 | ).toEqual("AES256"); |
| 1004 | |
| 1005 | yield* stack.deploy( |
| 1006 | Bucket("EncryptionBucket", { |
| 1007 | bucketName: name, |
| 1008 | encryption: { sseAlgorithm: "AES256", bucketKeyEnabled: true }, |
| 1009 | forceDestroy: true, |
| 1010 | }), |
| 1011 | ); |
| 1012 | |
| 1013 | const e2 = yield* S3.getBucketEncryption({ Bucket: bucket.bucketName }); |
| 1014 | expect(e2.ServerSideEncryptionConfiguration?.Rules?.[0]?.BucketKeyEnabled).toEqual(true); |
| 1015 | |
| 1016 | yield* stack.destroy(); |
| 1017 | yield* assertBucketDeleted(bucket.bucketName); |
| 1018 | }), |
| 1019 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 1020 | ); |
| 1021 | |
| 1022 | test.provider( |
| 1023 | "public access block set, update, remove", |
| 1024 | (stack) => |
| 1025 | Effect.gen(function* () { |
| 1026 | yield* stack.destroy(); |
| 1027 | |
| 1028 | const name = "alchemy-test-bucket-pab"; |
| 1029 | const bucket = yield* stack.deploy( |
| 1030 | Bucket("PabBucket", { |
| 1031 | bucketName: name, |
| 1032 | publicAccessBlock: { |
| 1033 | blockPublicAcls: true, |
| 1034 | ignorePublicAcls: true, |
| 1035 | blockPublicPolicy: true, |
| 1036 | restrictPublicBuckets: true, |
| 1037 | }, |
| 1038 | forceDestroy: true, |
| 1039 | }), |
| 1040 | ); |
| 1041 | |
| 1042 | const p1 = yield* S3.getPublicAccessBlock({ Bucket: bucket.bucketName }); |
| 1043 | expect(p1.PublicAccessBlockConfiguration?.BlockPublicAcls).toEqual(true); |
| 1044 | expect(p1.PublicAccessBlockConfiguration?.RestrictPublicBuckets).toEqual(true); |
| 1045 | |
| 1046 | yield* stack.deploy( |
| 1047 | Bucket("PabBucket", { |
| 1048 | bucketName: name, |
| 1049 | publicAccessBlock: { |
| 1050 | blockPublicAcls: true, |
| 1051 | ignorePublicAcls: false, |
| 1052 | blockPublicPolicy: true, |
| 1053 | restrictPublicBuckets: false, |
| 1054 | }, |
| 1055 | forceDestroy: true, |
| 1056 | }), |
| 1057 | ); |
| 1058 | |
| 1059 | const p2 = yield* S3.getPublicAccessBlock({ Bucket: bucket.bucketName }); |
| 1060 | expect(p2.PublicAccessBlockConfiguration?.RestrictPublicBuckets).toEqual(false); |
| 1061 | |
| 1062 | yield* stack.destroy(); |
| 1063 | yield* assertBucketDeleted(bucket.bucketName); |
| 1064 | }), |
| 1065 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 1066 | ); |
| 1067 | |
| 1068 | test.provider( |
| 1069 | "cors add rule then remove all", |
| 1070 | (stack) => |
| 1071 | Effect.gen(function* () { |
| 1072 | yield* stack.destroy(); |
| 1073 | |
| 1074 | const name = "alchemy-test-bucket-cors"; |
| 1075 | const bucket = yield* stack.deploy( |
| 1076 | Bucket("CorsBucket", { |
| 1077 | bucketName: name, |
| 1078 | cors: [ |
| 1079 | { |
| 1080 | AllowedMethods: ["GET"], |
| 1081 | AllowedOrigins: ["https://example.com"], |
| 1082 | AllowedHeaders: ["*"], |
| 1083 | MaxAgeSeconds: 3000, |
| 1084 | }, |
| 1085 | ], |
| 1086 | forceDestroy: true, |
| 1087 | }), |
| 1088 | ); |
| 1089 | |
| 1090 | const c1 = yield* S3.getBucketCors({ Bucket: bucket.bucketName }); |
| 1091 | expect(c1.CORSRules).toHaveLength(1); |
| 1092 | |
| 1093 | yield* stack.deploy( |
| 1094 | Bucket("CorsBucket", { |
| 1095 | bucketName: name, |
| 1096 | cors: [ |
| 1097 | { |
| 1098 | AllowedMethods: ["GET"], |
| 1099 | AllowedOrigins: ["https://example.com"], |
| 1100 | AllowedHeaders: ["*"], |
| 1101 | MaxAgeSeconds: 3000, |
| 1102 | }, |
| 1103 | { AllowedMethods: ["PUT", "POST"], AllowedOrigins: ["https://app.example.com"] }, |
| 1104 | ], |
| 1105 | forceDestroy: true, |
| 1106 | }), |
| 1107 | ); |
| 1108 | |
| 1109 | const c2 = yield* S3.getBucketCors({ Bucket: bucket.bucketName }); |
| 1110 | expect(c2.CORSRules).toHaveLength(2); |
| 1111 | |
| 1112 | yield* stack.deploy(Bucket("CorsBucket", { bucketName: name, cors: [], forceDestroy: true })); |
| 1113 | |
| 1114 | const removed = yield* S3.getBucketCors({ Bucket: bucket.bucketName }).pipe( |
| 1115 | Effect.map(() => "has-cors" as const), |
| 1116 | Effect.catchTag("NoSuchCORSConfiguration", () => Effect.succeed("no-cors" as const)), |
| 1117 | ); |
| 1118 | expect(removed).toEqual("no-cors"); |
| 1119 | |
| 1120 | yield* stack.destroy(); |
| 1121 | yield* assertBucketDeleted(bucket.bucketName); |
| 1122 | }), |
| 1123 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 1124 | ); |
| 1125 | |
| 1126 | test.provider( |
| 1127 | "lifecycle add rule then remove", |
| 1128 | (stack) => |
| 1129 | Effect.gen(function* () { |
| 1130 | yield* stack.destroy(); |
| 1131 | |
| 1132 | const name = "alchemy-test-bucket-lifecycle"; |
| 1133 | const bucket = yield* stack.deploy( |
| 1134 | Bucket("LifecycleBucket", { |
| 1135 | bucketName: name, |
| 1136 | lifecycleRules: [ |
| 1137 | { |
| 1138 | ID: "expire-logs", |
| 1139 | Status: "Enabled", |
| 1140 | Filter: { Prefix: "logs/" }, |
| 1141 | Expiration: { Days: 30 }, |
| 1142 | AbortIncompleteMultipartUpload: { DaysAfterInitiation: 7 }, |
| 1143 | }, |
| 1144 | ], |
| 1145 | forceDestroy: true, |
| 1146 | }), |
| 1147 | ); |
| 1148 | |
| 1149 | const l1 = yield* S3.getBucketLifecycleConfiguration({ Bucket: bucket.bucketName }); |
| 1150 | expect(l1.Rules).toHaveLength(1); |
| 1151 | expect(l1.Rules?.[0]?.Expiration?.Days).toEqual(30); |
| 1152 | |
| 1153 | yield* stack.deploy( |
| 1154 | Bucket("LifecycleBucket", { bucketName: name, lifecycleRules: [], forceDestroy: true }), |
| 1155 | ); |
| 1156 | |
| 1157 | // Lifecycle config is eventually consistent — the rule can linger on reads |
| 1158 | // for a few seconds after deleteBucketLifecycle. Retry until it clears. |
| 1159 | const removed = yield* S3.getBucketLifecycleConfiguration({ Bucket: bucket.bucketName }).pipe( |
| 1160 | Effect.map(() => "has-lifecycle" as const), |
| 1161 | Effect.catchTag("NoSuchLifecycleConfiguration", () => |
| 1162 | Effect.succeed("no-lifecycle" as const), |
| 1163 | ), |
| 1164 | Effect.repeat({ |
| 1165 | schedule: Schedule.spaced("3 seconds"), |
| 1166 | until: (r) => r === "no-lifecycle", |
| 1167 | // S3 lifecycle deletion can take well over 30s to become visible on |
| 1168 | // reads. Give it ~90s (30 × 3s) before failing — the loop short-circuits |
| 1169 | // via `until` the moment the config clears, so this only costs wall-clock |
| 1170 | // time on the (rare) slow-propagation runs that were flaking at times: 10. |
| 1171 | times: 30, |
| 1172 | }), |
| 1173 | ); |
| 1174 | expect(removed).toEqual("no-lifecycle"); |
| 1175 | |
| 1176 | yield* stack.destroy(); |
| 1177 | yield* assertBucketDeleted(bucket.bucketName); |
| 1178 | }), |
| 1179 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 1180 | ); |
| 1181 | |
| 1182 | test.provider( |
| 1183 | "ownership controls and website hosting serves index.html", |
| 1184 | (stack) => |
| 1185 | Effect.gen(function* () { |
| 1186 | yield* stack.destroy(); |
| 1187 | |
| 1188 | const name = "alchemy-test-bucket-website"; |
| 1189 | const bucket = yield* stack.deploy( |
| 1190 | Bucket("WebsiteBucket", { |
| 1191 | bucketName: name, |
| 1192 | objectOwnership: "BucketOwnerPreferred", |
| 1193 | // The website endpoint serves anonymously, so the bucket must allow |
| 1194 | // a public-read policy: PAB flags all off + a public GetObject |
| 1195 | // statement. (publicAccessBlock syncs before policy in reconcile.) |
| 1196 | publicAccessBlock: { |
| 1197 | blockPublicAcls: false, |
| 1198 | ignorePublicAcls: false, |
| 1199 | blockPublicPolicy: false, |
| 1200 | restrictPublicBuckets: false, |
| 1201 | }, |
| 1202 | policy: [ |
| 1203 | { |
| 1204 | Sid: "PublicReadGetObject", |
| 1205 | Effect: "Allow", |
| 1206 | Principal: { AWS: "*" }, |
| 1207 | Action: ["s3:GetObject"], |
| 1208 | Resource: [`arn:aws:s3:::${name}/*`], |
| 1209 | }, |
| 1210 | ], |
| 1211 | website: { |
| 1212 | indexDocument: { suffix: "index.html" }, |
| 1213 | errorDocument: { key: "error.html" }, |
| 1214 | }, |
| 1215 | forceDestroy: true, |
| 1216 | }), |
| 1217 | ); |
| 1218 | |
| 1219 | const own = yield* S3.getBucketOwnershipControls({ Bucket: bucket.bucketName }); |
| 1220 | expect(own.OwnershipControls?.Rules?.[0]?.ObjectOwnership).toEqual("BucketOwnerPreferred"); |
| 1221 | |
| 1222 | const web = yield* S3.getBucketWebsite({ Bucket: bucket.bucketName }); |
| 1223 | expect(web.IndexDocument?.Suffix).toEqual("index.html"); |
| 1224 | expect(web.ErrorDocument?.Key).toEqual("error.html"); |
| 1225 | |
| 1226 | // Prove the website endpoint actually serves: upload an index document |
| 1227 | // and GET it anonymously over HTTP (marker-anchored, with propagation |
| 1228 | // retry — fresh website endpoints can take a few seconds to serve). |
| 1229 | const marker = "alchemy-s3-website-test-marker"; |
| 1230 | yield* S3.putObject({ |
| 1231 | Bucket: bucket.bucketName, |
| 1232 | Key: "index.html", |
| 1233 | Body: `<html><body>${marker}</body></html>`, |
| 1234 | ContentType: "text/html", |
| 1235 | }); |
| 1236 | |
| 1237 | const websiteUrl = `http://${name}.s3-website-${bucket.region}.amazonaws.com/`; |
| 1238 | const body = yield* HttpClient.get(websiteUrl).pipe( |
| 1239 | Effect.flatMap((response) => |
| 1240 | response.status === 200 |
| 1241 | ? response.text |
| 1242 | : Effect.fail(new Error(`website endpoint returned ${response.status}`)), |
| 1243 | ), |
| 1244 | Effect.retry({ schedule: Schedule.max([Schedule.exponential(1000), Schedule.recurs(8)]) }), |
| 1245 | ); |
| 1246 | expect(body).toContain(marker); |
| 1247 | |
| 1248 | // In-place update of the index document; website config is in-place |
| 1249 | // updatable. (Omitting the prop leaves the config untouched — to clear it |
| 1250 | // a user removes the resource; we never silently drop unmanaged config.) |
| 1251 | yield* stack.deploy( |
| 1252 | Bucket("WebsiteBucket", { |
| 1253 | bucketName: name, |
| 1254 | objectOwnership: "BucketOwnerPreferred", |
| 1255 | publicAccessBlock: { |
| 1256 | blockPublicAcls: false, |
| 1257 | ignorePublicAcls: false, |
| 1258 | blockPublicPolicy: false, |
| 1259 | restrictPublicBuckets: false, |
| 1260 | }, |
| 1261 | policy: [ |
| 1262 | { |
| 1263 | Sid: "PublicReadGetObject", |
| 1264 | Effect: "Allow", |
| 1265 | Principal: { AWS: "*" }, |
| 1266 | Action: ["s3:GetObject"], |
| 1267 | Resource: [`arn:aws:s3:::${name}/*`], |
| 1268 | }, |
| 1269 | ], |
| 1270 | website: { indexDocument: { suffix: "home.html" }, errorDocument: { key: "error.html" } }, |
| 1271 | forceDestroy: true, |
| 1272 | }), |
| 1273 | ); |
| 1274 | |
| 1275 | const web2 = yield* S3.getBucketWebsite({ Bucket: bucket.bucketName }); |
| 1276 | expect(web2.IndexDocument?.Suffix).toEqual("home.html"); |
| 1277 | |
| 1278 | yield* stack.destroy(); |
| 1279 | yield* assertBucketDeleted(bucket.bucketName); |
| 1280 | }), |
| 1281 | { tags: ["provider:aws", "provider:aws:s3", "live"], timeout: 120_000 }, |
| 1282 | ); |
| 1283 | |
| 1284 | // IAM's grammar takes a bare `"*"` for `Principal` (not only a principal |
| 1285 | // map) — the usual form for resource-policy denies like "TLS only". |
| 1286 | test.provider( |
| 1287 | "bucket policy with a wildcard string principal", |
| 1288 | (stack) => |
| 1289 | Effect.gen(function* () { |
| 1290 | yield* stack.destroy(); |
| 1291 | |
| 1292 | const definition = Effect.gen(function* () { |
| 1293 | const bucket = yield* Bucket("TlsOnlyBucket", { forceDestroy: true }); |
| 1294 | yield* bucket.bind`DenyInsecureTransport`({ |
| 1295 | policyStatements: [ |
| 1296 | { |
| 1297 | Sid: "DenyInsecureTransport", |
| 1298 | Effect: "Deny", |
| 1299 | Principal: "*", |
| 1300 | Action: ["s3:*"], |
| 1301 | Resource: [bucket.bucketArn, Output.interpolate`${bucket.bucketArn}/*`], |
| 1302 | Condition: { Bool: { "aws:SecureTransport": "false" } }, |
| 1303 | }, |
| 1304 | ], |
| 1305 | }); |
| 1306 | return bucket; |
| 1307 | }); |
| 1308 | |
| 1309 | const bucket = yield* stack.deploy(definition); |
| 1310 | const policy = yield* S3.getBucketPolicy({ Bucket: bucket.bucketName }); |
| 1311 | const statements = JSON.parse(policy.Policy ?? "{}").Statement as Array<{ |
| 1312 | Sid?: string; |
| 1313 | Principal?: unknown; |
| 1314 | }>; |
| 1315 | expect(statements.find((s) => s.Sid === "DenyInsecureTransport")?.Principal).toBe("*"); |
| 1316 | |
| 1317 | const unchanged = yield* stack.plan(definition); |
| 1318 | expect(unchanged.resources.TlsOnlyBucket?.action).toBe("noop"); |
| 1319 | |
| 1320 | yield* stack.destroy(); |
| 1321 | yield* assertBucketDeleted(bucket.bucketName); |
| 1322 | }), |
| 1323 | { tags: ["provider:aws", "provider:aws:s3", "live"], timeout: 120_000 }, |
| 1324 | ); |
| 1325 | |
| 1326 | // Server-access logging needs a same-stack target bucket that grants the S3 |
| 1327 | // logging service principal permission to deliver logs (the modern, ACL-free |
| 1328 | // grant — works with the BucketOwnerEnforced default). |
| 1329 | test.provider( |
| 1330 | "server access logging set and update prefix", |
| 1331 | (stack) => |
| 1332 | Effect.gen(function* () { |
| 1333 | yield* stack.destroy(); |
| 1334 | |
| 1335 | const target = "alchemy-test-bucket-logging-target"; |
| 1336 | const source = "alchemy-test-bucket-logging-src"; |
| 1337 | |
| 1338 | const deployWith = (targetPrefix: string) => |
| 1339 | stack.deploy( |
| 1340 | Effect.gen(function* () { |
| 1341 | const targetBucket = yield* Bucket("LogTargetBucket", { |
| 1342 | bucketName: target, |
| 1343 | policy: [ |
| 1344 | { |
| 1345 | Sid: "S3ServerAccessLogsPolicy", |
| 1346 | Effect: "Allow", |
| 1347 | Principal: { Service: "logging.s3.amazonaws.com" }, |
| 1348 | Action: ["s3:PutObject"], |
| 1349 | Resource: [`arn:aws:s3:::${target}/*`], |
| 1350 | Condition: { ArnLike: { "aws:SourceArn": `arn:aws:s3:::${source}` } }, |
| 1351 | }, |
| 1352 | ], |
| 1353 | forceDestroy: true, |
| 1354 | }); |
| 1355 | const sourceBucket = yield* Bucket("LogSourceBucket", { |
| 1356 | bucketName: source, |
| 1357 | logging: { |
| 1358 | // Output reference so the target (and its delivery policy) |
| 1359 | // reconciles before the source's putBucketLogging. |
| 1360 | targetBucket: targetBucket.bucketName, |
| 1361 | targetPrefix, |
| 1362 | }, |
| 1363 | forceDestroy: true, |
| 1364 | }); |
| 1365 | return { sourceBucket, targetBucket }; |
| 1366 | }), |
| 1367 | ); |
| 1368 | |
| 1369 | const buckets = yield* deployWith("logs/"); |
| 1370 | |
| 1371 | const l1 = yield* S3.getBucketLogging({ Bucket: buckets.sourceBucket.bucketName }); |
| 1372 | expect(l1.LoggingEnabled?.TargetBucket).toEqual(target); |
| 1373 | expect(l1.LoggingEnabled?.TargetPrefix).toEqual("logs/"); |
| 1374 | |
| 1375 | // In-place update of the log prefix. |
| 1376 | yield* deployWith("access/"); |
| 1377 | |
| 1378 | const l2 = yield* S3.getBucketLogging({ Bucket: buckets.sourceBucket.bucketName }); |
| 1379 | expect(l2.LoggingEnabled?.TargetPrefix).toEqual("access/"); |
| 1380 | |
| 1381 | yield* stack.destroy(); |
| 1382 | yield* assertBucketDeleted(buckets.sourceBucket.bucketName); |
| 1383 | yield* assertBucketDeleted(buckets.targetBucket.bucketName); |
| 1384 | }), |
| 1385 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 1386 | ); |
| 1387 | |
| 1388 | test.provider( |
| 1389 | "transfer acceleration and request payment", |
| 1390 | (stack) => |
| 1391 | Effect.gen(function* () { |
| 1392 | yield* stack.destroy(); |
| 1393 | |
| 1394 | const name = "alchemy-test-bucket-accel-pay"; |
| 1395 | const bucket = yield* stack.deploy( |
| 1396 | Bucket("AccelPayBucket", { |
| 1397 | bucketName: name, |
| 1398 | transferAcceleration: "Enabled", |
| 1399 | requestPayer: "Requester", |
| 1400 | forceDestroy: true, |
| 1401 | }), |
| 1402 | ); |
| 1403 | |
| 1404 | const a1 = yield* S3.getBucketAccelerateConfiguration({ Bucket: bucket.bucketName }); |
| 1405 | expect(a1.Status).toEqual("Enabled"); |
| 1406 | |
| 1407 | const r1 = yield* S3.getBucketRequestPayment({ Bucket: bucket.bucketName }); |
| 1408 | expect(r1.Payer).toEqual("Requester"); |
| 1409 | |
| 1410 | yield* stack.deploy( |
| 1411 | Bucket("AccelPayBucket", { |
| 1412 | bucketName: name, |
| 1413 | transferAcceleration: "Suspended", |
| 1414 | requestPayer: "BucketOwner", |
| 1415 | forceDestroy: true, |
| 1416 | }), |
| 1417 | ); |
| 1418 | |
| 1419 | const a2 = yield* S3.getBucketAccelerateConfiguration({ Bucket: bucket.bucketName }); |
| 1420 | expect(a2.Status).toEqual("Suspended"); |
| 1421 | |
| 1422 | const r2 = yield* S3.getBucketRequestPayment({ Bucket: bucket.bucketName }); |
| 1423 | expect(r2.Payer).toEqual("BucketOwner"); |
| 1424 | |
| 1425 | yield* stack.destroy(); |
| 1426 | yield* assertBucketDeleted(bucket.bucketName); |
| 1427 | }), |
| 1428 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 1429 | ); |
| 1430 | |
| 1431 | test.provider( |
| 1432 | "object lock default retention", |
| 1433 | (stack) => |
| 1434 | Effect.gen(function* () { |
| 1435 | yield* stack.destroy(); |
| 1436 | |
| 1437 | const name = "alchemy-test-bucket-objlock-retention"; |
| 1438 | const bucket = yield* stack.deploy( |
| 1439 | Bucket("ObjLockRetentionBucket", { |
| 1440 | bucketName: name, |
| 1441 | objectLockEnabled: true, |
| 1442 | objectLockConfiguration: { mode: "GOVERNANCE", days: "1 day" }, |
| 1443 | forceDestroy: true, |
| 1444 | }), |
| 1445 | ); |
| 1446 | |
| 1447 | const cfg = yield* S3.getObjectLockConfiguration({ Bucket: bucket.bucketName }); |
| 1448 | expect(cfg.ObjectLockConfiguration?.Rule?.DefaultRetention?.Mode).toEqual("GOVERNANCE"); |
| 1449 | expect(cfg.ObjectLockConfiguration?.Rule?.DefaultRetention?.Days).toEqual(1); |
| 1450 | |
| 1451 | yield* stack.destroy(); |
| 1452 | yield* assertBucketDeleted(bucket.bucketName); |
| 1453 | }), |
| 1454 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 1455 | ); |
| 1456 | |
| 1457 | test.provider( |
| 1458 | "intelligent tiering add and remove id", |
| 1459 | (stack) => |
| 1460 | Effect.gen(function* () { |
| 1461 | yield* stack.destroy(); |
| 1462 | |
| 1463 | const name = "alchemy-test-bucket-int-tiering"; |
| 1464 | const bucket = yield* stack.deploy( |
| 1465 | Bucket("IntTieringBucket", { |
| 1466 | bucketName: name, |
| 1467 | intelligentTiering: [ |
| 1468 | { |
| 1469 | Id: "archive", |
| 1470 | Status: "Enabled", |
| 1471 | Tierings: [{ Days: 90, AccessTier: "ARCHIVE_ACCESS" }], |
| 1472 | }, |
| 1473 | ], |
| 1474 | forceDestroy: true, |
| 1475 | }), |
| 1476 | ); |
| 1477 | |
| 1478 | const t1 = yield* S3.getBucketIntelligentTieringConfiguration({ |
| 1479 | Bucket: bucket.bucketName, |
| 1480 | Id: "archive", |
| 1481 | }); |
| 1482 | expect(t1.IntelligentTieringConfiguration?.Status).toEqual("Enabled"); |
| 1483 | |
| 1484 | yield* stack.deploy( |
| 1485 | Bucket("IntTieringBucket", { |
| 1486 | bucketName: name, |
| 1487 | intelligentTiering: [], |
| 1488 | forceDestroy: true, |
| 1489 | }), |
| 1490 | ); |
| 1491 | |
| 1492 | const removed = yield* S3.getBucketIntelligentTieringConfiguration({ |
| 1493 | Bucket: bucket.bucketName, |
| 1494 | Id: "archive", |
| 1495 | }).pipe( |
| 1496 | Effect.map(() => "has-config" as const), |
| 1497 | Effect.catchTag("NoSuchConfiguration", () => Effect.succeed("no-config" as const)), |
| 1498 | ); |
| 1499 | expect(removed).toEqual("no-config"); |
| 1500 | |
| 1501 | yield* stack.destroy(); |
| 1502 | yield* assertBucketDeleted(bucket.bucketName); |
| 1503 | }), |
| 1504 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 1505 | ); |
| 1506 | |
| 1507 | test.provider( |
| 1508 | "explicit bucket policy prop", |
| 1509 | (stack) => |
| 1510 | Effect.gen(function* () { |
| 1511 | yield* stack.destroy(); |
| 1512 | |
| 1513 | const name = "alchemy-test-bucket-policy-prop"; |
| 1514 | const bucketArn = `arn:aws:s3:::${name}`; |
| 1515 | const bucket = yield* stack.deploy( |
| 1516 | Bucket("PolicyPropBucket", { |
| 1517 | bucketName: name, |
| 1518 | policy: [ |
| 1519 | { |
| 1520 | Sid: "AllowCloudFront", |
| 1521 | Effect: "Allow", |
| 1522 | Principal: { Service: "cloudfront.amazonaws.com" }, |
| 1523 | Action: ["s3:GetObject"], |
| 1524 | Resource: [`${bucketArn}/*`], |
| 1525 | }, |
| 1526 | ], |
| 1527 | forceDestroy: true, |
| 1528 | }), |
| 1529 | ); |
| 1530 | |
| 1531 | const policy = yield* S3.getBucketPolicy({ Bucket: bucket.bucketName }).pipe( |
| 1532 | Effect.map((r) => JSON.parse(r.Policy!)), |
| 1533 | ); |
| 1534 | expect(policy.Statement[0].Sid).toEqual("AllowCloudFront"); |
| 1535 | |
| 1536 | yield* stack.destroy(); |
| 1537 | yield* assertBucketDeleted(bucket.bucketName); |
| 1538 | }), |
| 1539 | { tags: ["provider:aws", "provider:aws:s3", "live"] }, |
| 1540 | ); |
| 1541 | |
| 1542 | // Replication needs an IAM role S3 can assume + a versioned destination bucket. |
| 1543 | // The test provisions all of it (role + dest bucket) so it is self-contained. |
| 1544 | test.provider( |
| 1545 | "replication configuration", |
| 1546 | (stack) => |
| 1547 | Effect.gen(function* () { |
| 1548 | yield* stack.destroy(); |
| 1549 | |
| 1550 | const dest = "alchemy-test-bucket-replication-dest"; |
| 1551 | const src = "alchemy-test-bucket-replication-src"; |
| 1552 | |
| 1553 | const buckets = yield* stack.deploy( |
| 1554 | Effect.gen(function* () { |
| 1555 | const destBucket = yield* Bucket("ReplDestBucket", { |
| 1556 | bucketName: dest, |
| 1557 | versioning: "Enabled", |
| 1558 | forceDestroy: true, |
| 1559 | }); |
| 1560 | const replRole = yield* Role("ReplRole", { |
| 1561 | roleName: "alchemy-test-s3-replication-role", |
| 1562 | assumeRolePolicyDocument: { |
| 1563 | Version: "2012-10-17", |
| 1564 | Statement: [ |
| 1565 | { |
| 1566 | Effect: "Allow", |
| 1567 | Principal: { Service: "s3.amazonaws.com" }, |
| 1568 | Action: ["sts:AssumeRole"], |
| 1569 | }, |
| 1570 | ], |
| 1571 | }, |
| 1572 | inlinePolicies: { |
| 1573 | Replication: { |
| 1574 | Version: "2012-10-17", |
| 1575 | Statement: [ |
| 1576 | { |
| 1577 | Effect: "Allow", |
| 1578 | Action: ["s3:GetReplicationConfiguration", "s3:ListBucket"], |
| 1579 | Resource: [`arn:aws:s3:::${src}`], |
| 1580 | }, |
| 1581 | { |
| 1582 | Effect: "Allow", |
| 1583 | Action: [ |
| 1584 | "s3:GetObjectVersionForReplication", |
| 1585 | "s3:GetObjectVersionAcl", |
| 1586 | "s3:GetObjectVersionTagging", |
| 1587 | ], |
| 1588 | Resource: [`arn:aws:s3:::${src}/*`], |
| 1589 | }, |
| 1590 | { |
| 1591 | Effect: "Allow", |
| 1592 | Action: ["s3:ReplicateObject", "s3:ReplicateDelete", "s3:ReplicateTags"], |
| 1593 | Resource: [`arn:aws:s3:::${dest}/*`], |
| 1594 | }, |
| 1595 | ], |
| 1596 | }, |
| 1597 | }, |
| 1598 | }); |
| 1599 | const srcBucket = yield* Bucket("ReplSrcBucket", { |
| 1600 | bucketName: src, |
| 1601 | versioning: "Enabled", |
| 1602 | replication: { |
| 1603 | role: replRole.roleArn, |
| 1604 | rules: [ |
| 1605 | { |
| 1606 | ID: "replicate-all", |
| 1607 | Status: "Enabled", |
| 1608 | Priority: 1, |
| 1609 | Filter: {}, |
| 1610 | DeleteMarkerReplication: { Status: "Disabled" }, |
| 1611 | Destination: { Bucket: `arn:aws:s3:::${dest}` }, |
| 1612 | }, |
| 1613 | ], |
| 1614 | }, |
| 1615 | forceDestroy: true, |
| 1616 | }); |
| 1617 | return { srcBucket, destBucket, roleArn: replRole.roleArn }; |
| 1618 | }), |
| 1619 | ); |
| 1620 | |
| 1621 | const repl = yield* S3.getBucketReplication({ Bucket: buckets.srcBucket.bucketName }); |
| 1622 | expect(repl.ReplicationConfiguration?.Role).toEqual(buckets.roleArn); |
| 1623 | expect(repl.ReplicationConfiguration?.Rules).toHaveLength(1); |
| 1624 | |
| 1625 | yield* stack.destroy(); |
| 1626 | yield* assertBucketDeleted(buckets.srcBucket.bucketName); |
| 1627 | yield* assertBucketDeleted(buckets.destBucket.bucketName); |
| 1628 | }), |
| 1629 | { tags: ["provider:aws", "provider:aws:iam", "provider:aws:s3", "live"], timeout: 180_000 }, |
| 1630 | ); |
| 1631 | |
| 1632 | // Idempotent out-of-band delete for the adoption test's deterministically |
| 1633 | // named bucket. Tolerates the bucket never existing / already deleted |
| 1634 | // (typed NoSuchBucket) and retries transient failures a bounded number of |
| 1635 | // times. The bucket is always empty in that test, so deleteBucket suffices. |
| 1636 | const deleteBucketIfExists = Effect.fn(function* (bucketName: string) { |
| 1637 | yield* S3.deleteBucket({ Bucket: bucketName }).pipe( |
| 1638 | Effect.catchTag("NoSuchBucket", () => Effect.void), |
| 1639 | Effect.retry({ schedule: Schedule.max([Schedule.exponential(200), Schedule.recurs(5)]) }), |
| 1640 | ); |
| 1641 | }); |
| 1642 | |
| 1643 | class BucketStillExists extends Data.TaggedError("BucketStillExists") {} |
| 1644 | |
| 1645 | // #2659: `read` must observe every managed setting, or an out-of-band change |
| 1646 | // reads as `unchanged` and drift never repairs it. |
| 1647 | test.provider( |
| 1648 | "drift detects and repairs out-of-band configuration changes", |
| 1649 | (stack) => |
| 1650 | Effect.gen(function* () { |
| 1651 | yield* stack.destroy(); |
| 1652 | |
| 1653 | const bucket = yield* stack.deploy( |
| 1654 | Bucket("DriftConfigBucket", { |
| 1655 | forceDestroy: true, |
| 1656 | tags: { team: "storage" }, |
| 1657 | versioning: "Enabled", |
| 1658 | publicAccessBlock: { |
| 1659 | blockPublicAcls: true, |
| 1660 | ignorePublicAcls: true, |
| 1661 | blockPublicPolicy: true, |
| 1662 | restrictPublicBuckets: true, |
| 1663 | }, |
| 1664 | cors: [{ AllowedMethods: ["GET"], AllowedOrigins: ["*"], MaxAgeSeconds: 300 }], |
| 1665 | lifecycleRules: [ |
| 1666 | { |
| 1667 | ID: "expire-logs", |
| 1668 | Status: "Enabled", |
| 1669 | Filter: { Prefix: "logs/" }, |
| 1670 | Expiration: { Days: 30 }, |
| 1671 | }, |
| 1672 | ], |
| 1673 | requestPayer: "BucketOwner", |
| 1674 | }), |
| 1675 | ); |
| 1676 | const bucketName = bucket.bucketName; |
| 1677 | const target = { name: stack.name, stage: stack.stage }; |
| 1678 | const detect = Drift.detect(target).pipe( |
| 1679 | Effect.map((result) => result.resources.DriftConfigBucket), |
| 1680 | Effect.provide(stack.state), |
| 1681 | ); |
| 1682 | // S3 bucket configuration is eventually consistent: CORS and lifecycle |
| 1683 | // reads can lag their writes on some replicas, so wait for a quiet read. |
| 1684 | const unchanged = detect.pipe( |
| 1685 | Effect.repeat({ |
| 1686 | until: (result) => result?.action === "unchanged", |
| 1687 | schedule: Schedule.spaced("3 seconds"), |
| 1688 | times: 20, |
| 1689 | }), |
| 1690 | ); |
| 1691 | expect((yield* unchanged)?.action).toBe("unchanged"); |
| 1692 | |
| 1693 | yield* S3.putBucketVersioning({ |
| 1694 | Bucket: bucketName, |
| 1695 | VersioningConfiguration: { Status: "Suspended" }, |
| 1696 | }); |
| 1697 | yield* S3.deleteBucketCors({ Bucket: bucketName }); |
| 1698 | yield* S3.putBucketLifecycleConfiguration({ |
| 1699 | Bucket: bucketName, |
| 1700 | LifecycleConfiguration: { |
| 1701 | Rules: [ |
| 1702 | { |
| 1703 | ID: "expire-logs", |
| 1704 | Status: "Enabled", |
| 1705 | Filter: { Prefix: "logs/" }, |
| 1706 | Expiration: { Days: 7 }, |
| 1707 | }, |
| 1708 | ], |
| 1709 | }, |
| 1710 | }); |
| 1711 | yield* S3.putBucketTagging({ |
| 1712 | Bucket: bucketName, |
| 1713 | Tagging: { TagSet: [{ Key: "team", Value: "other" }] }, |
| 1714 | }); |
| 1715 | yield* S3.deletePublicAccessBlock({ Bucket: bucketName }); |
| 1716 | yield* S3.putBucketEncryption({ |
| 1717 | Bucket: bucketName, |
| 1718 | ServerSideEncryptionConfiguration: { |
| 1719 | Rules: [ |
| 1720 | { |
| 1721 | ApplyServerSideEncryptionByDefault: { SSEAlgorithm: "AES256" }, |
| 1722 | BucketKeyEnabled: true, |
| 1723 | }, |
| 1724 | ], |
| 1725 | }, |
| 1726 | }); |
| 1727 | yield* S3.putBucketRequestPayment({ |
| 1728 | Bucket: bucketName, |
| 1729 | RequestPaymentConfiguration: { Payer: "Requester" }, |
| 1730 | }); |
| 1731 | |
| 1732 | // Bucket configuration is eventually consistent: wait until every |
| 1733 | // out-of-band change is observed. |
| 1734 | const drifted = yield* detect.pipe( |
| 1735 | Effect.repeat({ |
| 1736 | until: (result) => |
| 1737 | result?.attr?.versioning === "Suspended" && |
| 1738 | result.attr.cors?.length === 0 && |
| 1739 | result.attr.lifecycleRules?.[0]?.Expiration?.Days === 7 && |
| 1740 | result.attr.tags?.team === "other" && |
| 1741 | result.attr.publicAccessBlock?.blockPublicAcls === false && |
| 1742 | result.attr.encryption?.bucketKeyEnabled === true && |
| 1743 | result.attr.requestPayer === "Requester", |
| 1744 | schedule: Schedule.spaced("2 seconds"), |
| 1745 | times: 15, |
| 1746 | }), |
| 1747 | ); |
| 1748 | expect(drifted?.action).toBe("drifted"); |
| 1749 | expect(drifted?.attr).toMatchObject({ |
| 1750 | versioning: "Suspended", |
| 1751 | cors: [], |
| 1752 | tags: { team: "other" }, |
| 1753 | requestPayer: "Requester", |
| 1754 | }); |
| 1755 | |
| 1756 | const repaired = yield* Drift.repair(target).pipe(Effect.provide(stack.state)); |
| 1757 | expect(repaired.resources.DriftConfigBucket?.action).toBe("repaired"); |
| 1758 | const after = yield* unchanged; |
| 1759 | expect(after?.attr).toEqual(repaired.resources.DriftConfigBucket?.attr); |
| 1760 | expect(after?.action).toBe("unchanged"); |
| 1761 | |
| 1762 | // Out-of-band reads lag too: poll until S3 reports the repaired state. |
| 1763 | const observeRepaired = Effect.gen(function* () { |
| 1764 | const versioning = yield* S3.getBucketVersioning({ Bucket: bucketName }); |
| 1765 | const cors = yield* S3.getBucketCors({ Bucket: bucketName }).pipe( |
| 1766 | Effect.map((r) => r.CORSRules ?? []), |
| 1767 | Effect.catchTag("NoSuchCORSConfiguration", () => Effect.succeed([])), |
| 1768 | ); |
| 1769 | const lifecycle = yield* S3.getBucketLifecycleConfiguration({ Bucket: bucketName }); |
| 1770 | const tags = yield* S3.getBucketTagging({ Bucket: bucketName }); |
| 1771 | const pab = yield* S3.getPublicAccessBlock({ Bucket: bucketName }); |
| 1772 | const encryption = yield* S3.getBucketEncryption({ Bucket: bucketName }); |
| 1773 | const payment = yield* S3.getBucketRequestPayment({ Bucket: bucketName }); |
| 1774 | return { |
| 1775 | versioning: versioning.Status, |
| 1776 | corsMethods: cors[0]?.AllowedMethods, |
| 1777 | expirationDays: lifecycle.Rules?.[0]?.Expiration?.Days, |
| 1778 | tags: tags.TagSet, |
| 1779 | blockPublicAcls: pab.PublicAccessBlockConfiguration?.BlockPublicAcls, |
| 1780 | bucketKeyEnabled: |
| 1781 | encryption.ServerSideEncryptionConfiguration?.Rules?.[0]?.BucketKeyEnabled, |
| 1782 | payer: payment.Payer, |
| 1783 | }; |
| 1784 | }); |
| 1785 | const expected = { |
| 1786 | versioning: "Enabled", |
| 1787 | corsMethods: ["GET"], |
| 1788 | expirationDays: 30, |
| 1789 | tags: [{ Key: "team", Value: "storage" }], |
| 1790 | blockPublicAcls: true, |
| 1791 | bucketKeyEnabled: false, |
| 1792 | payer: "BucketOwner", |
| 1793 | }; |
| 1794 | const observed = yield* observeRepaired.pipe( |
| 1795 | Effect.repeat({ |
| 1796 | until: (observed) => JSON.stringify(observed) === JSON.stringify(expected), |
| 1797 | schedule: Schedule.spaced("3 seconds"), |
| 1798 | times: 20, |
| 1799 | }), |
| 1800 | ); |
| 1801 | expect(observed).toEqual(expected); |
| 1802 | |
| 1803 | yield* stack.destroy(); |
| 1804 | yield* assertBucketDeleted(bucketName); |
| 1805 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 1806 | { tags: ["provider:aws", "provider:aws:s3", "live"], timeout: 240_000 }, |
| 1807 | ); |
| 1808 | |
| 1809 | const assertBucketDeleted = Effect.fn(function* (bucketName: string) { |
| 1810 | yield* S3.headBucket({ Bucket: bucketName }).pipe( |
| 1811 | Effect.flatMap(() => Effect.fail(new BucketStillExists())), |
| 1812 | Effect.retry({ |
| 1813 | while: (e) => e._tag === "BucketStillExists", |
| 1814 | schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(10)]), |
| 1815 | }), |
| 1816 | Effect.catchTag("NotFound", () => Effect.void), |
| 1817 | Effect.catch(() => Effect.void), |
| 1818 | ); |
| 1819 | }); |
| 1820 | |
| 1821 | // ── destructive deletes require explicit opt-in ──────────────────────── |
| 1822 | // |
| 1823 | // DATA-PROTECTION INVARIANT: `delete` may remove the bucket, but it must |
| 1824 | // NEVER destroy the bucket's CONTENTS unless the user opted in on the |
| 1825 | // resource (`forceDestroy`) or an operator ran `alchemy unsafe nuke` |
| 1826 | // (which passes `force: true`). |
| 1827 | // |
| 1828 | // S3 refuses to delete a non-empty bucket (`BucketNotEmpty`). That refusal |
| 1829 | // is the last line of defense for production data, and emptying the bucket |
| 1830 | // first silently converts a routine teardown into irreversible data loss |
| 1831 | // (see https://github.com/alchemy-run/alchemy/issues/1248 for the R2 |
| 1832 | // incident this guards against here too). |
| 1833 | // |
| 1834 | // A live test can only observe that a bucket survived, not that no |
| 1835 | // destructive request was ever issued. These run the REAL provider |
| 1836 | // `delete` against a recording transport and assert on the wire traffic. |
| 1837 | |
| 1838 | type Recorded = { method: string; url: string }; |
| 1839 | |
| 1840 | /** Fetch transport that records every request and answers from `respond`. */ |
| 1841 | const recordingTransport = (respond: (call: Recorded) => Response) => { |
| 1842 | const calls: Recorded[] = []; |
| 1843 | const fetch = async (input: string | URL | Request, init?: RequestInit): Promise<Response> => { |
| 1844 | calls.push({ |
| 1845 | method: (input instanceof Request ? input.method : init?.method) ?? "GET", |
| 1846 | url: input instanceof Request ? input.url : String(input), |
| 1847 | }); |
| 1848 | return respond(calls[calls.length - 1]!); |
| 1849 | }; |
| 1850 | return { |
| 1851 | calls, |
| 1852 | layer: FetchHttpClient.layer.pipe( |
| 1853 | Layer.provide(Layer.succeed(FetchHttpClient.Fetch, fetch as typeof globalThis.fetch)), |
| 1854 | ), |
| 1855 | }; |
| 1856 | }; |
| 1857 | |
| 1858 | const TEST_REGION = "us-east-1"; |
| 1859 | const INSTANCE_ID = "0123456789abcdef0123456789abcdef"; |
| 1860 | |
| 1861 | const testStack: Omit<StackSpec, "output"> = { |
| 1862 | name: "my-stack", |
| 1863 | stage: "dev", |
| 1864 | resources: {}, |
| 1865 | bindings: {}, |
| 1866 | actions: {}, |
| 1867 | }; |
| 1868 | |
| 1869 | const testCredentials = fromCredentials( |
| 1870 | { |
| 1871 | accessKeyId: Redacted.make("AKIAIOSFODNN7EXAMPLE"), |
| 1872 | secretAccessKey: Redacted.make("test-secret-key"), |
| 1873 | }, |
| 1874 | TEST_REGION, |
| 1875 | ); |
| 1876 | |
| 1877 | const stubbedEnv = (transport: Layer.Layer<HttpClient.HttpClient>) => |
| 1878 | Layer.mergeAll( |
| 1879 | Layer.effect( |
| 1880 | AWSEnvironment, |
| 1881 | Effect.map(Credentials, (credentials) => |
| 1882 | Effect.succeed({ accountId: "123456789012", region: TEST_REGION, credentials } as never), |
| 1883 | ), |
| 1884 | ).pipe(Layer.provide(testCredentials)), |
| 1885 | testCredentials, |
| 1886 | Layer.succeed(Region, Effect.succeed(TEST_REGION)), |
| 1887 | Layer.succeed(Stack, testStack), |
| 1888 | Layer.succeed(Stage, testStack.stage), |
| 1889 | Layer.succeed(ResourceContext, resourceContext(INSTANCE_ID)), |
| 1890 | Layer.succeed(AlchemyContext, { dotAlchemy: "/tmp/.alchemy-test", dev: false, adopt: false }), |
| 1891 | Layer.sync(ArtifactStore, createArtifactStore), |
| 1892 | inMemoryState(), |
| 1893 | NodeServices.layer, |
| 1894 | ).pipe(Layer.provideMerge(transport)); |
| 1895 | |
| 1896 | const stubbedOutput = { |
| 1897 | bucketName: "my-bucket", |
| 1898 | bucketArn: "arn:aws:s3:::my-bucket", |
| 1899 | region: TEST_REGION, |
| 1900 | bucketDomainName: "my-bucket.s3.amazonaws.com", |
| 1901 | bucketRegionalDomainName: "my-bucket.s3.us-east-1.amazonaws.com", |
| 1902 | hostedZoneId: "Z3AQBSTGFYJSTF", |
| 1903 | tags: {}, |
| 1904 | }; |
| 1905 | |
| 1906 | /** `POST /?delete` — S3's bulk object/version delete. */ |
| 1907 | const objectDeletes = (calls: Recorded[]) => |
| 1908 | calls.filter((c) => c.method === "POST" && /[?&]delete/.test(c.url)); |
| 1909 | |
| 1910 | /** `GET /?versions` — enumerating what to wipe. */ |
| 1911 | const versionListings = (calls: Recorded[]) => |
| 1912 | calls.filter((c) => c.method === "GET" && /[?&]versions/.test(c.url)); |
| 1913 | |
| 1914 | const bucketDeletes = (calls: Recorded[]) => |
| 1915 | calls.filter((c) => c.method === "DELETE" && !/[?&]/.test(c.url)); |
| 1916 | |
| 1917 | /** One object version, so the empty path has something to delete. */ |
| 1918 | const stubResponse = (call: Recorded) => |
| 1919 | /[?&]versions/.test(call.url) |
| 1920 | ? new Response( |
| 1921 | `<?xml version="1.0" encoding="UTF-8"?> |
| 1922 | <ListVersionsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> |
| 1923 | <Name>my-bucket</Name> |
| 1924 | <IsTruncated>false</IsTruncated> |
| 1925 | <Version> |
| 1926 | <Key>precious.txt</Key> |
| 1927 | <VersionId>null</VersionId> |
| 1928 | </Version> |
| 1929 | </ListVersionsResult>`, |
| 1930 | { status: 200, headers: { "content-type": "application/xml" } }, |
| 1931 | ) |
| 1932 | : new Response("", { status: 204, headers: { "content-type": "application/xml" } }); |
| 1933 | |
| 1934 | /** Run the real provider delete; return every request it made. */ |
| 1935 | const recordDelete = (props: { forceDestroy?: boolean }, options?: { force?: boolean }) => |
| 1936 | Effect.gen(function* () { |
| 1937 | const transport = recordingTransport(stubResponse); |
| 1938 | yield* Effect.gen(function* () { |
| 1939 | const provider = yield* Provider.Provider<Bucket>("AWS.S3.Bucket"); |
| 1940 | yield* provider.delete({ |
| 1941 | id: "Bucket", |
| 1942 | fqn: "Bucket", |
| 1943 | instanceId: INSTANCE_ID, |
| 1944 | olds: props, |
| 1945 | output: stubbedOutput as never, |
| 1946 | bindings: [] as never, |
| 1947 | session: { note: () => Effect.void }, |
| 1948 | force: options?.force, |
| 1949 | }); |
| 1950 | }).pipe(Effect.provide(BucketProvider()), Effect.provide(stubbedEnv(transport.layer))); |
| 1951 | return transport.calls; |
| 1952 | }); |
| 1953 | |
| 1954 | describe( |
| 1955 | "destructive delete requires explicit opt-in", |
| 1956 | { tags: ["unit", "provider:aws", "provider:aws:s3", "local"] }, |
| 1957 | () => { |
| 1958 | it.effect("no forceDestroy never empties the bucket", () => |
| 1959 | Effect.gen(function* () { |
| 1960 | const calls = yield* recordDelete({}); |
| 1961 | |
| 1962 | expect(objectDeletes(calls)).toEqual([]); |
| 1963 | expect(versionListings(calls)).toEqual([]); |
| 1964 | // The bucket delete itself is still attempted — S3 answers |
| 1965 | // `BucketNotEmpty`, which is the protection. |
| 1966 | expect(bucketDeletes(calls).length).toBeGreaterThan(0); |
| 1967 | }), |
| 1968 | ); |
| 1969 | |
| 1970 | it.effect("forceDestroy empties the bucket first", () => |
| 1971 | Effect.gen(function* () { |
| 1972 | const calls = yield* recordDelete({ forceDestroy: true }); |
| 1973 | |
| 1974 | expect(objectDeletes(calls).length).toBeGreaterThan(0); |
| 1975 | expect(bucketDeletes(calls).length).toBeGreaterThan(0); |
| 1976 | }), |
| 1977 | ); |
| 1978 | |
| 1979 | // Nuke enumerates buckets from the cloud, so `olds` carries Attributes and |
| 1980 | // never has `forceDestroy` — the operator's confirmation IS the flag. |
| 1981 | it.effect("nuke's force empties without the prop", () => |
| 1982 | Effect.gen(function* () { |
| 1983 | const calls = yield* recordDelete({}, { force: true }); |
| 1984 | |
| 1985 | expect(objectDeletes(calls).length).toBeGreaterThan(0); |
| 1986 | }), |
| 1987 | ); |
| 1988 | }, |
| 1989 | ); |