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

main
ts 138 lines 5.18 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 { ExperimentTemplate } from "./ExperimentTemplate.ts";
5
6 /**
7 * Shared scaffolding for the AWS Fault Injection Service (FIS) HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
10 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders
11 * below. Everything except the operation, the IAM action list, and the
12 * injected identifier is boilerplate.
13 */
14
15 /**
16 * Build the impl Effect for a FIS operation scoped to a bound
17 * {@link ExperimentTemplate}: the deploy-time half grants `actions` on the
18 * template's ARN (plus, when `grantExperimentWildcard` is set, the account's
19 * `experiment/*` ARNs — `fis:StartExperiment` authorizes against both the
20 * template being started and the experiment being created), and the runtime
21 * half injects the template's id into every request as `requestKey`.
22 *
23 * `clientToken` (an idempotency token) is auto-generated by distilled when
24 * absent, so it is omitted from the runtime request shape.
25 */
26 export const makeFisTemplateHttpBinding = <I, A, E, R>(options: {
27 /** Fully-qualified binding tag, e.g. `AWS.FIS.StartExperiment`. */
28 tag: string;
29 /** The distilled operation. */
30 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
31 /** IAM actions granted on the experiment template ARN. */
32 actions: readonly string[];
33 /** The request field the bound template's id is injected as. */
34 requestKey: "id" | "experimentTemplateId";
35 /**
36 * Additionally grant `actions` on the account's `experiment/*` ARN —
37 * `fis:StartExperiment` is authorized against both the template and the
38 * experiment it creates.
39 */
40 grantExperimentWildcard?: boolean;
41 /**
42 * Additionally grant `iam:CreateServiceLinkedRole` scoped to FIS — the
43 * first `fis:StartExperiment` in an account creates the
44 * `AWSServiceRoleForFIS` service-linked role on the caller's behalf and is
45 * denied without it.
46 */
47 grantServiceLinkedRole?: boolean;
48 }) =>
49 Effect.gen(function* () {
50 const bind = yield* AWSBindings.Bind;
51 const op = yield* AWSBindings.withCredentials(options.operation);
52
53 return Effect.fn(function* (template: ExperimentTemplate) {
54 const TemplateId = yield* template.id;
55 if (!globalThis.__ALCHEMY_RUNTIME__) {
56 yield* bind`${options.tag}(${template})`({
57 policyStatements: [
58 {
59 Effect: "Allow",
60 Action: [...options.actions],
61 Resource: [
62 Output.interpolate`${template.arn}`,
63 ...(options.grantExperimentWildcard
64 ? [
65 // arn:…:experiment-template/EXT… → arn:…:experiment/*
66 template.arn.pipe(
67 Output.map((arn) => `${arn.slice(0, arn.lastIndexOf(":"))}:experiment/*`),
68 ),
69 ]
70 : []),
71 ],
72 },
73 ...(options.grantServiceLinkedRole
74 ? [
75 {
76 Effect: "Allow" as const,
77 Action: ["iam:CreateServiceLinkedRole"],
78 Resource: [
79 "arn:aws:iam::*:role/aws-service-role/fis.amazonaws.com/AWSServiceRoleForFIS",
80 ],
81 Condition: {
82 StringEquals: {
83 "iam:AWSServiceName": "fis.amazonaws.com",
84 },
85 },
86 },
87 ]
88 : []),
89 ],
90 });
91 }
92 return Effect.fn(`${options.tag}(${template.LogicalId})`)(function* (
93 request?: Omit<I, "id" | "experimentTemplateId" | "clientToken">,
94 ) {
95 return yield* op({
96 ...request,
97 [options.requestKey]: yield* TemplateId,
98 } as I);
99 });
100 });
101 });
102
103 /**
104 * Build the impl Effect for an account-level FIS operation (experiment
105 * control and reads, the action/target-resource-type catalogs, the safety
106 * lever). The deploy-time half grants `actions` on `*` — experiments are
107 * created dynamically at runtime and the catalog/list actions are not scoped
108 * to a single resource.
109 */
110 export const makeFisAccountHttpBinding = <I, A, E, R>(options: {
111 /** Fully-qualified binding tag, e.g. `AWS.FIS.GetExperiment`. */
112 tag: string;
113 /** The distilled operation, invoked with the caller's request as-is. */
114 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
115 /** IAM actions granted on `*`. */
116 actions: readonly string[];
117 }) =>
118 Effect.gen(function* () {
119 const bind = yield* AWSBindings.Bind;
120 const op = yield* AWSBindings.withCredentials(options.operation);
121
122 return Effect.fn(function* () {
123 if (!globalThis.__ALCHEMY_RUNTIME__) {
124 yield* bind`${options.tag}()`({
125 policyStatements: [
126 {
127 Effect: "Allow",
128 Action: [...options.actions],
129 Resource: ["*"],
130 },
131 ],
132 });
133 }
134 return Effect.fn(options.tag)(function* (request?: I) {
135 return yield* op((request ?? {}) as I);
136 });
137 });
138 });