The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Core from "@alchemy.run/test/Core"; |
| 5 | import * as IAM from "@distilled.cloud/aws/iam"; |
| 6 | import * as Lambda from "@distilled.cloud/aws/lambda"; |
| 7 | import * as S3 from "@distilled.cloud/aws/s3"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as HttpClient from "effect/http/HttpClient"; |
| 11 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import * as Schema from "effect/Schema"; |
| 14 | import * as Stream from "effect/Stream"; |
| 15 | import S3VersionedObjectLockFunctionLive, { |
| 16 | S3VersionedObjectLockFunction, |
| 17 | } from "./fixtures/versioned-object-lock-handler.ts"; |
| 18 | |
| 19 | const options = { providers: AWS.providers(), dev: false }; |
| 20 | const { test, beforeAll, afterAll } = Test.make(options); |
| 21 | const stack = Core.scratchStack( |
| 22 | options, |
| 23 | "S3VersionedObjectLock", |
| 24 | "test/AWS/S3/VersionedObjectLock.test.ts", |
| 25 | ); |
| 26 | const actions = { |
| 27 | GetObjectRetention: "s3:GetObjectRetention", |
| 28 | PutObjectRetention: "s3:PutObjectRetention", |
| 29 | GetObjectLegalHold: "s3:GetObjectLegalHold", |
| 30 | PutObjectLegalHold: "s3:PutObjectLegalHold", |
| 31 | RestoreObject: "s3:RestoreObject", |
| 32 | } as const; |
| 33 | type Binding = keyof typeof actions; |
| 34 | const bucketInfo = Schema.Array( |
| 35 | Schema.Struct({ |
| 36 | binding: Schema.Literals([ |
| 37 | "GetObjectRetention", |
| 38 | "PutObjectRetention", |
| 39 | "GetObjectLegalHold", |
| 40 | "PutObjectLegalHold", |
| 41 | "RestoreObject", |
| 42 | ]), |
| 43 | bucketName: Schema.String, |
| 44 | bucketArn: Schema.String, |
| 45 | }), |
| 46 | ); |
| 47 | let buckets: typeof bucketInfo.Type = []; |
| 48 | let baseUrl: string; |
| 49 | let roleName: string; |
| 50 | let functionName: string; |
| 51 | |
| 52 | class FixtureNotReady extends Data.TaggedError("FixtureNotReady") {} |
| 53 | class UnexpectedFixtureStatus extends Data.TaggedError("UnexpectedFixtureStatus")<{ |
| 54 | readonly status: number; |
| 55 | }> {} |
| 56 | class BucketStillExists extends Data.TaggedError("BucketStillExists") {} |
| 57 | |
| 58 | const bucketFor = (binding: Binding) => { |
| 59 | const bucket = buckets.find((bucket) => bucket.binding === binding); |
| 60 | if (!bucket) throw new Error(`Missing fixture bucket for ${binding}`); |
| 61 | return bucket.bucketName; |
| 62 | }; |
| 63 | |
| 64 | const post = <A, I>(path: string, body: object, schema: Schema.Codec<A, I>) => |
| 65 | Effect.gen(function* () { |
| 66 | const response = yield* HttpClient.execute( |
| 67 | HttpClientRequest.post(`${baseUrl}${path}`).pipe(HttpClientRequest.bodyJsonUnsafe(body)), |
| 68 | ); |
| 69 | if (response.status !== 200) { |
| 70 | return yield* Effect.fail( |
| 71 | new Error(`${path}: HTTP ${response.status}: ${yield* response.text}`), |
| 72 | ); |
| 73 | } |
| 74 | return yield* Schema.decodeUnknownEffect(schema)(yield* response.json); |
| 75 | }); |
| 76 | |
| 77 | const rejected = (path: string, body: object, status: number, tag: string) => |
| 78 | Effect.gen(function* () { |
| 79 | const response = yield* HttpClient.execute( |
| 80 | HttpClientRequest.post(`${baseUrl}${path}`).pipe(HttpClientRequest.bodyJsonUnsafe(body)), |
| 81 | ); |
| 82 | const result = yield* response.json; |
| 83 | expect({ status: response.status, body: result }).toEqual({ status, body: { tag } }); |
| 84 | }); |
| 85 | |
| 86 | const assertProtectedDelete = (Bucket: string, Key: string, VersionId: string) => |
| 87 | S3.deleteObject({ Bucket, Key, VersionId }).pipe( |
| 88 | Effect.as("deleted"), |
| 89 | Effect.catchTag("AccessDeniedException", () => Effect.succeed("AccessDeniedException")), |
| 90 | Effect.tap((tag) => Effect.sync(() => expect(tag).toBe("AccessDeniedException"))), |
| 91 | ); |
| 92 | |
| 93 | const policyDocument = Schema.fromJsonString( |
| 94 | Schema.Struct({ |
| 95 | Statement: Schema.Array( |
| 96 | Schema.Struct({ |
| 97 | Effect: Schema.String, |
| 98 | Action: Schema.Union([Schema.String, Schema.Array(Schema.String)]), |
| 99 | Resource: Schema.Union([Schema.String, Schema.Array(Schema.String)]), |
| 100 | }), |
| 101 | ), |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | const assertPermissions = Effect.gen(function* () { |
| 106 | const configuration = yield* Lambda.getFunctionConfiguration({ FunctionName: functionName }); |
| 107 | expect(configuration.FunctionName).toBe(functionName); |
| 108 | expect(configuration.Role?.endsWith(`/${roleName}`)).toBe(true); |
| 109 | const attached = yield* IAM.listAttachedRolePolicies |
| 110 | .pages({ RoleName: roleName }) |
| 111 | .pipe(Stream.runCollect); |
| 112 | expect( |
| 113 | attached.flatMap((page) => page.AttachedPolicies ?? []).map((policy) => policy.PolicyArn), |
| 114 | ).toEqual(["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"]); |
| 115 | const pages = yield* IAM.listRolePolicies.pages({ RoleName: roleName }).pipe(Stream.runCollect); |
| 116 | const policies = yield* Effect.forEach( |
| 117 | // Skip the function's log-group guard; only binding grants count. |
| 118 | pages |
| 119 | .flatMap((page) => page.PolicyNames) |
| 120 | .filter((name) => name !== "AlchemyDenyLogGroupCreate"), |
| 121 | (PolicyName) => |
| 122 | Effect.gen(function* () { |
| 123 | const policy = yield* IAM.getRolePolicy({ RoleName: roleName, PolicyName }); |
| 124 | const decoded = yield* Effect.try(() => decodeURIComponent(policy.PolicyDocument)); |
| 125 | return yield* Schema.decodeUnknownEffect(policyDocument)(decoded); |
| 126 | }), |
| 127 | ); |
| 128 | const actual = policies |
| 129 | .flatMap((policy) => |
| 130 | policy.Statement.flatMap((statement) => { |
| 131 | const actionList = |
| 132 | typeof statement.Action === "string" ? [statement.Action] : statement.Action; |
| 133 | const resources = |
| 134 | typeof statement.Resource === "string" ? [statement.Resource] : statement.Resource; |
| 135 | return actionList.flatMap((action) => |
| 136 | resources.map((resource) => `${statement.Effect} ${action} ${resource}`), |
| 137 | ); |
| 138 | }), |
| 139 | ) |
| 140 | .sort(); |
| 141 | expect(actual).toEqual( |
| 142 | buckets.map((bucket) => `Allow ${actions[bucket.binding]} ${bucket.bucketArn}/*`).sort(), |
| 143 | ); |
| 144 | for (const bucket of buckets) { |
| 145 | expect((yield* S3.getBucketVersioning({ Bucket: bucket.bucketName })).Status).toBe("Enabled"); |
| 146 | if (bucket.binding !== "RestoreObject") { |
| 147 | const lock = yield* S3.getObjectLockConfiguration({ Bucket: bucket.bucketName }); |
| 148 | expect(lock.ObjectLockConfiguration?.ObjectLockEnabled).toBe("Enabled"); |
| 149 | expect(lock.ObjectLockConfiguration?.Rule).toBeUndefined(); |
| 150 | } |
| 151 | } |
| 152 | }); |
| 153 | |
| 154 | const assertBucketDeleted = (Bucket: string) => |
| 155 | S3.headBucket({ Bucket }).pipe( |
| 156 | Effect.flatMap(() => Effect.fail(new BucketStillExists())), |
| 157 | Effect.retry({ |
| 158 | while: (error) => error._tag === "BucketStillExists", |
| 159 | schedule: Schedule.spaced("1 second"), |
| 160 | times: 9, |
| 161 | }), |
| 162 | Effect.catchTag("NotFound", () => Effect.void), |
| 163 | ); |
| 164 | |
| 165 | const seedVersions = Effect.fn(function* ( |
| 166 | Bucket: string, |
| 167 | Key: string, |
| 168 | oldStorageClass: "STANDARD" | "GLACIER" = "STANDARD", |
| 169 | ) { |
| 170 | const oldBody = "original object-lock version"; |
| 171 | const currentBody = "latest object-lock version has different content"; |
| 172 | const old = yield* S3.putObject({ Bucket, Key, Body: oldBody, StorageClass: oldStorageClass }); |
| 173 | const current = yield* S3.putObject({ Bucket, Key, Body: currentBody, StorageClass: "STANDARD" }); |
| 174 | expect(old.VersionId).toBeTruthy(); |
| 175 | expect(current.VersionId).toBeTruthy(); |
| 176 | expect(old.VersionId).not.toBe("null"); |
| 177 | expect(current.VersionId).not.toBe(old.VersionId); |
| 178 | return { old: old.VersionId!, current: current.VersionId!, oldBody, currentBody }; |
| 179 | }); |
| 180 | |
| 181 | const assertVersions = Effect.fn(function* ( |
| 182 | Bucket: string, |
| 183 | Key: string, |
| 184 | versions: { old: string; current: string; oldBody: string; currentBody: string }, |
| 185 | ) { |
| 186 | for (const [VersionId, expectedId, body] of [ |
| 187 | [undefined, versions.current, versions.currentBody], |
| 188 | [versions.old, versions.old, versions.oldBody], |
| 189 | ] as const) { |
| 190 | const object = yield* S3.getObject({ Bucket, Key, VersionId }); |
| 191 | expect(object.VersionId).toBe(expectedId); |
| 192 | expect(object.Body).toBeDefined(); |
| 193 | expect(yield* object.Body!.pipe(Stream.decodeText, Stream.mkString)).toBe(body); |
| 194 | } |
| 195 | const listed = yield* S3.listObjectVersions({ Bucket, Prefix: Key }); |
| 196 | expect( |
| 197 | listed.Versions?.filter((version) => version.Key === Key) |
| 198 | .map((version) => ({ id: version.VersionId, latest: version.IsLatest })) |
| 199 | .sort((a, b) => a.id!.localeCompare(b.id!)), |
| 200 | ).toEqual( |
| 201 | [ |
| 202 | { id: versions.old, latest: false }, |
| 203 | { id: versions.current, latest: true }, |
| 204 | ].sort((a, b) => a.id.localeCompare(b.id)), |
| 205 | ); |
| 206 | }); |
| 207 | |
| 208 | const retentionDates = Effect.sync(() => { |
| 209 | const now = Math.floor(Date.now() / 1000) * 1000; |
| 210 | return { |
| 211 | old: new Date(now + 86_400_000), |
| 212 | current: new Date(now + 2 * 86_400_000), |
| 213 | extended: new Date(now + 3 * 86_400_000), |
| 214 | }; |
| 215 | }); |
| 216 | |
| 217 | const removeRetention = (Bucket: string, Key: string, versionIds: string[]) => |
| 218 | Effect.forEach( |
| 219 | versionIds, |
| 220 | (VersionId) => |
| 221 | S3.putObjectRetention({ |
| 222 | Bucket, |
| 223 | Key, |
| 224 | VersionId, |
| 225 | Retention: {}, |
| 226 | BypassGovernanceRetention: true, |
| 227 | }), |
| 228 | { discard: true }, |
| 229 | ).pipe(Effect.orDie); |
| 230 | |
| 231 | const removeHolds = (Bucket: string, Key: string, versionIds: string[]) => |
| 232 | Effect.forEach( |
| 233 | versionIds, |
| 234 | (VersionId) => S3.putObjectLegalHold({ Bucket, Key, VersionId, LegalHold: { Status: "OFF" } }), |
| 235 | { discard: true }, |
| 236 | ).pipe(Effect.orDie); |
| 237 | |
| 238 | const retentionResponse = Schema.Struct({ |
| 239 | Retention: Schema.Struct({ Mode: Schema.Literal("GOVERNANCE"), RetainUntilDate: Schema.String }), |
| 240 | }); |
| 241 | const holdResponse = Schema.Struct({ |
| 242 | LegalHold: Schema.Struct({ Status: Schema.Literals(["ON", "OFF"]) }), |
| 243 | }); |
| 244 | |
| 245 | beforeAll( |
| 246 | Effect.gen(function* () { |
| 247 | yield* stack.destroy(); |
| 248 | const deployed = yield* stack.deploy( |
| 249 | S3VersionedObjectLockFunction.pipe(Effect.provide(S3VersionedObjectLockFunctionLive)), |
| 250 | ); |
| 251 | expect(deployed.functionUrl).toBeTruthy(); |
| 252 | baseUrl = deployed.functionUrl!.replace(/\/+$/, ""); |
| 253 | roleName = deployed.roleName; |
| 254 | functionName = deployed.functionName; |
| 255 | buckets = yield* HttpClient.get(`${baseUrl}/info`).pipe( |
| 256 | Effect.flatMap((response) => |
| 257 | Effect.gen(function* () { |
| 258 | if (response.status === 503 || response.status === 502) { |
| 259 | return yield* Effect.fail(new FixtureNotReady()); |
| 260 | } |
| 261 | if (response.status === 200) return yield* response.json; |
| 262 | return yield* Effect.fail(new UnexpectedFixtureStatus({ status: response.status })); |
| 263 | }), |
| 264 | ), |
| 265 | Effect.flatMap(Schema.decodeUnknownEffect(bucketInfo)), |
| 266 | Effect.retry({ |
| 267 | while: (error) => |
| 268 | error._tag === "FixtureNotReady" || |
| 269 | (error._tag === "HttpClientError" && error.reason._tag === "TransportError"), |
| 270 | schedule: Schedule.spaced("2 seconds"), |
| 271 | times: 9, |
| 272 | }), |
| 273 | ); |
| 274 | expect(buckets.map((bucket) => bucket.binding).sort()).toEqual(Object.keys(actions).sort()); |
| 275 | expect(new Set(buckets.map((bucket) => bucket.bucketName)).size).toBe(5); |
| 276 | yield* Core.withProviders(assertPermissions, options, "S3VersionedObjectLock"); |
| 277 | }), |
| 278 | { timeout: 120_000, retry: 0 }, |
| 279 | ); |
| 280 | |
| 281 | afterAll( |
| 282 | Effect.gen(function* () { |
| 283 | yield* stack.destroy(); |
| 284 | yield* Core.withProviders( |
| 285 | Effect.forEach(buckets, (bucket) => assertBucketDeleted(bucket.bucketName), { |
| 286 | discard: true, |
| 287 | }), |
| 288 | options, |
| 289 | "S3VersionedObjectLock", |
| 290 | ); |
| 291 | }), |
| 292 | { timeout: 120_000, retry: 0 }, |
| 293 | ); |
| 294 | |
| 295 | describe.sequential( |
| 296 | "versioned object lock bindings", |
| 297 | { tags: ["provider:aws", "provider:aws:iam", "provider:aws:lambda", "provider:aws:s3", "live"] }, |
| 298 | () => { |
| 299 | for (const [binding, path] of [ |
| 300 | ["GetObjectRetention", "/get-retention"], |
| 301 | ["PutObjectRetention", "/put-retention"], |
| 302 | ["GetObjectLegalHold", "/get-hold"], |
| 303 | ["PutObjectLegalHold", "/put-hold"], |
| 304 | ["RestoreObject", "/restore"], |
| 305 | ] as const) { |
| 306 | describe(binding, () => { |
| 307 | test.provider( |
| 308 | "returns typed errors for missing versions and a selected delete marker", |
| 309 | () => |
| 310 | Effect.gen(function* () { |
| 311 | const Bucket = bucketFor(binding); |
| 312 | const Key = "versioned-object-lock/version-errors.txt"; |
| 313 | const versions = yield* seedVersions(Bucket, Key); |
| 314 | const dates = yield* retentionDates; |
| 315 | const input = { Key, retainUntil: dates.old.toISOString(), Status: "OFF" }; |
| 316 | yield* Effect.gen(function* () { |
| 317 | yield* S3.deleteObject({ Bucket, Key, VersionId: versions.old }); |
| 318 | for (const VersionId of [versions.old, "null"]) { |
| 319 | yield* rejected(path, { ...input, VersionId }, 403, "AccessDeniedException"); |
| 320 | } |
| 321 | const marker = yield* S3.deleteObject({ Bucket, Key }); |
| 322 | expect(marker.VersionId).toBeTruthy(); |
| 323 | yield* rejected( |
| 324 | path, |
| 325 | { ...input, VersionId: marker.VersionId! }, |
| 326 | binding === "RestoreObject" ? 405 : 403, |
| 327 | binding === "RestoreObject" ? "MethodNotAllowed" : "AccessDeniedException", |
| 328 | ); |
| 329 | const current = yield* S3.getObject({ Bucket, Key, VersionId: versions.current }); |
| 330 | expect(current.VersionId).toBe(versions.current); |
| 331 | expect(yield* current.Body!.pipe(Stream.decodeText, Stream.mkString)).toBe( |
| 332 | versions.currentBody, |
| 333 | ); |
| 334 | const listed = yield* S3.listObjectVersions({ Bucket, Prefix: Key }); |
| 335 | expect(listed.Versions?.map((version) => version.VersionId)).toEqual([ |
| 336 | versions.current, |
| 337 | ]); |
| 338 | expect(listed.DeleteMarkers?.map((version) => version.VersionId)).toEqual([ |
| 339 | marker.VersionId, |
| 340 | ]); |
| 341 | }).pipe( |
| 342 | Effect.ensuring( |
| 343 | binding === "PutObjectRetention" |
| 344 | ? removeRetention(Bucket, Key, [versions.current]) |
| 345 | : Effect.void, |
| 346 | ), |
| 347 | ); |
| 348 | }), |
| 349 | { timeout: 120_000, retry: 0 }, |
| 350 | ); |
| 351 | }); |
| 352 | } |
| 353 | |
| 354 | describe("GetObjectRetention", () => { |
| 355 | test.provider( |
| 356 | "reads the selected old version's retention separately from the latest version", |
| 357 | () => |
| 358 | Effect.gen(function* () { |
| 359 | const Bucket = bucketFor("GetObjectRetention"); |
| 360 | const Key = "versioned-object-lock/get-retention.txt"; |
| 361 | const versions = yield* seedVersions(Bucket, Key); |
| 362 | const dates = yield* retentionDates; |
| 363 | yield* Effect.gen(function* () { |
| 364 | yield* S3.putObjectRetention({ |
| 365 | Bucket, |
| 366 | Key, |
| 367 | VersionId: versions.old, |
| 368 | Retention: { Mode: "GOVERNANCE", RetainUntilDate: dates.old }, |
| 369 | }); |
| 370 | yield* S3.putObjectRetention({ |
| 371 | Bucket, |
| 372 | Key, |
| 373 | VersionId: versions.current, |
| 374 | Retention: { Mode: "GOVERNANCE", RetainUntilDate: dates.current }, |
| 375 | }); |
| 376 | yield* assertProtectedDelete(Bucket, Key, versions.old); |
| 377 | const old = yield* post( |
| 378 | "/get-retention", |
| 379 | { Key, VersionId: versions.old }, |
| 380 | retentionResponse, |
| 381 | ); |
| 382 | const current = yield* post("/get-retention", { Key }, retentionResponse); |
| 383 | expect(old.Retention).toEqual({ |
| 384 | Mode: "GOVERNANCE", |
| 385 | RetainUntilDate: dates.old.toISOString(), |
| 386 | }); |
| 387 | expect(current.Retention).toEqual({ |
| 388 | Mode: "GOVERNANCE", |
| 389 | RetainUntilDate: dates.current.toISOString(), |
| 390 | }); |
| 391 | expect(old.Retention).not.toEqual(current.Retention); |
| 392 | expect( |
| 393 | yield* post( |
| 394 | "/get-retention", |
| 395 | { Key, VersionId: versions.current }, |
| 396 | retentionResponse, |
| 397 | ), |
| 398 | ).toEqual(current); |
| 399 | expect( |
| 400 | (yield* S3.getObjectRetention({ Bucket, Key, VersionId: versions.old })).Retention, |
| 401 | ).toEqual({ Mode: "GOVERNANCE", RetainUntilDate: dates.old }); |
| 402 | expect((yield* S3.getObjectRetention({ Bucket, Key })).Retention).toEqual({ |
| 403 | Mode: "GOVERNANCE", |
| 404 | RetainUntilDate: dates.current, |
| 405 | }); |
| 406 | yield* assertVersions(Bucket, Key, versions); |
| 407 | }).pipe( |
| 408 | Effect.ensuring(removeRetention(Bucket, Key, [versions.old, versions.current])), |
| 409 | ); |
| 410 | }), |
| 411 | { timeout: 120_000, retry: 0 }, |
| 412 | ); |
| 413 | }); |
| 414 | |
| 415 | describe("PutObjectRetention", () => { |
| 416 | test.provider( |
| 417 | "extends GOVERNANCE retention on the old version without modifying latest retention", |
| 418 | () => |
| 419 | Effect.gen(function* () { |
| 420 | const Bucket = bucketFor("PutObjectRetention"); |
| 421 | const Key = "versioned-object-lock/put-retention.txt"; |
| 422 | const versions = yield* seedVersions(Bucket, Key); |
| 423 | const dates = yield* retentionDates; |
| 424 | yield* Effect.gen(function* () { |
| 425 | yield* S3.putObjectRetention({ |
| 426 | Bucket, |
| 427 | Key, |
| 428 | VersionId: versions.old, |
| 429 | Retention: { Mode: "GOVERNANCE", RetainUntilDate: dates.old }, |
| 430 | }); |
| 431 | yield* S3.putObjectRetention({ |
| 432 | Bucket, |
| 433 | Key, |
| 434 | VersionId: versions.current, |
| 435 | Retention: { Mode: "GOVERNANCE", RetainUntilDate: dates.current }, |
| 436 | }); |
| 437 | expect( |
| 438 | yield* post( |
| 439 | "/put-retention", |
| 440 | { Key, VersionId: versions.old, retainUntil: dates.extended.toISOString() }, |
| 441 | Schema.Struct({ retained: Schema.Boolean }), |
| 442 | ), |
| 443 | ).toEqual({ retained: true }); |
| 444 | yield* rejected( |
| 445 | "/put-retention", |
| 446 | { Key, VersionId: versions.old, retainUntil: dates.old.toISOString() }, |
| 447 | 403, |
| 448 | "AccessDeniedException", |
| 449 | ); |
| 450 | yield* assertProtectedDelete(Bucket, Key, versions.old); |
| 451 | expect( |
| 452 | (yield* S3.getObjectRetention({ Bucket, Key, VersionId: versions.old })).Retention, |
| 453 | ).toEqual({ Mode: "GOVERNANCE", RetainUntilDate: dates.extended }); |
| 454 | expect((yield* S3.getObjectRetention({ Bucket, Key })).Retention).toEqual({ |
| 455 | Mode: "GOVERNANCE", |
| 456 | RetainUntilDate: dates.current, |
| 457 | }); |
| 458 | expect( |
| 459 | (yield* S3.getObjectRetention({ Bucket, Key, VersionId: versions.current })) |
| 460 | .Retention, |
| 461 | ).toEqual({ Mode: "GOVERNANCE", RetainUntilDate: dates.current }); |
| 462 | yield* assertVersions(Bucket, Key, versions); |
| 463 | }).pipe( |
| 464 | Effect.ensuring(removeRetention(Bucket, Key, [versions.old, versions.current])), |
| 465 | ); |
| 466 | }), |
| 467 | { timeout: 120_000, retry: 0 }, |
| 468 | ); |
| 469 | }); |
| 470 | |
| 471 | describe("GetObjectLegalHold", () => { |
| 472 | test.provider( |
| 473 | "reads ON for the selected old version and OFF for the current version", |
| 474 | () => |
| 475 | Effect.gen(function* () { |
| 476 | const Bucket = bucketFor("GetObjectLegalHold"); |
| 477 | const Key = "versioned-object-lock/get-hold.txt"; |
| 478 | const versions = yield* seedVersions(Bucket, Key); |
| 479 | yield* Effect.gen(function* () { |
| 480 | yield* S3.putObjectLegalHold({ |
| 481 | Bucket, |
| 482 | Key, |
| 483 | VersionId: versions.old, |
| 484 | LegalHold: { Status: "ON" }, |
| 485 | }); |
| 486 | yield* S3.putObjectLegalHold({ |
| 487 | Bucket, |
| 488 | Key, |
| 489 | VersionId: versions.current, |
| 490 | LegalHold: { Status: "OFF" }, |
| 491 | }); |
| 492 | expect( |
| 493 | yield* post("/get-hold", { Key, VersionId: versions.old }, holdResponse), |
| 494 | ).toEqual({ LegalHold: { Status: "ON" } }); |
| 495 | yield* assertProtectedDelete(Bucket, Key, versions.old); |
| 496 | expect(yield* post("/get-hold", { Key }, holdResponse)).toEqual({ |
| 497 | LegalHold: { Status: "OFF" }, |
| 498 | }); |
| 499 | expect( |
| 500 | yield* post("/get-hold", { Key, VersionId: versions.current }, holdResponse), |
| 501 | ).toEqual({ LegalHold: { Status: "OFF" } }); |
| 502 | expect( |
| 503 | (yield* S3.getObjectLegalHold({ Bucket, Key, VersionId: versions.old })).LegalHold |
| 504 | ?.Status, |
| 505 | ).toBe("ON"); |
| 506 | expect((yield* S3.getObjectLegalHold({ Bucket, Key })).LegalHold?.Status).toBe("OFF"); |
| 507 | yield* assertVersions(Bucket, Key, versions); |
| 508 | }).pipe(Effect.ensuring(removeHolds(Bucket, Key, [versions.old, versions.current]))); |
| 509 | }), |
| 510 | { timeout: 120_000, retry: 0 }, |
| 511 | ); |
| 512 | }); |
| 513 | |
| 514 | describe("PutObjectLegalHold", () => { |
| 515 | test.provider( |
| 516 | "toggles only the selected old version's legal hold while latest stays OFF", |
| 517 | () => |
| 518 | Effect.gen(function* () { |
| 519 | const Bucket = bucketFor("PutObjectLegalHold"); |
| 520 | const Key = "versioned-object-lock/put-hold.txt"; |
| 521 | const versions = yield* seedVersions(Bucket, Key); |
| 522 | yield* Effect.gen(function* () { |
| 523 | yield* S3.putObjectLegalHold({ |
| 524 | Bucket, |
| 525 | Key, |
| 526 | VersionId: versions.old, |
| 527 | LegalHold: { Status: "OFF" }, |
| 528 | }); |
| 529 | yield* S3.putObjectLegalHold({ |
| 530 | Bucket, |
| 531 | Key, |
| 532 | VersionId: versions.current, |
| 533 | LegalHold: { Status: "OFF" }, |
| 534 | }); |
| 535 | for (const Status of ["ON", "OFF"] as const) { |
| 536 | expect( |
| 537 | yield* post( |
| 538 | "/put-hold", |
| 539 | { Key, VersionId: versions.old, Status }, |
| 540 | Schema.Struct({ updated: Schema.Boolean }), |
| 541 | ), |
| 542 | ).toEqual({ updated: true }); |
| 543 | if (Status === "ON") { |
| 544 | yield* assertProtectedDelete(Bucket, Key, versions.old); |
| 545 | } |
| 546 | expect( |
| 547 | (yield* S3.getObjectLegalHold({ Bucket, Key, VersionId: versions.old })).LegalHold |
| 548 | ?.Status, |
| 549 | ).toBe(Status); |
| 550 | expect((yield* S3.getObjectLegalHold({ Bucket, Key })).LegalHold?.Status).toBe( |
| 551 | "OFF", |
| 552 | ); |
| 553 | expect( |
| 554 | (yield* S3.getObjectLegalHold({ Bucket, Key, VersionId: versions.current })) |
| 555 | .LegalHold?.Status, |
| 556 | ).toBe("OFF"); |
| 557 | } |
| 558 | yield* assertVersions(Bucket, Key, versions); |
| 559 | }).pipe(Effect.ensuring(removeHolds(Bucket, Key, [versions.old, versions.current]))); |
| 560 | }), |
| 561 | { timeout: 120_000, retry: 0 }, |
| 562 | ); |
| 563 | }); |
| 564 | |
| 565 | describe("RestoreObject", () => { |
| 566 | test.provider( |
| 567 | "selects a suspended null STANDARD version without changing current data", |
| 568 | () => |
| 569 | Effect.gen(function* () { |
| 570 | const Bucket = bucketFor("RestoreObject"); |
| 571 | const Key = "versioned-object-lock/restore-null.txt"; |
| 572 | yield* Effect.gen(function* () { |
| 573 | yield* S3.putBucketVersioning({ |
| 574 | Bucket, |
| 575 | VersioningConfiguration: { Status: "Suspended" }, |
| 576 | }); |
| 577 | yield* S3.putObject({ Bucket, Key, Body: "null restore version" }); |
| 578 | }).pipe( |
| 579 | Effect.ensuring( |
| 580 | S3.putBucketVersioning({ |
| 581 | Bucket, |
| 582 | VersioningConfiguration: { Status: "Enabled" }, |
| 583 | }).pipe(Effect.orDie), |
| 584 | ), |
| 585 | ); |
| 586 | const current = yield* S3.putObject({ Bucket, Key, Body: "current restore version" }); |
| 587 | expect(current.VersionId).toBeTruthy(); |
| 588 | expect(current.VersionId).not.toBe("null"); |
| 589 | expect( |
| 590 | yield* post( |
| 591 | "/restore", |
| 592 | { Key, VersionId: "null" }, |
| 593 | Schema.Struct({ tag: Schema.Literal("InvalidObjectState") }), |
| 594 | ), |
| 595 | ).toEqual({ tag: "InvalidObjectState" }); |
| 596 | for (const [VersionId, expectedBody] of [ |
| 597 | ["null", "null restore version"], |
| 598 | [current.VersionId!, "current restore version"], |
| 599 | ]) { |
| 600 | const head = yield* S3.headObject({ Bucket, Key, VersionId }); |
| 601 | expect(head.VersionId).toBe(VersionId); |
| 602 | expect(head.Restore).toBeUndefined(); |
| 603 | const object = yield* S3.getObject({ Bucket, Key, VersionId }); |
| 604 | expect(yield* object.Body!.pipe(Stream.decodeText, Stream.mkString)).toBe( |
| 605 | expectedBody, |
| 606 | ); |
| 607 | } |
| 608 | const listed = yield* S3.listObjectVersions({ Bucket, Prefix: Key }); |
| 609 | expect( |
| 610 | listed.Versions?.map((version) => ({ |
| 611 | id: version.VersionId, |
| 612 | latest: version.IsLatest, |
| 613 | })), |
| 614 | ).toEqual([ |
| 615 | { id: current.VersionId, latest: true }, |
| 616 | { id: "null", latest: false }, |
| 617 | ]); |
| 618 | expect(listed.DeleteMarkers ?? []).toEqual([]); |
| 619 | }), |
| 620 | { timeout: 120_000, retry: 0 }, |
| 621 | ); |
| 622 | test.provider( |
| 623 | "returns typed InvalidObjectState for a selected STANDARD version without restoring or changing latest", |
| 624 | () => |
| 625 | Effect.gen(function* () { |
| 626 | const Bucket = bucketFor("RestoreObject"); |
| 627 | const Key = "versioned-object-lock/restore-standard.txt"; |
| 628 | const versions = yield* seedVersions(Bucket, Key); |
| 629 | const response = Schema.Struct({ tag: Schema.Literal("InvalidObjectState") }); |
| 630 | expect(yield* post("/restore", { Key, VersionId: versions.old }, response)).toEqual({ |
| 631 | tag: "InvalidObjectState", |
| 632 | }); |
| 633 | expect(yield* post("/restore", { Key }, response)).toEqual({ |
| 634 | tag: "InvalidObjectState", |
| 635 | }); |
| 636 | for (const VersionId of [versions.old, versions.current]) { |
| 637 | const head = yield* S3.headObject({ Bucket, Key, VersionId }); |
| 638 | expect(head.VersionId).toBe(VersionId); |
| 639 | expect(head.StorageClass ?? "STANDARD").toBe("STANDARD"); |
| 640 | expect(head.Restore).toBeUndefined(); |
| 641 | } |
| 642 | yield* assertVersions(Bucket, Key, versions); |
| 643 | }), |
| 644 | { timeout: 120_000, retry: 0 }, |
| 645 | ); |
| 646 | |
| 647 | test.provider( |
| 648 | "accepts restoration of an old GLACIER version without waiting for retrieval or changing latest STANDARD", |
| 649 | () => |
| 650 | Effect.gen(function* () { |
| 651 | const Bucket = bucketFor("RestoreObject"); |
| 652 | const Key = "versioned-object-lock/restore-glacier.txt"; |
| 653 | const versions = yield* seedVersions(Bucket, Key, "GLACIER"); |
| 654 | const archived = yield* S3.headObject({ Bucket, Key, VersionId: versions.old }); |
| 655 | expect(archived.VersionId).toBe(versions.old); |
| 656 | expect(archived.StorageClass).toBe("GLACIER"); |
| 657 | expect(archived.ContentLength).toBe(versions.oldBody.length); |
| 658 | expect(archived.Restore).toBeUndefined(); |
| 659 | expect( |
| 660 | yield* post( |
| 661 | "/restore", |
| 662 | { Key, VersionId: versions.old }, |
| 663 | Schema.Struct({ tag: Schema.Literal("accepted"), versionId: Schema.String }), |
| 664 | ), |
| 665 | ).toEqual({ tag: "accepted", versionId: versions.old }); |
| 666 | |
| 667 | // Poll only for restore-request visibility, never for retrieval completion. |
| 668 | const restored = yield* S3.headObject({ Bucket, Key, VersionId: versions.old }).pipe( |
| 669 | Effect.repeat({ |
| 670 | until: (head) => head.Restore !== undefined, |
| 671 | schedule: Schedule.spaced("2 seconds"), |
| 672 | times: 9, |
| 673 | }), |
| 674 | ); |
| 675 | expect(restored.VersionId).toBe(versions.old); |
| 676 | expect(restored.StorageClass).toBe("GLACIER"); |
| 677 | expect(restored.ETag).toBe(archived.ETag); |
| 678 | expect(restored.ContentLength).toBe(archived.ContentLength); |
| 679 | expect(restored.Restore).toMatch(/ongoing-request="(?:true|false)"/); |
| 680 | const pending = restored.Restore!.includes('ongoing-request="true"'); |
| 681 | if (!pending) { |
| 682 | expect(restored.Restore).toContain('expiry-date="'); |
| 683 | } |
| 684 | yield* Effect.logInfo( |
| 685 | `RestoreObject accepted for ${versions.old}; retrieval ${pending ? "pending" : "completed"}`, |
| 686 | ); |
| 687 | |
| 688 | for (const VersionId of [undefined, versions.current]) { |
| 689 | const latest = yield* S3.headObject({ Bucket, Key, VersionId }); |
| 690 | expect(latest.VersionId).toBe(versions.current); |
| 691 | expect(latest.StorageClass ?? "STANDARD").toBe("STANDARD"); |
| 692 | expect(latest.ContentLength).toBe(versions.currentBody.length); |
| 693 | expect(latest.Restore).toBeUndefined(); |
| 694 | } |
| 695 | const current = yield* S3.getObject({ Bucket, Key }); |
| 696 | expect(current.VersionId).toBe(versions.current); |
| 697 | expect(current.Body).toBeDefined(); |
| 698 | expect(yield* current.Body!.pipe(Stream.decodeText, Stream.mkString)).toBe( |
| 699 | versions.currentBody, |
| 700 | ); |
| 701 | const listed = yield* S3.listObjectVersions({ Bucket, Prefix: Key }); |
| 702 | expect( |
| 703 | listed.Versions?.filter((version) => version.Key === Key) |
| 704 | .map((version) => ({ |
| 705 | id: version.VersionId, |
| 706 | latest: version.IsLatest, |
| 707 | storageClass: version.StorageClass, |
| 708 | })) |
| 709 | .sort((a, b) => a.id!.localeCompare(b.id!)), |
| 710 | ).toEqual( |
| 711 | [ |
| 712 | { id: versions.old, latest: false, storageClass: "GLACIER" }, |
| 713 | { id: versions.current, latest: true, storageClass: "STANDARD" }, |
| 714 | ].sort((a, b) => a.id.localeCompare(b.id)), |
| 715 | ); |
| 716 | }), |
| 717 | { timeout: 120_000, retry: 0 }, |
| 718 | ); |
| 719 | }); |
| 720 | }, |
| 721 | ); |