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

main
ts 187 lines 6.83 KB
Raw
1 import * as Binding from "alchemy/Binding";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import { isTask } from "../ECS/Task.ts";
5 import type { Function } from "../Lambda/Function.ts";
6 import type { AccessPoint } from "./AccessPoint.ts";
7 import type { FileSystem } from "./FileSystem.ts";
8
9 /**
10 * Options for {@link Mount}.
11 */
12 export interface MountOptions {
13 /**
14 * Local path the file system is mounted at inside the compute environment.
15 * On Lambda the path must begin with `/mnt/` (e.g. `/mnt/data`); on ECS any
16 * absolute container path works.
17 */
18 path: string;
19 /**
20 * Mount read-only: the role is granted `elasticfilesystem:ClientMount`
21 * only (no `ClientWrite`), and on ECS the container mount point is marked
22 * `readOnly`.
23 * @default false
24 */
25 readOnly?: boolean;
26 }
27
28 /**
29 * The runtime view of a mounted EFS file system: the local path it is
30 * available at inside the running Function/Task.
31 */
32 export interface MountedFileSystem {
33 /** The local mount path (same value as {@link MountOptions.path}). */
34 path: string;
35 }
36
37 /**
38 * Host-agnostic EFS mount binding.
39 *
40 * `yield* EFS.mount(accessPoint, { path: "/mnt/data" })` inside a compute
41 * body wires the file system into whatever host the code deploys to:
42 *
43 * - **Lambda** — injects a `FileSystemConfigs` entry (the access point ARN +
44 * local mount path) through the Function's binding channel and grants the
45 * execution role `elasticfilesystem:ClientMount`/`ClientWrite` scoped to
46 * the access point. Lambda requires an `AWS.EFS.AccessPoint` (not a bare
47 * file system) and a `/mnt/…` path, and the Function must have `vpc` set
48 * to subnets that can reach an EFS mount target.
49 * - **ECS Task** — injects a task-level EFS volume (transit encryption on,
50 * IAM auth on, access-point scoped when one is given) plus a container
51 * mount point, and grants the task role the matching client actions.
52 *
53 * Provide the `EFS.MountLive` layer on the Function/Task Effect to satisfy
54 * the binding. Mount targets for the file system's VPC/subnets must already
55 * exist (`AWS.EFS.MountTarget`).
56 *
57 * @binding
58 */
59 export interface Mount extends Binding.Service<
60 Mount,
61 "AWS.EFS.Mount",
62 (target: AccessPoint | FileSystem, options: MountOptions) => Effect.Effect<MountedFileSystem>
63 > {}
64 export const Mount = Binding.Service<Mount>("AWS.EFS.Mount");
65
66 /**
67 * Ergonomic alias of {@link Mount} — `yield* EFS.mount(accessPoint, { path })`.
68 */
69 export const mount = Mount;
70
71 const isAccessPoint = (target: AccessPoint | FileSystem): target is AccessPoint =>
72 (target as { Type?: string }).Type === "AWS.EFS.AccessPoint";
73
74 const isLambdaFunction = (value: unknown): value is Function =>
75 typeof value === "object" &&
76 value !== null &&
77 (value as { Type?: string }).Type === "AWS.Lambda.Function";
78
79 /**
80 * Host-agnostic implementation of the {@link Mount} binding. Detects the
81 * host (Lambda Function vs ECS Task) at deploy time and registers the
82 * host-appropriate mount config + IAM through the binding channel; at
83 * runtime it is a no-op that returns the mount path.
84 */
85 export const MountLive = Layer.effect(
86 Mount,
87 Effect.gen(function* () {
88 return Effect.fn(function* (target: AccessPoint | FileSystem, options: MountOptions) {
89 if (!globalThis.__ALCHEMY_RUNTIME__) {
90 const host = yield* Binding.Host;
91 const actions = options.readOnly
92 ? ["elasticfilesystem:ClientMount"]
93 : ["elasticfilesystem:ClientMount", "elasticfilesystem:ClientWrite"];
94
95 if (isTask(host)) {
96 // ECS: task-level EFS volume + container mount point. IAM auth +
97 // transit encryption are always on so the task-role grant below is
98 // what authorizes the mount.
99 const volumeName = `efs-${target.LogicalId.replace(/[^A-Za-z0-9_-]+/g, "-")}`;
100 yield* host.bind`Allow(${host}, AWS.EFS.Mount(${target}))`({
101 policyStatements: [
102 isAccessPoint(target)
103 ? {
104 Effect: "Allow" as const,
105 Action: actions,
106 Resource: ["*"],
107 Condition: {
108 StringEquals: {
109 "elasticfilesystem:AccessPointArn": target.accessPointArn,
110 },
111 },
112 }
113 : {
114 Effect: "Allow" as const,
115 Action: actions,
116 Resource: [target.fileSystemArn],
117 },
118 ],
119 volumes: [
120 {
121 name: volumeName,
122 efsVolumeConfiguration: {
123 fileSystemId: target.fileSystemId,
124 transitEncryption: "ENABLED",
125 authorizationConfig: isAccessPoint(target)
126 ? { accessPointId: target.accessPointId, iam: "ENABLED" }
127 : { iam: "ENABLED" },
128 },
129 },
130 ],
131 mountPoints: [
132 {
133 sourceVolume: volumeName,
134 containerPath: options.path,
135 readOnly: options.readOnly,
136 },
137 ],
138 });
139 } else if (isLambdaFunction(host)) {
140 // Lambda: FileSystemConfigs entry + access-point-scoped client
141 // access on the execution role. Lambda only mounts access points.
142 if (!isAccessPoint(target)) {
143 return yield* Effect.die(
144 new Error(
145 `AWS.EFS.Mount(${target.LogicalId}): Lambda can only mount an EFS access point — create an AWS.EFS.AccessPoint for the file system and mount that instead`,
146 ),
147 );
148 }
149 if (!options.path.startsWith("/mnt/")) {
150 return yield* Effect.die(
151 new Error(
152 `AWS.EFS.Mount(${target.LogicalId}): Lambda mount paths must begin with /mnt/ (got ${options.path})`,
153 ),
154 );
155 }
156 yield* host.bind`Allow(${host}, AWS.EFS.Mount(${target}))`({
157 policyStatements: [
158 {
159 Effect: "Allow" as const,
160 Action: actions,
161 Resource: ["*"],
162 Condition: {
163 StringEquals: {
164 "elasticfilesystem:AccessPointArn": target.accessPointArn,
165 },
166 },
167 },
168 ],
169 fileSystemConfigs: [
170 {
171 arn: target.accessPointArn,
172 localMountPath: options.path,
173 },
174 ],
175 });
176 } else {
177 return yield* Effect.die(
178 new Error(
179 `AWS.EFS.Mount(${target.LogicalId}): unsupported host ${(host as { Type?: string }).Type} — EFS mounts are supported on AWS.Lambda.Function and AWS.ECS.Task`,
180 ),
181 );
182 }
183 }
184 return { path: options.path };
185 });
186 }),
187 );