The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 secretsmanager from "@distilled.cloud/aws/secrets-manager"; |
| 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 GetSecretOnlyTestFunctionLive, { |
| 16 | GetSecretOnlyTestFunction, |
| 17 | } from "./fixtures/get-secret-only-handler.ts"; |
| 18 | import SecretsManagerTestFunctionLive, { SecretsManagerTestFunction } from "./handler"; |
| 19 | |
| 20 | const testOptions = { providers: AWS.providers() }; |
| 21 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 22 | const sharedStack = Core.scratchStack( |
| 23 | testOptions, |
| 24 | "SecretsManagerBindings", |
| 25 | "test/AWS/SecretsManager/Bindings.test.ts", |
| 26 | ); |
| 27 | |
| 28 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take |
| 29 | // well over 60s on a fresh deploy under parallel-suite load. Budget ~150s |
| 30 | // of readiness polling so we don't fail the whole suite on a slow init. |
| 31 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 32 | |
| 33 | let baseUrl: string; |
| 34 | let getOnlyUrl: string; |
| 35 | let getOnlyFunction: { functionName: string; roleName: string } | undefined; |
| 36 | let getOnlySecret: { secretArn: string; secretName: string } | undefined; |
| 37 | |
| 38 | const policyDocument = Schema.fromJsonString( |
| 39 | Schema.Struct({ |
| 40 | Version: Schema.optional(Schema.String), |
| 41 | Id: Schema.optional(Schema.String), |
| 42 | Statement: Schema.Array( |
| 43 | Schema.Struct({ |
| 44 | Sid: Schema.optional(Schema.String), |
| 45 | Effect: Schema.String, |
| 46 | Action: Schema.Union([Schema.String, Schema.Array(Schema.String)]), |
| 47 | Resource: Schema.Union([Schema.String, Schema.Array(Schema.String)]), |
| 48 | }), |
| 49 | ), |
| 50 | }), |
| 51 | ); |
| 52 | |
| 53 | const secretRolePermissions = Effect.fn(function* (roleName: string) { |
| 54 | const pages = yield* IAM.listRolePolicies.pages({ RoleName: roleName }).pipe(Stream.runCollect); |
| 55 | const policies = yield* Effect.forEach( |
| 56 | // Skip the function's log-group guard; only binding grants count. |
| 57 | pages |
| 58 | .flatMap((page) => page.PolicyNames) |
| 59 | .filter((name) => name !== "AlchemyDenyLogGroupCreate"), |
| 60 | Effect.fn(function* (PolicyName) { |
| 61 | const policy = yield* IAM.getRolePolicy({ RoleName: roleName, PolicyName }); |
| 62 | const decoded = yield* Effect.try(() => decodeURIComponent(policy.PolicyDocument)); |
| 63 | return yield* Schema.decodeUnknownEffect(policyDocument, { onExcessProperty: "error" })( |
| 64 | decoded, |
| 65 | ); |
| 66 | }), |
| 67 | ); |
| 68 | return policies.flatMap((policy) => |
| 69 | policy.Statement.flatMap((statement) => { |
| 70 | const actions = typeof statement.Action === "string" ? [statement.Action] : statement.Action; |
| 71 | const resources = |
| 72 | typeof statement.Resource === "string" ? [statement.Resource] : statement.Resource; |
| 73 | return actions.flatMap((action) => |
| 74 | resources.map((resource) => ({ effect: statement.Effect, action, resource })), |
| 75 | ); |
| 76 | }), |
| 77 | ); |
| 78 | }); |
| 79 | |
| 80 | const getOnlyValue = Schema.Struct({ |
| 81 | arn: Schema.String, |
| 82 | name: Schema.String, |
| 83 | versionId: Schema.String, |
| 84 | secretString: Schema.String, |
| 85 | }); |
| 86 | |
| 87 | const readGetOnlyValue = (query = "") => |
| 88 | HttpClient.get(`${getOnlyUrl}/get-value${query}`).pipe( |
| 89 | Effect.flatMap((response) => |
| 90 | response.status === 200 |
| 91 | ? response.json |
| 92 | : Effect.fail(new Error(`GetSecretValue failed: ${response.status}`)), |
| 93 | ), |
| 94 | Effect.flatMap(Schema.decodeUnknownEffect(getOnlyValue)), |
| 95 | Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 10 }), |
| 96 | ); |
| 97 | |
| 98 | class FixtureStillExists extends Data.TaggedError("FixtureStillExists")<{ |
| 99 | readonly resource: string; |
| 100 | }> {} |
| 101 | |
| 102 | const assertGetOnlyResourcesDeleted = Effect.gen(function* () { |
| 103 | yield* Effect.all( |
| 104 | [ |
| 105 | getOnlySecret |
| 106 | ? secretsmanager.describeSecret({ SecretId: getOnlySecret.secretArn }).pipe( |
| 107 | Effect.flatMap(() => Effect.fail(new FixtureStillExists({ resource: "secret" }))), |
| 108 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 109 | Effect.retry({ |
| 110 | while: (error) => error._tag === "FixtureStillExists", |
| 111 | schedule: Schedule.spaced("2 seconds"), |
| 112 | times: 10, |
| 113 | }), |
| 114 | ) |
| 115 | : Effect.void, |
| 116 | getOnlyFunction |
| 117 | ? Lambda.getFunction({ FunctionName: getOnlyFunction.functionName }).pipe( |
| 118 | Effect.flatMap(() => Effect.fail(new FixtureStillExists({ resource: "function" }))), |
| 119 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 120 | Effect.retry({ |
| 121 | while: (error) => error._tag === "FixtureStillExists", |
| 122 | schedule: Schedule.spaced("2 seconds"), |
| 123 | times: 10, |
| 124 | }), |
| 125 | ) |
| 126 | : Effect.void, |
| 127 | ], |
| 128 | { concurrency: "unbounded" }, |
| 129 | ); |
| 130 | }); |
| 131 | |
| 132 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 133 | readonly status: number; |
| 134 | readonly body: string; |
| 135 | }> {} |
| 136 | |
| 137 | // The shared Lambda fixture occasionally answers a transient 5xx under |
| 138 | // full-suite parallel load (cold re-init, IAM propagation on the freshly |
| 139 | // attached secretsmanager policy that the handler's `Effect.orDie` |
| 140 | // surfaces as a 500). Those are not assertion failures: retry the request |
| 141 | // a few times before surfacing it. A genuine 4xx/assertion failure is |
| 142 | // returned immediately (only 5xx is retried). |
| 143 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 144 | HttpClient.execute(request).pipe( |
| 145 | Effect.flatMap((response) => |
| 146 | response.status >= 500 |
| 147 | ? response.text.pipe( |
| 148 | Effect.flatMap((body) => |
| 149 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 150 | ), |
| 151 | ) |
| 152 | : Effect.succeed(response), |
| 153 | ), |
| 154 | Effect.retry({ |
| 155 | while: (e) => e._tag === "TransientUpstream", |
| 156 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 157 | }), |
| 158 | ); |
| 159 | |
| 160 | // Deterministic binary payload (checked-in constant — never generated at |
| 161 | // test time): base64 of bytes [0,1,2,3,250,251,252,253,254,255,42,7], |
| 162 | // exercising non-UTF8 bytes through the base64 transport. |
| 163 | const BINARY_BASE64 = "AAECA/r7/P3+/yoH"; |
| 164 | |
| 165 | // GetSecretValue's round-trip and PutSecretValue both act on ONE shared |
| 166 | // fixture secret; the global vitest `sequence: { concurrent: true }` would |
| 167 | // let the put race the read and fail it with BindingNotConsistent. |
| 168 | describe.sequential( |
| 169 | "SecretsManager Bindings", |
| 170 | { |
| 171 | tags: [ |
| 172 | "provider:aws", |
| 173 | "provider:aws:batch", |
| 174 | "provider:aws:lambda", |
| 175 | "provider:aws:secretsmanager", |
| 176 | "live", |
| 177 | ], |
| 178 | }, |
| 179 | () => { |
| 180 | beforeAll( |
| 181 | Effect.gen(function* () { |
| 182 | yield* Effect.logInfo("SecretsManager test setup: destroying previous resources"); |
| 183 | yield* sharedStack.destroy(); |
| 184 | |
| 185 | yield* Effect.logInfo("SecretsManager test setup: deploying fixture"); |
| 186 | const { shared, getOnly } = yield* sharedStack.deploy( |
| 187 | Effect.gen(function* () { |
| 188 | return { |
| 189 | shared: yield* SecretsManagerTestFunction, |
| 190 | getOnly: yield* GetSecretOnlyTestFunction, |
| 191 | }; |
| 192 | }).pipe( |
| 193 | Effect.provide(SecretsManagerTestFunctionLive), |
| 194 | Effect.provide(GetSecretOnlyTestFunctionLive), |
| 195 | ), |
| 196 | ); |
| 197 | |
| 198 | getOnlyFunction = getOnly; |
| 199 | expect(getOnly.functionUrl).toBeTruthy(); |
| 200 | getOnlyUrl = getOnly.functionUrl!.replace(/\/+$/, ""); |
| 201 | getOnlySecret = yield* HttpClient.get(`${getOnlyUrl}/info`).pipe( |
| 202 | Effect.flatMap((response) => |
| 203 | response.status === 200 |
| 204 | ? response.json |
| 205 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 206 | ), |
| 207 | Effect.flatMap( |
| 208 | Schema.decodeUnknownEffect( |
| 209 | Schema.Struct({ secretArn: Schema.String, secretName: Schema.String }), |
| 210 | ), |
| 211 | ), |
| 212 | Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 10 }), |
| 213 | ); |
| 214 | |
| 215 | expect(shared.functionUrl).toBeTruthy(); |
| 216 | baseUrl = shared.functionUrl!.replace(/\/+$/, ""); |
| 217 | const readinessUrl = `${baseUrl}/describe`; |
| 218 | |
| 219 | yield* Effect.logInfo(`SecretsManager test setup: probing readiness at ${readinessUrl}`); |
| 220 | |
| 221 | yield* HttpClient.get(readinessUrl).pipe( |
| 222 | Effect.flatMap((response) => |
| 223 | response.status === 200 |
| 224 | ? Effect.succeed(response) |
| 225 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 226 | ), |
| 227 | Effect.tap(() => |
| 228 | Effect.logInfo("SecretsManager test setup: fixture responded successfully"), |
| 229 | ), |
| 230 | Effect.tapError((error) => |
| 231 | Effect.logWarning( |
| 232 | `SecretsManager test setup: fixture not ready yet (${String(error)})`, |
| 233 | ), |
| 234 | ), |
| 235 | Effect.retry({ schedule: readinessPolicy }), |
| 236 | ); |
| 237 | }), |
| 238 | { timeout: 240_000 }, |
| 239 | ); |
| 240 | |
| 241 | afterAll.skipIf(!!process.env.NO_DESTROY)( |
| 242 | sharedStack |
| 243 | .destroy() |
| 244 | .pipe( |
| 245 | Effect.andThen( |
| 246 | Core.withProviders( |
| 247 | assertGetOnlyResourcesDeleted, |
| 248 | testOptions, |
| 249 | "SecretsManagerBindings", |
| 250 | ), |
| 251 | ), |
| 252 | ), |
| 253 | { timeout: 120_000 }, |
| 254 | ); |
| 255 | |
| 256 | describe("GetSecretValue", () => { |
| 257 | test.provider( |
| 258 | "reads a string with only the GetSecretValue binding", |
| 259 | (_stack) => |
| 260 | Effect.gen(function* () { |
| 261 | const value = yield* readGetOnlyValue(); |
| 262 | expect(value.secretString).toBe("alchemy-sm-get-only-value"); |
| 263 | expect(value.arn).toBe(getOnlySecret!.secretArn); |
| 264 | expect(value.name).toBe(getOnlySecret!.secretName); |
| 265 | expect(value.versionId).toBeTruthy(); |
| 266 | |
| 267 | const query = yield* Effect.sync(() => |
| 268 | new URLSearchParams({ |
| 269 | versionId: value.versionId, |
| 270 | versionStage: "AWSCURRENT", |
| 271 | }).toString(), |
| 272 | ); |
| 273 | const versioned = yield* readGetOnlyValue(`?${query}`); |
| 274 | expect(versioned).toEqual(value); |
| 275 | }), |
| 276 | { timeout: 120_000 }, |
| 277 | ); |
| 278 | |
| 279 | test.provider( |
| 280 | "grants exactly GetSecretValue on the isolated secret ARN", |
| 281 | (_stack) => |
| 282 | Effect.gen(function* () { |
| 283 | const roleName = getOnlyFunction!.roleName; |
| 284 | const permissions = yield* secretRolePermissions(roleName); |
| 285 | expect(permissions).toEqual([ |
| 286 | { |
| 287 | effect: "Allow", |
| 288 | action: "secretsmanager:GetSecretValue", |
| 289 | resource: getOnlySecret!.secretArn, |
| 290 | }, |
| 291 | ]); |
| 292 | const attached = yield* IAM.listAttachedRolePolicies |
| 293 | .pages({ RoleName: roleName }) |
| 294 | .pipe(Stream.runCollect); |
| 295 | expect( |
| 296 | attached.flatMap((page) => |
| 297 | (page.AttachedPolicies ?? []).map((policy) => policy.PolicyArn), |
| 298 | ), |
| 299 | ).toEqual(["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"]); |
| 300 | }), |
| 301 | { tags: ["provider:aws:iam"], timeout: 120_000 }, |
| 302 | ); |
| 303 | |
| 304 | test.provider( |
| 305 | "denies raw DescribeSecret after an authorized GetSecretValue read", |
| 306 | (_stack) => |
| 307 | Effect.gen(function* () { |
| 308 | const value = yield* readGetOnlyValue(); |
| 309 | expect(value.secretString).toBe("alchemy-sm-get-only-value"); |
| 310 | expect(value.arn).toBe(getOnlySecret!.secretArn); |
| 311 | |
| 312 | const response = yield* HttpClient.get(`${getOnlyUrl}/describe-denied`); |
| 313 | expect(response.status).toBe(200); |
| 314 | const denied = yield* response.json.pipe( |
| 315 | Effect.flatMap( |
| 316 | Schema.decodeUnknownEffect( |
| 317 | Schema.Struct({ tag: Schema.Literal("AccessDeniedException") }), |
| 318 | ), |
| 319 | ), |
| 320 | ); |
| 321 | expect(denied.tag).toBe("AccessDeniedException"); |
| 322 | }), |
| 323 | { timeout: 120_000 }, |
| 324 | ); |
| 325 | |
| 326 | test.provider("reads the string secret value round-trip", (_stack) => |
| 327 | Effect.gen(function* () { |
| 328 | // GetSecretValue is eventually consistent right after the fixture |
| 329 | // secret is (re)created; poll until the fixture value is observed. |
| 330 | const response = yield* fetchUntil( |
| 331 | send(HttpClientRequest.get(`${baseUrl}/string-value`)).pipe( |
| 332 | Effect.flatMap((r) => r.json), |
| 333 | ), |
| 334 | (body) => body?.secretString === "alchemy-sm-fixture-value", |
| 335 | ); |
| 336 | |
| 337 | expect((response as any).secretString).toBe("alchemy-sm-fixture-value"); |
| 338 | expect((response as any).arn).toContain("arn:aws:secretsmanager:"); |
| 339 | expect((response as any).versionId).toBeTruthy(); |
| 340 | }), |
| 341 | ); |
| 342 | }); |
| 343 | |
| 344 | describe("PutSecretValue", () => { |
| 345 | test.provider("rotates the string secret value", (_stack) => |
| 346 | Effect.gen(function* () { |
| 347 | const put = yield* send( |
| 348 | HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(`${baseUrl}/put-string`), { |
| 349 | value: "alchemy-sm-rotated-value", |
| 350 | }), |
| 351 | ).pipe(Effect.flatMap((r) => r.json)); |
| 352 | |
| 353 | expect((put as any).versionId).toBeTruthy(); |
| 354 | |
| 355 | // Read-after-write on a fresh version is eventually consistent; |
| 356 | // poll until the new version is served as AWSCURRENT. |
| 357 | const got = yield* fetchUntil( |
| 358 | send(HttpClientRequest.get(`${baseUrl}/string-value`)).pipe( |
| 359 | Effect.flatMap((r) => r.json), |
| 360 | ), |
| 361 | (body) => body?.versionId === (put as any).versionId, |
| 362 | ); |
| 363 | |
| 364 | expect((got as any).secretString).toBe("alchemy-sm-rotated-value"); |
| 365 | expect((got as any).versionId).toBe((put as any).versionId); |
| 366 | }), |
| 367 | ); |
| 368 | |
| 369 | test.provider("writes and reads back a binary secret value", (_stack) => |
| 370 | Effect.gen(function* () { |
| 371 | const put = yield* send( |
| 372 | HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(`${baseUrl}/put-binary`), { |
| 373 | base64: BINARY_BASE64, |
| 374 | }), |
| 375 | ).pipe(Effect.flatMap((r) => r.json)); |
| 376 | |
| 377 | expect((put as any).versionId).toBeTruthy(); |
| 378 | |
| 379 | // Read-after-write on a fresh version is eventually consistent; |
| 380 | // poll until the new version is served as AWSCURRENT. |
| 381 | const got = yield* fetchUntil( |
| 382 | send(HttpClientRequest.get(`${baseUrl}/binary-value`)).pipe( |
| 383 | Effect.flatMap((r) => r.json), |
| 384 | ), |
| 385 | (body) => body?.versionId === (put as any).versionId, |
| 386 | ); |
| 387 | |
| 388 | expect((got as any).base64).toBe(BINARY_BASE64); |
| 389 | // A binary version carries no SecretString. |
| 390 | expect((got as any).secretString).toBeUndefined(); |
| 391 | expect((got as any).versionId).toBe((put as any).versionId); |
| 392 | }), |
| 393 | ); |
| 394 | }); |
| 395 | |
| 396 | describe("DescribeSecret", () => { |
| 397 | test.provider("describes the bound secret", (_stack) => |
| 398 | Effect.gen(function* () { |
| 399 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/describe`)).pipe( |
| 400 | Effect.flatMap((r) => r.json), |
| 401 | ); |
| 402 | |
| 403 | expect((response as any).arn).toContain("arn:aws:secretsmanager:"); |
| 404 | expect((response as any).name).toBeTruthy(); |
| 405 | expect((response as any).description).toBe("alchemy binding fixture (string value)"); |
| 406 | }), |
| 407 | ); |
| 408 | }); |
| 409 | |
| 410 | describe("GetRandomPassword", () => { |
| 411 | test.provider("generates a password of the requested length", (_stack) => |
| 412 | Effect.gen(function* () { |
| 413 | const response = yield* send( |
| 414 | HttpClientRequest.get(`${baseUrl}/random-password?length=24`), |
| 415 | ).pipe(Effect.flatMap((r) => r.json)); |
| 416 | |
| 417 | expect(typeof (response as any).password).toBe("string"); |
| 418 | expect((response as any).password).toHaveLength(24); |
| 419 | }), |
| 420 | ); |
| 421 | }); |
| 422 | |
| 423 | describe("ListSecrets", () => { |
| 424 | test.provider("lists the bound secret by name filter", (_stack) => |
| 425 | Effect.gen(function* () { |
| 426 | const described = yield* send(HttpClientRequest.get(`${baseUrl}/describe`)).pipe( |
| 427 | Effect.flatMap((r) => r.json), |
| 428 | ); |
| 429 | const name = (described as any).name as string; |
| 430 | |
| 431 | // ListSecrets is eventually consistent; poll until the freshly |
| 432 | // created secret surfaces in the filtered listing. |
| 433 | const response = yield* fetchUntil( |
| 434 | send(HttpClientRequest.get(`${baseUrl}/list?name=${encodeURIComponent(name)}`)).pipe( |
| 435 | Effect.flatMap((r) => r.json), |
| 436 | ), |
| 437 | (body) => Array.isArray(body?.names) && body.names.includes(name), |
| 438 | ); |
| 439 | |
| 440 | expect((response as any).names).toContain(name); |
| 441 | }), |
| 442 | ); |
| 443 | }); |
| 444 | |
| 445 | describe("ListSecretVersionIds", () => { |
| 446 | test.provider("lists the string secret's versions with stages", (_stack) => |
| 447 | Effect.gen(function* () { |
| 448 | const response = yield* fetchUntil( |
| 449 | send(HttpClientRequest.get(`${baseUrl}/versions`)).pipe(Effect.flatMap((r) => r.json)), |
| 450 | (body) => |
| 451 | Array.isArray(body?.versions) && |
| 452 | body.versions.some((version: any) => version.stages?.includes("AWSCURRENT")), |
| 453 | ); |
| 454 | |
| 455 | const current = (response as any).versions.find((version: any) => |
| 456 | version.stages.includes("AWSCURRENT"), |
| 457 | ); |
| 458 | expect(current.versionId).toBeTruthy(); |
| 459 | }), |
| 460 | ); |
| 461 | }); |
| 462 | |
| 463 | describe("BatchGetSecretValue", () => { |
| 464 | test.provider("reads both bound secrets in one call", (_stack) => |
| 465 | Effect.gen(function* () { |
| 466 | // BatchGetSecretValue is eventually consistent right after the |
| 467 | // fixture secrets are created; poll until both values are served. |
| 468 | const response = yield* fetchUntil( |
| 469 | send(HttpClientRequest.get(`${baseUrl}/batch`)).pipe(Effect.flatMap((r) => r.json)), |
| 470 | (body) => |
| 471 | Array.isArray(body?.values) && |
| 472 | body.values.length === 2 && |
| 473 | body.values.every( |
| 474 | (entry: any) => |
| 475 | typeof entry.secretString === "string" && entry.secretString.length > 0, |
| 476 | ), |
| 477 | ); |
| 478 | |
| 479 | expect((response as any).values).toHaveLength(2); |
| 480 | expect((response as any).errors).toHaveLength(0); |
| 481 | }), |
| 482 | ); |
| 483 | }); |
| 484 | |
| 485 | // The RotationEventSource wires this same fixture Lambda as the rotation |
| 486 | // function: RotateSecret kicks off the 4-step protocol |
| 487 | // (createSecret -> setSecret -> testSecret -> finishSecret), which the |
| 488 | // handler implements via the GetRandomPassword / PutSecretValue / |
| 489 | // DescribeSecret / UpdateSecretVersionStage bindings. Runs LAST — it |
| 490 | // permanently changes the rotation secret's value. |
| 491 | describe("RotationEventSource", () => { |
| 492 | test.provider("rotation is configured on the secret", (_stack) => |
| 493 | Effect.gen(function* () { |
| 494 | const status = yield* send(HttpClientRequest.get(`${baseUrl}/rotation-status`)).pipe( |
| 495 | Effect.flatMap((r) => r.json), |
| 496 | ); |
| 497 | |
| 498 | expect((status as any).rotationEnabled).toBe(true); |
| 499 | }), |
| 500 | ); |
| 501 | |
| 502 | test.provider( |
| 503 | "RotateSecret triggers the rotation protocol end-to-end", |
| 504 | (_stack) => |
| 505 | Effect.gen(function* () { |
| 506 | const before = yield* fetchUntil( |
| 507 | send(HttpClientRequest.get(`${baseUrl}/rotation-value`)).pipe( |
| 508 | Effect.flatMap((r) => r.json), |
| 509 | ), |
| 510 | (body) => typeof body?.secretString === "string", |
| 511 | ); |
| 512 | |
| 513 | const rotate = yield* send(HttpClientRequest.post(`${baseUrl}/rotate`)).pipe( |
| 514 | Effect.flatMap((r) => r.json), |
| 515 | ); |
| 516 | // The fixture surfaces typed RotateSecret failures as |
| 517 | // `{ error, message }` — assert none so failures are readable. |
| 518 | expect(rotate).not.toHaveProperty("error"); |
| 519 | expect((rotate as any).versionId).toBeTruthy(); |
| 520 | |
| 521 | // Secrets Manager drives the protocol asynchronously — poll until |
| 522 | // the handler's finishSecret promoted the new version. |
| 523 | const after = yield* fetchUntil( |
| 524 | send(HttpClientRequest.get(`${baseUrl}/rotation-value`)).pipe( |
| 525 | Effect.flatMap((r) => r.json), |
| 526 | ), |
| 527 | (body) => |
| 528 | typeof body?.secretString === "string" && |
| 529 | body.secretString.startsWith("alchemy-sm-rotated-"), |
| 530 | 45, |
| 531 | ); |
| 532 | |
| 533 | expect((after as any).secretString).toMatch(/^alchemy-sm-rotated-/); |
| 534 | expect((after as any).versionId).not.toBe((before as any).versionId); |
| 535 | }), |
| 536 | { timeout: 150_000 }, |
| 537 | ); |
| 538 | }); |
| 539 | }, |
| 540 | ); |
| 541 | |
| 542 | // A request hit a Lambda instance observing not-yet-consistent control-plane |
| 543 | // state (ListSecrets lags fresh creates by a few seconds). Retry. |
| 544 | class BindingNotConsistent extends Data.TaggedError("BindingNotConsistent") {} |
| 545 | |
| 546 | const fetchUntil = <A>( |
| 547 | fetch: Effect.Effect<unknown, any, HttpClient.HttpClient>, |
| 548 | ready: (body: any) => boolean, |
| 549 | attempts = 20, |
| 550 | ) => |
| 551 | fetch.pipe( |
| 552 | Effect.flatMap((body) => |
| 553 | ready(body) ? Effect.succeed(body as A) : Effect.fail(new BindingNotConsistent()), |
| 554 | ), |
| 555 | Effect.retry({ |
| 556 | while: (e) => e._tag === "BindingNotConsistent", |
| 557 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(attempts)]), |
| 558 | }), |
| 559 | ); |