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

main
ts 134 lines 4.97 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 import type { FHIRDatastore } from "./FHIRDatastore.ts";
6
7 /**
8 * Shared scaffolding for AWS HealthLake HTTP bindings.
9 *
10 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
11 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
12 * below. Everything except the operation and the IAM action list is
13 * boilerplate: every HealthLake job operation is scoped to one FHIR data
14 * store, whose id is injected as `DatastoreId` and whose ARN receives the
15 * grant. The `Start*Job` operations additionally inject the bound
16 * data-access role and a scoped `iam:PassRole` grant.
17 */
18
19 /**
20 * Build the impl Effect for a data-store-scoped HealthLake operation
21 * (describe/list import and export jobs): the runtime callable injects the
22 * bound {@link FHIRDatastore}'s id as `DatastoreId` and the deploy-time half
23 * grants `actions` on the data store ARN.
24 */
25 export const makeHealthLakeDatastoreHttpBinding = <
26 I extends { DatastoreId: string },
27 A,
28 E,
29 R,
30 >(options: {
31 /** Fully-qualified binding tag, e.g. `AWS.HealthLake.DescribeFHIRImportJob`. */
32 tag: string;
33 /** The distilled operation; `DatastoreId` is injected from the data store. */
34 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
35 /** IAM actions granted on the data store ARN. */
36 actions: readonly string[];
37 }) =>
38 Effect.gen(function* () {
39 const bind = yield* AWSBindings.Bind;
40 const op = yield* AWSBindings.withCredentials(options.operation);
41
42 return Effect.fn(function* (datastore: FHIRDatastore) {
43 const DatastoreId = yield* datastore.datastoreId;
44 if (!globalThis.__ALCHEMY_RUNTIME__) {
45 yield* bind`${options.tag}(${datastore})`({
46 policyStatements: [
47 {
48 Effect: "Allow",
49 Action: [...options.actions],
50 Resource: [Output.interpolate`${datastore.datastoreArn}`],
51 },
52 ],
53 });
54 }
55 return Effect.fn(`${options.tag}(${datastore.LogicalId})`)(function* (
56 request?: Omit<I, "DatastoreId">,
57 ) {
58 return yield* op({
59 ...request,
60 DatastoreId: yield* DatastoreId,
61 } as I);
62 });
63 });
64 });
65
66 /**
67 * Build the impl Effect for a HealthLake `Start*Job` operation: the binding
68 * is constructed with the {@link FHIRDatastore} **and the data-access role**
69 * (the IAM role HealthLake assumes to read/write the S3 locations; its trust
70 * policy must allow `healthlake.amazonaws.com`). The runtime callable injects
71 * the data store id as `DatastoreId` and the role's ARN as
72 * `DataAccessRoleArn`; the deploy-time half grants `actions` on the data
73 * store ARN plus `iam:PassRole` on the role — without the PassRole grant,
74 * `Start*Job` fails only at runtime with an AccessDeniedException.
75 */
76 export const makeHealthLakeStartJobHttpBinding = <
77 I extends { DatastoreId: string; DataAccessRoleArn: string },
78 A,
79 E,
80 R,
81 >(options: {
82 /** Fully-qualified binding tag, e.g. `AWS.HealthLake.StartFHIRExportJob`. */
83 tag: string;
84 /**
85 * The distilled operation; `DatastoreId` and `DataAccessRoleArn` are
86 * injected from the bound resources.
87 */
88 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
89 /** IAM actions granted on the data store ARN. */
90 actions: readonly string[];
91 }) =>
92 Effect.gen(function* () {
93 const bind = yield* AWSBindings.Bind;
94 const op = yield* AWSBindings.withCredentials(options.operation);
95
96 return Effect.fn(function* (datastore: FHIRDatastore, dataAccessRole: Role) {
97 const DatastoreId = yield* datastore.datastoreId;
98 const RoleArn = yield* dataAccessRole.roleArn;
99 if (!globalThis.__ALCHEMY_RUNTIME__) {
100 yield* bind`${options.tag}(${datastore}, ${dataAccessRole})`({
101 policyStatements: [
102 {
103 Effect: "Allow",
104 Action: [...options.actions],
105 Resource: [Output.interpolate`${datastore.datastoreArn}`],
106 },
107 // CRITICAL: without iam:PassRole on the data-access role,
108 // Start*Job fails only at runtime with an AccessDenied.
109 {
110 Effect: "Allow",
111 Action: ["iam:PassRole"],
112 Resource: [Output.interpolate`${dataAccessRole.roleArn}`],
113 Condition: {
114 StringEquals: {
115 "iam:PassedToService": "healthlake.amazonaws.com",
116 },
117 },
118 },
119 ],
120 });
121 }
122 return Effect.fn(`${options.tag}(${datastore.LogicalId})`)(function* (
123 request: Omit<I, "DatastoreId" | "DataAccessRoleArn"> & {
124 DataAccessRoleArn?: string;
125 },
126 ) {
127 return yield* op({
128 ...request,
129 DatastoreId: yield* DatastoreId,
130 DataAccessRoleArn: request.DataAccessRoleArn ?? (yield* RoleArn),
131 } as I);
132 });
133 });
134 });