@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 187 lines 7.54 KB
Raw
1 import * as fms from "@distilled.cloud/aws/fms";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import * as Schedule from "effect/Schedule";
7 import { AWSEnvironment } from "../Environment.ts";
8 import type { Providers } from "../Providers.ts";
9 import { Region } from "../Region.ts";
10
11 // FMS admin-account management (get/associate/disassociate AdminAccount) is a
12 // global, organization-level operation served exclusively from the us-east-1
13 // endpoint — every other region rejects it with `InvalidOperationException:
14 // This operation is not supported in the '<region>' region.` Pin every
15 // control-plane call there regardless of the ambient stack region (same
16 // pattern as Cost Explorer / ECR Public / WAFv2 CloudFront-scope).
17 const FMS_ADMIN_REGION = "us-east-1";
18
19 /** Pin an FMS admin-account operation to the us-east-1 endpoint. */
20 export const pinFms = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
21 effect.pipe(Effect.provideService(Region, Effect.succeed(FMS_ADMIN_REGION)));
22
23 export interface AdminAccountProps {
24 /**
25 * The AWS account ID to designate as the AWS Firewall Manager administrator
26 * account for the organization. Must be an account in the same AWS
27 * Organization as the caller (which must be the Organizations management
28 * account). If omitted, the caller's account is used.
29 */
30 adminAccount?: string;
31 }
32
33 /** @resource */
34 export interface AdminAccount extends Resource<
35 "AWS.FMS.AdminAccount",
36 AdminAccountProps,
37 {
38 /** The account designated as the FMS administrator. */
39 adminAccount: string;
40 /** Status of the FMS administrator IAM role (`READY` / `CREATING` / ...). */
41 roleStatus: string | undefined;
42 },
43 never,
44 Providers
45 > {}
46
47 /**
48 * The AWS Firewall Manager administrator account — an organization-level
49 * singleton that designates which account manages FMS security policies.
50 *
51 * :::caution
52 * FMS requires the caller to be the AWS Organizations **management account**,
53 * and the organization must have AWS Config enabled. On accounts that are not
54 * an Organizations management account this resource fails at association time
55 * with a typed `InvalidOperationException`.
56 *
57 * FMS admin-account APIs are served only from the us-east-1 endpoint; this
58 * resource pins every control-plane call there regardless of the ambient
59 * stack region.
60 * :::
61 *
62 * This is a capture-and-restore singleton: FMS exposes no tags, so ownership is
63 * tracked by Alchemy state — adopting a pre-existing admin account that Alchemy
64 * did not create requires `--adopt`, and destroy disassociates the admin.
65 *
66 * ### Designating the FMS admin
67 * **Example:** Designate the caller as the FMS admin
68 * ```typescript
69 * const admin = yield* FMS.AdminAccount("FmsAdmin", {});
70 * ```
71 *
72 * **Example:** Designate a specific member account
73 * ```typescript
74 * const admin = yield* FMS.AdminAccount("FmsAdmin", {
75 * adminAccount: "123456789012",
76 * });
77 * ```
78 */
79 const AdminAccountResource = Resource<AdminAccount>("AWS.FMS.AdminAccount");
80
81 export { AdminAccountResource as AdminAccount };
82
83 // `getAdminAccount` throws `ResourceNotFoundException` when no FMS admin has
84 // been designated — collapse to `undefined`.
85 const getAdmin = pinFms(fms.getAdminAccount({})).pipe(
86 Effect.map((r) => r as fms.GetAdminAccountResponse | undefined),
87 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
88 );
89
90 export const AdminAccountProvider = () =>
91 Provider.effect(
92 AdminAccountResource,
93 Effect.gen(function* () {
94 // Association is asynchronous; the FMS role transitions CREATING → READY.
95 const waitUntilReady = getAdmin.pipe(
96 Effect.repeat({
97 schedule: Schedule.spaced("3 seconds"),
98 until: (a) => a?.RoleStatus === "READY",
99 times: 20,
100 }),
101 );
102
103 return {
104 read: Effect.fn(function* ({ output }) {
105 const admin = yield* getAdmin;
106 if (!admin?.AdminAccount) return undefined;
107 const attrs = {
108 adminAccount: admin.AdminAccount,
109 roleStatus: admin.RoleStatus,
110 };
111 // FMS admin has no tags — ownership can't be verified from the cloud.
112 // With no prior state, treat an existing admin as foreign.
113 return output ? attrs : Unowned(attrs);
114 }),
115
116 // Organization-level singleton — report the single admin account.
117 list: () =>
118 getAdmin.pipe(
119 Effect.map((a) =>
120 a?.AdminAccount ? [{ adminAccount: a.AdminAccount, roleStatus: a.RoleStatus }] : [],
121 ),
122 ),
123
124 reconcile: Effect.fn(function* ({ news = {}, session }) {
125 const { accountId } = yield* AWSEnvironment.current;
126 const desired = news.adminAccount ?? accountId;
127
128 // 1. OBSERVE
129 let admin = yield* getAdmin;
130
131 // 2. ENSURE — associate the admin account if none is set.
132 if (!admin?.AdminAccount) {
133 // On a fresh account the first associate call kicks off creation
134 // of the AWSServiceRoleForFMS service-linked role and fails
135 // transiently while it materializes — either with a bare
136 // `InvalidOperationException: Operation is invalid.` or with
137 // `Service role name AWSServiceRoleForFMS already exists ...
138 // Otherwise, try your request again.` Both are eventual-
139 // consistency races, not terminal failures — retry bounded.
140 yield* pinFms(fms.associateAdminAccount({ AdminAccount: desired })).pipe(
141 Effect.retry({
142 // `: boolean` blocks TS 5.5+ inferred type predicates — see
143 // the note on the disassociate retry in `delete` below.
144 while: (e): boolean =>
145 e._tag === "InvalidOperationException" &&
146 (e.message === "Operation is invalid." ||
147 (e.message?.includes("AWSServiceRoleForFMS") ?? false)),
148 schedule: Schedule.spaced("5 seconds"),
149 times: 8,
150 }),
151 );
152 admin = yield* waitUntilReady;
153 }
154
155 // 3. RETURN fresh attributes.
156 const final = yield* getAdmin;
157 yield* session.note(desired);
158 return {
159 adminAccount: final?.AdminAccount ?? desired,
160 roleStatus: final?.RoleStatus,
161 };
162 }),
163
164 delete: Effect.fn(function* () {
165 // For several minutes after association FMS reports the org as
166 // "currently onboarding with AWS Firewall Manager and cannot be
167 // offboarded" (a typed `InvalidOperationException`). Retry bounded;
168 // onboarding can take >10 minutes, so a destroy issued immediately
169 // after create may need to be re-run once onboarding settles.
170 yield* pinFms(fms.disassociateAdminAccount({})).pipe(
171 Effect.retry({
172 // `: boolean` prevents TS 5.5+ from inferring a type predicate
173 // for this lambda — an inferred `e is InvalidOperationException`
174 // refinement makes `Effect.retry` collapse the error channel to
175 // `unknown`, which breaks the `catchTag` below.
176 while: (e): boolean =>
177 e._tag === "InvalidOperationException" &&
178 (e.message?.includes("cannot be offboarded") ?? false),
179 schedule: Schedule.spaced("8 seconds"),
180 times: 8,
181 }),
182 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
183 );
184 }),
185 };
186 }),
187 );