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

main
ts 142 lines 5.37 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 { Datastore } from "./Datastore.ts";
6
7 /**
8 * Shared scaffolding for AWS HealthImaging (medical-imaging) 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 HealthImaging data-plane operation is scoped to one
14 * data store, whose id is injected as `datastoreId` and whose ARN (plus its
15 * `…/imageset/*` children — image-set operations are authorized against the
16 * image set sub-resource) receives the grant. `StartDICOMImportJob`
17 * additionally injects the bound data-access role and a scoped
18 * `iam:PassRole` grant.
19 */
20
21 /**
22 * Build the impl Effect for a data-store-scoped HealthImaging operation
23 * (image set reads/writes, search, import job describes/lists): the runtime
24 * callable injects the bound {@link Datastore}'s id as `datastoreId` and the
25 * deploy-time half grants `actions` on the data store ARN and its
26 * `…/imageset/*` sub-resources.
27 */
28 export const makeMedicalImagingDatastoreHttpBinding = <
29 I extends { datastoreId: string },
30 A,
31 E,
32 R,
33 >(options: {
34 /** Fully-qualified binding tag, e.g. `AWS.MedicalImaging.GetImageSet`. */
35 tag: string;
36 /** The distilled operation; `datastoreId` is injected from the data store. */
37 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
38 /** IAM actions granted on the data store ARN + `…/imageset/*`. */
39 actions: readonly string[];
40 }) =>
41 Effect.gen(function* () {
42 const bind = yield* AWSBindings.Bind;
43 const op = yield* AWSBindings.withCredentials(options.operation);
44
45 return Effect.fn(function* (datastore: Datastore) {
46 const DatastoreId = yield* datastore.datastoreId;
47 if (!globalThis.__ALCHEMY_RUNTIME__) {
48 yield* bind`${options.tag}(${datastore})`({
49 policyStatements: [
50 {
51 Effect: "Allow",
52 Action: [...options.actions],
53 Resource: [
54 Output.interpolate`${datastore.datastoreArn}`,
55 Output.interpolate`${datastore.datastoreArn}/imageset/*`,
56 ],
57 },
58 ],
59 });
60 }
61 return Effect.fn(`${options.tag}(${datastore.LogicalId})`)(function* (
62 request?: Omit<I, "datastoreId">,
63 ) {
64 return yield* op({
65 ...request,
66 datastoreId: yield* DatastoreId,
67 } as I);
68 });
69 });
70 });
71
72 /**
73 * Build the impl Effect for the HealthImaging `StartDICOMImportJob`
74 * operation: the binding is constructed with the {@link Datastore} **and the
75 * data-access role** (the IAM role HealthImaging assumes to read the DICOM
76 * P10 input from S3 and write the import manifests; its trust policy must
77 * allow `medical-imaging.amazonaws.com`). The runtime callable injects the
78 * data store id as `datastoreId` and the role's ARN as `dataAccessRoleArn`;
79 * the deploy-time half grants `actions` on the data store ARN plus
80 * `iam:PassRole` on the role — without the PassRole grant, the import fails
81 * only at runtime with an AccessDeniedException.
82 */
83 export const makeMedicalImagingStartJobHttpBinding = <
84 I extends { datastoreId: string; dataAccessRoleArn: string },
85 A,
86 E,
87 R,
88 >(options: {
89 /** Fully-qualified binding tag, e.g. `AWS.MedicalImaging.StartDICOMImportJob`. */
90 tag: string;
91 /**
92 * The distilled operation; `datastoreId` and `dataAccessRoleArn` are
93 * injected from the bound resources.
94 */
95 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
96 /** IAM actions granted on the data store ARN. */
97 actions: readonly string[];
98 }) =>
99 Effect.gen(function* () {
100 const bind = yield* AWSBindings.Bind;
101 const op = yield* AWSBindings.withCredentials(options.operation);
102
103 return Effect.fn(function* (datastore: Datastore, dataAccessRole: Role) {
104 const DatastoreId = yield* datastore.datastoreId;
105 const RoleArn = yield* dataAccessRole.roleArn;
106 if (!globalThis.__ALCHEMY_RUNTIME__) {
107 yield* bind`${options.tag}(${datastore}, ${dataAccessRole})`({
108 policyStatements: [
109 {
110 Effect: "Allow",
111 Action: [...options.actions],
112 Resource: [Output.interpolate`${datastore.datastoreArn}`],
113 },
114 // CRITICAL: without iam:PassRole on the data-access role,
115 // StartDICOMImportJob fails only at runtime with an
116 // AccessDenied.
117 {
118 Effect: "Allow",
119 Action: ["iam:PassRole"],
120 Resource: [Output.interpolate`${dataAccessRole.roleArn}`],
121 Condition: {
122 StringEquals: {
123 "iam:PassedToService": "medical-imaging.amazonaws.com",
124 },
125 },
126 },
127 ],
128 });
129 }
130 return Effect.fn(`${options.tag}(${datastore.LogicalId})`)(function* (
131 request: Omit<I, "datastoreId" | "dataAccessRoleArn"> & {
132 dataAccessRoleArn?: string;
133 },
134 ) {
135 return yield* op({
136 ...request,
137 datastoreId: yield* DatastoreId,
138 dataAccessRoleArn: request.dataAccessRoleArn ?? (yield* RoleArn),
139 } as I);
140 });
141 });
142 });