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 { AWSEnvironment } from "@alchemy.run/aws/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as organizations from "@distilled.cloud/aws/organizations"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as OrganizationLease from "./OrganizationLease.ts"; |
| 8 | |
| 9 | // Exclusive: the local lifecycle creates and deletes the account's single |
| 10 | // organization, so no other organization suite may hold it meanwhile. |
| 11 | const { test } = OrganizationLease.make({ providers: AWS.providers() }, "exclusive"); |
| 12 | |
| 13 | // Account singleton: an AWS Organization has no list API. `list()` calls |
| 14 | // `describeOrganization` and returns the single org as a one-element array, or |
| 15 | // `[]` when the account isn't a management account (the typed |
| 16 | // `AWSOrganizationsNotInUseException` is caught to `[]`). This runs read-only — |
| 17 | // it neither creates nor deletes an organization. |
| 18 | test.provider( |
| 19 | "list returns the organization singleton", |
| 20 | (stack) => |
| 21 | Effect.gen(function* () { |
| 22 | const provider = yield* Provider.findProvider(AWS.Organizations.Organization); |
| 23 | const all = yield* provider.list(); |
| 24 | |
| 25 | // 0 (account is not a management account) or 1 (it is) — never more. |
| 26 | expect(all.length).toBeLessThanOrEqual(1); |
| 27 | |
| 28 | // When the account is an organization management account, the single |
| 29 | // entry carries a well-typed Attributes shape. |
| 30 | if (all.length === 1) { |
| 31 | const org = all[0]; |
| 32 | expect(typeof org.organizationId).toBe("string"); |
| 33 | expect(org.organizationId.length).toBeGreaterThan(0); |
| 34 | expect(typeof org.organizationArn).toBe("string"); |
| 35 | expect(org.organizationArn.startsWith("arn:aws:organizations::")).toBe(true); |
| 36 | expect(Array.isArray(org.availablePolicyTypes)).toBe(true); |
| 37 | if (org.managementAccountEmail != null) { |
| 38 | expect(typeof org.managementAccountEmail).toBe("string"); |
| 39 | expect(org.managementAccountEmail.length).toBeGreaterThan(0); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | yield* stack.destroy(); |
| 44 | }), |
| 45 | { tags: ["provider:aws", "provider:aws:organizations", "live"] }, |
| 46 | ); |
| 47 | |
| 48 | // Creating and deleting an organization is safe only in the local emulator. |
| 49 | test.provider.skipIf(process.env.ALCHEMY_TEST_DEV !== "1")( |
| 50 | "local organization resources create, update, and clean up", |
| 51 | (stack) => |
| 52 | Effect.gen(function* () { |
| 53 | const environment = yield* AWSEnvironment.current; |
| 54 | expect(environment.endpoint).toBe("http://localhost:4566"); |
| 55 | expect(environment.accountId).toBe("000000000000"); |
| 56 | yield* stack.destroy(); |
| 57 | |
| 58 | const organization = () => |
| 59 | AWS.Organizations.Organization("Organization", { featureSet: "ALL" }); |
| 60 | const created = yield* stack.deploy(organization()); |
| 61 | expect(created.managementAccountId).toBe("000000000000"); |
| 62 | |
| 63 | const member = yield* organizations.createAccount({ |
| 64 | AccountName: "floci-local-member", |
| 65 | Email: "floci-local-member@example.com", |
| 66 | }); |
| 67 | const accountId = member.CreateAccountStatus?.AccountId; |
| 68 | expect(accountId).toBeDefined(); |
| 69 | |
| 70 | const resources = (phase: string) => |
| 71 | Effect.gen(function* () { |
| 72 | yield* organization(); |
| 73 | const root = yield* AWS.Organizations.Root("Root", { tags: { phase } }); |
| 74 | const unit = yield* AWS.Organizations.OrganizationalUnit("Unit", { |
| 75 | parentId: root.rootId, |
| 76 | tags: { phase }, |
| 77 | }); |
| 78 | const enabled = yield* AWS.Organizations.RootPolicyType("PolicyType", { |
| 79 | rootId: root.rootId, |
| 80 | policyType: "TAG_POLICY", |
| 81 | }); |
| 82 | const policy = yield* AWS.Organizations.Policy("Policy", { |
| 83 | type: enabled.policyType, |
| 84 | description: phase, |
| 85 | document: JSON.stringify({ tags: {} }), |
| 86 | tags: { phase }, |
| 87 | }); |
| 88 | yield* AWS.Organizations.PolicyAttachment("Attachment", { |
| 89 | policyId: policy.policyId, |
| 90 | targetId: unit.ouId, |
| 91 | }); |
| 92 | const trusted = yield* AWS.Organizations.TrustedServiceAccess("Trusted", { |
| 93 | servicePrincipal: "cloudtrail.amazonaws.com", |
| 94 | }); |
| 95 | yield* AWS.Organizations.DelegatedAdministrator("Administrator", { |
| 96 | accountId: accountId!, |
| 97 | servicePrincipal: trusted.servicePrincipal, |
| 98 | }); |
| 99 | const resourcePolicy = yield* AWS.Organizations.OrganizationResourcePolicy( |
| 100 | "ResourcePolicy", |
| 101 | { |
| 102 | document: { |
| 103 | Version: "2012-10-17", |
| 104 | Statement: [ |
| 105 | { |
| 106 | Effect: "Allow", |
| 107 | Principal: { AWS: accountId! }, |
| 108 | Action: [ |
| 109 | phase === "created" |
| 110 | ? "organizations:DescribeOrganization" |
| 111 | : "organizations:ListAccounts", |
| 112 | ], |
| 113 | Resource: "*", |
| 114 | }, |
| 115 | ], |
| 116 | }, |
| 117 | }, |
| 118 | ); |
| 119 | return { root, unit, policy, resourcePolicy }; |
| 120 | }); |
| 121 | |
| 122 | yield* Effect.gen(function* () { |
| 123 | const first = yield* stack.deploy(resources("created")); |
| 124 | const updated = yield* stack.deploy(resources("updated")); |
| 125 | expect(updated.unit.ouId).toBe(first.unit.ouId); |
| 126 | expect(updated.policy.policyId).toBe(first.policy.policyId); |
| 127 | const tags = yield* organizations.listTagsForResource({ ResourceId: updated.unit.ouId }); |
| 128 | expect(tags.Tags).toContainEqual({ Key: "phase", Value: "updated" }); |
| 129 | const policy = yield* organizations.describePolicy({ PolicyId: updated.policy.policyId }); |
| 130 | expect(policy.Policy?.PolicySummary?.Description).toBe("updated"); |
| 131 | const attachment = yield* organizations.listTargetsForPolicy({ |
| 132 | PolicyId: updated.policy.policyId, |
| 133 | }); |
| 134 | expect(attachment.Targets?.map((target) => target.TargetId)).toContain(updated.unit.ouId); |
| 135 | const administrators = yield* organizations.listDelegatedAdministrators({ |
| 136 | ServicePrincipal: "cloudtrail.amazonaws.com", |
| 137 | }); |
| 138 | expect(administrators.DelegatedAdministrators?.map((admin) => admin.Id)).toContain( |
| 139 | accountId, |
| 140 | ); |
| 141 | const observed = yield* organizations.describeResourcePolicy({}); |
| 142 | expect(JSON.parse(observed.ResourcePolicy!.Content!)).toEqual( |
| 143 | updated.resourcePolicy.document, |
| 144 | ); |
| 145 | }).pipe( |
| 146 | Effect.ensuring( |
| 147 | Effect.gen(function* () { |
| 148 | // Remove dependent resources before the member and organization. |
| 149 | yield* stack.deploy(organization()); |
| 150 | yield* organizations.removeAccountFromOrganization({ AccountId: accountId! }); |
| 151 | yield* stack.destroy(); |
| 152 | }).pipe(Effect.orDie), |
| 153 | ), |
| 154 | ); |
| 155 | const removed = yield* organizations.describeOrganization({}).pipe( |
| 156 | Effect.map(() => false), |
| 157 | Effect.catchTag("AWSOrganizationsNotInUseException", () => Effect.succeed(true)), |
| 158 | ); |
| 159 | expect(removed).toBe(true); |
| 160 | }), |
| 161 | { timeout: 120_000, tags: ["provider:aws", "provider:aws:organizations", "local"] }, |
| 162 | ); |