The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { RuleGroup } from "@alchemy.run/aws/NetworkFirewall"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as nfw from "@distilled.cloud/aws/network-firewall"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: AWS.providers() }); |
| 10 | |
| 11 | // Ungated typed-error probe: prove the distilled error union carries the |
| 12 | // not-found tag this provider's read/delete paths depend on. |
| 13 | test.provider( |
| 14 | "describeRuleGroup on a nonexistent rule group fails with ResourceNotFoundException", |
| 15 | () => |
| 16 | Effect.gen(function* () { |
| 17 | const error = yield* Effect.flip( |
| 18 | nfw.describeRuleGroup({ |
| 19 | RuleGroupName: "alchemy-nonexistent-nfw-rulegroup-probe", |
| 20 | Type: "STATEFUL", |
| 21 | }), |
| 22 | ); |
| 23 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 24 | }), |
| 25 | { tags: ["provider:aws", "provider:aws:networkfirewall", "live"] }, |
| 26 | ); |
| 27 | |
| 28 | const suricataV1 = 'pass tcp any any -> any 80 (msg:"allow http"; sid:100001; rev:1;)'; |
| 29 | const suricataV2 = 'drop tcp any any -> any 23 (msg:"block telnet"; sid:100002; rev:1;)'; |
| 30 | |
| 31 | const statelessRuleGroup: nfw.RuleGroup = { |
| 32 | RulesSource: { |
| 33 | StatelessRulesAndCustomActions: { |
| 34 | StatelessRules: [ |
| 35 | { |
| 36 | Priority: 1, |
| 37 | RuleDefinition: { |
| 38 | Actions: ["aws:pass"], |
| 39 | MatchAttributes: { |
| 40 | Sources: [{ AddressDefinition: "0.0.0.0/0" }], |
| 41 | Destinations: [{ AddressDefinition: "0.0.0.0/0" }], |
| 42 | Protocols: [6], |
| 43 | DestinationPorts: [{ FromPort: 80, ToPort: 80 }], |
| 44 | }, |
| 45 | }, |
| 46 | }, |
| 47 | ], |
| 48 | }, |
| 49 | }, |
| 50 | }; |
| 51 | |
| 52 | // Deletion transitions through a DELETING status before the rule group |
| 53 | // disappears — poll (bounded) until describe returns the typed not-found |
| 54 | // tag. Throttled polls count as "still waiting". |
| 55 | const assertRuleGroupGone = (arn: string) => |
| 56 | Effect.gen(function* () { |
| 57 | const status = yield* nfw.describeRuleGroup({ RuleGroupArn: arn }).pipe( |
| 58 | Effect.map((r) => r.RuleGroupResponse.RuleGroupStatus ?? ("UNKNOWN" as const)), |
| 59 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)), |
| 60 | Effect.catchTag("ThrottlingException", () => Effect.succeed("THROTTLED" as const)), |
| 61 | ); |
| 62 | if (status !== "gone" && status !== "DELETING") { |
| 63 | yield* Effect.log(`rule group '${arn}' status: ${status}`); |
| 64 | } |
| 65 | if (status !== "gone") { |
| 66 | return yield* Effect.fail(new Error(`rule group '${arn}' still exists (status: ${status})`)); |
| 67 | } |
| 68 | }).pipe( |
| 69 | Effect.retry({ |
| 70 | schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]), |
| 71 | }), |
| 72 | ); |
| 73 | |
| 74 | test.provider( |
| 75 | "create, update, and delete stateless + stateful rule groups", |
| 76 | (stack) => |
| 77 | Effect.gen(function* () { |
| 78 | yield* stack.destroy(); |
| 79 | |
| 80 | const deploy = (rules: string, description: string) => |
| 81 | stack.deploy( |
| 82 | Effect.gen(function* () { |
| 83 | const stateless = yield* RuleGroup("Stateless", { |
| 84 | type: "STATELESS", |
| 85 | capacity: 10, |
| 86 | ruleGroup: statelessRuleGroup, |
| 87 | tags: { fixture: "nfw-rulegroup" }, |
| 88 | }); |
| 89 | const stateful = yield* RuleGroup("Stateful", { |
| 90 | type: "STATEFUL", |
| 91 | capacity: 100, |
| 92 | rules, |
| 93 | description, |
| 94 | }); |
| 95 | return { stateless, stateful }; |
| 96 | }), |
| 97 | ); |
| 98 | |
| 99 | const { stateless, stateful } = yield* deploy(suricataV1, "v1"); |
| 100 | |
| 101 | expect(stateless.ruleGroupArn).toContain(":stateless-rulegroup/"); |
| 102 | expect(stateless.type).toBe("STATELESS"); |
| 103 | expect(stateless.capacity).toBe(10); |
| 104 | expect(stateful.ruleGroupArn).toContain(":stateful-rulegroup/"); |
| 105 | expect(stateful.type).toBe("STATEFUL"); |
| 106 | |
| 107 | // Out-of-band verification via distilled. |
| 108 | const observed = yield* nfw.describeRuleGroup({ |
| 109 | RuleGroupArn: stateful.ruleGroupArn, |
| 110 | }); |
| 111 | expect(observed.RuleGroupResponse.RuleGroupStatus).toBe("ACTIVE"); |
| 112 | expect(observed.RuleGroup?.RulesSource?.RulesString).toBe(suricataV1); |
| 113 | expect(observed.RuleGroupResponse.Description).toBe("v1"); |
| 114 | |
| 115 | // Update in place — same ARN, new rules + description. |
| 116 | const updated = yield* deploy(suricataV2, "v2"); |
| 117 | expect(updated.stateful.ruleGroupArn).toBe(stateful.ruleGroupArn); |
| 118 | expect(updated.stateless.ruleGroupArn).toBe(stateless.ruleGroupArn); |
| 119 | const reobserved = yield* nfw.describeRuleGroup({ |
| 120 | RuleGroupArn: stateful.ruleGroupArn, |
| 121 | }); |
| 122 | expect(reobserved.RuleGroup?.RulesSource?.RulesString).toBe(suricataV2); |
| 123 | expect(reobserved.RuleGroupResponse.Description).toBe("v2"); |
| 124 | |
| 125 | // Internal + user tags landed on the cloud resource. |
| 126 | const tags = Object.fromEntries( |
| 127 | (yield* nfw.listTagsForResource({ |
| 128 | ResourceArn: stateless.ruleGroupArn, |
| 129 | })).Tags?.map((t) => [t.Key, t.Value]) ?? [], |
| 130 | ); |
| 131 | expect(tags.fixture).toBe("nfw-rulegroup"); |
| 132 | |
| 133 | yield* stack.destroy(); |
| 134 | yield* assertRuleGroupGone(stateless.ruleGroupArn); |
| 135 | yield* assertRuleGroupGone(stateful.ruleGroupArn); |
| 136 | }), |
| 137 | { |
| 138 | tags: ["provider:aws", "provider:aws:networkfirewall", "live"], |
| 139 | timeout: 240_000, |
| 140 | }, |
| 141 | ); |
| 142 | |
| 143 | test.provider( |
| 144 | "changing capacity replaces the rule group", |
| 145 | (stack) => |
| 146 | Effect.gen(function* () { |
| 147 | yield* stack.destroy(); |
| 148 | |
| 149 | const deploy = (capacity: number) => |
| 150 | stack.deploy( |
| 151 | Effect.gen(function* () { |
| 152 | const group = yield* RuleGroup("Replaceable", { |
| 153 | type: "STATEFUL", |
| 154 | capacity, |
| 155 | rules: suricataV1, |
| 156 | }); |
| 157 | return { group }; |
| 158 | }), |
| 159 | ); |
| 160 | |
| 161 | const first = yield* deploy(50); |
| 162 | const second = yield* deploy(60); |
| 163 | |
| 164 | // Capacity is create-only — the engine must have provisioned a new |
| 165 | // physical rule group (new instance-suffixed name, new ARN). |
| 166 | expect(second.group.ruleGroupArn).not.toBe(first.group.ruleGroupArn); |
| 167 | expect(second.group.capacity).toBe(60); |
| 168 | |
| 169 | const observed = yield* nfw.describeRuleGroup({ |
| 170 | RuleGroupArn: second.group.ruleGroupArn, |
| 171 | }); |
| 172 | expect(observed.RuleGroupResponse.Capacity).toBe(60); |
| 173 | |
| 174 | yield* stack.destroy(); |
| 175 | yield* assertRuleGroupGone(second.group.ruleGroupArn); |
| 176 | }), |
| 177 | { |
| 178 | tags: ["provider:aws", "provider:aws:networkfirewall", "live"], |
| 179 | timeout: 240_000, |
| 180 | }, |
| 181 | ); |