@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 164 lines 5.9 KB
Raw
1 import * as wafv2 from "@distilled.cloud/aws/wafv2";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import type { Providers } from "../Providers.ts";
7 import { retryUnavailableEntity, retryUnavailableEntityLong } from "./internal.ts";
8
9 export interface WebACLAssociationProps {
10 /**
11 * ARN of the `REGIONAL` web ACL to associate.
12 *
13 * Changing the web ACL re-associates the resource in place
14 * (`AssociateWebACL` overwrites any existing association).
15 */
16 webAclArn: string;
17 /**
18 * ARN of the regional resource to protect — an Application Load
19 * Balancer, API Gateway REST API stage, AppSync GraphQL API, Cognito
20 * user pool, App Runner service, Amplify application or Verified Access
21 * instance. Changing the resource replaces the association.
22 *
23 * CloudFront distributions are NOT associated this way — set the
24 * distribution's `webAclId` property to the web ACL's ARN instead.
25 */
26 resourceArn: string;
27 }
28
29 export interface WebACLAssociation extends Resource<
30 "AWS.WAFv2.WebACLAssociation",
31 WebACLAssociationProps,
32 {
33 /**
34 * ARN of the associated web ACL.
35 */
36 webAclArn: string;
37 /**
38 * ARN of the protected resource.
39 */
40 resourceArn: string;
41 },
42 never,
43 Providers
44 > {}
45
46 /**
47 * Associates a `REGIONAL` AWS WAFv2 {@link WebACL} with a regional resource
48 * (ALB, API Gateway stage, AppSync API, Cognito user pool, App Runner
49 * service, Amplify app, Verified Access instance) to protect it.
50 *
51 * A resource can have at most one web ACL association; associating a
52 * different web ACL overwrites the previous association in place.
53 * CloudFront distributions are protected by setting
54 * `Distribution.webAclId` instead — never through this resource.
55 *
56 * ### Associating Web ACLs
57 * **Example:** Protect a Cognito User Pool
58 * ```typescript
59 * const pool = yield* AWS.Cognito.UserPool("Users", {});
60 *
61 * const acl = yield* AWS.WAFv2.WebACL("PoolFirewall", {
62 * defaultAction: { Allow: {} },
63 * });
64 *
65 * const association = yield* AWS.WAFv2.WebACLAssociation("PoolAssociation", {
66 * webAclArn: acl.webAclArn,
67 * resourceArn: pool.userPoolArn,
68 * });
69 * ```
70 *
71 * @resource
72 */
73 export const WebACLAssociation = Resource<WebACLAssociation>("AWS.WAFv2.WebACLAssociation");
74
75 export const WebACLAssociationProvider = () =>
76 Provider.effect(
77 WebACLAssociation,
78 Effect.gen(function* () {
79 // Read the ARN of the web ACL currently protecting a resource
80 // (undefined when the resource has no association or is gone). A
81 // freshly created protected resource (e.g. a Cognito user pool) can
82 // surface WAFUnavailableEntityException until WAF can "retrieve" it —
83 // retry through propagation.
84 const observeAssociation = Effect.fn(function* (resourceArn: string) {
85 const response = yield* retryUnavailableEntity(
86 wafv2.getWebACLForResource({ ResourceArn: resourceArn }),
87 ).pipe(
88 Effect.catchTag("WAFNonexistentItemException", () =>
89 Effect.succeed({ WebACL: undefined }),
90 ),
91 );
92 return response.WebACL?.ARN;
93 });
94
95 return {
96 stables: ["webAclArn", "resourceArn"],
97
98 // Associations have no enumeration API of their own (they are read
99 // per-resource via getWebACLForResource), so nuke can't list them.
100 // They dissolve with either endpoint anyway.
101 list: () => Effect.succeed([]),
102
103 diff: Effect.fn(function* ({ news, olds }) {
104 if (!isResolved(news)) return undefined;
105 if (olds.resourceArn !== news.resourceArn) {
106 return { action: "replace" } as const;
107 }
108 // webAclArn changes apply in place — associateWebACL overwrites.
109 }),
110
111 read: Effect.fn(function* ({ olds, output }) {
112 const resourceArn = output?.resourceArn ?? olds?.resourceArn;
113 const webAclArn = output?.webAclArn ?? olds?.webAclArn;
114 if (resourceArn === undefined) {
115 return undefined;
116 }
117 const observed = yield* observeAssociation(resourceArn);
118 if (observed === undefined || observed !== webAclArn) {
119 // Nothing associated, or a foreign web ACL owns the slot.
120 return undefined;
121 }
122 return { webAclArn: observed, resourceArn };
123 }),
124
125 reconcile: Effect.fn(function* ({ news, session }) {
126 // 1. Observe — what protects the resource right now?
127 const observed = yield* observeAssociation(news.resourceArn);
128
129 // 2. Ensure — associate when missing or pointing elsewhere.
130 // associateWebACL is an upsert; a freshly created web ACL or
131 // protected resource (e.g. a new Cognito user pool) can be
132 // unavailable well past 90s, so retry on the long budget.
133 if (observed !== news.webAclArn) {
134 yield* retryUnavailableEntityLong(
135 wafv2.associateWebACL({
136 WebACLArn: news.webAclArn,
137 ResourceArn: news.resourceArn,
138 }),
139 );
140 }
141
142 yield* session.note(`Associated ${news.webAclArn} with ${news.resourceArn}`);
143 return {
144 webAclArn: news.webAclArn,
145 resourceArn: news.resourceArn,
146 };
147 }),
148
149 delete: Effect.fn(function* ({ output }) {
150 // Idempotent: a missing resource or absent association is fine.
151 // Retry transient unavailability so the web ACL can be deleted
152 // right after (deleteWebACL fails while still associated).
153 yield* retryUnavailableEntity(
154 wafv2.disassociateWebACL({ ResourceArn: output.resourceArn }),
155 ).pipe(
156 Effect.catchTag(
157 ["WAFNonexistentItemException", "WAFUnavailableEntityException"],
158 () => Effect.void,
159 ),
160 );
161 }),
162 };
163 }),
164 );