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

main
ts 228 lines 8.1 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import { type WafScope, withWafScope } from "./internal.ts";
4 import type { IPSet } from "./IPSet.ts";
5 import type { RuleGroup } from "./RuleGroup.ts";
6 import type { WebACL } from "./WebACL.ts";
7
8 /**
9 * Shared scaffolding for AWS WAFv2 HTTP bindings.
10 *
11 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
12 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
13 * below. Everything except the operation, the IAM action list, and the
14 * injected identifiers is boilerplate:
15 *
16 * - {@link makeWafv2WebAclHttpBinding} — operations scoped to one bound
17 * {@link WebACL}; `inject` picks which resolved identifiers (ARN, name,
18 * id, scope) the request needs. CLOUDFRONT-scoped web ACLs are pinned to
19 * `us-east-1` automatically.
20 * - {@link makeWafv2IPSetHttpBinding} — operations scoped to one bound
21 * {@link IPSet}; injects `Name`/`Scope`/`Id`.
22 * - {@link makeWafv2RuleGroupHttpBinding} — operations keyed by a rule
23 * group ARN (the permission-policy interface); injects `ResourceArn`.
24 * - {@link makeWafv2AccountHttpBinding} — account-level operations (API
25 * keys, capacity checks, managed rule group catalog); the caller's
26 * request passes through and the grant is on `*`. A `Scope: "CLOUDFRONT"`
27 * request is pinned to `us-east-1`.
28 */
29
30 /** Identifiers of a bound {@link WebACL}, resolved at request time. */
31 export interface ResolvedWebAcl {
32 readonly arn: string;
33 readonly name: string;
34 readonly id: string;
35 readonly scope: WafScope;
36 }
37
38 /**
39 * Build the impl Effect for an operation scoped to a single bound
40 * {@link WebACL}. `inject` maps the resolved web ACL identifiers onto the
41 * request fields the operation expects; the deploy-time half grants
42 * `actions` on the web ACL ARN.
43 */
44 export const makeWafv2WebAclHttpBinding = <I, A, E, R>(options: {
45 /** Fully-qualified binding tag, e.g. `AWS.WAFv2.GetSampledRequests`. */
46 tag: string;
47 /** The distilled operation. */
48 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
49 /** IAM actions granted on the web ACL ARN. */
50 actions: readonly string[];
51 /** Request fields injected from the bound web ACL. */
52 inject: (webAcl: ResolvedWebAcl) => Partial<I>;
53 }) =>
54 Effect.gen(function* () {
55 const bind = yield* AWSBindings.Bind;
56 const op = yield* AWSBindings.withCredentials(options.operation);
57
58 return Effect.fn(function* (webAcl: WebACL) {
59 const Arn = yield* webAcl.webAclArn;
60 const Name = yield* webAcl.webAclName;
61 const Id = yield* webAcl.webAclId;
62 const Scope = yield* webAcl.scope;
63 if (!globalThis.__ALCHEMY_RUNTIME__) {
64 yield* bind`${options.tag}(${webAcl})`({
65 policyStatements: [
66 {
67 Effect: "Allow",
68 Action: [...options.actions],
69 Resource: [webAcl.webAclArn],
70 },
71 ],
72 });
73 }
74 return Effect.fn(`${options.tag}(${webAcl.LogicalId})`)(function* (request?: Partial<I>) {
75 const resolved: ResolvedWebAcl = {
76 arn: yield* Arn,
77 name: yield* Name,
78 id: yield* Id,
79 scope: yield* Scope,
80 };
81 return yield* withWafScope(
82 resolved.scope,
83 op({ ...request, ...options.inject(resolved) } as I),
84 );
85 });
86 });
87 });
88
89 /**
90 * Build the impl Effect for an operation scoped to a single bound
91 * {@link IPSet}: the runtime callable injects the IP set's `Name`, `Scope`,
92 * and `Id`; the deploy-time half grants `actions` on the IP set ARN.
93 * CLOUDFRONT-scoped IP sets are pinned to `us-east-1` automatically.
94 */
95 export const makeWafv2IPSetHttpBinding = <
96 I extends { Name?: string; Scope?: string; Id?: string },
97 A,
98 E,
99 R,
100 >(options: {
101 /** Fully-qualified binding tag, e.g. `AWS.WAFv2.GetIPSet`. */
102 tag: string;
103 /** The distilled operation; `Name`/`Scope`/`Id` are injected. */
104 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
105 /** IAM actions granted on the IP set ARN. */
106 actions: readonly string[];
107 }) =>
108 Effect.gen(function* () {
109 const bind = yield* AWSBindings.Bind;
110 const op = yield* AWSBindings.withCredentials(options.operation);
111
112 return Effect.fn(function* (ipSet: IPSet) {
113 const Name = yield* ipSet.ipSetName;
114 const Id = yield* ipSet.ipSetId;
115 const Scope = yield* ipSet.scope;
116 if (!globalThis.__ALCHEMY_RUNTIME__) {
117 yield* bind`${options.tag}(${ipSet})`({
118 policyStatements: [
119 {
120 Effect: "Allow",
121 Action: [...options.actions],
122 Resource: [ipSet.ipSetArn],
123 },
124 ],
125 });
126 }
127 return Effect.fn(`${options.tag}(${ipSet.LogicalId})`)(function* (
128 request?: Omit<I, "Name" | "Scope" | "Id">,
129 ) {
130 const scope = yield* Scope;
131 return yield* withWafScope(
132 scope,
133 op({
134 ...request,
135 Name: yield* Name,
136 Scope: scope,
137 Id: yield* Id,
138 } as I),
139 );
140 });
141 });
142 });
143
144 /**
145 * Build the impl Effect for an operation keyed by a bound {@link RuleGroup}
146 * ARN (the permission-policy interface): the runtime callable injects the
147 * rule group's ARN as `ResourceArn`; the deploy-time half grants `actions`
148 * on the rule group ARN. CLOUDFRONT-scoped rule groups are pinned to
149 * `us-east-1` automatically.
150 */
151 export const makeWafv2RuleGroupHttpBinding = <
152 I extends { ResourceArn?: string },
153 A,
154 E,
155 R,
156 >(options: {
157 /** Fully-qualified binding tag, e.g. `AWS.WAFv2.GetPermissionPolicy`. */
158 tag: string;
159 /** The distilled operation; `ResourceArn` is injected. */
160 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
161 /** IAM actions granted on the rule group ARN. */
162 actions: readonly string[];
163 }) =>
164 Effect.gen(function* () {
165 const bind = yield* AWSBindings.Bind;
166 const op = yield* AWSBindings.withCredentials(options.operation);
167
168 return Effect.fn(function* (ruleGroup: RuleGroup) {
169 const Arn = yield* ruleGroup.ruleGroupArn;
170 const Scope = yield* ruleGroup.scope;
171 if (!globalThis.__ALCHEMY_RUNTIME__) {
172 yield* bind`${options.tag}(${ruleGroup})`({
173 policyStatements: [
174 {
175 Effect: "Allow",
176 Action: [...options.actions],
177 Resource: [ruleGroup.ruleGroupArn],
178 },
179 ],
180 });
181 }
182 return Effect.fn(`${options.tag}(${ruleGroup.LogicalId})`)(function* (
183 request?: Omit<I, "ResourceArn">,
184 ) {
185 return yield* withWafScope(yield* Scope, op({ ...request, ResourceArn: yield* Arn } as I));
186 });
187 });
188 });
189
190 /**
191 * Build the impl Effect for an account-level WAFv2 operation (API keys,
192 * capacity checks, managed rule group catalog reads): the caller's request
193 * passes through unchanged and the deploy-time half grants `actions` on
194 * `*` (these operations authorize account-wide, not against a resource
195 * ARN). A request with `Scope: "CLOUDFRONT"` is pinned to `us-east-1`.
196 */
197 export const makeWafv2AccountHttpBinding = <I, A, E, R>(options: {
198 /** Fully-qualified binding tag, e.g. `AWS.WAFv2.CheckCapacity`. */
199 tag: string;
200 /** The distilled operation, invoked with the caller's request as-is. */
201 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
202 /** IAM actions granted on `*`. */
203 actions: readonly string[];
204 }) =>
205 Effect.gen(function* () {
206 const bind = yield* AWSBindings.Bind;
207 const op = yield* AWSBindings.withCredentials(options.operation);
208
209 return Effect.fn(function* () {
210 if (!globalThis.__ALCHEMY_RUNTIME__) {
211 yield* bind`${options.tag}()`({
212 policyStatements: [
213 {
214 Effect: "Allow",
215 Action: [...options.actions],
216 Resource: ["*"],
217 },
218 ],
219 });
220 }
221 return Effect.fn(options.tag)(function* (request?: I) {
222 const input = (request ?? {}) as I;
223 const scope: WafScope =
224 (input as { Scope?: string }).Scope === "CLOUDFRONT" ? "CLOUDFRONT" : "REGIONAL";
225 return yield* withWafScope(scope, op(input));
226 });
227 });
228 });