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 { FirewallPolicy, 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. |
| 12 | test.provider( |
| 13 | "describeFirewallPolicy on a nonexistent policy fails with ResourceNotFoundException", |
| 14 | () => |
| 15 | Effect.gen(function* () { |
| 16 | const error = yield* Effect.flip( |
| 17 | nfw.describeFirewallPolicy({ |
| 18 | FirewallPolicyName: "alchemy-nonexistent-nfw-policy-probe", |
| 19 | }), |
| 20 | ); |
| 21 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 22 | }), |
| 23 | { tags: ["provider:aws", "provider:aws:networkfirewall", "live"] }, |
| 24 | ); |
| 25 | |
| 26 | // Deletion transitions through a DELETING status before the policy |
| 27 | // disappears — poll (bounded) until describe returns the typed not-found |
| 28 | // tag. Throttled polls count as "still waiting". |
| 29 | const assertPolicyGone = (arn: string) => |
| 30 | Effect.gen(function* () { |
| 31 | const status = yield* nfw.describeFirewallPolicy({ FirewallPolicyArn: arn }).pipe( |
| 32 | Effect.map((r) => r.FirewallPolicyResponse.FirewallPolicyStatus ?? ("UNKNOWN" as const)), |
| 33 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)), |
| 34 | Effect.catchTag("ThrottlingException", () => Effect.succeed("THROTTLED" as const)), |
| 35 | ); |
| 36 | if (status !== "gone") { |
| 37 | return yield* Effect.fail( |
| 38 | new Error(`firewall policy '${arn}' still exists (status: ${status})`), |
| 39 | ); |
| 40 | } |
| 41 | }).pipe( |
| 42 | Effect.retry({ |
| 43 | schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]), |
| 44 | }), |
| 45 | ); |
| 46 | |
| 47 | test.provider( |
| 48 | "create, update, and delete a firewall policy referencing a rule group", |
| 49 | (stack) => |
| 50 | Effect.gen(function* () { |
| 51 | yield* stack.destroy(); |
| 52 | |
| 53 | const { stateful, policy } = yield* stack.deploy( |
| 54 | Effect.gen(function* () { |
| 55 | const stateful = yield* RuleGroup("Stateful", { |
| 56 | type: "STATEFUL", |
| 57 | capacity: 100, |
| 58 | rules: 'pass tcp any any -> any 443 (msg:"allow https"; sid:200001; rev:1;)', |
| 59 | }); |
| 60 | const policy = yield* FirewallPolicy("Policy", { |
| 61 | firewallPolicy: { |
| 62 | StatelessDefaultActions: ["aws:forward_to_sfe"], |
| 63 | StatelessFragmentDefaultActions: ["aws:forward_to_sfe"], |
| 64 | StatefulRuleGroupReferences: [{ ResourceArn: stateful.ruleGroupArn }], |
| 65 | }, |
| 66 | description: "v1", |
| 67 | tags: { fixture: "nfw-policy" }, |
| 68 | }); |
| 69 | return { stateful, policy }; |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | expect(policy.firewallPolicyArn).toContain(":firewall-policy/"); |
| 74 | expect(policy.firewallPolicyId).toBeDefined(); |
| 75 | |
| 76 | // Out-of-band verification via distilled. |
| 77 | const observed = yield* nfw.describeFirewallPolicy({ |
| 78 | FirewallPolicyArn: policy.firewallPolicyArn, |
| 79 | }); |
| 80 | expect(observed.FirewallPolicyResponse.FirewallPolicyStatus).toBe("ACTIVE"); |
| 81 | expect(observed.FirewallPolicy?.StatefulRuleGroupReferences?.[0]?.ResourceArn).toBe( |
| 82 | stateful.ruleGroupArn, |
| 83 | ); |
| 84 | expect(observed.FirewallPolicyResponse.Description).toBe("v1"); |
| 85 | |
| 86 | yield* stack.destroy(); |
| 87 | yield* assertPolicyGone(policy.firewallPolicyArn); |
| 88 | }), |
| 89 | { |
| 90 | tags: ["provider:aws", "provider:aws:networkfirewall", "live"], |
| 91 | timeout: 240_000, |
| 92 | }, |
| 93 | ); |
| 94 | |
| 95 | test.provider( |
| 96 | "updates the policy definition in place", |
| 97 | (stack) => |
| 98 | Effect.gen(function* () { |
| 99 | yield* stack.destroy(); |
| 100 | |
| 101 | const deploy = (statelessDefaultActions: string[], description: string) => |
| 102 | stack.deploy( |
| 103 | Effect.gen(function* () { |
| 104 | const policy = yield* FirewallPolicy("PassPolicy", { |
| 105 | firewallPolicy: { |
| 106 | StatelessDefaultActions: statelessDefaultActions, |
| 107 | StatelessFragmentDefaultActions: statelessDefaultActions, |
| 108 | }, |
| 109 | description, |
| 110 | }); |
| 111 | return { policy }; |
| 112 | }), |
| 113 | ); |
| 114 | |
| 115 | const first = yield* deploy(["aws:pass"], "pass everything"); |
| 116 | const second = yield* deploy(["aws:drop"], "drop everything"); |
| 117 | |
| 118 | // In-place update: same ARN, new definition. |
| 119 | expect(second.policy.firewallPolicyArn).toBe(first.policy.firewallPolicyArn); |
| 120 | const observed = yield* nfw.describeFirewallPolicy({ |
| 121 | FirewallPolicyArn: second.policy.firewallPolicyArn, |
| 122 | }); |
| 123 | expect(observed.FirewallPolicy?.StatelessDefaultActions).toEqual(["aws:drop"]); |
| 124 | expect(observed.FirewallPolicyResponse.Description).toBe("drop everything"); |
| 125 | |
| 126 | yield* stack.destroy(); |
| 127 | yield* assertPolicyGone(second.policy.firewallPolicyArn); |
| 128 | }), |
| 129 | { |
| 130 | tags: ["provider:aws", "provider:aws:networkfirewall", "live"], |
| 131 | timeout: 240_000, |
| 132 | }, |
| 133 | ); |