@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 252 lines 9.68 KB
Raw
1 import * as AuditManager from "@alchemy.run/aws/AuditManager";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import path from "pathe";
8
9 const main = path.resolve(import.meta.dirname, "handler.ts");
10
11 // A well-formed-but-nonexistent control domain id — drives the typed error
12 // path for ListControlInsightsByControlDomain.
13 const NONEXISTENT_CONTROL_DOMAIN = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
14
15 export class AuditManagerTestFunction extends Lambda.Function<Lambda.Function>()(
16 "AuditManagerTestFunction",
17 ) {}
18
19 /**
20 * Account-level Audit Manager bindings fixture.
21 *
22 * The testing account is NOT registered with Audit Manager (the service
23 * entered maintenance mode on 2026-04-30, so it can never be registered).
24 * `GetAccountStatus` still succeeds on unregistered accounts — proving a
25 * real end-to-end success through the binding + attached IAM policy — while
26 * every other operation answers the typed `AccessDeniedException`, proving
27 * the wiring and typed error union for each capability.
28 */
29 export default AuditManagerTestFunction.make(
30 {
31 main,
32 functionUrl: true,
33 },
34 Effect.gen(function* () {
35 const getAccountStatus = yield* AuditManager.GetAccountStatus();
36 const getServicesInScope = yield* AuditManager.GetServicesInScope();
37 const getInsights = yield* AuditManager.GetInsights();
38 const listControlDomainInsights = yield* AuditManager.ListControlDomainInsights();
39 const listControlInsightsByControlDomain =
40 yield* AuditManager.ListControlInsightsByControlDomain();
41 const getDelegations = yield* AuditManager.GetDelegations();
42 const getEvidenceFileUploadUrl = yield* AuditManager.GetEvidenceFileUploadUrl();
43 const listAssessmentReports = yield* AuditManager.ListAssessmentReports();
44 const validateAssessmentReportIntegrity =
45 yield* AuditManager.ValidateAssessmentReportIntegrity();
46 const listKeywordsForDataSource = yield* AuditManager.ListKeywordsForDataSource();
47 const listNotifications = yield* AuditManager.ListNotifications();
48
49 const bound = {
50 getAccountStatus,
51 getServicesInScope,
52 getInsights,
53 listControlDomainInsights,
54 listControlInsightsByControlDomain,
55 getDelegations,
56 getEvidenceFileUploadUrl,
57 listAssessmentReports,
58 validateAssessmentReportIntegrity,
59 listKeywordsForDataSource,
60 listNotifications,
61 };
62
63 return {
64 fetch: Effect.gen(function* () {
65 const request = yield* HttpServerRequest;
66 const url = new URL(request.originalUrl);
67 const pathname = url.pathname;
68
69 if (request.method === "GET" && pathname === "/bindings") {
70 return yield* HttpServerResponse.json({
71 bound: Object.keys(bound),
72 });
73 }
74
75 if (request.method === "GET" && pathname === "/account-status") {
76 // Succeeds even on unregistered accounts — a REAL success through
77 // the binding's IAM grant.
78 const result = yield* getAccountStatus();
79 return yield* HttpServerResponse.json({
80 ok: true,
81 status: result.status ?? null,
82 });
83 }
84
85 if (request.method === "GET" && pathname === "/services-in-scope") {
86 const result = yield* getServicesInScope().pipe(
87 Effect.map((r) => ({
88 ok: true as const,
89 count: (r.serviceMetadata ?? []).length,
90 })),
91 Effect.catchTag(["AccessDeniedException", "ValidationException"], (e) =>
92 Effect.succeed({ ok: false as const, tag: e._tag }),
93 ),
94 );
95 return yield* HttpServerResponse.json(result);
96 }
97
98 if (request.method === "GET" && pathname === "/insights") {
99 const result = yield* getInsights().pipe(
100 Effect.map((r) => ({
101 ok: true as const,
102 activeAssessmentsCount: r.insights?.activeAssessmentsCount ?? 0,
103 })),
104 Effect.catchTag("AccessDeniedException", (e) =>
105 Effect.succeed({ ok: false as const, tag: e._tag }),
106 ),
107 );
108 return yield* HttpServerResponse.json(result);
109 }
110
111 if (request.method === "GET" && pathname === "/control-domain-insights") {
112 const result = yield* listControlDomainInsights({
113 maxResults: 20,
114 }).pipe(
115 Effect.map((r) => ({
116 ok: true as const,
117 count: (r.controlDomainInsights ?? []).length,
118 })),
119 Effect.catchTag(
120 ["AccessDeniedException", "ResourceNotFoundException", "ValidationException"],
121 (e) => Effect.succeed({ ok: false as const, tag: e._tag }),
122 ),
123 );
124 return yield* HttpServerResponse.json(result);
125 }
126
127 if (request.method === "GET" && pathname === "/control-insights") {
128 const result = yield* listControlInsightsByControlDomain({
129 controlDomainId: NONEXISTENT_CONTROL_DOMAIN,
130 }).pipe(
131 Effect.map((r) => ({
132 ok: true as const,
133 count: (r.controlInsightsMetadata ?? []).length,
134 })),
135 Effect.catchTag(
136 ["AccessDeniedException", "ResourceNotFoundException", "ValidationException"],
137 (e) => Effect.succeed({ ok: false as const, tag: e._tag }),
138 ),
139 );
140 return yield* HttpServerResponse.json(result);
141 }
142
143 if (request.method === "GET" && pathname === "/delegations") {
144 const result = yield* getDelegations({ maxResults: 20 }).pipe(
145 Effect.map((r) => ({
146 ok: true as const,
147 count: (r.delegations ?? []).length,
148 })),
149 Effect.catchTag(["AccessDeniedException", "ValidationException"], (e) =>
150 Effect.succeed({ ok: false as const, tag: e._tag }),
151 ),
152 );
153 return yield* HttpServerResponse.json(result);
154 }
155
156 if (request.method === "GET" && pathname === "/upload-url") {
157 const result = yield* getEvidenceFileUploadUrl({
158 fileName: "alchemy-evidence.txt",
159 }).pipe(
160 Effect.map((r) => ({
161 ok: true as const,
162 hasUrl: typeof r.uploadUrl === "string",
163 })),
164 Effect.catchTag(
165 ["AccessDeniedException", "ThrottlingException", "ValidationException"],
166 (e) => Effect.succeed({ ok: false as const, tag: e._tag }),
167 ),
168 );
169 return yield* HttpServerResponse.json(result);
170 }
171
172 if (request.method === "GET" && pathname === "/reports") {
173 const result = yield* listAssessmentReports({ maxResults: 20 }).pipe(
174 Effect.map((r) => ({
175 ok: true as const,
176 count: (r.assessmentReports ?? []).length,
177 })),
178 Effect.catchTag(["AccessDeniedException", "ValidationException"], (e) =>
179 Effect.succeed({ ok: false as const, tag: e._tag }),
180 ),
181 );
182 return yield* HttpServerResponse.json(result);
183 }
184
185 if (request.method === "GET" && pathname === "/validate-report") {
186 const result = yield* validateAssessmentReportIntegrity({
187 s3RelativePath: "s3://alchemy-nonexistent-bucket/report.zip",
188 }).pipe(
189 Effect.map((r) => ({
190 ok: true as const,
191 valid: r.signatureValid ?? false,
192 })),
193 Effect.catchTag(
194 ["AccessDeniedException", "ResourceNotFoundException", "ValidationException"],
195 (e) => Effect.succeed({ ok: false as const, tag: e._tag }),
196 ),
197 );
198 return yield* HttpServerResponse.json(result);
199 }
200
201 if (request.method === "GET" && pathname === "/keywords") {
202 const result = yield* listKeywordsForDataSource({
203 source: "AWS_Cloudtrail",
204 }).pipe(
205 Effect.map((r) => ({
206 ok: true as const,
207 count: (r.keywords ?? []).length,
208 })),
209 Effect.catchTag(["AccessDeniedException", "ValidationException"], (e) =>
210 Effect.succeed({ ok: false as const, tag: e._tag }),
211 ),
212 );
213 return yield* HttpServerResponse.json(result);
214 }
215
216 if (request.method === "GET" && pathname === "/notifications") {
217 const result = yield* listNotifications({ maxResults: 20 }).pipe(
218 Effect.map((r) => ({
219 ok: true as const,
220 count: (r.notifications ?? []).length,
221 })),
222 Effect.catchTag(["AccessDeniedException", "ValidationException"], (e) =>
223 Effect.succeed({ ok: false as const, tag: e._tag }),
224 ),
225 );
226 return yield* HttpServerResponse.json(result);
227 }
228
229 return yield* HttpServerResponse.json(
230 { error: "Not found", method: request.method, pathname },
231 { status: 404 },
232 );
233 }).pipe(Effect.orDie),
234 };
235 }).pipe(
236 Effect.provide(
237 Layer.mergeAll(
238 AuditManager.GetAccountStatusHttp,
239 AuditManager.GetServicesInScopeHttp,
240 AuditManager.GetInsightsHttp,
241 AuditManager.ListControlDomainInsightsHttp,
242 AuditManager.ListControlInsightsByControlDomainHttp,
243 AuditManager.GetDelegationsHttp,
244 AuditManager.GetEvidenceFileUploadUrlHttp,
245 AuditManager.ListAssessmentReportsHttp,
246 AuditManager.ValidateAssessmentReportIntegrityHttp,
247 AuditManager.ListKeywordsForDataSourceHttp,
248 AuditManager.ListNotificationsHttp,
249 ),
250 ),
251 ),
252 );