@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 79 lines 2.9 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as RolesAnywhere from "@alchemy.run/aws/RolesAnywhere";
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 // A well-formed-but-nonexistent subject id — drives the typed error path for
13 // GetSubject (proving the IAM grant + typed union; an IAM gap would surface
14 // AccessDeniedException instead).
15 const NONEXISTENT_SUBJECT_ID = "00000000-0000-0000-0000-000000000000";
16
17 export class RolesAnywhereTestFunction extends Lambda.Function<Lambda.Function>()(
18 "RolesAnywhereTestFunction",
19 ) {}
20
21 export default RolesAnywhereTestFunction.make(
22 {
23 main,
24 functionUrl: true,
25 timeout: Duration.seconds(30),
26 },
27 Effect.gen(function* () {
28 // --- account-level bindings (subjects are the audit records of every
29 // certificate identity that authenticated via Roles Anywhere) ---
30 const getSubject = yield* RolesAnywhere.GetSubject();
31 const listSubjects = yield* RolesAnywhere.ListSubjects();
32
33 const bound = { getSubject, listSubjects };
34
35 return {
36 fetch: Effect.gen(function* () {
37 const request = yield* HttpServerRequest;
38 const url = new URL(request.originalUrl);
39 const pathname = url.pathname;
40
41 if (request.method === "GET" && pathname === "/bindings") {
42 return yield* HttpServerResponse.json({
43 bound: Object.keys(bound),
44 });
45 }
46
47 if (request.method === "GET" && pathname === "/subjects") {
48 // The testing account has no external workloads authenticating via
49 // Roles Anywhere, so an empty list still proves the
50 // rolesanywhere:ListSubjects grant end-to-end.
51 const result = yield* listSubjects().pipe(
52 Effect.map((r) => ({
53 ok: true as const,
54 count: r.subjects?.length ?? 0,
55 })),
56 );
57 return yield* HttpServerResponse.json(result);
58 }
59
60 if (request.method === "GET" && pathname === "/subject-not-found") {
61 const tag = yield* getSubject({
62 subjectId: NONEXISTENT_SUBJECT_ID,
63 }).pipe(
64 Effect.map((r) => (r.subject === undefined ? "NoSubject" : "Found")),
65 Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)),
66 );
67 return yield* HttpServerResponse.json({ tag });
68 }
69
70 return yield* HttpServerResponse.json(
71 { error: "Not found", method: request.method, pathname },
72 { status: 404 },
73 );
74 }).pipe(Effect.orDie),
75 };
76 }).pipe(
77 Effect.provide(Layer.mergeAll(RolesAnywhere.GetSubjectHttp, RolesAnywhere.ListSubjectsHttp)),
78 ),
79 );