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

main
ts 198 lines 8.34 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { SecurityGroup, SecurityGroupRule, Vpc } from "@alchemy.run/aws/EC2";
3 import { expect } from "@alchemy.run/test";
4 import * as EC2 from "@distilled.cloud/aws/ec2";
5 import * as Drift from "alchemy/Drift";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import { assertSecurityGroupGone, assertVpcGone } from "./Gone.ts";
11 import * as Test from "./VpcTest.ts";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 test.provider(
18 "list enumerates the deployed Security Group Rule",
19 (stack) =>
20 Effect.gen(function* () {
21 yield* stack.destroy();
22
23 const { vpc, sg, rule } = yield* stack.deploy(
24 Effect.gen(function* () {
25 const vpc = yield* Vpc("ListSgrVpc", { cidrBlock: "10.0.0.0/16" });
26 const sg = yield* SecurityGroup("ListSgrSg", { vpcId: vpc.vpcId });
27 const rule = yield* SecurityGroupRule("ListSgr", {
28 groupId: sg.groupId,
29 type: "ingress",
30 ipProtocol: "tcp",
31 fromPort: 443,
32 toPort: 443,
33 cidrIpv4: "10.0.0.0/16",
34 });
35 return { vpc, sg, rule };
36 }),
37 );
38
39 // Modification requires canonical CIDRs; authorization normalizes them.
40 const rejection = yield* EC2.modifySecurityGroupRules({
41 GroupId: sg.groupId,
42 SecurityGroupRules: [
43 {
44 SecurityGroupRuleId: rule.securityGroupRuleId,
45 SecurityGroupRule: {
46 IpProtocol: "tcp",
47 FromPort: 443,
48 ToPort: 443,
49 CidrIpv4: "10.0.0.7/16",
50 },
51 },
52 ],
53 }).pipe(
54 Effect.as(undefined),
55 Effect.catchTag("InvalidParameterValue", (error) => Effect.succeed(error)),
56 );
57 expect(rejection?._tag).toBe("InvalidParameterValue");
58 expect(rejection?.message).toBe("CIDR block 10.0.0.7/16 is not in canonical form");
59
60 const provider = yield* Provider.findProvider(SecurityGroupRule);
61 const all = yield* provider.list();
62
63 expect(all.some((x) => x.securityGroupRuleId === rule.securityGroupRuleId)).toBe(true);
64
65 yield* stack.destroy();
66
67 // The rule dies with its security group; group + VPC gone proves full
68 // teardown.
69 yield* assertSecurityGroupGone(sg.groupId);
70 yield* assertVpcGone(vpc.vpcId);
71 }).pipe(logLevel),
72 { tags: ["provider:aws", "provider:aws:ec2", "live"] },
73 );
74
75 for (const type of ["ingress", "egress"] as const) {
76 test.provider(
77 `repairs standalone ${type} rule drift with unchanged inputs`,
78 (stack) =>
79 Effect.gen(function* () {
80 yield* stack.destroy();
81 const program = (description?: string) =>
82 Effect.gen(function* () {
83 const vpc = yield* Vpc("DriftVpc", { cidrBlock: "10.0.0.0/16" });
84 const sg = yield* SecurityGroup("DriftSg", { vpcId: vpc.vpcId, egress: [] });
85 const rule = yield* SecurityGroupRule("DriftRule", {
86 groupId: sg.groupId,
87 type,
88 ipProtocol: "6",
89 fromPort: 443,
90 toPort: 443,
91 cidrIpv4: "10.0.0.7/16",
92 description,
93 tags: { purpose: "managed" },
94 });
95 return { sg, vpc, rule };
96 });
97 const desired = program("managed rule");
98 const { sg, vpc, rule } = yield* stack.deploy(desired);
99 const readRule = EC2.describeSecurityGroupRules({
100 SecurityGroupRuleIds: [rule.securityGroupRuleId],
101 }).pipe(Effect.map((result) => result.SecurityGroupRules![0]!));
102 expect((yield* stack.plan(desired)).resources.DriftRule?.action).toBe("noop");
103 // #1968: the standalone rule belongs to DriftRule, not to the group,
104 // so the group does not read as drifted — but a rogue rule does.
105 const detect = Drift.detect({ name: stack.name, stage: stack.stage }).pipe(
106 Effect.provide(stack.state),
107 );
108 const clean = yield* detect;
109 expect(clean.resources.DriftSg?.action).toBe("unchanged");
110 expect(clean.resources.DriftRule?.action).toBe("unchanged");
111 const rogue = yield* EC2.authorizeSecurityGroupIngress({
112 GroupId: sg.groupId,
113 IpPermissions: [
114 { IpProtocol: "tcp", FromPort: 22, ToPort: 22, IpRanges: [{ CidrIp: "0.0.0.0/0" }] },
115 ],
116 });
117 const rogueId = rogue.SecurityGroupRules![0]!.SecurityGroupRuleId!;
118 const withRogue = (yield* detect).resources.DriftSg;
119 expect(withRogue?.action).toBe("drifted");
120 expect(
121 withRogue?.attr.ingressRules.map(
122 (value: { securityGroupRuleId: string }) => value.securityGroupRuleId,
123 ),
124 ).toEqual([rogueId]);
125 yield* EC2.deleteTags({
126 Resources: [rule.securityGroupRuleId],
127 Tags: [{ Key: "alchemy::id" }],
128 });
129 yield* EC2.createTags({
130 Resources: [rule.securityGroupRuleId],
131 Tags: [{ Key: "purpose", Value: "external" }],
132 });
133 for (const drift of [
134 { IpProtocol: "udp", FromPort: 443, ToPort: 443, CidrIpv4: "10.0.0.0/16" },
135 { IpProtocol: "tcp", FromPort: 8443, ToPort: 8443, CidrIpv4: "10.0.0.0/16" },
136 { IpProtocol: "tcp", FromPort: 443, ToPort: 443, CidrIpv4: "0.0.0.0/0" },
137 ]) {
138 yield* EC2.modifySecurityGroupRules({
139 GroupId: sg.groupId,
140 SecurityGroupRules: [
141 {
142 SecurityGroupRuleId: rule.securityGroupRuleId,
143 SecurityGroupRule: { ...drift, Description: "managed rule" },
144 },
145 ],
146 });
147 const observed = yield* readRule.pipe(
148 Effect.repeat({
149 until: (value) =>
150 value.IpProtocol === drift.IpProtocol &&
151 value.FromPort === drift.FromPort &&
152 value.CidrIpv4 === drift.CidrIpv4,
153 schedule: Schedule.spaced("1 second"),
154 times: 8,
155 }),
156 );
157 expect(observed.IpProtocol).toBe(drift.IpProtocol);
158 expect(observed.FromPort).toBe(drift.FromPort);
159 expect(observed.CidrIpv4).toBe(drift.CidrIpv4);
160 expect(observed.Description).toBe("managed rule");
161 expect((yield* stack.plan(desired)).resources.DriftRule?.action).toBe("update");
162 const repaired = yield* stack.deploy(desired);
163 expect(repaired.rule.securityGroupRuleId).toBe(rule.securityGroupRuleId);
164 const actual = yield* readRule;
165 expect(actual.IpProtocol).toBe("tcp");
166 expect(actual.FromPort).toBe(443);
167 expect(actual.ToPort).toBe(443);
168 expect(actual.CidrIpv4).toBe("10.0.0.0/16");
169 expect(actual.Description).toBe("managed rule");
170 expect(actual.Tags?.find((tag) => tag.Key === "purpose")?.Value).toBe("managed");
171 expect(actual.Tags?.find((tag) => tag.Key === "alchemy::id")?.Value).toBe("DriftRule");
172 expect((yield* stack.plan(desired)).resources.DriftRule?.action).toBe("noop");
173 }
174 const groupRules = (yield* EC2.describeSecurityGroupRules({
175 Filters: [{ Name: "group-id", Values: [sg.groupId] }],
176 })).SecurityGroupRules!;
177 expect(groupRules.some((value) => value.SecurityGroupRuleId === rogueId)).toBe(false);
178 expect(
179 groupRules.some((value) => value.SecurityGroupRuleId === rule.securityGroupRuleId),
180 ).toBe(true);
181 yield* stack.deploy(program());
182 expect((yield* readRule).Description ?? "").toBe("");
183 expect((yield* stack.plan(program())).resources.DriftRule?.action).toBe("noop");
184 yield* stack.destroy();
185 const absent = yield* EC2.describeSecurityGroupRules({
186 SecurityGroupRuleIds: [rule.securityGroupRuleId],
187 }).pipe(
188 Effect.as(false),
189 Effect.catchTag("InvalidSecurityGroupRuleId.NotFound", () => Effect.succeed(true)),
190 Effect.repeat({ until: Boolean, schedule: Schedule.spaced("1 second"), times: 8 }),
191 );
192 expect(absent).toBe(true);
193 yield* assertSecurityGroupGone(sg.groupId);
194 yield* assertVpcGone(vpc.vpcId);
195 }),
196 { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 },
197 );
198 }