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

main
ts 89 lines 3.46 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 { PolicyStatement } from "../IAM/Policy.ts";
5 import type { Application } from "./Application.ts";
6
7 /**
8 * Shared scaffolding for the Amazon EMR Serverless HTTP bindings.
9 *
10 * Every EMR Serverless data-plane operation is addressed by an
11 * `applicationId`, so every binding is scoped to a bound {@link Application}:
12 * the deploy-time half grants `actions` on the application's ARN (plus any
13 * `subresources` wildcards — job runs and sessions are authorized against
14 * `{applicationArn}/jobruns/*` / `{applicationArn}/sessions/*` sub-resource
15 * ARNs), and the runtime half injects the application's id into every
16 * request. Operations that hand the service an execution role to assume
17 * (`StartJobRun`, `StartSession`) additionally grant `iam:PassRole`
18 * conditioned to `emr-serverless.amazonaws.com`.
19 *
20 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
21 * a `Layer.effect(Cap, makeEmrServerlessHttpBinding({ … }))`.
22 */
23 export const makeEmrServerlessHttpBinding = <I, A, E, R>(options: {
24 /** Fully-qualified binding tag, e.g. `AWS.EMRServerless.StartJobRun`. */
25 tag: string;
26 /** The distilled operation. */
27 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
28 /** IAM actions granted on the application ARN (+ `subresources`). */
29 actions: readonly string[];
30 /**
31 * Sub-resource wildcards additionally granted below the application ARN,
32 * e.g. `"/jobruns/*"` for job-run-addressed actions.
33 */
34 subresources?: readonly string[];
35 /**
36 * Grant `iam:PassRole` (conditioned to `emr-serverless.amazonaws.com`) so
37 * the function can hand the service the execution role a job run or
38 * session assumes. Set on `StartJobRun` and `StartSession`.
39 */
40 passRole?: boolean;
41 }) =>
42 Effect.gen(function* () {
43 const bind = yield* AWSBindings.Bind;
44 const op = yield* AWSBindings.withCredentials(options.operation);
45
46 return Effect.fn(function* (application: Application) {
47 // Outputs yield a DEFERRED effect — resolve again per invocation below.
48 const ApplicationId = yield* application.applicationId;
49 if (!globalThis.__ALCHEMY_RUNTIME__) {
50 const passRoleStatements: PolicyStatement[] = options.passRole
51 ? [
52 {
53 Effect: "Allow",
54 Action: ["iam:PassRole"],
55 Resource: ["*"],
56 Condition: {
57 StringEquals: {
58 "iam:PassedToService": "emr-serverless.amazonaws.com",
59 },
60 },
61 },
62 ]
63 : [];
64 yield* bind`${options.tag}(${application})`({
65 policyStatements: [
66 {
67 Effect: "Allow",
68 Action: [...options.actions],
69 Resource: [
70 Output.interpolate`${application.applicationArn}`,
71 ...(options.subresources ?? []).map(
72 (suffix) => Output.interpolate`${application.applicationArn}${suffix}`,
73 ),
74 ],
75 },
76 ...passRoleStatements,
77 ],
78 });
79 }
80 return Effect.fn(`${options.tag}(${application.LogicalId})`)(function* (
81 request?: Omit<I, "applicationId" | "clientToken">,
82 ) {
83 return yield* op({
84 ...request,
85 applicationId: yield* ApplicationId,
86 } as I);
87 });
88 });
89 });