@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, 06:00 UTC

main
ts 365 lines 14.4 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as ServiceCatalog from "@alchemy.run/aws/ServiceCatalog";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 /**
13 * Well-formed identifiers that cannot exist — drive the typed not-found
14 * paths for the service-action and StackSet bindings (whose live setup
15 * needs an SSM-backed service action / StackSet product), proving the
16 * grants reach the API: an IAM gap would surface AccessDeniedException
17 * (an untyped defect) instead of the typed tag.
18 */
19 const FAKE_PROVISIONED_PRODUCT_ID = "pp-aaaaaaaaaaaaa";
20 const FAKE_SERVICE_ACTION_ID = "act-aaaaaaaaaaaaa";
21
22 export class ServiceCatalogTestFunction extends Lambda.Function<Lambda.Function>()(
23 "ServiceCatalogTestFunction",
24 ) {}
25
26 export default ServiceCatalogTestFunction.make(
27 {
28 main,
29 functionUrl: true,
30 timeout: Duration.seconds(30),
31 },
32 Effect.gen(function* () {
33 const searchProducts = yield* ServiceCatalog.SearchProducts();
34 const describeProduct = yield* ServiceCatalog.DescribeProduct();
35 const listLaunchPaths = yield* ServiceCatalog.ListLaunchPaths();
36 const describeProvisioningParameters = yield* ServiceCatalog.DescribeProvisioningParameters();
37 const provisionProduct = yield* ServiceCatalog.ProvisionProduct();
38 const updateProvisionedProduct = yield* ServiceCatalog.UpdateProvisionedProduct();
39 const terminateProvisionedProduct = yield* ServiceCatalog.TerminateProvisionedProduct();
40 const describeProvisionedProduct = yield* ServiceCatalog.DescribeProvisionedProduct();
41 const searchProvisionedProducts = yield* ServiceCatalog.SearchProvisionedProducts();
42 const describeRecord = yield* ServiceCatalog.DescribeRecord();
43 const listRecordHistory = yield* ServiceCatalog.ListRecordHistory();
44 const getProvisionedProductOutputs = yield* ServiceCatalog.GetProvisionedProductOutputs();
45 const listStackInstancesForProvisionedProduct =
46 yield* ServiceCatalog.ListStackInstancesForProvisionedProduct();
47 const executeProvisionedProductServiceAction =
48 yield* ServiceCatalog.ExecuteProvisionedProductServiceAction();
49 const describeServiceActionExecutionParameters =
50 yield* ServiceCatalog.DescribeServiceActionExecutionParameters();
51
52 const bound = {
53 searchProducts,
54 describeProduct,
55 listLaunchPaths,
56 describeProvisioningParameters,
57 provisionProduct,
58 updateProvisionedProduct,
59 terminateProvisionedProduct,
60 describeProvisionedProduct,
61 searchProvisionedProducts,
62 describeRecord,
63 listRecordHistory,
64 getProvisionedProductOutputs,
65 listStackInstancesForProvisionedProduct,
66 executeProvisionedProductServiceAction,
67 describeServiceActionExecutionParameters,
68 };
69
70 return {
71 fetch: Effect.gen(function* () {
72 const request = yield* HttpServerRequest;
73 const url = new URL(request.originalUrl);
74 const pathname = url.pathname;
75 const q = (name: string) => url.searchParams.get(name) ?? undefined;
76
77 if (pathname === "/bindings") {
78 return yield* HttpServerResponse.json({ bound: Object.keys(bound) });
79 }
80
81 // IAM policy propagation on the freshly attached binding grants can
82 // lag the deploy — surface the typed AccessDeniedException as a tag
83 // so the test's bounded polls ride it out instead of exhausting the
84 // 5xx retry budget.
85 if (pathname === "/search") {
86 const result = yield* searchProducts().pipe(
87 Effect.map((r) => ({
88 tag: "Ok" as string,
89 count: (r.ProductViewSummaries ?? []).length,
90 productIds: (r.ProductViewSummaries ?? []).map((p) => p.ProductId),
91 })),
92 Effect.catchTag(["InvalidParametersException", "AccessDeniedException"], (e) =>
93 Effect.succeed({ tag: e._tag as string, count: -1 }),
94 ),
95 );
96 return yield* HttpServerResponse.json(result);
97 }
98
99 if (pathname === "/product") {
100 const result = yield* describeProduct({ Id: q("id") }).pipe(
101 Effect.map((r) => ({
102 tag: "Ok" as string,
103 name: r.ProductViewSummary?.Name,
104 })),
105 Effect.catchTag(
106 ["ResourceNotFoundException", "InvalidParametersException", "AccessDeniedException"],
107 (e) => Effect.succeed({ tag: e._tag as string, name: undefined }),
108 ),
109 );
110 return yield* HttpServerResponse.json(result);
111 }
112
113 if (pathname === "/launch-paths") {
114 const result = yield* listLaunchPaths({
115 ProductId: q("productId")!,
116 }).pipe(
117 Effect.map((r) => ({
118 tag: "Ok" as string,
119 count: (r.LaunchPathSummaries ?? []).length,
120 pathId: r.LaunchPathSummaries?.[0]?.Id,
121 })),
122 Effect.catchTag(
123 ["ResourceNotFoundException", "InvalidParametersException", "AccessDeniedException"],
124 (e) =>
125 Effect.succeed({
126 tag: e._tag as string,
127 count: 0,
128 pathId: undefined,
129 }),
130 ),
131 );
132 return yield* HttpServerResponse.json(result);
133 }
134
135 if (pathname === "/params") {
136 const result = yield* describeProvisioningParameters({
137 ProductId: q("productId"),
138 ProvisioningArtifactId: q("artifactId"),
139 PathId: q("pathId"),
140 }).pipe(
141 Effect.map((r) => ({
142 tag: "Ok" as string,
143 count: (r.ProvisioningArtifactParameters ?? []).length,
144 })),
145 Effect.catchTag(
146 ["ResourceNotFoundException", "InvalidParametersException", "AccessDeniedException"],
147 (e) => Effect.succeed({ tag: e._tag as string, count: 0 }),
148 ),
149 );
150 return yield* HttpServerResponse.json(result);
151 }
152
153 if (pathname === "/provision") {
154 const result = yield* provisionProduct({
155 ProductId: q("productId")!,
156 ProvisioningArtifactId: q("artifactId")!,
157 PathId: q("pathId"),
158 ProvisionedProductName: q("name")!,
159 ProvisionToken: q("token")!,
160 }).pipe(
161 Effect.map((r) => ({
162 tag: "Ok" as string,
163 recordId: r.RecordDetail?.RecordId,
164 provisionedProductId: r.RecordDetail?.ProvisionedProductId,
165 status: r.RecordDetail?.Status,
166 })),
167 Effect.catchTag(
168 [
169 "ResourceNotFoundException",
170 "InvalidParametersException",
171 "DuplicateResourceException",
172 "AccessDeniedException",
173 ],
174 (e) => Effect.succeed({ tag: e._tag as string }),
175 ),
176 );
177 return yield* HttpServerResponse.json(result);
178 }
179
180 if (pathname === "/record") {
181 const result = yield* describeRecord({ Id: q("id")! }).pipe(
182 Effect.map((r) => ({
183 tag: "Ok" as string,
184 status: r.RecordDetail?.Status,
185 errors: (r.RecordDetail?.RecordErrors ?? []).map((e) => e.Description),
186 })),
187 Effect.catchTag(["ResourceNotFoundException", "AccessDeniedException"], (e) =>
188 Effect.succeed({ tag: e._tag as string }),
189 ),
190 );
191 return yield* HttpServerResponse.json(result);
192 }
193
194 if (pathname === "/pp") {
195 const result = yield* describeProvisionedProduct({
196 Name: q("name"),
197 }).pipe(
198 Effect.map((r) => ({
199 tag: "Ok" as string,
200 status: r.ProvisionedProductDetail?.Status,
201 provisionedProductId: r.ProvisionedProductDetail?.Id,
202 lastRecordId: r.ProvisionedProductDetail?.LastRecordId,
203 })),
204 Effect.catchTag(
205 ["ResourceNotFoundException", "InvalidParametersException", "AccessDeniedException"],
206 (e) => Effect.succeed({ tag: e._tag as string }),
207 ),
208 );
209 return yield* HttpServerResponse.json(result);
210 }
211
212 if (pathname === "/search-pp") {
213 const result = yield* searchProvisionedProducts().pipe(
214 Effect.map((r) => ({
215 tag: "Ok" as string,
216 count: (r.ProvisionedProducts ?? []).length,
217 })),
218 Effect.catchTag(["InvalidParametersException", "AccessDeniedException"], (e) =>
219 Effect.succeed({ tag: e._tag as string, count: -1 }),
220 ),
221 );
222 return yield* HttpServerResponse.json(result);
223 }
224
225 if (pathname === "/history") {
226 const result = yield* listRecordHistory().pipe(
227 Effect.map((r) => ({
228 tag: "Ok" as string,
229 count: (r.RecordDetails ?? []).length,
230 })),
231 Effect.catchTag(["InvalidParametersException", "AccessDeniedException"], (e) =>
232 Effect.succeed({ tag: e._tag as string, count: -1 }),
233 ),
234 );
235 return yield* HttpServerResponse.json(result);
236 }
237
238 if (pathname === "/outputs") {
239 const result = yield* getProvisionedProductOutputs({
240 ProvisionedProductName: q("name"),
241 }).pipe(
242 Effect.map((r) => ({
243 tag: "Ok" as string,
244 count: (r.Outputs ?? []).length,
245 })),
246 Effect.catchTag(
247 ["ResourceNotFoundException", "InvalidParametersException", "AccessDeniedException"],
248 (e) => Effect.succeed({ tag: e._tag as string, count: 0 }),
249 ),
250 );
251 return yield* HttpServerResponse.json(result);
252 }
253
254 if (pathname === "/terminate") {
255 const result = yield* terminateProvisionedProduct({
256 ProvisionedProductName: q("name"),
257 TerminateToken: q("token")!,
258 }).pipe(
259 Effect.map((r) => ({
260 tag: "Ok" as string,
261 recordId: r.RecordDetail?.RecordId,
262 })),
263 Effect.catchTag(["ResourceNotFoundException", "AccessDeniedException"], (e) =>
264 Effect.succeed({ tag: e._tag as string, recordId: undefined }),
265 ),
266 );
267 return yield* HttpServerResponse.json(result);
268 }
269
270 if (pathname === "/update-nonexistent") {
271 // Typed not-found — proves the UpdateProvisionedProduct grant
272 // reaches the API without mutating anything real.
273 const result = yield* updateProvisionedProduct({
274 ProvisionedProductName: "alchemy-sc-does-not-exist",
275 UpdateToken: "alchemyscupdateprobe",
276 }).pipe(
277 Effect.map(() => "Ok"),
278 Effect.catchTag(["ResourceNotFoundException", "InvalidParametersException"], (e) =>
279 Effect.succeed(e._tag),
280 ),
281 );
282 return yield* HttpServerResponse.json({ tag: result });
283 }
284
285 if (pathname === "/stack-instances-nonexistent") {
286 const result = yield* listStackInstancesForProvisionedProduct({
287 ProvisionedProductId: FAKE_PROVISIONED_PRODUCT_ID,
288 }).pipe(
289 Effect.map(() => "Ok"),
290 Effect.catchTag(["ResourceNotFoundException", "InvalidParametersException"], (e) =>
291 Effect.succeed(e._tag),
292 ),
293 );
294 return yield* HttpServerResponse.json({ tag: result });
295 }
296
297 if (pathname === "/service-action-params-nonexistent") {
298 const result = yield* describeServiceActionExecutionParameters({
299 ProvisionedProductId: FAKE_PROVISIONED_PRODUCT_ID,
300 ServiceActionId: FAKE_SERVICE_ACTION_ID,
301 }).pipe(
302 Effect.map(() => "Ok"),
303 Effect.catchTag(["ResourceNotFoundException", "InvalidParametersException"], (e) =>
304 Effect.succeed(e._tag),
305 ),
306 );
307 return yield* HttpServerResponse.json({ tag: result });
308 }
309
310 if (pathname === "/execute-nonexistent") {
311 // Service Catalog misuses ValidationException for a missing
312 // provisioned product on this op — distilled remaps it to the
313 // synthetic ProvisionedProductNotFound tag
314 // (patches/service-catalog.json). ValidationException (a typed
315 // CommonErrors member) is also caught so the probe works against
316 // a distilled lib built before the patch.
317 const result = yield* executeProvisionedProductServiceAction({
318 ProvisionedProductId: FAKE_PROVISIONED_PRODUCT_ID,
319 ServiceActionId: FAKE_SERVICE_ACTION_ID,
320 ExecuteToken: "alchemyscexecuteprobe",
321 }).pipe(
322 Effect.map(() => "Ok"),
323 Effect.catchTag(
324 [
325 "ProvisionedProductNotFound",
326 "ValidationException",
327 "ResourceNotFoundException",
328 "InvalidParametersException",
329 "InvalidStateException",
330 ],
331 (e) => Effect.succeed(e._tag),
332 ),
333 );
334 return yield* HttpServerResponse.json({ tag: result });
335 }
336
337 return yield* HttpServerResponse.json(
338 { error: "Not found", method: request.method, pathname },
339 { status: 404 },
340 );
341 }).pipe(Effect.orDie),
342 };
343 }).pipe(
344 Effect.provide(
345 Layer.mergeAll(
346 Lambda.EventSource,
347 ServiceCatalog.SearchProductsHttp,
348 ServiceCatalog.DescribeProductHttp,
349 ServiceCatalog.ListLaunchPathsHttp,
350 ServiceCatalog.DescribeProvisioningParametersHttp,
351 ServiceCatalog.ProvisionProductHttp,
352 ServiceCatalog.UpdateProvisionedProductHttp,
353 ServiceCatalog.TerminateProvisionedProductHttp,
354 ServiceCatalog.DescribeProvisionedProductHttp,
355 ServiceCatalog.SearchProvisionedProductsHttp,
356 ServiceCatalog.DescribeRecordHttp,
357 ServiceCatalog.ListRecordHistoryHttp,
358 ServiceCatalog.GetProvisionedProductOutputsHttp,
359 ServiceCatalog.ListStackInstancesForProvisionedProductHttp,
360 ServiceCatalog.ExecuteProvisionedProductServiceActionHttp,
361 ServiceCatalog.DescribeServiceActionExecutionParametersHttp,
362 ),
363 ),
364 ),
365 );