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

main
ts 128 lines 4.48 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { RuleGroup } from "@alchemy.run/aws/WAFv2";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import type * as WAFV2 from "@distilled.cloud/aws/wafv2";
6 import * as wafv2 from "@distilled.cloud/aws/wafv2";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 class RuleGroupStillExists extends Data.TaggedError("RuleGroupStillExists")<{
14 readonly name: string;
15 }> {}
16
17 const assertRuleGroupDeleted = (name: string, id: string) =>
18 wafv2.getRuleGroup({ Name: name, Scope: "REGIONAL", Id: id }).pipe(
19 Effect.flatMap(() => Effect.fail(new RuleGroupStillExists({ name }))),
20 Effect.catchTag("WAFNonexistentItemException", () => Effect.void),
21 Effect.retry({
22 while: (e) => e._tag === "RuleGroupStillExists",
23 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
24 }),
25 );
26
27 const visibility = (metricName: string): WAFV2.VisibilityConfig => ({
28 SampledRequestsEnabled: true,
29 CloudWatchMetricsEnabled: true,
30 MetricName: metricName,
31 });
32
33 const blockPathRule = (searchString: string): WAFV2.Rule => ({
34 Name: "block-path",
35 Priority: 0,
36 Statement: {
37 ByteMatchStatement: {
38 SearchString: new TextEncoder().encode(searchString),
39 FieldToMatch: { UriPath: {} },
40 TextTransformations: [{ Priority: 0, Type: "LOWERCASE" }],
41 PositionalConstraint: "STARTS_WITH",
42 },
43 },
44 Action: { Block: {} },
45 VisibilityConfig: visibility("block-path"),
46 });
47
48 test.provider(
49 "create, update rules, replace on capacity change, delete",
50 (stack) =>
51 Effect.gen(function* () {
52 // reconcile away any prior partial/crashed deployment
53 yield* stack.destroy();
54
55 const group = yield* stack.deploy(
56 Effect.gen(function* () {
57 return yield* RuleGroup("LifecycleRuleGroup", {
58 capacity: 50,
59 rules: [blockPathRule("/admin")],
60 tags: { Environment: "test" },
61 });
62 }),
63 );
64
65 expect(group.capacity).toBe(50);
66 expect(group.scope).toBe("REGIONAL");
67 expect(group.ruleGroupArn).toContain("/rulegroup/");
68
69 // out-of-band verification via distilled
70 const created = yield* wafv2.getRuleGroup({
71 Name: group.ruleGroupName,
72 Scope: "REGIONAL",
73 Id: group.ruleGroupId,
74 });
75 expect(created.RuleGroup?.Capacity).toBe(50);
76 expect(created.RuleGroup?.Rules?.length).toBe(1);
77 expect(
78 created.RuleGroup?.Rules?.[0]?.Statement?.ByteMatchStatement?.PositionalConstraint,
79 ).toBe("STARTS_WITH");
80
81 const tags = yield* wafv2.listTagsForResource({
82 ResourceARN: group.ruleGroupArn,
83 });
84 const tagRecord = Object.fromEntries(
85 (tags.TagInfoForResource?.TagList ?? []).map((t) => [t.Key, t.Value]),
86 );
87 expect(tagRecord["alchemy::id"]).toBe("LifecycleRuleGroup");
88
89 // rules are mutable within the fixed capacity — updated in place
90 const updated = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* RuleGroup("LifecycleRuleGroup", {
93 capacity: 50,
94 rules: [blockPathRule("/internal")],
95 description: "blocks internal paths",
96 tags: { Environment: "test" },
97 });
98 }),
99 );
100 expect(updated.ruleGroupId).toBe(group.ruleGroupId);
101
102 const afterUpdate = yield* wafv2.getRuleGroup({
103 Name: group.ruleGroupName,
104 Scope: "REGIONAL",
105 Id: group.ruleGroupId,
106 });
107 expect(afterUpdate.RuleGroup?.Description).toBe("blocks internal paths");
108 const search = afterUpdate.RuleGroup?.Rules?.[0]?.Statement?.ByteMatchStatement?.SearchString;
109 expect(new TextDecoder().decode(search)).toBe("/internal");
110
111 // capacity is immutable ⇒ replacement
112 const replaced = yield* stack.deploy(
113 Effect.gen(function* () {
114 return yield* RuleGroup("LifecycleRuleGroup", {
115 capacity: 60,
116 rules: [blockPathRule("/internal")],
117 });
118 }),
119 );
120 expect(replaced.ruleGroupId).not.toBe(group.ruleGroupId);
121 expect(replaced.capacity).toBe(60);
122 yield* assertRuleGroupDeleted(group.ruleGroupName, group.ruleGroupId);
123
124 yield* stack.destroy();
125 yield* assertRuleGroupDeleted(replaced.ruleGroupName, replaced.ruleGroupId);
126 }),
127 { tags: ["provider:aws", "provider:aws:wafv2", "live"], timeout: 120_000 },
128 );