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

main
ts 113 lines 4.25 KB
Raw
1 import type { Output } from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { ConfigRule } from "./ConfigRule.ts";
5 import type { DeliveryChannel } from "./DeliveryChannel.ts";
6
7 /**
8 * Shared scaffolding for AWS Config HTTP bindings.
9 *
10 * NOT exported from `index.ts` — every single-operation `{Op}Http.ts` in
11 * this service is a thin `Layer.effect(Cap, make…HttpBinding({ … }))` over
12 * one of the two builders below. Everything except the operation, the IAM
13 * action, and the injected identifier is boilerplate.
14 *
15 * AWS Config actions do not support resource-level permissions (the service
16 * authorization reference lists no resource types for the data-plane
17 * read/evaluate actions), so the deploy-time half always grants `actions`
18 * on `*`. Resource-scoped bindings still scope the *API call* to the bound
19 * resource by injecting its physical identifier into every request.
20 */
21
22 /** Config resources a resource-scoped binding can be bound to. */
23 export type BindableConfigResource = ConfigRule | DeliveryChannel;
24
25 /**
26 * Build the impl Effect for an account-level AWS Config operation. The
27 * runtime callable passes the caller's request through unchanged; the
28 * deploy-time half grants `actions` on `*`.
29 */
30 export const makeConfigAccountHttpBinding = <I, A, E, R>(options: {
31 /** Fully-qualified binding tag, e.g. `AWS.Config.SelectResourceConfig`. */
32 tag: string;
33 /** The distilled operation, invoked with the caller's request as-is. */
34 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
35 /** IAM actions granted on `*`. */
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* () {
43 if (!globalThis.__ALCHEMY_RUNTIME__) {
44 yield* bind`${options.tag}()`({
45 policyStatements: [
46 {
47 Effect: "Allow",
48 Action: [...options.actions],
49 Resource: ["*"],
50 },
51 ],
52 });
53 }
54 return Effect.fn(options.tag)(function* (request?: I) {
55 return yield* op((request ?? {}) as I);
56 });
57 });
58 });
59
60 /**
61 * Build the impl Effect for an operation scoped to a single bound Config
62 * resource ({@link ConfigRule} or {@link DeliveryChannel}). The runtime
63 * callable injects the resolved `identifier` under `requestKey` (wrapped in
64 * a single-element array when `asList` is set, e.g.
65 * `StartConfigRulesEvaluation`'s `ConfigRuleNames`); the deploy-time half
66 * grants `actions` on `*` (Config actions are not resource-scoped in IAM).
67 */
68 export const makeConfigResourceHttpBinding = <
69 Res extends BindableConfigResource,
70 K extends string,
71 I,
72 A,
73 E,
74 R,
75 >(options: {
76 /** Fully-qualified binding tag, e.g. `AWS.Config.GetComplianceDetailsByConfigRule`. */
77 tag: string;
78 /** The distilled operation; the identifier is injected from the resource. */
79 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
80 /** IAM actions granted on `*`. */
81 actions: readonly string[];
82 /** Request field the resolved identifier is injected under. */
83 requestKey: K;
84 /** Wrap the injected identifier in a single-element array. */
85 asList?: boolean;
86 /** Resolve the injected identifier from the bound resource. */
87 identifier: (resource: Res) => Output<string, never>;
88 }) =>
89 Effect.gen(function* () {
90 const bind = yield* AWSBindings.Bind;
91 const op = yield* AWSBindings.withCredentials(options.operation);
92
93 return Effect.fn(function* (resource: Res) {
94 const identifier = yield* options.identifier(resource);
95 if (!globalThis.__ALCHEMY_RUNTIME__) {
96 yield* bind`${options.tag}(${resource})`({
97 policyStatements: [
98 {
99 Effect: "Allow",
100 Action: [...options.actions],
101 Resource: ["*"],
102 },
103 ],
104 });
105 }
106 return Effect.fn(`${options.tag}(${resource.LogicalId})`)(function* (request?: Omit<I, K>) {
107 const input: Record<string, unknown> = { ...request };
108 const id = yield* identifier;
109 input[options.requestKey] = options.asList ? [id] : id;
110 return yield* op(input as unknown as I);
111 });
112 });
113 });