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

main
ts 146 lines 4.95 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 { Graph } from "./Graph.ts";
6
7 /**
8 * Shared scaffolding for the AWS NeptuneGraph (Neptune Analytics) HTTP
9 * bindings.
10 *
11 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
12 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
13 * below. Everything except the operation, the IAM action list, and the
14 * identifier plumbing is boilerplate.
15 */
16
17 /**
18 * Graph snapshot ARNs embed a server-generated id that is only known at
19 * runtime, so snapshot-scoped grants use this wildcard.
20 */
21 export const SNAPSHOT_ARN_WILDCARD = "arn:aws:neptune-graph:*:*:graph-snapshot/*";
22
23 /**
24 * Import-task ARNs embed a server-generated task id that is only known at
25 * runtime, so import-task-scoped grants use this wildcard.
26 */
27 export const IMPORT_TASK_ARN_WILDCARD = "arn:aws:neptune-graph:*:*:import-task/*";
28
29 /**
30 * Export-task ARNs embed a server-generated task id that is only known at
31 * runtime, so export-task-scoped grants use this wildcard.
32 */
33 export const EXPORT_TASK_ARN_WILDCARD = "arn:aws:neptune-graph:*:*:export-task/*";
34
35 /**
36 * `iam:PassRole` restricted to Neptune Analytics — required by the
37 * import/export bindings, whose requests hand the service a data-access
38 * role (`roleArn`) chosen at runtime.
39 */
40 const passRoleStatement: PolicyStatement = {
41 Effect: "Allow",
42 Action: ["iam:PassRole"],
43 Resource: ["*"],
44 Condition: {
45 StringEquals: {
46 "iam:PassedToService": "neptune-graph.amazonaws.com",
47 },
48 },
49 };
50
51 /**
52 * Build the impl Effect for a graph-scoped operation: the runtime callable
53 * injects the bound {@link Graph}'s id as `graphIdentifier` and the
54 * deploy-time half grants `actions` on the graph ARN (plus any
55 * `extraResources`).
56 */
57 export const makeNeptuneGraphGraphHttpBinding = <
58 I extends { graphIdentifier?: string },
59 A,
60 E,
61 R,
62 >(options: {
63 /** Fully-qualified binding tag, e.g. `AWS.NeptuneGraph.StopGraph`. */
64 tag: string;
65 /** The distilled operation; `graphIdentifier` is injected from the graph. */
66 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
67 /** IAM actions granted on the graph ARN. */
68 actions: readonly string[];
69 /** Static IAM resources granted in addition to the graph ARN. */
70 extraResources?: readonly string[];
71 /**
72 * Grant `iam:PassRole` (conditioned to `neptune-graph.amazonaws.com`) so
73 * the function can hand Neptune Analytics the role it assumes to read or
74 * write Amazon S3 data. Set on `StartImportTask` / `StartExportTask`.
75 */
76 passRole?: boolean;
77 }) =>
78 Effect.gen(function* () {
79 const bind = yield* AWSBindings.Bind;
80 const op = yield* AWSBindings.withCredentials(options.operation);
81
82 return Effect.fn(function* (graph: Graph) {
83 const graphId = yield* graph.graphId;
84 if (!globalThis.__ALCHEMY_RUNTIME__) {
85 yield* bind`${options.tag}(${graph})`({
86 policyStatements: [
87 {
88 Effect: "Allow",
89 Action: [...options.actions],
90 Resource: [Output.interpolate`${graph.graphArn}`, ...(options.extraResources ?? [])],
91 },
92 ...(options.passRole ? [passRoleStatement] : []),
93 ],
94 });
95 }
96 return Effect.fn(`${options.tag}(${graph.LogicalId})`)(function* (
97 request?: Omit<I, "graphIdentifier">,
98 ) {
99 return yield* op({
100 ...request,
101 graphIdentifier: yield* graphId,
102 } as I);
103 });
104 });
105 });
106
107 /**
108 * Build the impl Effect for an account-level operation (snapshot reads and
109 * deletes, import/export task polling and cancellation). The deploy-time
110 * half grants `actions` on `resources` (default `*`) — these operations
111 * address snapshots and tasks by identifiers that are runtime data.
112 */
113 export const makeNeptuneGraphAccountHttpBinding = <I, A, E, R>(options: {
114 /** Fully-qualified binding tag, e.g. `AWS.NeptuneGraph.GetImportTask`. */
115 tag: string;
116 /** The distilled operation, invoked with the caller's request as-is. */
117 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
118 /** IAM actions granted. */
119 actions: readonly string[];
120 /**
121 * IAM resources the actions are granted on.
122 * @default ["*"]
123 */
124 resources?: readonly string[];
125 }) =>
126 Effect.gen(function* () {
127 const bind = yield* AWSBindings.Bind;
128 const op = yield* AWSBindings.withCredentials(options.operation);
129
130 return Effect.fn(function* () {
131 if (!globalThis.__ALCHEMY_RUNTIME__) {
132 yield* bind`${options.tag}()`({
133 policyStatements: [
134 {
135 Effect: "Allow",
136 Action: [...options.actions],
137 Resource: [...(options.resources ?? ["*"])],
138 },
139 ],
140 });
141 }
142 return Effect.fn(options.tag)(function* (request?: I) {
143 return yield* op((request ?? {}) as I);
144 });
145 });
146 });