@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 141 lines 5.77 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { GraphqlApi } from "@alchemy.run/aws/AppSync";
3 import { WebACL, WebACLAssociation } from "@alchemy.run/aws/WAFv2";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as sts from "@distilled.cloud/aws/sts";
7 import * as wafv2 from "@distilled.cloud/aws/wafv2";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Result from "effect/Result";
11 import * as Schedule from "effect/Schedule";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 class WebACLStillExists extends Data.TaggedError("WebACLStillExists")<{ readonly name: string }> {}
16
17 class AssociationPending extends Data.TaggedError("AssociationPending")<{
18 readonly resourceArn: string;
19 readonly expected: string | undefined;
20 readonly actual: string | undefined;
21 }> {}
22
23 const assertWebAclDeleted = (name: string, id: string) =>
24 wafv2.getWebACL({ Name: name, Scope: "REGIONAL", Id: id }).pipe(
25 Effect.flatMap(() => Effect.fail(new WebACLStillExists({ name }))),
26 Effect.catchTag("WAFNonexistentItemException", () => Effect.void),
27 Effect.retry({
28 while: (e) => e._tag === "WebACLStillExists",
29 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
30 }),
31 );
32
33 // Association reads are eventually consistent — poll until the observed web
34 // ACL matches the expectation (bounded).
35 const assertAssociated = (resourceArn: string, expected: string | undefined) =>
36 wafv2.getWebACLForResource({ ResourceArn: resourceArn }).pipe(
37 Effect.flatMap((response) =>
38 response.WebACL?.ARN === expected
39 ? Effect.void
40 : Effect.fail(
41 new AssociationPending({ resourceArn, expected, actual: response.WebACL?.ARN }),
42 ),
43 ),
44 Effect.retry({
45 while: (e) => e._tag === "AssociationPending",
46 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(15)]),
47 }),
48 );
49
50 // Ungated probe: associateWebACL with well-formed but nonexistent ARNs
51 // returns the typed WAFNonexistentItemException — proves the distilled
52 // wiring and the typed error union at near-zero cost, without creating
53 // any resources.
54 test.provider(
55 "associateWebACL surfaces typed WAFNonexistentItemException",
56 () =>
57 Effect.gen(function* () {
58 const identity = yield* sts.getCallerIdentity({});
59 const account = identity.Account;
60 const result = yield* Effect.result(
61 wafv2.associateWebACL({
62 WebACLArn: `arn:aws:wafv2:us-west-2:${account}:regional/webacl/alchemy-probe-nonexistent/00000000-0000-0000-0000-000000000000`,
63 ResourceArn: `arn:aws:appsync:us-west-2:${account}:apis/alchemyprobenonexistent`,
64 }),
65 );
66 expect(Result.isFailure(result)).toBe(true);
67 if (Result.isFailure(result)) {
68 expect(result.failure._tag).toBe("WAFNonexistentItemException");
69 }
70 }),
71 { tags: ["provider:aws", "provider:aws:wafv2", "live"] },
72 );
73
74 // Live lifecycle is gated: associating a FRESHLY CREATED web ACL (or
75 // protected resource) intermittently fails with
76 // WAFUnavailableEntityException: AWS WAF couldn’t retrieve the resource
77 // that you requested. Retry your request.
78 // for well over 150s (~50% of back-to-back runs; observed with both
79 // Cognito user pool and AppSync API targets; terraform-provider-aws
80 // retries this for 5 minutes — see hashicorp/terraform-provider-aws#24386).
81 // The provider retries the tag on a ~150s budget which covers routine
82 // propagation; the deterministic suite can't absorb the multi-minute tail.
83 test.provider.skipIf(!process.env.AWS_TEST_WAF_ASSOCIATION)(
84 "associate a web ACL with an AppSync API, re-point, disassociate",
85 (stack) =>
86 Effect.gen(function* () {
87 // reconcile away any prior partial/crashed deployment
88 yield* stack.destroy();
89
90 // create API + ACL + association in one deploy
91 const outputs = yield* stack.deploy(
92 Effect.gen(function* () {
93 const api = yield* GraphqlApi("WafProtectedApi", {});
94 const acl = yield* WebACL("PoolAclA", {});
95 yield* WebACLAssociation("PoolAssociation", {
96 webAclArn: acl.webAclArn,
97 resourceArn: api.apiArn,
98 });
99 return {
100 apiArn: api.apiArn,
101 aclName: acl.webAclName,
102 aclId: acl.webAclId,
103 aclArn: acl.webAclArn,
104 };
105 }),
106 );
107
108 // out-of-band verification via getWebACLForResource
109 yield* assertAssociated(outputs.apiArn, outputs.aclArn);
110
111 // re-point the association at a second web ACL in place (keep the
112 // first ACL deployed — never drop a dependency in the same step)
113 const repointed = yield* stack.deploy(
114 Effect.gen(function* () {
115 const api = yield* GraphqlApi("WafProtectedApi", {});
116 yield* WebACL("PoolAclA", {});
117 const aclB = yield* WebACL("PoolAclB", {});
118 yield* WebACLAssociation("PoolAssociation", {
119 webAclArn: aclB.webAclArn,
120 resourceArn: api.apiArn,
121 });
122 return { aclBName: aclB.webAclName, aclBId: aclB.webAclId, aclBArn: aclB.webAclArn };
123 }),
124 );
125
126 expect(repointed.aclBArn).not.toBe(outputs.aclArn);
127 yield* assertAssociated(outputs.apiArn, repointed.aclBArn);
128
129 // destroy everything; deletion order proves the association was
130 // removed before its web ACL (deleteWebACL fails while associated)
131 yield* stack.destroy();
132 yield* assertWebAclDeleted(outputs.aclName, outputs.aclId);
133 yield* assertWebAclDeleted(repointed.aclBName, repointed.aclBId);
134 }),
135 // First associate can wait up to ~150s for the fresh user pool to
136 // propagate to WAF (retryUnavailableEntityLong).
137 {
138 tags: ["provider:aws", "provider:aws:appsync", "provider:aws:wafv2", "live"],
139 timeout: 240_000,
140 },
141 );