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

main
ts 266 lines 10 KB
Raw
1 import * as Glue from "@alchemy.run/aws/Glue";
2 import * as LakeFormation from "@alchemy.run/aws/LakeFormation";
3 import * as Lambda from "@alchemy.run/aws/Lambda";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import { HttpServerRequest } from "effect/http/HttpServerRequest";
7 import * as HttpServerResponse from "effect/http/HttpServerResponse";
8 import * as Layer from "effect/Layer";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "handler.ts");
12
13 /**
14 * Deterministic fixture names — the handler needs the database name at
15 * runtime, so it is a constant shared by deploy time and runtime.
16 */
17 export const FIXTURE_DATABASE = "alchemy_lf_bindings_fixture";
18
19 /** A tag key that never exists — drives the typed EntityNotFound paths. */
20 const MISSING_TAG_KEY = "alchemy-lf-bindings-missing";
21
22 export class LakeFormationTestFunction extends Lambda.Function<Lambda.Function>()(
23 "LakeFormationTestFunction",
24 ) {}
25
26 export default LakeFormationTestFunction.make(
27 {
28 main,
29 functionUrl: true,
30 // Lake Formation calls routinely take a few seconds; the default 3s
31 // Lambda timeout is too tight.
32 timeout: Duration.seconds(30),
33 },
34 Effect.gen(function* () {
35 // A database for GetResourceLFTags to observe (deterministic name so
36 // the runtime routes can reference it).
37 yield* Glue.Database("LfBindingsDb", {
38 databaseName: FIXTURE_DATABASE,
39 });
40
41 // --- account-level bindings ---
42 const getDataLakePrincipal = yield* LakeFormation.GetDataLakePrincipal();
43 const getTemporaryGlueTableCredentials =
44 yield* LakeFormation.GetTemporaryGlueTableCredentials();
45 const getTemporaryGluePartitionCredentials =
46 yield* LakeFormation.GetTemporaryGluePartitionCredentials();
47 const getTemporaryDataLocationCredentials =
48 yield* LakeFormation.GetTemporaryDataLocationCredentials();
49 const searchDatabasesByLFTags = yield* LakeFormation.SearchDatabasesByLFTags();
50 const searchTablesByLFTags = yield* LakeFormation.SearchTablesByLFTags();
51 const getResourceLFTags = yield* LakeFormation.GetResourceLFTags();
52 const getEffectivePermissionsForPath = yield* LakeFormation.GetEffectivePermissionsForPath();
53 const getLFTag = yield* LakeFormation.GetLFTag();
54 const listLFTags = yield* LakeFormation.ListLFTags();
55 const listPermissions = yield* LakeFormation.ListPermissions();
56
57 const bound = {
58 getDataLakePrincipal,
59 getTemporaryGlueTableCredentials,
60 getTemporaryGluePartitionCredentials,
61 getTemporaryDataLocationCredentials,
62 searchDatabasesByLFTags,
63 searchTablesByLFTags,
64 getResourceLFTags,
65 getEffectivePermissionsForPath,
66 getLFTag,
67 listLFTags,
68 listPermissions,
69 };
70
71 // A syntactically valid, foreign-account Glue table ARN — the credential
72 // vending probes prove the IAM grant reaches the API by observing a
73 // *typed* Lake Formation rejection rather than an IAM signature failure.
74 const foreignTableArn = Effect.sync(
75 () => `arn:aws:glue:${process.env.AWS_REGION}:123456789012:table/missing_db/missing_table`,
76 );
77
78 return {
79 fetch: Effect.gen(function* () {
80 const request = yield* HttpServerRequest;
81 const url = new URL(request.originalUrl);
82 const pathname = url.pathname;
83
84 if (request.method === "GET" && pathname === "/bindings") {
85 return yield* HttpServerResponse.json({
86 bound: Object.keys(bound),
87 });
88 }
89
90 if (request.method === "GET" && pathname === "/principal") {
91 const result = yield* getDataLakePrincipal().pipe(
92 Effect.map((r) => ({ tag: "Ok", identity: r.Identity ?? null })),
93 );
94 return yield* HttpServerResponse.json(result);
95 }
96
97 if (request.method === "GET" && pathname === "/tags") {
98 const result = yield* listLFTags().pipe(
99 Effect.map((r) => ({ tag: "Ok", count: (r.LFTags ?? []).length })),
100 );
101 return yield* HttpServerResponse.json(result);
102 }
103
104 if (request.method === "GET" && pathname === "/tag-missing") {
105 const result = yield* getLFTag({ TagKey: MISSING_TAG_KEY }).pipe(
106 Effect.map(() => ({ tag: "Ok" })),
107 Effect.catchTag(["EntityNotFoundException", "AccessDeniedException"], (e) =>
108 Effect.succeed({ tag: e._tag }),
109 ),
110 );
111 return yield* HttpServerResponse.json(result);
112 }
113
114 if (request.method === "GET" && pathname === "/permissions") {
115 const result = yield* listPermissions().pipe(
116 Effect.map((r) => ({
117 tag: "Ok",
118 count: (r.PrincipalResourcePermissions ?? []).length,
119 })),
120 );
121 return yield* HttpServerResponse.json(result);
122 }
123
124 if (request.method === "GET" && pathname === "/search-databases") {
125 const result = yield* searchDatabasesByLFTags({
126 Expression: [{ TagKey: MISSING_TAG_KEY, TagValues: ["x"] }],
127 }).pipe(
128 Effect.map((r) => ({
129 tag: "Ok",
130 count: (r.DatabaseList ?? []).length,
131 })),
132 Effect.catchTag(["EntityNotFoundException", "AccessDeniedException"], (e) =>
133 Effect.succeed({ tag: e._tag, count: 0 }),
134 ),
135 );
136 return yield* HttpServerResponse.json(result);
137 }
138
139 if (request.method === "GET" && pathname === "/search-tables") {
140 const result = yield* searchTablesByLFTags({
141 Expression: [{ TagKey: MISSING_TAG_KEY, TagValues: ["x"] }],
142 }).pipe(
143 Effect.map((r) => ({
144 tag: "Ok",
145 count: (r.TableList ?? []).length,
146 })),
147 Effect.catchTag(["EntityNotFoundException", "AccessDeniedException"], (e) =>
148 Effect.succeed({ tag: e._tag, count: 0 }),
149 ),
150 );
151 return yield* HttpServerResponse.json(result);
152 }
153
154 if (request.method === "GET" && pathname === "/resource-tags") {
155 const result = yield* getResourceLFTags({
156 Resource: { Database: { Name: FIXTURE_DATABASE } },
157 }).pipe(
158 Effect.map((r) => ({
159 tag: "Ok",
160 count: (r.LFTagOnDatabase ?? []).length,
161 })),
162 Effect.catchTag(
163 ["EntityNotFoundException", "AccessDeniedException", "GlueEncryptionException"],
164 (e) => Effect.succeed({ tag: e._tag, count: 0 }),
165 ),
166 );
167 return yield* HttpServerResponse.json(result);
168 }
169
170 if (request.method === "GET" && pathname === "/effective-permissions") {
171 const result = yield* getEffectivePermissionsForPath({
172 ResourceArn: "arn:aws:s3:::alchemy-lf-bindings-nonexistent",
173 }).pipe(
174 Effect.map((r) => ({
175 tag: "Ok",
176 count: (r.Permissions ?? []).length,
177 })),
178 Effect.catchTag(["EntityNotFoundException", "InvalidInputException"], (e) =>
179 Effect.succeed({ tag: e._tag, count: 0 }),
180 ),
181 );
182 return yield* HttpServerResponse.json(result);
183 }
184
185 if (request.method === "GET" && pathname === "/table-credentials") {
186 const TableArn = yield* foreignTableArn;
187 const result = yield* getTemporaryGlueTableCredentials({
188 TableArn,
189 Permissions: ["SELECT"],
190 SupportedPermissionTypes: ["COLUMN_PERMISSION"],
191 }).pipe(
192 Effect.map(() => ({ tag: "Ok" })),
193 Effect.catchTag(
194 [
195 "AccessDeniedException",
196 "EntityNotFoundException",
197 "InvalidInputException",
198 "PermissionTypeMismatchException",
199 ],
200 (e) => Effect.succeed({ tag: e._tag }),
201 ),
202 );
203 return yield* HttpServerResponse.json(result);
204 }
205
206 if (request.method === "GET" && pathname === "/partition-credentials") {
207 const TableArn = yield* foreignTableArn;
208 const result = yield* getTemporaryGluePartitionCredentials({
209 TableArn,
210 Partition: { Values: ["x"] },
211 Permissions: ["SELECT"],
212 }).pipe(
213 Effect.map(() => ({ tag: "Ok" })),
214 Effect.catchTag(
215 [
216 "AccessDeniedException",
217 "EntityNotFoundException",
218 "InvalidInputException",
219 "PermissionTypeMismatchException",
220 ],
221 (e) => Effect.succeed({ tag: e._tag }),
222 ),
223 );
224 return yield* HttpServerResponse.json(result);
225 }
226
227 if (request.method === "GET" && pathname === "/location-credentials") {
228 const result = yield* getTemporaryDataLocationCredentials({
229 DataLocations: ["arn:aws:s3:::alchemy-lf-bindings-nonexistent"],
230 CredentialsScope: "READ",
231 }).pipe(
232 Effect.map(() => ({ tag: "Ok" })),
233 Effect.catchTag(
234 [
235 "AccessDeniedException",
236 "EntityNotFoundException",
237 "InvalidInputException",
238 "ConflictException",
239 ],
240 (e) => Effect.succeed({ tag: e._tag }),
241 ),
242 );
243 return yield* HttpServerResponse.json(result);
244 }
245
246 return yield* HttpServerResponse.json({ error: "not found" }, { status: 404 });
247 }).pipe(Effect.orDie),
248 };
249 }).pipe(
250 Effect.provide(
251 Layer.mergeAll(
252 LakeFormation.GetDataLakePrincipalHttp,
253 LakeFormation.GetTemporaryGlueTableCredentialsHttp,
254 LakeFormation.GetTemporaryGluePartitionCredentialsHttp,
255 LakeFormation.GetTemporaryDataLocationCredentialsHttp,
256 LakeFormation.SearchDatabasesByLFTagsHttp,
257 LakeFormation.SearchTablesByLFTagsHttp,
258 LakeFormation.GetResourceLFTagsHttp,
259 LakeFormation.GetEffectivePermissionsForPathHttp,
260 LakeFormation.GetLFTagHttp,
261 LakeFormation.ListLFTagsHttp,
262 LakeFormation.ListPermissionsHttp,
263 ),
264 ),
265 ),
266 );