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

main
ts 233 lines 8.53 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 { Crawler } from "./Crawler.ts";
5 import type { Database } from "./Database.ts";
6 import type { Job } from "./Job.ts";
7 import type { Table } from "./Table.ts";
8
9 /**
10 * Shared scaffolding for AWS Glue HTTP bindings.
11 *
12 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
13 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
14 * below. Everything except the operation, the identifier injection, and the
15 * IAM action list is boilerplate.
16 *
17 * Glue's catalog IAM model authorizes table- and partition-addressed actions
18 * against the *catalog*, *database*, and *table* ARNs together, so the
19 * database/table builders grant on all the ARNs the action evaluates.
20 */
21
22 /**
23 * Build the impl Effect for a Glue operation addressed to a {@link Job} by
24 * `JobName`: the runtime callable injects the bound job's name and the
25 * deploy-time half grants `actions` on the job ARN — or on `*` when
26 * `anyResource` is set, for the handful of Glue actions that do not support
27 * resource-level permissions (verified live: `glue:GetJobBookmark` /
28 * `glue:ResetJobBookmark` are evaluated with no resource — an ARN-scoped
29 * Allow never matches and IAM implicit-denies).
30 */
31 export const makeGlueJobHttpBinding = <I extends { JobName: string }, A, E, R>(options: {
32 /** Fully-qualified binding tag, e.g. `AWS.Glue.GetJobRun`. */
33 tag: string;
34 /** The distilled operation; `JobName` is injected from the job. */
35 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
36 /** IAM actions granted on the job ARN. */
37 actions: readonly string[];
38 /**
39 * Grant on `Resource: "*"` instead of the job ARN — required for actions
40 * Glue evaluates without a resource (job bookmarks).
41 */
42 anyResource?: boolean;
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* (job: Job) {
49 // Outputs yield a DEFERRED effect — resolve again per invocation below.
50 const JobName = yield* job.jobName;
51 if (!globalThis.__ALCHEMY_RUNTIME__) {
52 yield* bind`${options.tag}(${job})`({
53 policyStatements: [
54 {
55 Effect: "Allow",
56 Action: [...options.actions],
57 Resource: options.anyResource ? ["*"] : [job.jobArn],
58 },
59 ],
60 });
61 }
62 return Effect.fn(`${options.tag}(${job.LogicalId})`)(function* (
63 request?: Omit<I, "JobName">,
64 ) {
65 return yield* op({ ...request, JobName: yield* JobName } as I);
66 });
67 });
68 });
69
70 /**
71 * Build the impl Effect for a Glue operation addressed to a {@link Crawler}
72 * by `Name`: the runtime callable injects the bound crawler's name and the
73 * deploy-time half grants `actions` on the crawler ARN.
74 */
75 export const makeGlueCrawlerHttpBinding = <I extends { Name: string }, A, E, R>(options: {
76 /** Fully-qualified binding tag, e.g. `AWS.Glue.StartCrawler`. */
77 tag: string;
78 /** The distilled operation; `Name` is injected from the crawler. */
79 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
80 /** IAM actions granted on the crawler ARN. */
81 actions: readonly string[];
82 }) =>
83 Effect.gen(function* () {
84 const bind = yield* AWSBindings.Bind;
85 const op = yield* AWSBindings.withCredentials(options.operation);
86
87 return Effect.fn(function* (crawler: Crawler) {
88 const Name = yield* crawler.crawlerName;
89 if (!globalThis.__ALCHEMY_RUNTIME__) {
90 yield* bind`${options.tag}(${crawler})`({
91 policyStatements: [
92 {
93 Effect: "Allow",
94 Action: [...options.actions],
95 Resource: [crawler.crawlerArn],
96 },
97 ],
98 });
99 }
100 return Effect.fn(`${options.tag}(${crawler.LogicalId})`)(function* (
101 request?: Omit<I, "Name">,
102 ) {
103 return yield* op({ ...request, Name: yield* Name } as I);
104 });
105 });
106 });
107
108 /**
109 * Build the impl Effect for a Glue Data Catalog operation addressed to a
110 * {@link Database}: the runtime callable injects `DatabaseName` (+
111 * `CatalogId`) and the deploy-time half grants `actions` on the catalog,
112 * database, and contained-tables ARNs (Glue evaluates catalog actions
113 * against all three).
114 */
115 export const makeGlueDatabaseHttpBinding = <
116 I extends { DatabaseName: string; CatalogId?: string },
117 A,
118 E,
119 R,
120 >(options: {
121 /** Fully-qualified binding tag, e.g. `AWS.Glue.GetTables`. */
122 tag: string;
123 /** The distilled operation; `DatabaseName`/`CatalogId` are injected. */
124 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
125 /** IAM actions granted on the catalog + database + tables ARNs. */
126 actions: readonly string[];
127 }) =>
128 Effect.gen(function* () {
129 const bind = yield* AWSBindings.Bind;
130 const op = yield* AWSBindings.withCredentials(options.operation);
131
132 return Effect.fn(function* (database: Database) {
133 const DatabaseName = yield* database.databaseName;
134 const CatalogId = yield* database.catalogId;
135 if (!globalThis.__ALCHEMY_RUNTIME__) {
136 yield* bind`${options.tag}(${database})`({
137 policyStatements: [
138 {
139 Effect: "Allow",
140 Action: [...options.actions],
141 Resource: [
142 // arn:…:database/{db} → arn:…:catalog
143 Output.map(database.databaseArn, (arn) =>
144 arn.replace(/:database\/.*$/, ":catalog"),
145 ),
146 database.databaseArn,
147 // arn:…:database/{db} → arn:…:table/{db}/*
148 Output.map(
149 database.databaseArn,
150 (arn) => `${arn.replace(":database/", ":table/")}/*`,
151 ),
152 ],
153 },
154 ],
155 });
156 }
157 return Effect.fn(`${options.tag}(${database.LogicalId})`)(function* (
158 request?: Omit<I, "DatabaseName" | "CatalogId">,
159 ) {
160 return yield* op({
161 ...request,
162 DatabaseName: yield* DatabaseName,
163 CatalogId: yield* CatalogId,
164 } as I);
165 });
166 });
167 });
168
169 /**
170 * Build the impl Effect for a Glue Data Catalog operation addressed to a
171 * {@link Table}: the runtime callable injects `DatabaseName` + the table
172 * name (+ `CatalogId`) and the deploy-time half grants `actions` on the
173 * catalog, database, and table ARNs. Most table-addressed inputs carry the
174 * table name as `TableName`; `GetTable` uses `Name` — pick with
175 * `tableNameKey`.
176 */
177 export const makeGlueTableHttpBinding = <
178 K extends "TableName" | "Name",
179 I extends { DatabaseName: string; CatalogId?: string } & {
180 [key in K]: string;
181 },
182 A,
183 E,
184 R,
185 >(options: {
186 /** Fully-qualified binding tag, e.g. `AWS.Glue.GetPartitions`. */
187 tag: string;
188 /** The distilled operation; database/table identifiers are injected. */
189 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
190 /** IAM actions granted on the catalog + database + table ARNs. */
191 actions: readonly string[];
192 /** The request key carrying the table name. */
193 tableNameKey: K;
194 }) =>
195 Effect.gen(function* () {
196 const bind = yield* AWSBindings.Bind;
197 const op = yield* AWSBindings.withCredentials(options.operation);
198
199 return Effect.fn(function* (table: Table) {
200 const DatabaseName = yield* table.databaseName;
201 const TableName = yield* table.tableName;
202 const CatalogId = yield* table.catalogId;
203 if (!globalThis.__ALCHEMY_RUNTIME__) {
204 yield* bind`${options.tag}(${table})`({
205 policyStatements: [
206 {
207 Effect: "Allow",
208 Action: [...options.actions],
209 Resource: [
210 // arn:…:table/{db}/{tbl} → arn:…:catalog
211 Output.map(table.tableArn, (arn) => arn.replace(/:table\/.*$/, ":catalog")),
212 // arn:…:table/{db}/{tbl} → arn:…:database/{db}
213 Output.map(table.tableArn, (arn) =>
214 arn.replace(/:table\/([^/]+)\/.*$/, ":database/$1"),
215 ),
216 table.tableArn,
217 ],
218 },
219 ],
220 });
221 }
222 return Effect.fn(`${options.tag}(${table.LogicalId})`)(function* (
223 request?: Omit<I, "DatabaseName" | "CatalogId" | K>,
224 ) {
225 return yield* op({
226 ...request,
227 DatabaseName: yield* DatabaseName,
228 [options.tableNameKey]: yield* TableName,
229 CatalogId: yield* CatalogId,
230 } as I);
231 });
232 });
233 });