@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, 04:45 UTC

main
ts 323 lines 12.4 KB
Raw
1 import * as FMS from "@alchemy.run/aws/FMS";
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 FMSTestFunction extends Lambda.Function<Lambda.Function>()("FMSTestFunction") {}
13
14 // The testing organization is deliberately NOT onboarded to Firewall Manager
15 // (see AdminAccount.test.ts — onboarding blocks offboarding for >10 minutes),
16 // so most routes below prove the binding + IAM grant by observing the exact
17 // typed not-an-FMS-admin rejection instead of data:
18 // - admin-scoped ops (ListPolicies, ListResourceSets, ListMemberAccounts,
19 // GetNotificationChannel, ListAppsLists, ListProtocolsLists) reject with
20 // `AccessDeniedException: No default admin could be found for account …`
21 // - `ListAdminAccountsForOrganization` (management-account-only) rejects
22 // with `InvalidOperationException: No default admin could be found for
23 // organization …`
24 // - `ListAdminsManagingAccount` rejects with `ResourceNotFoundException:
25 // The referenced item does not exist.` when the org has no FMS admins
26 // (all verified by live probe). A genuine IAM gap would surface as an
27 // UNCAUGHT AccessDeniedException with an "not authorized to perform" message
28 // on the admin-management routes, which never catch it — and on an FMS-
29 // onboarded account every route returns Ok data unchanged.
30 export default FMSTestFunction.make(
31 {
32 main,
33 functionUrl: true,
34 timeout: Duration.seconds(30),
35 },
36 Effect.gen(function* () {
37 // --- policies ---
38 const getPolicy = yield* FMS.GetPolicy();
39 const listPolicies = yield* FMS.ListPolicies();
40 const putPolicy = yield* FMS.PutPolicy();
41 const deletePolicy = yield* FMS.DeletePolicy();
42
43 // --- applications lists ---
44 const getAppsList = yield* FMS.GetAppsList();
45 const listAppsLists = yield* FMS.ListAppsLists();
46 const putAppsList = yield* FMS.PutAppsList();
47 const deleteAppsList = yield* FMS.DeleteAppsList();
48
49 // --- protocols lists ---
50 const getProtocolsList = yield* FMS.GetProtocolsList();
51 const listProtocolsLists = yield* FMS.ListProtocolsLists();
52 const putProtocolsList = yield* FMS.PutProtocolsList();
53 const deleteProtocolsList = yield* FMS.DeleteProtocolsList();
54
55 // --- resource sets ---
56 const getResourceSet = yield* FMS.GetResourceSet();
57 const listResourceSets = yield* FMS.ListResourceSets();
58 const putResourceSet = yield* FMS.PutResourceSet();
59 const deleteResourceSet = yield* FMS.DeleteResourceSet();
60 const listResourceSetResources = yield* FMS.ListResourceSetResources();
61 const batchAssociateResource = yield* FMS.BatchAssociateResource();
62 const batchDisassociateResource = yield* FMS.BatchDisassociateResource();
63 const listDiscoveredResources = yield* FMS.ListDiscoveredResources();
64
65 // --- compliance and protection status ---
66 const getComplianceDetail = yield* FMS.GetComplianceDetail();
67 const listComplianceStatus = yield* FMS.ListComplianceStatus();
68 const getProtectionStatus = yield* FMS.GetProtectionStatus();
69 const getViolationDetails = yield* FMS.GetViolationDetails();
70 const listMemberAccounts = yield* FMS.ListMemberAccounts();
71
72 // --- notification channel ---
73 const getNotificationChannel = yield* FMS.GetNotificationChannel();
74 const putNotificationChannel = yield* FMS.PutNotificationChannel();
75 const deleteNotificationChannel = yield* FMS.DeleteNotificationChannel();
76
77 // --- administrator management (pinned to us-east-1) ---
78 const getAdminScope = yield* FMS.GetAdminScope();
79 const listAdminAccountsForOrganization = yield* FMS.ListAdminAccountsForOrganization();
80 const listAdminsManagingAccount = yield* FMS.ListAdminsManagingAccount();
81
82 // --- third-party firewalls (marketplace-subscription gated) ---
83 const associateThirdPartyFirewall = yield* FMS.AssociateThirdPartyFirewall();
84 const disassociateThirdPartyFirewall = yield* FMS.DisassociateThirdPartyFirewall();
85 const getThirdPartyFirewallAssociationStatus =
86 yield* FMS.GetThirdPartyFirewallAssociationStatus();
87 const listThirdPartyFirewallFirewallPolicies =
88 yield* FMS.ListThirdPartyFirewallFirewallPolicies();
89
90 const bound = {
91 getPolicy,
92 listPolicies,
93 putPolicy,
94 deletePolicy,
95 getAppsList,
96 listAppsLists,
97 putAppsList,
98 deleteAppsList,
99 getProtocolsList,
100 listProtocolsLists,
101 putProtocolsList,
102 deleteProtocolsList,
103 getResourceSet,
104 listResourceSets,
105 putResourceSet,
106 deleteResourceSet,
107 listResourceSetResources,
108 batchAssociateResource,
109 batchDisassociateResource,
110 listDiscoveredResources,
111 getComplianceDetail,
112 listComplianceStatus,
113 getProtectionStatus,
114 getViolationDetails,
115 listMemberAccounts,
116 getNotificationChannel,
117 putNotificationChannel,
118 deleteNotificationChannel,
119 getAdminScope,
120 listAdminAccountsForOrganization,
121 listAdminsManagingAccount,
122 associateThirdPartyFirewall,
123 disassociateThirdPartyFirewall,
124 getThirdPartyFirewallAssociationStatus,
125 listThirdPartyFirewallFirewallPolicies,
126 };
127
128 return {
129 fetch: Effect.gen(function* () {
130 const request = yield* HttpServerRequest;
131 const url = new URL(request.originalUrl);
132 const pathname = url.pathname;
133
134 if (request.method === "GET" && pathname === "/bindings") {
135 return yield* HttpServerResponse.json({
136 bound: Object.keys(bound),
137 });
138 }
139
140 if (request.method === "GET" && pathname === "/admins-managing-account") {
141 const result = yield* listAdminsManagingAccount().pipe(
142 Effect.map((r) => ({
143 tag: "Ok",
144 count: (r.AdminAccounts ?? []).length,
145 })),
146 Effect.catchTag("ResourceNotFoundException", (e) =>
147 Effect.succeed({ tag: e._tag, count: 0 }),
148 ),
149 );
150 return yield* HttpServerResponse.json(result);
151 }
152
153 if (request.method === "GET" && pathname === "/admin-accounts-for-organization") {
154 const result = yield* listAdminAccountsForOrganization().pipe(
155 Effect.map((r) => ({
156 tag: "Ok",
157 count: (r.AdminAccounts ?? []).length,
158 })),
159 Effect.catchTag(["InvalidOperationException", "ResourceNotFoundException"], (e) =>
160 Effect.succeed({ tag: e._tag, count: 0 }),
161 ),
162 );
163 return yield* HttpServerResponse.json(result);
164 }
165
166 if (request.method === "GET" && pathname === "/policies") {
167 const result = yield* listPolicies().pipe(
168 Effect.map((r) => ({
169 tag: "Ok",
170 count: (r.PolicyList ?? []).length,
171 })),
172 Effect.catchTag(
173 ["AccessDeniedException", "InvalidOperationException", "ResourceNotFoundException"],
174 (e) => Effect.succeed({ tag: e._tag, count: 0 }),
175 ),
176 );
177 return yield* HttpServerResponse.json(result);
178 }
179
180 if (request.method === "GET" && pathname === "/resource-sets") {
181 const result = yield* listResourceSets().pipe(
182 Effect.map((r) => ({
183 tag: "Ok",
184 count: (r.ResourceSets ?? []).length,
185 })),
186 Effect.catchTag(["AccessDeniedException", "InvalidOperationException"], (e) =>
187 Effect.succeed({ tag: e._tag, count: 0 }),
188 ),
189 );
190 return yield* HttpServerResponse.json(result);
191 }
192
193 if (request.method === "GET" && pathname === "/member-accounts") {
194 const result = yield* listMemberAccounts().pipe(
195 Effect.map((r) => ({
196 tag: "Ok",
197 count: (r.MemberAccounts ?? []).length,
198 })),
199 Effect.catchTag(["AccessDeniedException", "ResourceNotFoundException"], (e) =>
200 Effect.succeed({ tag: e._tag, count: 0 }),
201 ),
202 );
203 return yield* HttpServerResponse.json(result);
204 }
205
206 if (request.method === "GET" && pathname === "/notification-channel") {
207 const result = yield* getNotificationChannel().pipe(
208 Effect.map((r) => ({
209 tag: "Ok",
210 topicArn: r.SnsTopicArn ?? null,
211 })),
212 Effect.catchTag(
213 ["AccessDeniedException", "InvalidOperationException", "ResourceNotFoundException"],
214 (e) => Effect.succeed({ tag: e._tag, topicArn: null }),
215 ),
216 );
217 return yield* HttpServerResponse.json(result);
218 }
219
220 if (request.method === "GET" && pathname === "/apps-lists") {
221 const result = yield* listAppsLists({ MaxResults: 10 }).pipe(
222 Effect.map((r) => ({
223 tag: "Ok",
224 count: (r.AppsLists ?? []).length,
225 })),
226 Effect.catchTag(
227 ["AccessDeniedException", "InvalidOperationException", "ResourceNotFoundException"],
228 (e) => Effect.succeed({ tag: e._tag, count: 0 }),
229 ),
230 );
231 return yield* HttpServerResponse.json(result);
232 }
233
234 if (request.method === "GET" && pathname === "/protocols-lists") {
235 const result = yield* listProtocolsLists({ MaxResults: 10 }).pipe(
236 Effect.map((r) => ({
237 tag: "Ok",
238 count: (r.ProtocolsLists ?? []).length,
239 })),
240 Effect.catchTag(
241 ["AccessDeniedException", "InvalidOperationException", "ResourceNotFoundException"],
242 (e) => Effect.succeed({ tag: e._tag, count: 0 }),
243 ),
244 );
245 return yield* HttpServerResponse.json(result);
246 }
247
248 if (request.method === "GET" && pathname === "/third-party-firewall-status") {
249 const result = yield* getThirdPartyFirewallAssociationStatus({
250 ThirdPartyFirewall: "PALO_ALTO_NETWORKS_CLOUD_NGFW",
251 }).pipe(
252 Effect.map((r) => ({
253 tag: "Ok",
254 status: r.ThirdPartyFirewallStatus ?? null,
255 marketplaceStatus: r.MarketplaceOnboardingStatus ?? null,
256 })),
257 Effect.catchTag(
258 [
259 "AccessDeniedException",
260 "InvalidOperationException",
261 "InvalidInputException",
262 "ResourceNotFoundException",
263 ],
264 (e) =>
265 Effect.succeed({
266 tag: e._tag,
267 status: null,
268 marketplaceStatus: null,
269 }),
270 ),
271 );
272 return yield* HttpServerResponse.json(result);
273 }
274
275 return yield* HttpServerResponse.json(
276 { error: "Not found", method: request.method, pathname },
277 { status: 404 },
278 );
279 }).pipe(Effect.orDie),
280 };
281 }).pipe(
282 Effect.provide(
283 Layer.mergeAll(
284 Lambda.EventSource,
285 FMS.GetPolicyHttp,
286 FMS.ListPoliciesHttp,
287 FMS.PutPolicyHttp,
288 FMS.DeletePolicyHttp,
289 FMS.GetAppsListHttp,
290 FMS.ListAppsListsHttp,
291 FMS.PutAppsListHttp,
292 FMS.DeleteAppsListHttp,
293 FMS.GetProtocolsListHttp,
294 FMS.ListProtocolsListsHttp,
295 FMS.PutProtocolsListHttp,
296 FMS.DeleteProtocolsListHttp,
297 FMS.GetResourceSetHttp,
298 FMS.ListResourceSetsHttp,
299 FMS.PutResourceSetHttp,
300 FMS.DeleteResourceSetHttp,
301 FMS.ListResourceSetResourcesHttp,
302 FMS.BatchAssociateResourceHttp,
303 FMS.BatchDisassociateResourceHttp,
304 FMS.ListDiscoveredResourcesHttp,
305 FMS.GetComplianceDetailHttp,
306 FMS.ListComplianceStatusHttp,
307 FMS.GetProtectionStatusHttp,
308 FMS.GetViolationDetailsHttp,
309 FMS.ListMemberAccountsHttp,
310 FMS.GetNotificationChannelHttp,
311 FMS.PutNotificationChannelHttp,
312 FMS.DeleteNotificationChannelHttp,
313 FMS.GetAdminScopeHttp,
314 FMS.ListAdminAccountsForOrganizationHttp,
315 FMS.ListAdminsManagingAccountHttp,
316 FMS.AssociateThirdPartyFirewallHttp,
317 FMS.DisassociateThirdPartyFirewallHttp,
318 FMS.GetThirdPartyFirewallAssociationStatusHttp,
319 FMS.ListThirdPartyFirewallFirewallPoliciesHttp,
320 ),
321 ),
322 ),
323 );