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

main
ts 131 lines 4.72 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { Cluster } from "./Cluster.ts";
5
6 /**
7 * Shared scaffolding for AWS EKS HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, makeEKS…HttpBinding({ … }))` over one of the
11 * builders below. Everything except the operation, the request key carrying
12 * the cluster name, and the IAM action list is boilerplate.
13 */
14
15 /**
16 * Build the impl Effect for an account-level operation (cluster enumeration,
17 * managed access-policy catalog, Kubernetes/add-on version catalogs). The
18 * deploy-time half grants `actions` on `*` — these read-only catalog and
19 * enumeration actions span the whole account/region and take no resource ARN.
20 */
21 export const makeEKSAccountHttpBinding = <I, A, E, R>(options: {
22 /** Fully-qualified binding tag, e.g. `AWS.EKS.ListClusters`. */
23 tag: string;
24 /** The distilled operation, invoked with the caller's request as-is. */
25 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
26 /** IAM actions granted on `*`. */
27 actions: readonly string[];
28 }) =>
29 Effect.gen(function* () {
30 const bind = yield* AWSBindings.Bind;
31 const op = yield* AWSBindings.withCredentials(options.operation);
32
33 return Effect.fn(function* () {
34 if (!globalThis.__ALCHEMY_RUNTIME__) {
35 yield* bind`${options.tag}()`({
36 policyStatements: [
37 {
38 Effect: "Allow",
39 Action: [...options.actions],
40 Resource: ["*"],
41 },
42 ],
43 });
44 }
45 return Effect.fn(options.tag)(function* (request?: I) {
46 return yield* op((request ?? {}) as I);
47 });
48 });
49 });
50
51 /**
52 * Which ARNs an action authorizes against, per the EKS service authorization
53 * reference:
54 *
55 * - `cluster` — the cluster ARN itself (`DescribeCluster`, the `List*`
56 * enumerations, insights).
57 * - `subresources` — the cluster's sub-resources. Nodegroups, add-ons,
58 * Fargate profiles, pod identity associations, and access entries all share
59 * the ARN shape `arn:aws:eks:{region}:{acct}:{type}/{clusterName}/…`, so a
60 * single wildcard pattern derived from the cluster ARN (resource type
61 * replaced by a `*` and a trailing `/{star}` appended) covers them without
62 * granting anything on other clusters.
63 * - `both` — actions like `ListUpdates`/`DescribeUpdate` that authorize
64 * against the cluster OR a sub-resource depending on the request.
65 */
66 export type EKSIamScope = "cluster" | "subresources" | "both";
67
68 const scopeResources = (cluster: Cluster, scope: EKSIamScope) => {
69 const clusterArn = Output.interpolate`${cluster.clusterArn}`;
70 const subresources = Output.map(
71 cluster.clusterArn,
72 (arn: string): string => `${arn.replace(":cluster/", ":*/")}/*`,
73 );
74 return scope === "cluster"
75 ? [clusterArn]
76 : scope === "subresources"
77 ? [subresources]
78 : [clusterArn, subresources];
79 };
80
81 /**
82 * Build the impl Effect for a cluster-scoped operation: the runtime callable
83 * injects the bound {@link Cluster}'s name under `key` (`clusterName` for the
84 * sub-resource lists and insights, `name` for `DescribeCluster`) and the
85 * deploy-time half grants `actions` on the cluster ARN and/or its
86 * sub-resource ARNs per `scope`.
87 */
88 export const makeEKSClusterHttpBinding = <
89 I extends { clusterName?: string } | { name?: string },
90 A,
91 E,
92 R,
93 >(options: {
94 /** Fully-qualified binding tag, e.g. `AWS.EKS.ListNodegroups`. */
95 tag: string;
96 /** The distilled operation; the cluster name is injected from the cluster. */
97 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
98 /** IAM actions granted per `scope`. */
99 actions: readonly string[];
100 /** Request key carrying the cluster name. */
101 key: "clusterName" | "name";
102 /** Which ARNs the actions authorize against. */
103 scope: EKSIamScope;
104 }) =>
105 Effect.gen(function* () {
106 const bind = yield* AWSBindings.Bind;
107 const op = yield* AWSBindings.withCredentials(options.operation);
108
109 return Effect.fn(function* (cluster: Cluster) {
110 const clusterName = yield* cluster.clusterName;
111 if (!globalThis.__ALCHEMY_RUNTIME__) {
112 yield* bind`${options.tag}(${cluster})`({
113 policyStatements: [
114 {
115 Effect: "Allow",
116 Action: [...options.actions],
117 Resource: scopeResources(cluster, options.scope),
118 },
119 ],
120 });
121 }
122 return Effect.fn(`${options.tag}(${cluster.LogicalId})`)(function* (
123 request?: Omit<I, "clusterName" | "name">,
124 ) {
125 return yield* op({
126 ...request,
127 [options.key]: yield* clusterName,
128 } as I);
129 });
130 });
131 });