@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 374 lines 15.1 KB
Raw
1 import * as Inspector2 from "@alchemy.run/aws/Inspector2";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
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 export class Inspector2TestFunction extends Lambda.Function<Lambda.Function>()(
13 "Inspector2TestFunction",
14 ) {}
15
16 /**
17 * Routes answer `{ …fields }` on success or `{ errorTag }` when the
18 * operation fails with a TYPED error — the test asserts the tag is in a
19 * route-specific allowlist, which proves both the binding wiring and the
20 * IAM grant. An untyped error crashes into a 500 instead.
21 */
22 const errorTagged = <A, E extends { _tag: string }, R>(
23 effect: Effect.Effect<A, E, R>,
24 ): Effect.Effect<A | { errorTag: string; errorMessage?: string }, never, R> =>
25 effect.pipe(
26 Effect.map((a): A | { errorTag: string; errorMessage?: string } => a),
27 Effect.catch((e) =>
28 Effect.succeed({
29 errorTag: e._tag,
30 errorMessage: (e as { message?: string }).message ?? (e as { Message?: string }).Message,
31 }),
32 ),
33 );
34
35 // One binding per Inspector2 capability. Write/org-admin operations with
36 // irreversible or cross-account side effects (UpdateConfiguration changes
37 // account scan settings; AssociateMember / Enable-DisableDelegatedAdmin
38 // require an organization; UpdateEncryptionKey re-keys scan results) are
39 // bound — proving init + IAM deploy — but not exercised against live state.
40 export default Inspector2TestFunction.make(
41 {
42 main,
43 functionUrl: true,
44 timeout: Duration.seconds(30),
45 },
46 Effect.gen(function* () {
47 // Findings & code snippets
48 const listFindings = yield* Inspector2.ListFindings();
49 const listFindingAggregations = yield* Inspector2.ListFindingAggregations();
50 const batchGetFindingDetails = yield* Inspector2.BatchGetFindingDetails();
51 const batchGetCodeSnippet = yield* Inspector2.BatchGetCodeSnippet();
52
53 // Coverage & vulnerability intel
54 const listCoverage = yield* Inspector2.ListCoverage();
55 const listCoverageStatistics = yield* Inspector2.ListCoverageStatistics();
56 const searchVulnerabilities = yield* Inspector2.SearchVulnerabilities();
57 const getClustersForImage = yield* Inspector2.GetClustersForImage();
58
59 // Findings reports & SBOM exports
60 const createFindingsReport = yield* Inspector2.CreateFindingsReport();
61 const getFindingsReportStatus = yield* Inspector2.GetFindingsReportStatus();
62 const cancelFindingsReport = yield* Inspector2.CancelFindingsReport();
63 const createSbomExport = yield* Inspector2.CreateSbomExport();
64 const getSbomExport = yield* Inspector2.GetSbomExport();
65 const cancelSbomExport = yield* Inspector2.CancelSbomExport();
66
67 // CIS scan results
68 const listCisScans = yield* Inspector2.ListCisScans();
69 const listCisScanResultsAggregatedByChecks =
70 yield* Inspector2.ListCisScanResultsAggregatedByChecks();
71 const listCisScanResultsAggregatedByTargetResource =
72 yield* Inspector2.ListCisScanResultsAggregatedByTargetResource();
73 const getCisScanResultDetails = yield* Inspector2.GetCisScanResultDetails();
74 const getCisScanReport = yield* Inspector2.GetCisScanReport();
75
76 // Code security scans
77 const startCodeSecurityScan = yield* Inspector2.StartCodeSecurityScan();
78 const getCodeSecurityScan = yield* Inspector2.GetCodeSecurityScan();
79
80 // Account settings & usage
81 const listUsageTotals = yield* Inspector2.ListUsageTotals();
82 const batchGetFreeTrialInfo = yield* Inspector2.BatchGetFreeTrialInfo();
83 const listAccountPermissions = yield* Inspector2.ListAccountPermissions();
84 const getConfiguration = yield* Inspector2.GetConfiguration();
85 const updateConfiguration = yield* Inspector2.UpdateConfiguration();
86 const getEc2DeepInspectionConfiguration = yield* Inspector2.GetEc2DeepInspectionConfiguration();
87 const updateEc2DeepInspectionConfiguration =
88 yield* Inspector2.UpdateEc2DeepInspectionConfiguration();
89 const getEncryptionKey = yield* Inspector2.GetEncryptionKey();
90 const updateEncryptionKey = yield* Inspector2.UpdateEncryptionKey();
91 const resetEncryptionKey = yield* Inspector2.ResetEncryptionKey();
92
93 // Organization & members
94 const getMember = yield* Inspector2.GetMember();
95 const listMembers = yield* Inspector2.ListMembers();
96 const associateMember = yield* Inspector2.AssociateMember();
97 const disassociateMember = yield* Inspector2.DisassociateMember();
98 const getDelegatedAdminAccount = yield* Inspector2.GetDelegatedAdminAccount();
99 const listDelegatedAdminAccounts = yield* Inspector2.ListDelegatedAdminAccounts();
100 const enableDelegatedAdminAccount = yield* Inspector2.EnableDelegatedAdminAccount();
101 const disableDelegatedAdminAccount = yield* Inspector2.DisableDelegatedAdminAccount();
102 const describeOrganizationConfiguration = yield* Inspector2.DescribeOrganizationConfiguration();
103 const updateOrganizationConfiguration = yield* Inspector2.UpdateOrganizationConfiguration();
104 const updateOrgEc2DeepInspectionConfiguration =
105 yield* Inspector2.UpdateOrgEc2DeepInspectionConfiguration();
106 const batchGetMemberEc2DeepInspectionStatus =
107 yield* Inspector2.BatchGetMemberEc2DeepInspectionStatus();
108 const batchUpdateMemberEc2DeepInspectionStatus =
109 yield* Inspector2.BatchUpdateMemberEc2DeepInspectionStatus();
110
111 const bound = {
112 listFindings,
113 listFindingAggregations,
114 batchGetFindingDetails,
115 batchGetCodeSnippet,
116 listCoverage,
117 listCoverageStatistics,
118 searchVulnerabilities,
119 getClustersForImage,
120 createFindingsReport,
121 getFindingsReportStatus,
122 cancelFindingsReport,
123 createSbomExport,
124 getSbomExport,
125 cancelSbomExport,
126 listCisScans,
127 listCisScanResultsAggregatedByChecks,
128 listCisScanResultsAggregatedByTargetResource,
129 getCisScanResultDetails,
130 getCisScanReport,
131 startCodeSecurityScan,
132 getCodeSecurityScan,
133 listUsageTotals,
134 batchGetFreeTrialInfo,
135 listAccountPermissions,
136 getConfiguration,
137 updateConfiguration,
138 getEc2DeepInspectionConfiguration,
139 updateEc2DeepInspectionConfiguration,
140 getEncryptionKey,
141 updateEncryptionKey,
142 resetEncryptionKey,
143 getMember,
144 listMembers,
145 associateMember,
146 disassociateMember,
147 getDelegatedAdminAccount,
148 listDelegatedAdminAccounts,
149 enableDelegatedAdminAccount,
150 disableDelegatedAdminAccount,
151 describeOrganizationConfiguration,
152 updateOrganizationConfiguration,
153 updateOrgEc2DeepInspectionConfiguration,
154 batchGetMemberEc2DeepInspectionStatus,
155 batchUpdateMemberEc2DeepInspectionStatus,
156 };
157
158 return {
159 fetch: Effect.gen(function* () {
160 const request = yield* HttpServerRequest;
161 const url = new URL(request.originalUrl);
162 const pathname = url.pathname;
163
164 if (request.method === "GET" && pathname === "/bindings") {
165 return yield* HttpServerResponse.json({
166 bound: Object.keys(bound),
167 });
168 }
169
170 // Findings — works whether or not Inspector scanning is enabled
171 // (an un-enabled account just has none).
172 if (request.method === "GET" && pathname === "/findings") {
173 const result = yield* errorTagged(
174 listFindings({ maxResults: 10 }).pipe(
175 Effect.map((r) => ({ count: (r.findings ?? []).length })),
176 ),
177 );
178 return yield* HttpServerResponse.json(result);
179 }
180
181 if (request.method === "GET" && pathname === "/coverage") {
182 const result = yield* errorTagged(
183 listCoverage({ maxResults: 10 }).pipe(
184 Effect.map((r) => ({
185 count: (r.coveredResources ?? []).length,
186 })),
187 ),
188 );
189 return yield* HttpServerResponse.json(result);
190 }
191
192 // Vulnerability intel — the public vulnerability database.
193 if (request.method === "GET" && pathname === "/vulnerability") {
194 const id = url.searchParams.get("id") ?? "CVE-2021-44228";
195 const result = yield* errorTagged(
196 searchVulnerabilities({
197 filterCriteria: { vulnerabilityIds: [id] },
198 }).pipe(
199 Effect.map((r) => ({
200 ids: (r.vulnerabilities ?? []).map((v) => v.id),
201 })),
202 ),
203 );
204 return yield* HttpServerResponse.json(result);
205 }
206
207 if (request.method === "GET" && pathname === "/usage") {
208 const result = yield* errorTagged(
209 listUsageTotals({}).pipe(Effect.map((r) => ({ totals: (r.totals ?? []).length }))),
210 );
211 return yield* HttpServerResponse.json(result);
212 }
213
214 if (request.method === "GET" && pathname === "/account-permissions") {
215 const result = yield* errorTagged(
216 listAccountPermissions({}).pipe(
217 Effect.map((r) => ({
218 permissions: (r.permissions ?? []).length,
219 })),
220 ),
221 );
222 return yield* HttpServerResponse.json(result);
223 }
224
225 if (request.method === "GET" && pathname === "/free-trial") {
226 const account = url.searchParams.get("account") ?? "";
227 const result = yield* errorTagged(
228 batchGetFreeTrialInfo({ accountIds: [account] }).pipe(
229 Effect.map((r) => ({
230 accounts: (r.accounts ?? []).length,
231 failed: (r.failedAccounts ?? []).length,
232 })),
233 ),
234 );
235 return yield* HttpServerResponse.json(result);
236 }
237
238 if (request.method === "GET" && pathname === "/config") {
239 const result = yield* errorTagged(
240 getConfiguration({}).pipe(
241 Effect.map((r) => ({
242 hasEcrConfiguration: r.ecrConfiguration !== undefined,
243 hasEc2Configuration: r.ec2Configuration !== undefined,
244 })),
245 ),
246 );
247 return yield* HttpServerResponse.json(result);
248 }
249
250 if (request.method === "GET" && pathname === "/deep-inspection") {
251 const result = yield* errorTagged(
252 getEc2DeepInspectionConfiguration({}).pipe(
253 Effect.map((r) => ({ status: r.status ?? "UNSET" })),
254 ),
255 );
256 return yield* HttpServerResponse.json(result);
257 }
258
259 if (request.method === "GET" && pathname === "/encryption-key") {
260 const result = yield* errorTagged(
261 getEncryptionKey({
262 resourceType: "AWS_ECR_CONTAINER_IMAGE",
263 scanType: "PACKAGE",
264 }).pipe(Effect.map((r) => ({ kmsKeyId: r.kmsKeyId }))),
265 );
266 return yield* HttpServerResponse.json(result);
267 }
268
269 if (request.method === "GET" && pathname === "/cis-scans") {
270 const result = yield* errorTagged(
271 listCisScans({}).pipe(Effect.map((r) => ({ scans: (r.scans ?? []).length }))),
272 );
273 return yield* HttpServerResponse.json(result);
274 }
275
276 if (request.method === "GET" && pathname === "/members") {
277 const result = yield* errorTagged(
278 listMembers({}).pipe(Effect.map((r) => ({ members: (r.members ?? []).length }))),
279 );
280 return yield* HttpServerResponse.json(result);
281 }
282
283 // Org administration: a standalone (non-organization) account gets a
284 // typed rejection, which still proves the binding + IAM wiring.
285 if (request.method === "GET" && pathname === "/org-config") {
286 const result = yield* errorTagged(
287 describeOrganizationConfiguration({}).pipe(
288 Effect.map((r) => ({
289 maxAccountLimitReached: r.maxAccountLimitReached ?? false,
290 })),
291 ),
292 );
293 return yield* HttpServerResponse.json(result);
294 }
295
296 if (request.method === "GET" && pathname === "/delegated-admin") {
297 const result = yield* errorTagged(
298 getDelegatedAdminAccount({}).pipe(
299 Effect.map((r) => ({
300 accountId: r.delegatedAdmin?.accountId,
301 })),
302 ),
303 );
304 return yield* HttpServerResponse.json(result);
305 }
306
307 // Report status for a well-formed but nonexistent report id — a
308 // typed ResourceNotFoundException proves the binding end-to-end.
309 if (request.method === "GET" && pathname === "/report-status") {
310 const result = yield* errorTagged(
311 getFindingsReportStatus({
312 reportId: url.searchParams.get("id") ?? "00000000-0000-0000-0000-000000000000",
313 }).pipe(Effect.map((r) => ({ status: r.status }))),
314 );
315 return yield* HttpServerResponse.json(result);
316 }
317
318 return yield* HttpServerResponse.json(
319 { error: "Not found", method: request.method, pathname },
320 { status: 404 },
321 );
322 }).pipe(Effect.orDie),
323 };
324 }).pipe(
325 Effect.provide(
326 Layer.mergeAll(
327 Inspector2.AssociateMemberHttp,
328 Inspector2.BatchGetCodeSnippetHttp,
329 Inspector2.BatchGetFindingDetailsHttp,
330 Inspector2.BatchGetFreeTrialInfoHttp,
331 Inspector2.BatchGetMemberEc2DeepInspectionStatusHttp,
332 Inspector2.BatchUpdateMemberEc2DeepInspectionStatusHttp,
333 Inspector2.CancelFindingsReportHttp,
334 Inspector2.CancelSbomExportHttp,
335 Inspector2.CreateFindingsReportHttp,
336 Inspector2.CreateSbomExportHttp,
337 Inspector2.DescribeOrganizationConfigurationHttp,
338 Inspector2.DisableDelegatedAdminAccountHttp,
339 Inspector2.DisassociateMemberHttp,
340 Inspector2.EnableDelegatedAdminAccountHttp,
341 Inspector2.GetCisScanReportHttp,
342 Inspector2.GetCisScanResultDetailsHttp,
343 Inspector2.GetClustersForImageHttp,
344 Inspector2.GetCodeSecurityScanHttp,
345 Inspector2.GetConfigurationHttp,
346 Inspector2.GetDelegatedAdminAccountHttp,
347 Inspector2.GetEc2DeepInspectionConfigurationHttp,
348 Inspector2.GetEncryptionKeyHttp,
349 Inspector2.GetFindingsReportStatusHttp,
350 Inspector2.GetMemberHttp,
351 Inspector2.GetSbomExportHttp,
352 Inspector2.ListAccountPermissionsHttp,
353 Inspector2.ListCisScanResultsAggregatedByChecksHttp,
354 Inspector2.ListCisScanResultsAggregatedByTargetResourceHttp,
355 Inspector2.ListCisScansHttp,
356 Inspector2.ListCoverageHttp,
357 Inspector2.ListCoverageStatisticsHttp,
358 Inspector2.ListDelegatedAdminAccountsHttp,
359 Inspector2.ListFindingAggregationsHttp,
360 Inspector2.ListFindingsHttp,
361 Inspector2.ListMembersHttp,
362 Inspector2.ListUsageTotalsHttp,
363 Inspector2.ResetEncryptionKeyHttp,
364 Inspector2.SearchVulnerabilitiesHttp,
365 Inspector2.StartCodeSecurityScanHttp,
366 Inspector2.UpdateConfigurationHttp,
367 Inspector2.UpdateEc2DeepInspectionConfigurationHttp,
368 Inspector2.UpdateEncryptionKeyHttp,
369 Inspector2.UpdateOrgEc2DeepInspectionConfigurationHttp,
370 Inspector2.UpdateOrganizationConfigurationHttp,
371 ),
372 ),
373 ),
374 );