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

main
ts 213 lines 7.52 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { Endpoint } from "./Endpoint.ts";
5 import type { ReplicationInstance } from "./ReplicationInstance.ts";
6
7 /**
8 * Shared HTTP scaffolding for the DMS runtime bindings.
9 *
10 * Every capability follows the same shape — resolve the distilled operation,
11 * register an IAM policy statement on the binding host, and return a runtime
12 * callable. The only variation is the operation, the IAM action(s), and the
13 * identifier(s) injected from the bound resource: an {@link Endpoint}'s ARN,
14 * a {@link ReplicationInstance}'s ARN, both (connection-shaped operations
15 * like `TestConnection`/`RefreshSchemas`), or none (account-level describes).
16 *
17 * DMS `Describe*` actions do not support resource-level IAM permissions, so
18 * describe-shaped capabilities grant on `*`; mutating capabilities
19 * (`TestConnection`, `RefreshSchemas`, `RebootReplicationInstance`) grant on
20 * the bound resource ARN(s).
21 *
22 * NOT exported from `index.ts`.
23 */
24
25 /**
26 * Build the impl Effect for an endpoint-scoped operation: the runtime
27 * callable injects the bound {@link Endpoint}'s ARN as `EndpointArn`. DMS
28 * describe actions are not resource-scoped in IAM, so the deploy-time half
29 * grants `actions` on `*`.
30 */
31 export const makeDmsEndpointScopedHttpBinding = <
32 I extends { EndpointArn: string },
33 A,
34 E,
35 R,
36 >(options: {
37 /** Fully-qualified binding tag, e.g. `AWS.DMS.DescribeSchemas`. */
38 tag: string;
39 /** The distilled operation; `EndpointArn` is injected from the resource. */
40 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
41 /** IAM actions granted on `*` (DMS describes are not resource-scoped). */
42 actions: readonly string[];
43 }) =>
44 Effect.gen(function* () {
45 const bind = yield* AWSBindings.Bind;
46 const op = yield* AWSBindings.withCredentials(options.operation);
47
48 return Effect.fn(function* (endpoint: Endpoint) {
49 const EndpointArn = yield* endpoint.endpointArn;
50 if (!globalThis.__ALCHEMY_RUNTIME__) {
51 yield* bind`${options.tag}(${endpoint})`({
52 policyStatements: [
53 {
54 Effect: "Allow",
55 Action: [...options.actions],
56 Resource: ["*"],
57 },
58 ],
59 });
60 }
61 return Effect.fn(`${options.tag}(${endpoint.LogicalId})`)(function* (
62 request?: Omit<I, "EndpointArn">,
63 ) {
64 return yield* op({
65 ...request,
66 EndpointArn: yield* EndpointArn,
67 } as I);
68 });
69 });
70 });
71
72 /**
73 * Build the impl Effect for a replication-instance-scoped operation: the
74 * runtime callable injects the bound {@link ReplicationInstance}'s ARN as
75 * `ReplicationInstanceArn`. Mutating actions (`RebootReplicationInstance`)
76 * support resource-level IAM and grant on the instance ARN; describe actions
77 * grant on `*` (set `iam: "wildcard"`).
78 */
79 export const makeDmsInstanceScopedHttpBinding = <
80 I extends { ReplicationInstanceArn: string },
81 A,
82 E,
83 R,
84 >(options: {
85 /** Fully-qualified binding tag, e.g. `AWS.DMS.RebootReplicationInstance`. */
86 tag: string;
87 /** The distilled operation; `ReplicationInstanceArn` is injected. */
88 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
89 /** IAM actions granted. */
90 actions: readonly string[];
91 /**
92 * `"resource"` grants on the instance ARN (actions with resource-level IAM
93 * support); `"wildcard"` grants on `*` (DMS describes).
94 */
95 iam: "resource" | "wildcard";
96 }) =>
97 Effect.gen(function* () {
98 const bind = yield* AWSBindings.Bind;
99 const op = yield* AWSBindings.withCredentials(options.operation);
100
101 return Effect.fn(function* (instance: ReplicationInstance) {
102 const ReplicationInstanceArn = yield* instance.replicationInstanceArn;
103 if (!globalThis.__ALCHEMY_RUNTIME__) {
104 yield* bind`${options.tag}(${instance})`({
105 policyStatements: [
106 {
107 Effect: "Allow",
108 Action: [...options.actions],
109 Resource:
110 options.iam === "resource"
111 ? [Output.interpolate`${instance.replicationInstanceArn}`]
112 : ["*"],
113 },
114 ],
115 });
116 }
117 return Effect.fn(`${options.tag}(${instance.LogicalId})`)(function* (
118 request?: Omit<I, "ReplicationInstanceArn">,
119 ) {
120 return yield* op({
121 ...request,
122 ReplicationInstanceArn: yield* ReplicationInstanceArn,
123 } as I);
124 });
125 });
126 });
127
128 /**
129 * Build the impl Effect for a connection-shaped operation that targets a
130 * (replication instance, endpoint) pair — `TestConnection` and
131 * `RefreshSchemas`. Both ARNs are injected from the bound resources and the
132 * deploy-time half grants `actions` on both ARNs (these actions support
133 * resource-level IAM on both resource types).
134 */
135 export const makeDmsConnectionScopedHttpBinding = <
136 I extends { ReplicationInstanceArn: string; EndpointArn: string },
137 A,
138 E,
139 R,
140 >(options: {
141 /** Fully-qualified binding tag, e.g. `AWS.DMS.TestConnection`. */
142 tag: string;
143 /** The distilled operation; both ARNs are injected from the resources. */
144 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
145 /** IAM actions granted on the instance + endpoint ARNs. */
146 actions: readonly string[];
147 }) =>
148 Effect.gen(function* () {
149 const bind = yield* AWSBindings.Bind;
150 const op = yield* AWSBindings.withCredentials(options.operation);
151
152 return Effect.fn(function* (instance: ReplicationInstance, endpoint: Endpoint) {
153 const ReplicationInstanceArn = yield* instance.replicationInstanceArn;
154 const EndpointArn = yield* endpoint.endpointArn;
155 if (!globalThis.__ALCHEMY_RUNTIME__) {
156 yield* bind`${options.tag}(${instance}, ${endpoint})`({
157 policyStatements: [
158 {
159 Effect: "Allow",
160 Action: [...options.actions],
161 Resource: [
162 Output.interpolate`${instance.replicationInstanceArn}`,
163 Output.interpolate`${endpoint.endpointArn}`,
164 ],
165 },
166 ],
167 });
168 }
169 return Effect.fn(`${options.tag}(${instance.LogicalId}, ${endpoint.LogicalId})`)(function* (
170 request?: Omit<I, "ReplicationInstanceArn" | "EndpointArn">,
171 ) {
172 return yield* op({
173 ...request,
174 ReplicationInstanceArn: yield* ReplicationInstanceArn,
175 EndpointArn: yield* EndpointArn,
176 } as I);
177 });
178 });
179 });
180
181 /**
182 * Build the impl Effect for an account-level operation (no target resource).
183 * The deploy-time half grants `actions` on `*`.
184 */
185 export const makeDmsAccountHttpBinding = <I, A, E, R>(options: {
186 /** Fully-qualified binding tag, e.g. `AWS.DMS.DescribeConnections`. */
187 tag: string;
188 /** The distilled operation, invoked with the caller's request as-is. */
189 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
190 /** IAM actions granted on `*`. */
191 actions: readonly string[];
192 }) =>
193 Effect.gen(function* () {
194 const bind = yield* AWSBindings.Bind;
195 const op = yield* AWSBindings.withCredentials(options.operation);
196
197 return Effect.fn(function* () {
198 if (!globalThis.__ALCHEMY_RUNTIME__) {
199 yield* bind`${options.tag}()`({
200 policyStatements: [
201 {
202 Effect: "Allow",
203 Action: [...options.actions],
204 Resource: ["*"],
205 },
206 ],
207 });
208 }
209 return Effect.fn(options.tag)(function* (request?: I) {
210 return yield* op((request ?? {}) as I);
211 });
212 });
213 });