@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 70 lines 2.94 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import { pinFms } from "./AdminAccount.ts";
4
5 /**
6 * Shared HTTP scaffolding for the AWS Firewall Manager runtime bindings.
7 *
8 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
9 * thin `Layer.effect(Cap, makeFmsHttpBinding({ … }))` over the builder
10 * below. Everything except the operation and the IAM action is boilerplate.
11 *
12 * All Firewall Manager operations are account-level: they act on the
13 * policies, lists, resource sets, and compliance data of the calling
14 * (administrator) account rather than on one bound Alchemy resource, so the
15 * builder grants the IAM actions on `Resource: ["*"]` and the runtime
16 * callable takes the raw request.
17 *
18 * Policy, list, resource-set, and compliance operations are regional (a
19 * Firewall Manager policy lives in the region it was created in), so they
20 * resolve against the ambient region. The organization-level administrator
21 * management reads (`GetAdminScope`, `ListAdminAccountsForOrganization`,
22 * `ListAdminsManagingAccount`) belong to the same global admin-account
23 * family as the {@link AdminAccount} resource APIs, so they are pinned to
24 * the us-east-1 endpoint via {@link pinFms}.
25 */
26 export const makeFmsHttpBinding = <I extends object, A, E, R>(options: {
27 /**
28 * Short capability name used in the binding sid and runtime span, e.g.
29 * `"ListPolicies"`.
30 */
31 capability: string;
32 /** IAM actions granted on `Resource: ["*"]`. */
33 iamActions: readonly string[];
34 /** The distilled operation implementing the capability. */
35 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
36 /**
37 * Pin the operation to the us-east-1 endpoint — set for the
38 * organization-level administrator-management operations, which are
39 * served from the global admin-account endpoint like the
40 * {@link AdminAccount} resource APIs.
41 */
42 pinToAdminRegion?: boolean;
43 }) =>
44 Effect.gen(function* () {
45 const bind = yield* AWSBindings.Bind;
46 const op = options.pinToAdminRegion
47 ? yield* options.operation.pipe(pinFms, AWSBindings.withCredentials)
48 : yield* AWSBindings.withCredentials(options.operation);
49
50 return Effect.fn(function* () {
51 if (!globalThis.__ALCHEMY_RUNTIME__) {
52 yield* bind`AWS.FMS.${options.capability}()`({
53 policyStatements: [
54 {
55 Effect: "Allow",
56 Action: [...options.iamActions],
57 Resource: ["*"],
58 },
59 ],
60 });
61 }
62 return Effect.fn(`AWS.FMS.${options.capability}`)(function* (request?: I) {
63 // The admin-region pin must also be applied at the call site: the
64 // yield-time snapshot is only a fallback — the calling fiber's
65 // ambient Region (the host Function's own region) wins over it.
66 const call = op((request ?? {}) as I);
67 return yield* options.pinToAdminRegion ? pinFms(call) : call;
68 });
69 });
70 });