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 organizations from "@distilled.cloud/aws/organizations"; |
| 2 | import * as Provider from "alchemy/Provider"; |
| 3 | import { Resource } from "alchemy/Resource"; |
| 4 | import * as Data from "effect/Data"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import type { PolicyDocument } from "../IAM/Policy.ts"; |
| 7 | import type { Providers } from "../Providers.ts"; |
| 8 | import { retryOrganizations } from "./common.ts"; |
| 9 | |
| 10 | export class OrganizationResourcePolicyUnexpectedResponse extends Data.TaggedError( |
| 11 | "OrganizationResourcePolicyUnexpectedResponse", |
| 12 | )<{ |
| 13 | readonly message: string; |
| 14 | readonly cause?: unknown; |
| 15 | }> {} |
| 16 | |
| 17 | export interface OrganizationResourcePolicyProps { |
| 18 | /** |
| 19 | * Typed resource policy document for the organization. |
| 20 | */ |
| 21 | document: PolicyDocument; |
| 22 | } |
| 23 | |
| 24 | export interface OrganizationResourcePolicy extends Resource< |
| 25 | "AWS.Organizations.OrganizationResourcePolicy", |
| 26 | OrganizationResourcePolicyProps, |
| 27 | { |
| 28 | /** |
| 29 | * ID of the resource policy. |
| 30 | */ |
| 31 | resourcePolicyId: string; |
| 32 | /** |
| 33 | * ARN of the resource policy. |
| 34 | */ |
| 35 | resourcePolicyArn: string; |
| 36 | /** |
| 37 | * Parsed resource policy document as stored by AWS Organizations. |
| 38 | */ |
| 39 | document: PolicyDocument; |
| 40 | }, |
| 41 | never, |
| 42 | Providers |
| 43 | > {} |
| 44 | |
| 45 | /** |
| 46 | * The singleton AWS Organizations resource policy — an org-level |
| 47 | * resource-based policy that grants other principals (typically delegated |
| 48 | * administrator accounts) permission to call Organizations APIs. |
| 49 | * |
| 50 | * There is at most one per organization; Alchemy adopts and reconciles the |
| 51 | * existing policy if one is already in place. |
| 52 | * ### Setting the Resource Policy |
| 53 | * **Example:** Allow a Member Account to Describe the Organization |
| 54 | * ```typescript |
| 55 | * const security = yield* Account("Security", { |
| 56 | * name: "security", |
| 57 | * email: "aws-security@example.com", |
| 58 | * parentId: root.rootId, |
| 59 | * }); |
| 60 | * |
| 61 | * yield* OrganizationResourcePolicy("OrgResourcePolicy", { |
| 62 | * document: { |
| 63 | * Version: "2012-10-17", |
| 64 | * Statement: [ |
| 65 | * { |
| 66 | * Effect: "Allow", |
| 67 | * Principal: { AWS: security.accountId }, |
| 68 | * Action: [ |
| 69 | * "organizations:DescribeOrganization", |
| 70 | * "organizations:ListAccounts", |
| 71 | * ], |
| 72 | * Resource: "*", |
| 73 | * }, |
| 74 | * ], |
| 75 | * }, |
| 76 | * }); |
| 77 | * ``` |
| 78 | * |
| 79 | * @resource |
| 80 | */ |
| 81 | export const OrganizationResourcePolicy = Resource<OrganizationResourcePolicy>( |
| 82 | "AWS.Organizations.OrganizationResourcePolicy", |
| 83 | ); |
| 84 | |
| 85 | const readResourcePolicy = () => |
| 86 | retryOrganizations( |
| 87 | organizations.describeResourcePolicy({}).pipe( |
| 88 | Effect.map((response) => response.ResourcePolicy), |
| 89 | Effect.catchTag("ResourcePolicyNotFoundException", () => Effect.undefined), |
| 90 | Effect.map((policy) => { |
| 91 | const summary = policy?.ResourcePolicySummary; |
| 92 | return summary?.Id && summary.Arn |
| 93 | ? ({ |
| 94 | resourcePolicyId: summary.Id, |
| 95 | resourcePolicyArn: summary.Arn, |
| 96 | document: JSON.parse(policy?.Content ?? "{}") as PolicyDocument, |
| 97 | } satisfies OrganizationResourcePolicy["Attributes"]) |
| 98 | : undefined; |
| 99 | }), |
| 100 | ), |
| 101 | ); |
| 102 | |
| 103 | export const OrganizationResourcePolicyProvider = () => |
| 104 | Provider.effect( |
| 105 | OrganizationResourcePolicy, |
| 106 | Effect.gen(function* () { |
| 107 | return { |
| 108 | stables: ["resourcePolicyId", "resourcePolicyArn"], |
| 109 | diff: Effect.fn(function* () {}), |
| 110 | read: Effect.fn(function* () { |
| 111 | return yield* readResourcePolicy(); |
| 112 | }), |
| 113 | // Org singleton: there is no list API. Describe the single resource |
| 114 | // policy and return a one-element array if it exists, else []. A |
| 115 | // missing policy or an account that isn't an org both yield []. |
| 116 | list: () => |
| 117 | readResourcePolicy().pipe( |
| 118 | Effect.map((policy) => (policy ? [policy] : [])), |
| 119 | Effect.catchTag("AWSOrganizationsNotInUseException", () => |
| 120 | Effect.succeed([] as OrganizationResourcePolicy["Attributes"][]), |
| 121 | ), |
| 122 | ), |
| 123 | reconcile: Effect.fn(function* ({ news, session }) { |
| 124 | const desiredContent = JSON.stringify(news.document); |
| 125 | |
| 126 | // Observe — fetch the live resource policy (or absence). |
| 127 | let state = yield* readResourcePolicy(); |
| 128 | |
| 129 | // Sync — `putResourcePolicy` is a single upsert that handles both |
| 130 | // first-create and update. We diff observed content against |
| 131 | // desired so the call only fires when there's drift. Reading by |
| 132 | // ID isn't possible (resource is a singleton with a server-issued |
| 133 | // ID), so we compare the JSON-stringified document. |
| 134 | const observedContent = state ? JSON.stringify(state.document) : undefined; |
| 135 | |
| 136 | if (observedContent !== desiredContent) { |
| 137 | yield* retryOrganizations( |
| 138 | organizations.putResourcePolicy({ |
| 139 | Content: desiredContent, |
| 140 | }), |
| 141 | ); |
| 142 | state = yield* readResourcePolicy(); |
| 143 | } |
| 144 | |
| 145 | if (!state) { |
| 146 | return yield* new OrganizationResourcePolicyUnexpectedResponse({ |
| 147 | message: "organization resource policy not found after reconcile", |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | yield* session.note(state.resourcePolicyArn); |
| 152 | return state; |
| 153 | }), |
| 154 | delete: Effect.fn(function* () { |
| 155 | yield* retryOrganizations( |
| 156 | organizations |
| 157 | .deleteResourcePolicy({}) |
| 158 | .pipe(Effect.catchTag("ResourcePolicyNotFoundException", () => Effect.void)), |
| 159 | ); |
| 160 | }), |
| 161 | }; |
| 162 | }), |
| 163 | ); |