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 { TrustedServiceAccess } from "@alchemy.run/aws/Organizations"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as organizations from "@distilled.cloud/aws/organizations"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { isResourceState, State, type ResourceState } from "alchemy/State"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import * as OrganizationLease from "./OrganizationLease.ts"; |
| 10 | |
| 11 | const { test } = OrganizationLease.make({ providers: AWS.providers() }, "shared"); |
| 12 | |
| 13 | // Read-only `list()` test (AWS account-scoped collection that enumerates every |
| 14 | // service principal granted trusted access to the organization). Resolve the |
| 15 | // provider from context via the typed `Provider.findProvider`, call `list()`, |
| 16 | // and assert the result is a well-typed `Attributes[]`. |
| 17 | // |
| 18 | // `list()` is designed to degrade gracefully off the org management account: |
| 19 | // `listAWSServiceAccessForOrganization` rejects with |
| 20 | // `AWSOrganizationsNotInUseException` / `AccessDeniedException` when the caller |
| 21 | // isn't an org management/delegated account, which `list()` catches and maps to |
| 22 | // `[]`. So this case passes on any account — it just returns `[]` when the |
| 23 | // account can't enumerate trusted service access. |
| 24 | test.provider( |
| 25 | "list enumerates trusted service access", |
| 26 | () => |
| 27 | Effect.gen(function* () { |
| 28 | const provider = yield* Provider.findProvider(TrustedServiceAccess); |
| 29 | const all = yield* provider.list(); |
| 30 | |
| 31 | expect(Array.isArray(all)).toBe(true); |
| 32 | |
| 33 | for (const item of all) { |
| 34 | expect(typeof item.servicePrincipal).toBe("string"); |
| 35 | if (item.dateEnabled !== undefined) { |
| 36 | expect(item.dateEnabled).toBeInstanceOf(Date); |
| 37 | } |
| 38 | } |
| 39 | }), |
| 40 | { tags: ["provider:aws", "provider:aws:organizations", "live"] }, |
| 41 | ); |
| 42 | |
| 43 | // Full lifecycle list test — requires an org MANAGEMENT account. Gate behind an |
| 44 | // env var so an entitled account runs it unchanged. Off a management account |
| 45 | // `enableAWSServiceAccess` rejects with `AWSOrganizationsNotInUseException` / |
| 46 | // `AccessDeniedException`, so this is skipped by default. |
| 47 | const servicePrincipal = process.env.AWS_ORG_TRUSTED_SERVICE_PRINCIPAL ?? "config.amazonaws.com"; |
| 48 | |
| 49 | // Destroying the test's resource disables trusted access, so refuse to take |
| 50 | // over an enablement that existed before the test. |
| 51 | const requireTrustedAccessUnused = organizations.listAWSServiceAccessForOrganization.pages({}).pipe( |
| 52 | Stream.runCollect, |
| 53 | Effect.map((pages) => |
| 54 | Array.from(pages) |
| 55 | .flatMap((page) => page.EnabledServicePrincipals ?? []) |
| 56 | .some((item) => item.ServicePrincipal === servicePrincipal), |
| 57 | ), |
| 58 | Effect.flatMap((enabled) => |
| 59 | enabled |
| 60 | ? Effect.fail( |
| 61 | new Error( |
| 62 | `trusted access for ${servicePrincipal} is already enabled; set AWS_ORG_TRUSTED_SERVICE_PRINCIPAL to an unused principal`, |
| 63 | ), |
| 64 | ) |
| 65 | : Effect.void, |
| 66 | ), |
| 67 | ); |
| 68 | |
| 69 | // Both lifecycles own the same principal, so they must not overlap. |
| 70 | describe.sequential("trusted service access lifecycle", () => { |
| 71 | test.provider.skipIf(!process.env.AWS_ORG_MANAGEMENT_ACCOUNT)( |
| 72 | "list contains the deployed trusted service access", |
| 73 | (stack) => |
| 74 | Effect.gen(function* () { |
| 75 | yield* stack.destroy(); |
| 76 | yield* requireTrustedAccessUnused; |
| 77 | |
| 78 | const access = yield* stack.deploy( |
| 79 | Effect.gen(function* () { |
| 80 | return yield* TrustedServiceAccess("ListTrustedServiceAccess", { servicePrincipal }); |
| 81 | }), |
| 82 | ); |
| 83 | |
| 84 | const provider = yield* Provider.findProvider(TrustedServiceAccess); |
| 85 | const all = yield* provider.list(); |
| 86 | |
| 87 | expect(all.some((item) => item.servicePrincipal === access.servicePrincipal)).toBe(true); |
| 88 | |
| 89 | yield* stack.destroy(); |
| 90 | }), |
| 91 | { tags: ["provider:aws", "provider:aws:organizations", "live"] }, |
| 92 | ); |
| 93 | |
| 94 | // Regression test for https://github.com/alchemy-run/alchemy/issues/736. |
| 95 | // |
| 96 | // An interrupted first deploy persists the enablement as `status: "creating"` |
| 97 | // with no attributes and props that could not round-trip. Plan's |
| 98 | // creating-recovery branch then calls `provider.read` with those junk olds; |
| 99 | // pre-fix the `olds!.servicePrincipal` dereference crashed the plan when the |
| 100 | // persisted row carried no recoverable props at all (`BaseResourceState` |
| 101 | // declares `props?:` — a row whose Output-valued props failed to round-trip |
| 102 | // can surface with none). |
| 103 | // |
| 104 | // Simulate exactly that state row after a real deploy and assert the next |
| 105 | // deploy recovers: `read` reports "not found", the engine re-drives the |
| 106 | // create, and reconcile observes the access already enabled — SAME |
| 107 | // servicePrincipal and SAME enablement date, no re-enable. |
| 108 | test.provider.skipIf(!process.env.AWS_ORG_MANAGEMENT_ACCOUNT)( |
| 109 | "recovers a half-created trusted service access whose creating-state lost its props (#736)", |
| 110 | (stack) => |
| 111 | Effect.gen(function* () { |
| 112 | yield* stack.destroy(); |
| 113 | yield* requireTrustedAccessUnused; |
| 114 | |
| 115 | const deployAccess = () => |
| 116 | stack.deploy( |
| 117 | Effect.gen(function* () { |
| 118 | return yield* TrustedServiceAccess("WedgedTrustedAccess", { servicePrincipal }); |
| 119 | }), |
| 120 | ); |
| 121 | |
| 122 | const created = yield* deployAccess(); |
| 123 | |
| 124 | // Rewrite the persisted row into the wedged shape an interrupted |
| 125 | // deploy leaves behind: `creating`, no attributes, and no recoverable |
| 126 | // props (the realistic field-level loss — `servicePrincipal: |
| 127 | // undefined` — is tolerated even pre-fix because |
| 128 | // `readTrustedServiceAccess` is list-and-find; the crash this fix |
| 129 | // guards is the `olds!` deref on a row with no props at all). |
| 130 | const state = yield* yield* State; |
| 131 | const stage = stack.stage; |
| 132 | const fqns = yield* state.list({ stack: stack.name, stage }); |
| 133 | const rows = yield* Effect.forEach(fqns, (fqn) => |
| 134 | state.get({ stack: stack.name, stage, fqn }).pipe(Effect.map((row) => ({ fqn, row }))), |
| 135 | ); |
| 136 | const wedged = rows.find( |
| 137 | (r): r is { fqn: string; row: ResourceState } => |
| 138 | isResourceState(r.row) && |
| 139 | r.row.resourceType === "AWS.Organizations.TrustedServiceAccess", |
| 140 | ); |
| 141 | if (!wedged) { |
| 142 | return yield* Effect.die( |
| 143 | new Error("no AWS.Organizations.TrustedServiceAccess state row found after deploy"), |
| 144 | ); |
| 145 | } |
| 146 | yield* state.set({ |
| 147 | stack: stack.name, |
| 148 | stage, |
| 149 | fqn: wedged.fqn, |
| 150 | value: { |
| 151 | ...wedged.row, |
| 152 | status: "creating", |
| 153 | attr: undefined, |
| 154 | props: undefined, |
| 155 | // `CreatingResourceState` requires `props`, but persisted rows may |
| 156 | // lack it (`BaseResourceState.props?:`) — simulate that corruption. |
| 157 | } as unknown as ResourceState, |
| 158 | }); |
| 159 | |
| 160 | // Before the fix this crashed in plan with |
| 161 | // `TypeError: undefined is not an object (evaluating 'olds.servicePrincipal')`. |
| 162 | const recovered = yield* deployAccess(); |
| 163 | expect(recovered.servicePrincipal).toEqual(created.servicePrincipal); |
| 164 | // Same enablement observed — not disabled/re-enabled. |
| 165 | expect(recovered.dateEnabled?.getTime()).toEqual(created.dateEnabled?.getTime()); |
| 166 | |
| 167 | yield* stack.destroy(); |
| 168 | }), |
| 169 | { tags: ["provider:aws", "provider:aws:organizations", "live"], timeout: 240_000 }, |
| 170 | ); |
| 171 | }); |