@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 116 lines 4.24 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 { Role } from "../IAM/Role.ts";
5
6 /**
7 * Shared scaffolding for AWS Comprehend HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two
11 * builders below. Everything except the operation and the IAM action list is
12 * boilerplate: Comprehend's real-time detect APIs and async job APIs have no
13 * resource-level IAM, so every grant is on `*`. The `Start*Job` operations
14 * additionally inject the bound data-access role and a scoped `iam:PassRole`
15 * grant.
16 */
17
18 /**
19 * Build the impl Effect for a Comprehend operation with no bound resource
20 * (real-time detection, job describe/list/stop). The deploy-time half grants
21 * `actions` on `*` — Comprehend detection and job IAM actions do not support
22 * resource-level scoping.
23 */
24 export const makeComprehendHttpBinding = <I, A, E, R>(options: {
25 /** Fully-qualified binding tag, e.g. `AWS.Comprehend.DetectEntities`. */
26 tag: string;
27 /** The distilled operation, invoked with the caller's request as-is. */
28 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
29 /** IAM actions granted on `*`. */
30 actions: readonly string[];
31 }) =>
32 Effect.gen(function* () {
33 const bind = yield* AWSBindings.Bind;
34 const op = yield* AWSBindings.withCredentials(options.operation);
35
36 return Effect.fn(function* () {
37 if (!globalThis.__ALCHEMY_RUNTIME__) {
38 yield* bind`${options.tag}()`({
39 policyStatements: [
40 {
41 Effect: "Allow",
42 Action: [...options.actions],
43 Resource: ["*"],
44 },
45 ],
46 });
47 }
48 return Effect.fn(options.tag)(function* (request?: I) {
49 return yield* op((request ?? {}) as I);
50 });
51 });
52 });
53
54 /**
55 * Build the impl Effect for a `Start*Job` operation: the binding is
56 * constructed with the **data-access role** (the IAM role Amazon Comprehend
57 * assumes to read input documents from S3 and write results; its trust
58 * policy must allow `comprehend.amazonaws.com`). The role's ARN is injected
59 * as `DataAccessRoleArn` on every runtime request, and the deploy-time half
60 * grants `actions` on `*` plus `iam:PassRole` on the role — without the
61 * PassRole grant, `Start*Job` fails only at runtime with an AccessDenied.
62 */
63 export const makeComprehendStartJobHttpBinding = <
64 I extends { DataAccessRoleArn: string },
65 A,
66 E,
67 R,
68 >(options: {
69 /** Fully-qualified binding tag, e.g. `AWS.Comprehend.StartSentimentDetectionJob`. */
70 tag: string;
71 /** The distilled operation; `DataAccessRoleArn` is injected from the role. */
72 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
73 /** IAM actions granted on `*`. */
74 actions: readonly string[];
75 }) =>
76 Effect.gen(function* () {
77 const bind = yield* AWSBindings.Bind;
78 const op = yield* AWSBindings.withCredentials(options.operation);
79
80 return Effect.fn(function* <Rl extends Role>(dataAccessRole: Rl) {
81 const RoleArn = yield* dataAccessRole.roleArn;
82 if (!globalThis.__ALCHEMY_RUNTIME__) {
83 yield* bind`${options.tag}(${dataAccessRole})`({
84 policyStatements: [
85 {
86 Effect: "Allow",
87 Action: [...options.actions],
88 Resource: ["*"],
89 },
90 // CRITICAL: without iam:PassRole on the data-access role,
91 // Start*Job fails only at runtime with an AccessDenied.
92 {
93 Effect: "Allow",
94 Action: ["iam:PassRole"],
95 Resource: [Output.interpolate`${dataAccessRole.roleArn}`],
96 Condition: {
97 StringEquals: {
98 "iam:PassedToService": "comprehend.amazonaws.com",
99 },
100 },
101 },
102 ],
103 });
104 }
105 return Effect.fn(`${options.tag}(${dataAccessRole.LogicalId})`)(function* (
106 request: Omit<I, "DataAccessRoleArn"> & {
107 DataAccessRoleArn?: string;
108 },
109 ) {
110 return yield* op({
111 ...request,
112 DataAccessRoleArn: request.DataAccessRoleArn ?? (yield* RoleArn),
113 } as I);
114 });
115 });
116 });