The best way to control Claude Code, Codex and more from any computer or phone

main
ts 125 lines 4.32 KB
Raw
1 import type * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { EnabledBaseline } from "./EnabledBaseline.ts";
5 import type { EnabledControl } from "./EnabledControl.ts";
6 import type { LandingZone } from "./LandingZone.ts";
7
8 /**
9 * Shared HTTP scaffolding for the AWS Control Tower runtime bindings.
10 *
11 * Every capability follows the same shape — resolve the distilled operation,
12 * register an IAM policy statement on the binding host, and return a runtime
13 * callable. The only variation is the operation, the IAM action(s), and
14 * whether the binding is scoped to one Control Tower resource (injecting its
15 * identifier ARN into the request) or to the whole account (the baseline
16 * catalog, list, and operation-status APIs, which are not resource-scoped).
17 *
18 * @internal — not exported from `index.ts`.
19 */
20
21 /** The Control Tower resources that resource-scoped bindings can target. */
22 export type ControlTowerBindable = EnabledControl | EnabledBaseline | LandingZone;
23
24 /**
25 * Build the implementation effect for a resource-scoped capability: the
26 * runtime callable injects the bound resource's identifier ARN as the
27 * request's `requestKey` field, and the deploy-time half grants `iamActions`
28 * on the resource's ARN.
29 */
30 export const makeControlTowerHttpBinding = <
31 Res extends ControlTowerBindable,
32 I extends object,
33 K extends keyof I & string,
34 A,
35 E,
36 R,
37 IdReq = never,
38 >(options: {
39 /**
40 * Short capability name used in the binding sid and runtime span, e.g.
41 * `"ResetEnabledControl"`.
42 */
43 capability: string;
44 /**
45 * IAM actions granted on the resource's ARN, e.g.
46 * `["controltower:ResetEnabledControl"]`.
47 */
48 iamActions: readonly string[];
49 /** The request field the resolved identifier ARN is injected as. */
50 requestKey: K;
51 /** Resolve the injected identifier ARN from the bound resource. */
52 identifier: (resource: Res) => Output.Output<string, IdReq>;
53 /** The distilled operation; `requestKey` is injected from the resource. */
54 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
55 }) =>
56 Effect.gen(function* () {
57 const bind = yield* AWSBindings.Bind;
58 const op = yield* AWSBindings.withCredentials(options.operation);
59
60 return Effect.fn(function* (resource: Res) {
61 const Identifier = yield* options.identifier(resource);
62 if (!globalThis.__ALCHEMY_RUNTIME__) {
63 yield* bind`AWS.ControlTower.${options.capability}(${resource})`({
64 policyStatements: [
65 {
66 Effect: "Allow",
67 Action: [...options.iamActions],
68 Resource: [options.identifier(resource)],
69 },
70 ],
71 });
72 }
73 return Effect.fn(`AWS.ControlTower.${options.capability}(${resource.LogicalId})`)(function* (
74 request?: Omit<I, K>,
75 ) {
76 return yield* op({
77 ...request,
78 [options.requestKey]: yield* Identifier,
79 } as I);
80 });
81 });
82 });
83
84 /**
85 * Build the implementation effect for an account-level capability (no
86 * resource argument — the baseline catalog, list, and operation-status
87 * APIs). The deploy-time half grants `iamActions` on `Resource: ["*"]`
88 * because these operations are not resource-scoped.
89 */
90 export const makeControlTowerAccountHttpBinding = <I extends object, A, E, R>(options: {
91 /**
92 * Short capability name used in the binding sid and runtime span, e.g.
93 * `"ListBaselines"`.
94 */
95 capability: string;
96 /**
97 * IAM actions granted on `Resource: ["*"]`.
98 */
99 iamActions: readonly string[];
100 /**
101 * The distilled operation implementing the capability.
102 */
103 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
104 }) =>
105 Effect.gen(function* () {
106 const bind = yield* AWSBindings.Bind;
107 const op = yield* AWSBindings.withCredentials(options.operation);
108
109 return Effect.fn(function* () {
110 if (!globalThis.__ALCHEMY_RUNTIME__) {
111 yield* bind`AWS.ControlTower.${options.capability}()`({
112 policyStatements: [
113 {
114 Effect: "Allow",
115 Action: [...options.iamActions],
116 Resource: ["*"],
117 },
118 ],
119 });
120 }
121 return Effect.fn(`AWS.ControlTower.${options.capability}`)(function* (request?: I) {
122 return yield* op((request ?? {}) as I);
123 });
124 });
125 });