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

main
ts 246 lines 8.94 KB
Raw
1 import * as Credentials from "@distilled.cloud/aws/Credentials";
2 import * as Region from "@distilled.cloud/aws/Region";
3 import * as SigV4 from "@distilled.cloud/aws/SigV4";
4 /**
5 * Shared scaffolding for Amazon OpenSearch Ingestion (OSIS) HTTP bindings.
6 *
7 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
8 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
9 * below. Everything except the operation, the injected identifier, and the
10 * IAM action list is boilerplate:
11 *
12 * - Pipeline-scoped operations (`osis:GetPipeline`, `osis:StartPipeline`,
13 * `osis:StopPipeline`, …) inject the bound {@link Pipeline}'s name or ARN
14 * into the request and are granted on the pipeline ARN.
15 * - Account-level operations (`osis:ValidatePipeline`,
16 * `osis:ListPipelineBlueprints`, …) take the caller's request as-is and
17 * are granted on `*` — they are not scoped to a single pipeline resource.
18 * - The `osis:Ingest` data plane has no distilled operation — it is a
19 * SigV4-signed HTTP POST (service `"osis"`) against the pipeline's ingest
20 * endpoint, made with the host Function's own credentials.
21 */
22 import type * as Output from "alchemy/Output";
23 import * as Data from "effect/Data";
24 import * as Effect from "effect/Effect";
25 import * as Redacted from "effect/Redacted";
26 import * as AWSBindings from "../AWSBindings.ts";
27 import type { Pipeline } from "./Pipeline.ts";
28
29 /**
30 * Build the impl Effect for an OSIS operation scoped to a {@link Pipeline}:
31 * the deploy-time half grants `actions` on the bound pipeline's ARN, and the
32 * runtime half injects the pipeline's identifier (name or ARN) as
33 * `requestKey` into every request.
34 */
35 export const makeOsisPipelineHttpBinding = <
36 I extends object,
37 K extends keyof I & string,
38 A,
39 E,
40 R,
41 >(options: {
42 /** Fully-qualified binding tag, e.g. `AWS.OSIS.StartPipeline`. */
43 tag: string;
44 /** The distilled operation; `requestKey` is injected from the pipeline. */
45 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
46 /** IAM actions granted on the pipeline ARN. */
47 actions: readonly string[];
48 /** The request field the resolved identifier is injected as. */
49 requestKey: K;
50 /** Resolve the injected identifier from the bound pipeline. */
51 identifier: (pipeline: Pipeline) => Output.Output<string, never>;
52 }) =>
53 Effect.gen(function* () {
54 const bind = yield* AWSBindings.Bind;
55 const op = yield* AWSBindings.withCredentials(options.operation);
56
57 return Effect.fn(function* (pipeline: Pipeline) {
58 const Identifier = yield* options.identifier(pipeline);
59 if (!globalThis.__ALCHEMY_RUNTIME__) {
60 yield* bind`${options.tag}(${pipeline})`({
61 policyStatements: [
62 {
63 Effect: "Allow",
64 Action: [...options.actions],
65 Resource: [pipeline.pipelineArn],
66 },
67 ],
68 });
69 }
70 return Effect.fn(`${options.tag}(${pipeline.LogicalId})`)(function* (request?: Omit<I, K>) {
71 return yield* op({
72 ...request,
73 [options.requestKey]: yield* Identifier,
74 } as I);
75 });
76 });
77 });
78
79 /**
80 * Build the impl Effect for an account-level OSIS operation (config
81 * validation, blueprint catalog reads, endpoint-connection listing). The
82 * deploy-time half grants `actions` on `*` — these operations are not scoped
83 * to a single pipeline resource.
84 */
85 export const makeOsisAccountHttpBinding = <I, A, E, R>(options: {
86 /** Fully-qualified binding tag, e.g. `AWS.OSIS.ValidatePipeline`. */
87 tag: string;
88 /** The distilled operation, invoked with the caller's request as-is. */
89 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
90 /** IAM actions granted on `*`. */
91 actions: readonly string[];
92 }) =>
93 Effect.gen(function* () {
94 const bind = yield* AWSBindings.Bind;
95 const op = yield* AWSBindings.withCredentials(options.operation);
96
97 return Effect.fn(function* () {
98 if (!globalThis.__ALCHEMY_RUNTIME__) {
99 yield* bind`${options.tag}()`({
100 policyStatements: [
101 {
102 Effect: "Allow",
103 Action: [...options.actions],
104 Resource: ["*"],
105 },
106 ],
107 });
108 }
109 return Effect.fn(options.tag)(function* (request?: I) {
110 return yield* op((request ?? {}) as I);
111 });
112 });
113 });
114
115 /**
116 * A failed `osis:Ingest` request against a pipeline's ingest endpoint —
117 * carries the HTTP status and response body returned by Data Prepper.
118 */
119 export class PipelineIngestError extends Data.TaggedError("OsisPipelineIngestError")<{
120 readonly pipelineName: string;
121 /** Request path on the ingest endpoint, e.g. `/logs/ingest`. */
122 readonly path: string;
123 /** HTTP status; `0` when the request never reached the endpoint. */
124 readonly status: number;
125 readonly body: string;
126 }> {}
127
128 /** A single `osis:Ingest` request against the pipeline's ingest endpoint. */
129 export interface IngestRequest {
130 /**
131 * Path of the pipeline's HTTP source, e.g. `/logs/ingest` — must match the
132 * `path` configured on the `http` (or `otel_*`) source in the pipeline
133 * configuration body.
134 */
135 path: string;
136 /**
137 * Events to ingest, JSON-serialized as the request body. Data Prepper's
138 * `http` source expects a JSON array of event objects.
139 */
140 events: ReadonlyArray<unknown>;
141 }
142
143 /**
144 * Build the impl Effect for the `osis:Ingest` data plane: the deploy-time
145 * half grants `osis:Ingest` on the bound pipeline's ARN, and the runtime half
146 * signs (SigV4, service `"osis"`) and POSTs each batch of events to the
147 * pipeline's ingest endpoint with the host Function's own credentials.
148 */
149 export const makeOsisIngestBinding = Effect.gen(function* () {
150 const bind = yield* AWSBindings.Bind;
151 const services = yield* AWSBindings.withCredentials(
152 Effect.context<Credentials.Credentials | Region.Region>(),
153 );
154
155 return Effect.fn(function* (pipeline: Pipeline) {
156 const PipelineName = yield* pipeline.pipelineName;
157 const IngestEndpointUrls = yield* pipeline.ingestEndpointUrls;
158 if (!globalThis.__ALCHEMY_RUNTIME__) {
159 yield* bind`AWS.OSIS.Ingest(${pipeline})`({
160 policyStatements: [
161 {
162 Effect: "Allow",
163 Action: ["osis:Ingest"],
164 Resource: [pipeline.pipelineArn],
165 },
166 ],
167 });
168 }
169
170 return Effect.fn(`AWS.OSIS.Ingest(${pipeline.LogicalId})`)(function* (request: IngestRequest) {
171 const pipelineName = yield* PipelineName;
172 const endpoints = yield* IngestEndpointUrls;
173 const endpoint = endpoints?.[0];
174 const fail = (status: number, body: string) =>
175 Effect.fail(
176 new PipelineIngestError({
177 pipelineName,
178 path: request.path,
179 status,
180 body,
181 }),
182 );
183 if (endpoint === undefined) {
184 return yield* fail(0, "pipeline has no ingestEndpointUrls");
185 }
186
187 // Ingest endpoint URLs are bare hostnames
188 // (`{name}-{id}.{region}.osis.amazonaws.com`); sign for the endpoint's
189 // own region, parsed from the hostname.
190 const base = endpoint.startsWith("https://") ? endpoint : `https://${endpoint}`;
191 const url = new URL(request.path.startsWith("/") ? request.path : `/${request.path}`, base);
192
193 // Resolve credentials fresh per request (STS sessions rotate).
194 const { credentials, region } = yield* Effect.gen(function* () {
195 const credentials = yield* yield* Credentials.Credentials;
196 const region =
197 /\.([a-z0-9-]+)\.osis\.amazonaws\.com$/.exec(url.hostname)?.[1] ??
198 (yield* yield* Region.Region);
199 return { credentials, region };
200 }).pipe(Effect.provideContext(services));
201
202 const body = JSON.stringify(request.events);
203 const signed = yield* SigV4.sign({
204 method: "POST",
205 url: url.toString(),
206 headers: { "content-type": "application/json" },
207 body,
208 accessKeyId: Redacted.value(credentials.accessKeyId),
209 secretAccessKey: credentials.secretAccessKey,
210 sessionToken: credentials.sessionToken,
211 service: "osis",
212 region,
213 allHeaders: true,
214 });
215
216 const response = yield* Effect.tryPromise({
217 try: () =>
218 fetch(signed.url, {
219 method: signed.method,
220 headers: signed.headers,
221 body,
222 }),
223 catch: (cause) =>
224 new PipelineIngestError({
225 pipelineName,
226 path: request.path,
227 status: 0,
228 body: cause instanceof Error ? cause.message : String(cause),
229 }),
230 });
231 const text = yield* Effect.tryPromise({
232 try: () => response.text(),
233 catch: (cause) =>
234 new PipelineIngestError({
235 pipelineName,
236 path: request.path,
237 status: response.status,
238 body: cause instanceof Error ? cause.message : String(cause),
239 }),
240 });
241 if (response.status < 200 || response.status >= 300) {
242 return yield* fail(response.status, text);
243 }
244 });
245 });
246 });