The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as organizations from "@distilled.cloud/aws/organizations"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import type { Providers } from "../Providers.ts"; |
| 8 | import { collectPages, retryOrganizations } from "./common.ts"; |
| 9 | |
| 10 | export class TrustedServiceAccessUnexpectedResponse extends Data.TaggedError( |
| 11 | "TrustedServiceAccessUnexpectedResponse", |
| 12 | )<{ |
| 13 | readonly message: string; |
| 14 | readonly cause?: unknown; |
| 15 | }> {} |
| 16 | |
| 17 | export interface TrustedServiceAccessProps { |
| 18 | /** |
| 19 | * Service principal granted trusted access to the organization. |
| 20 | */ |
| 21 | servicePrincipal: string; |
| 22 | } |
| 23 | |
| 24 | export interface TrustedServiceAccess extends Resource< |
| 25 | "AWS.Organizations.TrustedServiceAccess", |
| 26 | TrustedServiceAccessProps, |
| 27 | { |
| 28 | /** |
| 29 | * Service principal granted trusted access. |
| 30 | */ |
| 31 | servicePrincipal: string; |
| 32 | /** |
| 33 | * When trusted access was enabled. |
| 34 | */ |
| 35 | dateEnabled: Date | undefined; |
| 36 | }, |
| 37 | never, |
| 38 | Providers |
| 39 | > {} |
| 40 | |
| 41 | /** |
| 42 | * Enables trusted access for an AWS service principal, allowing that service |
| 43 | * to operate across all accounts in the organization. |
| 44 | * |
| 45 | * Typically paired with a {@link DelegatedAdministrator} that hands day-to-day |
| 46 | * administration of the service to a member account. Existence-only resource: |
| 47 | * changing `servicePrincipal` replaces it. |
| 48 | * ### Enabling Trusted Access |
| 49 | * **Example:** Enable IAM Identity Center |
| 50 | * ```typescript |
| 51 | * yield* TrustedServiceAccess("SsoTrustedAccess", { |
| 52 | * servicePrincipal: "sso.amazonaws.com", |
| 53 | * }); |
| 54 | * ``` |
| 55 | * |
| 56 | * **Example:** Trusted Access Plus a Delegated Administrator |
| 57 | * ```typescript |
| 58 | * const guardDutyAccess = yield* TrustedServiceAccess("GuardDutyAccess", { |
| 59 | * servicePrincipal: "guardduty.amazonaws.com", |
| 60 | * }); |
| 61 | * |
| 62 | * yield* DelegatedAdministrator("GuardDutyAdmin", { |
| 63 | * accountId: securityAccount.accountId, |
| 64 | * servicePrincipal: guardDutyAccess.servicePrincipal, |
| 65 | * }); |
| 66 | * ``` |
| 67 | * |
| 68 | * @resource |
| 69 | */ |
| 70 | export const TrustedServiceAccess = Resource<TrustedServiceAccess>( |
| 71 | "AWS.Organizations.TrustedServiceAccess", |
| 72 | ); |
| 73 | |
| 74 | export const TrustedServiceAccessProvider = () => |
| 75 | Provider.effect( |
| 76 | TrustedServiceAccess, |
| 77 | Effect.gen(function* () { |
| 78 | return { |
| 79 | stables: ["servicePrincipal"], |
| 80 | diff: Effect.fn(function* ({ olds, news }) { |
| 81 | if (!isResolved(news)) return; |
| 82 | if (olds?.servicePrincipal !== news.servicePrincipal) { |
| 83 | return { action: "replace" } as const; |
| 84 | } |
| 85 | }), |
| 86 | read: Effect.fn(function* ({ olds, output }) { |
| 87 | const servicePrincipal = output?.servicePrincipal ?? olds?.servicePrincipal; |
| 88 | if (servicePrincipal === undefined) { |
| 89 | // Output-valued props don't survive a `creating`-state round-trip |
| 90 | // (they deserialize as `undefined`) — report "not found" so the |
| 91 | // engine re-drives the create. |
| 92 | return undefined; |
| 93 | } |
| 94 | return yield* readTrustedServiceAccess(servicePrincipal); |
| 95 | }), |
| 96 | list: () => |
| 97 | Effect.gen(function* () { |
| 98 | // Enumerate every service principal granted trusted access to the |
| 99 | // organization. The list response already carries the full `read` |
| 100 | // shape (servicePrincipal + dateEnabled), so each enabled principal |
| 101 | // maps directly to one Attributes — no per-item hydration needed. |
| 102 | const principals = yield* retryOrganizations( |
| 103 | collectPages( |
| 104 | (NextToken) => |
| 105 | organizations.listAWSServiceAccessForOrganization({ |
| 106 | NextToken, |
| 107 | }), |
| 108 | (page) => page.EnabledServicePrincipals, |
| 109 | ), |
| 110 | ); |
| 111 | |
| 112 | return principals |
| 113 | .filter( |
| 114 | ( |
| 115 | candidate, |
| 116 | ): candidate is organizations.EnabledServicePrincipal & { |
| 117 | ServicePrincipal: string; |
| 118 | } => candidate.ServicePrincipal != null, |
| 119 | ) |
| 120 | .map( |
| 121 | (candidate) => |
| 122 | ({ |
| 123 | servicePrincipal: candidate.ServicePrincipal, |
| 124 | dateEnabled: candidate.DateEnabled, |
| 125 | }) satisfies TrustedServiceAccess["Attributes"], |
| 126 | ); |
| 127 | }).pipe( |
| 128 | // Not an org management account (or lacking access) — there's no |
| 129 | // organization to enumerate, so degrade to an empty list. |
| 130 | Effect.catchTags({ |
| 131 | AWSOrganizationsNotInUseException: () => |
| 132 | Effect.succeed([] as TrustedServiceAccess["Attributes"][]), |
| 133 | AccessDeniedException: () => |
| 134 | Effect.succeed([] as TrustedServiceAccess["Attributes"][]), |
| 135 | }), |
| 136 | ), |
| 137 | reconcile: Effect.fn(function* ({ news, session }) { |
| 138 | // Observe — fetch live trusted-access state. We never trust prior |
| 139 | // `output` blindly; if access was disabled out-of-band we re-enable. |
| 140 | let state = yield* readTrustedServiceAccess(news.servicePrincipal); |
| 141 | |
| 142 | // Ensure — enable trusted access if it's missing. This API is |
| 143 | // effectively idempotent for already-enabled principals, but we |
| 144 | // gate the call to avoid unnecessary churn. |
| 145 | if (!state) { |
| 146 | yield* retryOrganizations( |
| 147 | organizations.enableAWSServiceAccess({ |
| 148 | ServicePrincipal: news.servicePrincipal, |
| 149 | }), |
| 150 | ); |
| 151 | state = yield* readTrustedServiceAccess(news.servicePrincipal); |
| 152 | if (!state) { |
| 153 | return yield* new TrustedServiceAccessUnexpectedResponse({ |
| 154 | message: `trusted service access '${news.servicePrincipal}' not found after create`, |
| 155 | }); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | yield* session.note(state.servicePrincipal); |
| 160 | return state; |
| 161 | }), |
| 162 | delete: Effect.fn(function* ({ output }) { |
| 163 | if (!(yield* readTrustedServiceAccess(output.servicePrincipal))) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | yield* retryOrganizations( |
| 168 | organizations.disableAWSServiceAccess({ |
| 169 | ServicePrincipal: output.servicePrincipal, |
| 170 | }), |
| 171 | ); |
| 172 | }), |
| 173 | }; |
| 174 | }), |
| 175 | ); |
| 176 | |
| 177 | const readTrustedServiceAccess = Effect.fn(function* (servicePrincipal: string) { |
| 178 | const principals = yield* retryOrganizations( |
| 179 | collectPages( |
| 180 | (NextToken) => organizations.listAWSServiceAccessForOrganization({ NextToken }), |
| 181 | (page) => page.EnabledServicePrincipals, |
| 182 | ), |
| 183 | ); |
| 184 | |
| 185 | const match = principals.find((candidate) => candidate.ServicePrincipal === servicePrincipal); |
| 186 | |
| 187 | return match |
| 188 | ? ({ |
| 189 | servicePrincipal, |
| 190 | dateEnabled: match.DateEnabled, |
| 191 | } satisfies TrustedServiceAccess["Attributes"]) |
| 192 | : undefined; |
| 193 | }); |