The best way to control Claude Code, Codex and more from any computer or phone

main
ts 291 lines 12.1 KB
Raw
1 import * as EKS from "@alchemy.run/aws/EKS";
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, "cluster-bindings-handler.ts");
10
11 // The gated test supplies standing infrastructure via env (same convention as
12 // Cluster.test.ts). The fallbacks keep the module importable when the test is
13 // skipped — the fixture is only ever deployed when the env vars are set.
14 const roleArn = process.env.AWS_TEST_EKS_ROLE_ARN ?? "arn:aws:iam::000000000000:role/placeholder";
15 const subnetIds = (process.env.AWS_TEST_EKS_SUBNET_IDS ?? "").split(",");
16
17 // Well-formed-but-nonexistent identifiers: each /probes call drives a
18 // cluster-scoped binding against one so the route proves the IAM grant, the
19 // cluster-name injection, and the typed error decode without provisioning the
20 // sub-resource.
21 const BOGUS = "alchemy-nonexistent-probe";
22 const BOGUS_PRINCIPAL_ARN = "arn:aws:iam::000000000000:role/alchemy-nonexistent-probe";
23
24 export class EKSClusterTestFunction extends Lambda.Function<Lambda.Function>()(
25 "EKSClusterTestFunction",
26 ) {}
27
28 /**
29 * Cluster-scoped binding fixture: deploys a real EKS control plane (~10 min —
30 * gated behind AWS_TEST_EKS_ROLE_ARN + AWS_TEST_EKS_SUBNET_IDS) and a Lambda
31 * bound to it with every cluster-scoped EKS binding.
32 */
33 export default EKSClusterTestFunction.make(
34 {
35 main,
36 functionUrl: true,
37 },
38 Effect.gen(function* () {
39 const cluster = yield* EKS.Cluster("BindingsCluster", {
40 clusterName: "alchemy-test-eks-bindings",
41 roleArn,
42 resourcesVpcConfig: {
43 subnetIds,
44 endpointPublicAccess: true,
45 endpointPrivateAccess: true,
46 },
47 // ListAccessEntries requires the access-entry API; CONFIG_MAP-only
48 // clusters reject it with InvalidRequestException.
49 accessConfig: { authenticationMode: "API_AND_CONFIG_MAP" },
50 });
51
52 const describeCluster = yield* EKS.DescribeCluster(cluster);
53 const listNodegroups = yield* EKS.ListNodegroups(cluster);
54 const listAddons = yield* EKS.ListAddons(cluster);
55 const listFargateProfiles = yield* EKS.ListFargateProfiles(cluster);
56 const listPodIdentityAssociations = yield* EKS.ListPodIdentityAssociations(cluster);
57 const listAccessEntries = yield* EKS.ListAccessEntries(cluster);
58 const listInsights = yield* EKS.ListInsights(cluster);
59 const listUpdates = yield* EKS.ListUpdates(cluster);
60 const listCapabilities = yield* EKS.ListCapabilities(cluster);
61 const listIdentityProviderConfigs = yield* EKS.ListIdentityProviderConfigs(cluster);
62 const listAssociatedAccessPolicies = yield* EKS.ListAssociatedAccessPolicies(cluster);
63 const describeNodegroup = yield* EKS.DescribeNodegroup(cluster);
64 const describeAddon = yield* EKS.DescribeAddon(cluster);
65 const describeFargateProfile = yield* EKS.DescribeFargateProfile(cluster);
66 const describePodIdentityAssociation = yield* EKS.DescribePodIdentityAssociation(cluster);
67 const describeAccessEntry = yield* EKS.DescribeAccessEntry(cluster);
68 const describeUpdate = yield* EKS.DescribeUpdate(cluster);
69 const describeInsight = yield* EKS.DescribeInsight(cluster);
70 const describeCapability = yield* EKS.DescribeCapability(cluster);
71 const describeIdentityProviderConfig = yield* EKS.DescribeIdentityProviderConfig(cluster);
72 const describeInsightsRefresh = yield* EKS.DescribeInsightsRefresh(cluster);
73 const startInsightsRefresh = yield* EKS.StartInsightsRefresh(cluster);
74
75 const bound = {
76 describeCluster,
77 listNodegroups,
78 listAddons,
79 listFargateProfiles,
80 listPodIdentityAssociations,
81 listAccessEntries,
82 listInsights,
83 listUpdates,
84 listAssociatedAccessPolicies,
85 describeNodegroup,
86 describeAddon,
87 describeFargateProfile,
88 describePodIdentityAssociation,
89 describeAccessEntry,
90 describeUpdate,
91 describeInsight,
92 listCapabilities,
93 listIdentityProviderConfigs,
94 describeCapability,
95 describeIdentityProviderConfig,
96 describeInsightsRefresh,
97 startInsightsRefresh,
98 };
99
100 return {
101 fetch: Effect.gen(function* () {
102 const request = yield* HttpServerRequest;
103 const url = new URL(request.originalUrl);
104 const pathname = url.pathname;
105
106 if (request.method === "GET" && pathname === "/bindings") {
107 return yield* HttpServerResponse.json({
108 bound: Object.keys(bound),
109 });
110 }
111
112 if (request.method === "GET" && pathname === "/cluster") {
113 const result = yield* describeCluster();
114 return yield* HttpServerResponse.json({
115 status: result.cluster?.status,
116 endpointPresent: typeof result.cluster?.endpoint === "string",
117 caPresent: typeof result.cluster?.certificateAuthority?.data === "string",
118 });
119 }
120
121 if (request.method === "GET" && pathname === "/lists") {
122 // A fresh control plane has none of these — an empty page from each
123 // proves the grant + clusterName injection + response decode.
124 const nodegroups = yield* listNodegroups();
125 const addons = yield* listAddons();
126 const fargateProfiles = yield* listFargateProfiles();
127 const podIdentityAssociations = yield* listPodIdentityAssociations();
128 const accessEntries = yield* listAccessEntries();
129 const insights = yield* listInsights();
130 const updates = yield* listUpdates();
131 const capabilities = yield* listCapabilities();
132 const identityProviderConfigs = yield* listIdentityProviderConfigs();
133 return yield* HttpServerResponse.json({
134 nodegroups: (nodegroups.nodegroups ?? []).length,
135 addons: (addons.addons ?? []).length,
136 fargateProfiles: (fargateProfiles.fargateProfileNames ?? []).length,
137 podIdentityAssociations: (podIdentityAssociations.associations ?? []).length,
138 accessEntries: (accessEntries.accessEntries ?? []).length,
139 insights: (insights.insights ?? []).length,
140 updates: (updates.updateIds ?? []).length,
141 capabilities: (capabilities.capabilities ?? []).length,
142 identityProviderConfigs: (identityProviderConfigs.identityProviderConfigs ?? []).length,
143 });
144 }
145
146 if (request.method === "GET" && pathname === "/probes") {
147 // Nonexistent sub-resources must surface the service's typed
148 // not-found tags — an IAM gap or a broken cluster-name injection
149 // would surface AccessDeniedException / a 500 instead.
150 const nodegroupTag = yield* describeNodegroup({
151 nodegroupName: BOGUS,
152 }).pipe(
153 Effect.map(() => "Found"),
154 Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)),
155 );
156 const addonTag = yield* describeAddon({ addonName: BOGUS }).pipe(
157 Effect.map(() => "Found"),
158 Effect.catchTag(["ResourceNotFoundException", "InvalidParameterException"], (e) =>
159 Effect.succeed(e._tag),
160 ),
161 );
162 const fargateProfileTag = yield* describeFargateProfile({
163 fargateProfileName: BOGUS,
164 }).pipe(
165 Effect.map(() => "Found"),
166 Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)),
167 );
168 const podIdentityTag = yield* describePodIdentityAssociation({
169 associationId: `a-${BOGUS}`,
170 }).pipe(
171 Effect.map(() => "Found"),
172 Effect.catchTag(["ResourceNotFoundException", "InvalidParameterException"], (e) =>
173 Effect.succeed(e._tag),
174 ),
175 );
176 const accessEntryTag = yield* describeAccessEntry({
177 principalArn: BOGUS_PRINCIPAL_ARN,
178 }).pipe(
179 Effect.map(() => "Found"),
180 Effect.catchTag(["ResourceNotFoundException", "InvalidRequestException"], (e) =>
181 Effect.succeed(e._tag),
182 ),
183 );
184 const updateTag = yield* describeUpdate({
185 updateId: "00000000-0000-0000-0000-000000000000",
186 }).pipe(
187 Effect.map(() => "Found"),
188 Effect.catchTag(["ResourceNotFoundException", "InvalidParameterException"], (e) =>
189 Effect.succeed(e._tag),
190 ),
191 );
192 const insightTag = yield* describeInsight({
193 id: "00000000-0000-0000-0000-000000000000",
194 }).pipe(
195 Effect.map(() => "Found"),
196 Effect.catchTag(["ResourceNotFoundException", "InvalidParameterException"], (e) =>
197 Effect.succeed(e._tag),
198 ),
199 );
200 const associatedPoliciesTag = yield* listAssociatedAccessPolicies({
201 principalArn: BOGUS_PRINCIPAL_ARN,
202 }).pipe(
203 Effect.map(() => "Found"),
204 Effect.catchTag(["ResourceNotFoundException", "InvalidRequestException"], (e) =>
205 Effect.succeed(e._tag),
206 ),
207 );
208 const capabilityTag = yield* describeCapability({
209 capabilityName: BOGUS,
210 }).pipe(
211 Effect.map(() => "Found"),
212 Effect.catchTag(["ResourceNotFoundException", "InvalidParameterException"], (e) =>
213 Effect.succeed(e._tag),
214 ),
215 );
216 const identityProviderConfigTag = yield* describeIdentityProviderConfig({
217 identityProviderConfig: { type: "oidc", name: BOGUS },
218 }).pipe(
219 Effect.map(() => "Found"),
220 Effect.catchTag(["ResourceNotFoundException", "InvalidParameterException"], (e) =>
221 Effect.succeed(e._tag),
222 ),
223 );
224 return yield* HttpServerResponse.json({
225 nodegroupTag,
226 addonTag,
227 fargateProfileTag,
228 podIdentityTag,
229 accessEntryTag,
230 updateTag,
231 insightTag,
232 associatedPoliciesTag,
233 capabilityTag,
234 identityProviderConfigTag,
235 });
236 }
237
238 if (request.method === "GET" && pathname === "/insights-refresh") {
239 // Kick an on-demand refresh, then read its status back — proves both
240 // grants. A refresh already in flight surfaces the service's typed
241 // InvalidRequestException, which proves the same wiring.
242 const startTag = yield* startInsightsRefresh().pipe(
243 Effect.map((r) => r.status ?? "STARTED"),
244 Effect.catchTag(["InvalidRequestException", "ResourceNotFoundException"], (e) =>
245 Effect.succeed(e._tag),
246 ),
247 );
248 const describeTag = yield* describeInsightsRefresh().pipe(
249 Effect.map((r) => r.status ?? "UNKNOWN"),
250 Effect.catchTag(["InvalidRequestException", "ResourceNotFoundException"], (e) =>
251 Effect.succeed(e._tag),
252 ),
253 );
254 return yield* HttpServerResponse.json({ startTag, describeTag });
255 }
256
257 return yield* HttpServerResponse.json(
258 { error: "Not found", method: request.method, pathname },
259 { status: 404 },
260 );
261 }).pipe(Effect.orDie),
262 };
263 }).pipe(
264 Effect.provide(
265 Layer.mergeAll(
266 EKS.DescribeClusterHttp,
267 EKS.ListNodegroupsHttp,
268 EKS.ListAddonsHttp,
269 EKS.ListFargateProfilesHttp,
270 EKS.ListPodIdentityAssociationsHttp,
271 EKS.ListAccessEntriesHttp,
272 EKS.ListInsightsHttp,
273 EKS.ListUpdatesHttp,
274 EKS.ListAssociatedAccessPoliciesHttp,
275 EKS.DescribeNodegroupHttp,
276 EKS.DescribeAddonHttp,
277 EKS.DescribeFargateProfileHttp,
278 EKS.DescribePodIdentityAssociationHttp,
279 EKS.DescribeAccessEntryHttp,
280 EKS.DescribeUpdateHttp,
281 EKS.DescribeInsightHttp,
282 EKS.ListCapabilitiesHttp,
283 EKS.ListIdentityProviderConfigsHttp,
284 EKS.DescribeCapabilityHttp,
285 EKS.DescribeIdentityProviderConfigHttp,
286 EKS.DescribeInsightsRefreshHttp,
287 EKS.StartInsightsRefreshHttp,
288 ),
289 ),
290 ),
291 );