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
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { |
| 3 | DefaultSecurityGroup, |
| 4 | NetworkInterface, |
| 5 | PrefixList, |
| 6 | SecurityGroup, |
| 7 | SecurityGroupRule, |
| 8 | Subnet, |
| 9 | Vpc, |
| 10 | } from "@alchemy.run/aws/EC2"; |
| 11 | import type { DefaultSecurityGroupProps, SecurityGroupRuleData, VpcId } from "@alchemy.run/aws/EC2"; |
| 12 | import { expect } from "@alchemy.run/test"; |
| 13 | import * as Core from "@alchemy.run/test/Core"; |
| 14 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 15 | import * as Drift from "alchemy/Drift"; |
| 16 | import * as Output from "alchemy/Output"; |
| 17 | import { isActionState, State } from "alchemy/State/State"; |
| 18 | import * as Effect from "effect/Effect"; |
| 19 | import * as HttpClient from "effect/http/HttpClient"; |
| 20 | import { MinimumLogLevel } from "effect/References"; |
| 21 | import * as Schedule from "effect/Schedule"; |
| 22 | import * as Stream from "effect/Stream"; |
| 23 | import { assertVpcGone } from "./Gone.ts"; |
| 24 | import * as Test from "./VpcTest.ts"; |
| 25 | |
| 26 | const { test } = Test.make({ providers: AWS.providers() }, 2); |
| 27 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 28 | |
| 29 | test.provider( |
| 30 | "AWS creates the default group with its initial rules", |
| 31 | (stack) => |
| 32 | Effect.gen(function* () { |
| 33 | yield* stack.destroy(); |
| 34 | const vpc = yield* stack.deploy( |
| 35 | Effect.gen(function* () { |
| 36 | return yield* Vpc("DefaultSecurityGroupInitialVpc", { cidrBlock: "10.43.0.0/16" }); |
| 37 | }), |
| 38 | ); |
| 39 | |
| 40 | const group = yield* findDefaultGroup(vpc.vpcId); |
| 41 | yield* expectRules( |
| 42 | group.GroupId!, |
| 43 | [{ IpProtocol: "-1", ReferencedGroupInfo: { GroupId: group.GroupId! } }], |
| 44 | [{ IpProtocol: "-1", CidrIpv4: "0.0.0.0/0" }], |
| 45 | ); |
| 46 | |
| 47 | yield* stack.destroy(); |
| 48 | yield* assertVpcGone(vpc.vpcId); |
| 49 | }).pipe(logLevel), |
| 50 | { tags: ["provider:aws", "provider:aws:ec2", "live"] }, |
| 51 | ); |
| 52 | |
| 53 | // This changes the AWS-created default security group only inside the VPC this |
| 54 | // test creates and destroys. |
| 55 | test.provider( |
| 56 | "manages a temporary VPC's default security group without deleting it", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | // The VPC output is consumed in this same first deployment. This proves |
| 62 | // that the default group can be found and closed without a second deploy. |
| 63 | const initial = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | const vpc = yield* Vpc("DefaultSecurityGroupVpc", { cidrBlock: "10.42.0.0/16" }); |
| 66 | const defaultSecurityGroup = yield* DefaultSecurityGroup("DefaultSecurityGroup", { |
| 67 | vpcId: vpc.vpcId, |
| 68 | ingress: [], |
| 69 | egress: [], |
| 70 | }); |
| 71 | return { vpc, defaultSecurityGroup }; |
| 72 | }), |
| 73 | ); |
| 74 | |
| 75 | const defaultGroup = yield* findDefaultGroup(initial.vpc.vpcId); |
| 76 | expect(initial.defaultSecurityGroup.groupId).toEqual(defaultGroup.GroupId); |
| 77 | yield* expectRules(initial.defaultSecurityGroup.groupId, [], []); |
| 78 | |
| 79 | // A second identical deployment verifies idempotence against AWS readback. |
| 80 | yield* stack.deploy( |
| 81 | Effect.gen(function* () { |
| 82 | const vpc = yield* Vpc("DefaultSecurityGroupVpc", { cidrBlock: "10.42.0.0/16" }); |
| 83 | return yield* DefaultSecurityGroup("DefaultSecurityGroup", { |
| 84 | vpcId: vpc.vpcId, |
| 85 | ingress: [], |
| 86 | egress: [], |
| 87 | }); |
| 88 | }), |
| 89 | ); |
| 90 | yield* expectRules(initial.defaultSecurityGroup.groupId, [], []); |
| 91 | |
| 92 | // A changed inline declaration replaces the inline rule set. |
| 93 | yield* stack.deploy( |
| 94 | Effect.gen(function* () { |
| 95 | const vpc = yield* Vpc("DefaultSecurityGroupVpc", { cidrBlock: "10.42.0.0/16" }); |
| 96 | return yield* DefaultSecurityGroup("DefaultSecurityGroup", { |
| 97 | vpcId: vpc.vpcId, |
| 98 | ingress: [{ ipProtocol: "tcp", fromPort: 443, toPort: 443, cidrIpv4: "10.42.0.0/16" }], |
| 99 | egress: [], |
| 100 | }); |
| 101 | }), |
| 102 | ); |
| 103 | yield* expectRules( |
| 104 | initial.defaultSecurityGroup.groupId, |
| 105 | [{ IpProtocol: "tcp", FromPort: 443, ToPort: 443, CidrIpv4: "10.42.0.0/16" }], |
| 106 | [], |
| 107 | ); |
| 108 | |
| 109 | // Removing the Alchemy resource must not delete the AWS-owned group or |
| 110 | // restore its initial AWS rules. |
| 111 | yield* stack.deploy( |
| 112 | Effect.gen(function* () { |
| 113 | return yield* Vpc("DefaultSecurityGroupVpc", { cidrBlock: "10.42.0.0/16" }); |
| 114 | }), |
| 115 | ); |
| 116 | const preserved = yield* findDefaultGroup(initial.vpc.vpcId); |
| 117 | expect(preserved.GroupId).toEqual(initial.defaultSecurityGroup.groupId); |
| 118 | yield* expectRules( |
| 119 | initial.defaultSecurityGroup.groupId, |
| 120 | [{ IpProtocol: "tcp", FromPort: 443, ToPort: 443, CidrIpv4: "10.42.0.0/16" }], |
| 121 | [], |
| 122 | ); |
| 123 | |
| 124 | yield* stack.destroy(); |
| 125 | yield* assertVpcGone(initial.vpc.vpcId); |
| 126 | }).pipe(logLevel), |
| 127 | { tags: ["provider:aws", "provider:aws:ec2", "live"] }, |
| 128 | ); |
| 129 | |
| 130 | for (const direction of ["ingress", "egress"] as const) { |
| 131 | test.provider( |
| 132 | `repairs ${direction} drift and applies only changed rules`, |
| 133 | (stack) => |
| 134 | Effect.gen(function* () { |
| 135 | yield* stack.destroy(); |
| 136 | const retained: SecurityGroupRuleData = { |
| 137 | ipProtocol: "tcp", |
| 138 | fromPort: 443, |
| 139 | toPort: 443, |
| 140 | cidrIpv4: "10.44.0.0/16", |
| 141 | description: "retained", |
| 142 | }; |
| 143 | const changed: SecurityGroupRuleData = { |
| 144 | ipProtocol: "udp", |
| 145 | fromPort: 53, |
| 146 | toPort: 53, |
| 147 | cidrIpv4: "10.44.0.0/16", |
| 148 | description: "DNS", |
| 149 | }; |
| 150 | const program = (rules = [retained, changed]) => |
| 151 | Effect.gen(function* () { |
| 152 | const vpc = yield* Vpc("DriftVpc", { cidrBlock: "10.44.0.0/16" }); |
| 153 | const group = yield* DefaultSecurityGroup("DriftGroup", { |
| 154 | vpcId: vpc.vpcId, |
| 155 | ingress: direction === "ingress" ? rules : [retained], |
| 156 | egress: direction === "egress" ? rules : [retained], |
| 157 | }); |
| 158 | return { vpc, group }; |
| 159 | }); |
| 160 | const created = yield* stack.deploy(program()); |
| 161 | const groupId = created.group.groupId; |
| 162 | const initial = yield* readRules(groupId); |
| 163 | const stable = initial.filter((rule) => rule.FromPort === 443); |
| 164 | expect(stable).toHaveLength(2); |
| 165 | const missing = initial.find((rule) => rule.FromPort === 53)!; |
| 166 | const revoke = |
| 167 | direction === "ingress" ? EC2.revokeSecurityGroupIngress : EC2.revokeSecurityGroupEgress; |
| 168 | const authorize = |
| 169 | direction === "ingress" |
| 170 | ? EC2.authorizeSecurityGroupIngress |
| 171 | : EC2.authorizeSecurityGroupEgress; |
| 172 | yield* revoke({ GroupId: groupId, SecurityGroupRuleIds: [missing.SecurityGroupRuleId!] }); |
| 173 | const rogue = yield* authorize({ |
| 174 | GroupId: groupId, |
| 175 | IpPermissions: [ |
| 176 | { IpProtocol: "tcp", FromPort: 22, ToPort: 22, IpRanges: [{ CidrIp: "0.0.0.0/0" }] }, |
| 177 | ], |
| 178 | TagSpecifications: [ |
| 179 | { |
| 180 | ResourceType: "security-group-rule", |
| 181 | Tags: [{ Key: "alchemy::id", Value: "DriftGroup" }], |
| 182 | }, |
| 183 | ], |
| 184 | }); |
| 185 | const rogueId = rogue.SecurityGroupRules![0]!.SecurityGroupRuleId!; |
| 186 | yield* waitForRules( |
| 187 | groupId, |
| 188 | (rules) => |
| 189 | !rules.some((rule) => rule.SecurityGroupRuleId === missing.SecurityGroupRuleId) && |
| 190 | rules.some((rule) => rule.SecurityGroupRuleId === rogueId), |
| 191 | ); |
| 192 | const missingRuleError = yield* EC2.modifySecurityGroupRules({ |
| 193 | GroupId: groupId, |
| 194 | SecurityGroupRules: [ |
| 195 | { |
| 196 | SecurityGroupRuleId: missing.SecurityGroupRuleId!, |
| 197 | SecurityGroupRule: { |
| 198 | IpProtocol: "udp", |
| 199 | FromPort: 53, |
| 200 | ToPort: 53, |
| 201 | CidrIpv4: "10.44.0.0/16", |
| 202 | Description: "missing rule probe", |
| 203 | }, |
| 204 | }, |
| 205 | ], |
| 206 | }).pipe(Effect.flip); |
| 207 | expect(missingRuleError).toMatchObject({ _tag: "InvalidSecurityGroupRuleId.NotFound" }); |
| 208 | expect((yield* stack.plan(program())).resources.DriftGroup?.action).toBe("update"); |
| 209 | yield* stack.deploy(program()); |
| 210 | const repaired = yield* readRules(groupId); |
| 211 | expect(repaired).toHaveLength(3); |
| 212 | expect(repaired).toEqual(expect.arrayContaining(stable)); |
| 213 | const restored = repaired.find((rule) => rule.FromPort === 53)!; |
| 214 | expect(restored.SecurityGroupRuleId).not.toBe(missing.SecurityGroupRuleId); |
| 215 | expect(restored.IsEgress).toBe(direction === "egress"); |
| 216 | expect(restored.Description).toBe("DNS"); |
| 217 | expect(repaired.some((rule) => rule.SecurityGroupRuleId === rogueId)).toBe(false); |
| 218 | |
| 219 | yield* EC2.modifySecurityGroupRules({ |
| 220 | GroupId: groupId, |
| 221 | SecurityGroupRules: [ |
| 222 | { |
| 223 | SecurityGroupRuleId: restored.SecurityGroupRuleId!, |
| 224 | SecurityGroupRule: { |
| 225 | IpProtocol: "udp", |
| 226 | FromPort: 53, |
| 227 | ToPort: 53, |
| 228 | CidrIpv4: "10.44.0.0/16", |
| 229 | Description: "external description", |
| 230 | }, |
| 231 | }, |
| 232 | ], |
| 233 | }); |
| 234 | yield* waitForRules(groupId, (rules) => |
| 235 | rules.some( |
| 236 | (rule) => |
| 237 | rule.SecurityGroupRuleId === restored.SecurityGroupRuleId && |
| 238 | rule.Description === "external description", |
| 239 | ), |
| 240 | ); |
| 241 | expect((yield* stack.plan(program())).resources.DriftGroup?.action).toBe("update"); |
| 242 | yield* stack.deploy(program()); |
| 243 | const descriptions = yield* readRules(groupId); |
| 244 | expect(descriptions).toEqual(expect.arrayContaining([...stable, restored])); |
| 245 | |
| 246 | // External identity edits are removed, not preserved as extra access. |
| 247 | yield* EC2.modifySecurityGroupRules({ |
| 248 | GroupId: groupId, |
| 249 | SecurityGroupRules: [ |
| 250 | { |
| 251 | SecurityGroupRuleId: restored.SecurityGroupRuleId!, |
| 252 | SecurityGroupRule: { |
| 253 | IpProtocol: "tcp", |
| 254 | FromPort: 25, |
| 255 | ToPort: 25, |
| 256 | CidrIpv4: "0.0.0.0/0", |
| 257 | Description: "external access", |
| 258 | }, |
| 259 | }, |
| 260 | ], |
| 261 | }); |
| 262 | yield* waitForRules(groupId, (rules) => |
| 263 | rules.some( |
| 264 | (rule) => |
| 265 | rule.SecurityGroupRuleId === restored.SecurityGroupRuleId && rule.FromPort === 25, |
| 266 | ), |
| 267 | ); |
| 268 | expect((yield* stack.plan(program())).resources.DriftGroup?.action).toBe("update"); |
| 269 | yield* stack.deploy(program()); |
| 270 | const repairedIdentity = yield* readRules(groupId); |
| 271 | expect(repairedIdentity).toHaveLength(3); |
| 272 | expect(repairedIdentity).toEqual(expect.arrayContaining(stable)); |
| 273 | expect(repairedIdentity.some((rule) => rule.FromPort === 25)).toBe(false); |
| 274 | expect(repairedIdentity.find((rule) => rule.FromPort === 53)?.Description).toBe("DNS"); |
| 275 | |
| 276 | const replacement = { ...changed, fromPort: 123, toPort: 123, description: "NTP" }; |
| 277 | yield* stack.deploy(program([retained, replacement])); |
| 278 | const edited = yield* readRules(groupId); |
| 279 | expect(edited).toHaveLength(3); |
| 280 | expect(edited).toEqual(expect.arrayContaining(stable)); |
| 281 | expect(edited.some((rule) => rule.FromPort === 53)).toBe(false); |
| 282 | expect(edited.find((rule) => rule.FromPort === 123)?.Description).toBe("NTP"); |
| 283 | |
| 284 | yield* stack.deploy(program([])); |
| 285 | const opposite = stable.filter((rule) => rule.IsEgress !== (direction === "egress")); |
| 286 | expect(yield* readRules(groupId)).toEqual(opposite); |
| 287 | const external = yield* authorize({ |
| 288 | GroupId: groupId, |
| 289 | IpPermissions: [{ IpProtocol: "-1", IpRanges: [{ CidrIp: "0.0.0.0/0" }] }], |
| 290 | }); |
| 291 | yield* waitForRules(groupId, (rules) => |
| 292 | rules.some( |
| 293 | (rule) => |
| 294 | rule.SecurityGroupRuleId === external.SecurityGroupRules![0]!.SecurityGroupRuleId, |
| 295 | ), |
| 296 | ); |
| 297 | expect((yield* stack.plan(program([]))).resources.DriftGroup?.action).toBe("update"); |
| 298 | yield* stack.deploy(program([])); |
| 299 | expect(yield* readRules(groupId)).toEqual(opposite); |
| 300 | |
| 301 | const observer = yield* observeRuleRequests; |
| 302 | yield* Effect.gen(function* () { |
| 303 | expect((yield* stack.plan(program([]))).resources.DriftGroup?.action).toBe("noop"); |
| 304 | yield* stack.deploy(program([])); |
| 305 | }).pipe(Effect.provideService(HttpClient.HttpClient, observer.client)); |
| 306 | expect(observer.requests.length).toBeGreaterThan(0); |
| 307 | expect(observer.requests.filter((request) => request.write)).toEqual([]); |
| 308 | expect(yield* readRules(groupId)).toEqual(opposite); |
| 309 | yield* stack.destroy(); |
| 310 | yield* assertVpcGone(created.vpc.vpcId); |
| 311 | }).pipe(logLevel), |
| 312 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 313 | ); |
| 314 | } |
| 315 | |
| 316 | test.provider( |
| 317 | "canonicalizes and deduplicates rules while preserving IDs and updating descriptions in place", |
| 318 | (stack) => |
| 319 | Effect.gen(function* () { |
| 320 | yield* stack.destroy(); |
| 321 | const dual: SecurityGroupRuleData = { |
| 322 | ipProtocol: "6", |
| 323 | fromPort: 443, |
| 324 | toPort: 443, |
| 325 | cidrIpv4: "10.45.0.7/16", |
| 326 | cidrIpv6: "2001:0DB8:0:0:0:0:0:5/64", |
| 327 | description: "HTTPS", |
| 328 | }; |
| 329 | const supplied: SecurityGroupRuleData[] = [ |
| 330 | dual, |
| 331 | { ...dual, cidrIpv4: "10.45.0.0/16" }, |
| 332 | { ipProtocol: "58", cidrIpv6: "2001:db8::1/64" }, |
| 333 | { ipProtocol: "-1", fromPort: 0, toPort: 65535, cidrIpv4: "10.46.0.1/16" }, |
| 334 | { ipProtocol: "50", fromPort: 0, toPort: 65535, cidrIpv4: "10.47.0.1/16" }, |
| 335 | { ipProtocol: "17", fromPort: 53, toPort: 53, cidrIpv4: "10.45.0.7/16" }, |
| 336 | { ipProtocol: "1", fromPort: 8, toPort: -1, cidrIpv4: "10.45.0.7/16" }, |
| 337 | ]; |
| 338 | const canonical: SecurityGroupRuleData[] = [ |
| 339 | { ipProtocol: "icmp", fromPort: 8, toPort: -1, cidrIpv4: "10.45.0.0/16" }, |
| 340 | { ipProtocol: "udp", fromPort: 53, toPort: 53, cidrIpv4: "10.45.0.0/16" }, |
| 341 | { ipProtocol: "50", cidrIpv4: "10.47.0.0/16" }, |
| 342 | { ipProtocol: "-1", cidrIpv4: "10.46.0.0/16" }, |
| 343 | { ipProtocol: "icmpv6", fromPort: -1, toPort: -1, cidrIpv6: "2001:db8::/64" }, |
| 344 | { |
| 345 | ipProtocol: "tcp", |
| 346 | fromPort: 443, |
| 347 | toPort: 443, |
| 348 | cidrIpv6: "2001:db8::/64", |
| 349 | description: "HTTPS", |
| 350 | }, |
| 351 | { |
| 352 | ipProtocol: "tcp", |
| 353 | fromPort: 443, |
| 354 | toPort: 443, |
| 355 | cidrIpv4: "10.45.0.0/16", |
| 356 | description: "HTTPS", |
| 357 | }, |
| 358 | ]; |
| 359 | const program = (rules: SecurityGroupRuleData[]) => |
| 360 | Effect.gen(function* () { |
| 361 | const vpc = yield* Vpc("CanonicalVpc", { cidrBlock: "10.45.0.0/16" }); |
| 362 | const group = yield* DefaultSecurityGroup("CanonicalGroup", { |
| 363 | vpcId: vpc.vpcId, |
| 364 | ingress: rules, |
| 365 | egress: rules, |
| 366 | }); |
| 367 | return { vpc, group }; |
| 368 | }); |
| 369 | const created = yield* stack.deploy(program(supplied)); |
| 370 | const groupId = created.group.groupId; |
| 371 | const initial = yield* readRules(groupId); |
| 372 | expect(initial).toHaveLength(14); |
| 373 | for (const isEgress of [false, true]) { |
| 374 | const rules = initial.filter((rule) => rule.IsEgress === isEgress); |
| 375 | expect(rules).toHaveLength(7); |
| 376 | expect(rules.filter((rule) => rule.IpProtocol === "tcp")).toEqual( |
| 377 | expect.arrayContaining([ |
| 378 | expect.objectContaining({ CidrIpv4: "10.45.0.0/16", Description: "HTTPS" }), |
| 379 | expect.objectContaining({ CidrIpv6: "2001:db8::/64", Description: "HTTPS" }), |
| 380 | ]), |
| 381 | ); |
| 382 | expect( |
| 383 | rules.some( |
| 384 | (rule) => |
| 385 | ["58", "icmpv6"].includes(rule.IpProtocol!) && |
| 386 | rule.FromPort === -1 && |
| 387 | rule.ToPort === -1, |
| 388 | ), |
| 389 | ).toBe(true); |
| 390 | } |
| 391 | expect((yield* stack.plan(program(supplied))).resources.CanonicalGroup?.action).toBe("noop"); |
| 392 | const observer = yield* observeRuleRequests; |
| 393 | yield* stack |
| 394 | .deploy(program(canonical)) |
| 395 | .pipe(Effect.provideService(HttpClient.HttpClient, observer.client)); |
| 396 | expect(observer.requests.length).toBeGreaterThan(0); |
| 397 | expect(observer.requests.filter((request) => request.write)).toEqual([]); |
| 398 | expect(yield* readRules(groupId)).toEqual(expect.arrayContaining(initial)); |
| 399 | expect(yield* readRules(groupId)).toHaveLength(initial.length); |
| 400 | |
| 401 | for (const description of ["Updated HTTPS", undefined]) { |
| 402 | const desired = canonical.map((rule) => |
| 403 | rule.ipProtocol === "tcp" ? { ...rule, description } : rule, |
| 404 | ); |
| 405 | const writes = yield* observeRuleRequests; |
| 406 | yield* stack |
| 407 | .deploy(program(desired)) |
| 408 | .pipe(Effect.provideService(HttpClient.HttpClient, writes.client)); |
| 409 | const mutations = writes.requests.filter((request) => request.write); |
| 410 | expect(mutations.length).toBeGreaterThan(0); |
| 411 | expect(mutations.every((request) => request.action === "ModifySecurityGroupRules")).toBe( |
| 412 | true, |
| 413 | ); |
| 414 | expect([...new Set(mutations.flatMap((request) => request.ruleIds))].sort()).toEqual( |
| 415 | initial |
| 416 | .filter((rule) => rule.IpProtocol === "tcp") |
| 417 | .map((rule) => rule.SecurityGroupRuleId) |
| 418 | .sort(), |
| 419 | ); |
| 420 | const observed = yield* readRules(groupId); |
| 421 | expect(observed.map((rule) => rule.SecurityGroupRuleId).sort()).toEqual( |
| 422 | initial.map((rule) => rule.SecurityGroupRuleId).sort(), |
| 423 | ); |
| 424 | for (const rule of observed) { |
| 425 | if (rule.IpProtocol === "tcp") expect(rule.Description ?? "").toBe(description ?? ""); |
| 426 | } |
| 427 | expect((yield* stack.plan(program(desired))).resources.CanonicalGroup?.action).toBe("noop"); |
| 428 | const noop = yield* observeRuleRequests; |
| 429 | yield* stack |
| 430 | .deploy(program(desired)) |
| 431 | .pipe(Effect.provideService(HttpClient.HttpClient, noop.client)); |
| 432 | expect(noop.requests.length).toBeGreaterThan(0); |
| 433 | expect(noop.requests.filter((request) => request.write)).toEqual([]); |
| 434 | } |
| 435 | yield* stack.destroy(); |
| 436 | yield* assertVpcGone(created.vpc.vpcId); |
| 437 | }).pipe(logLevel), |
| 438 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 439 | ); |
| 440 | |
| 441 | test.provider( |
| 442 | "recovers cold state and adopts existing default-group rules authoritatively", |
| 443 | (stack) => |
| 444 | Effect.gen(function* () { |
| 445 | yield* stack.destroy(); |
| 446 | const program = (port: number, vpcId?: VpcId) => |
| 447 | Effect.gen(function* () { |
| 448 | const vpc = yield* Vpc("RecoveryVpc", { cidrBlock: "10.48.0.0/16" }); |
| 449 | const group = yield* DefaultSecurityGroup("RecoveryGroup", { |
| 450 | vpcId: vpcId ?? vpc.vpcId, |
| 451 | ingress: [ |
| 452 | { ipProtocol: "tcp", fromPort: port, toPort: port, cidrIpv4: "10.48.0.0/16" }, |
| 453 | ], |
| 454 | egress: [], |
| 455 | }); |
| 456 | return { vpc, group }; |
| 457 | }); |
| 458 | const created = yield* stack.deploy(program(443)); |
| 459 | const key = { stack: stack.name, stage: stack.stage, fqn: "RecoveryGroup" }; |
| 460 | const state = yield* Effect.gen(function* () { |
| 461 | return yield* yield* State; |
| 462 | }).pipe(Effect.provide(stack.state)); |
| 463 | const row = yield* state.get(key); |
| 464 | if (!row || isActionState(row) || (row.status !== "created" && row.status !== "updated")) { |
| 465 | return yield* Effect.fail(new Error("Expected a persisted default security group")); |
| 466 | } |
| 467 | const rules = yield* readRules(created.group.groupId); |
| 468 | yield* state.set({ ...key, value: { ...row, status: "creating", attr: undefined } }); |
| 469 | const recoveryPlan = yield* stack.plan(program(443, created.vpc.vpcId)); |
| 470 | expect(recoveryPlan.resources.RecoveryGroup?.state?.attr?.groupId).toBe( |
| 471 | created.group.groupId, |
| 472 | ); |
| 473 | const observer = yield* observeRuleRequests; |
| 474 | const recovered = yield* stack |
| 475 | .deploy(program(443, created.vpc.vpcId)) |
| 476 | .pipe(Effect.provideService(HttpClient.HttpClient, observer.client)); |
| 477 | expect(recovered.group.groupId).toBe(created.group.groupId); |
| 478 | expect(observer.requests.length).toBeGreaterThan(0); |
| 479 | expect(observer.requests.filter((request) => request.write)).toEqual([]); |
| 480 | expect(yield* readRules(created.group.groupId)).toEqual(rules); |
| 481 | |
| 482 | // Lose only the managed-group row; the VPC remains tracked for cleanup. |
| 483 | yield* state.delete(key); |
| 484 | const adoptedPlan = yield* stack.plan(program(8443, created.vpc.vpcId)); |
| 485 | expect(adoptedPlan.resources.RecoveryGroup?.state?.attr?.groupId).toBe(created.group.groupId); |
| 486 | const adopted = yield* stack.deploy(program(8443, created.vpc.vpcId)); |
| 487 | expect(adopted.group.groupId).toBe(created.group.groupId); |
| 488 | yield* expectRules( |
| 489 | created.group.groupId, |
| 490 | [{ IpProtocol: "tcp", FromPort: 8443, ToPort: 8443, CidrIpv4: "10.48.0.0/16" }], |
| 491 | [], |
| 492 | ); |
| 493 | expect( |
| 494 | (yield* stack.plan(program(8443, created.vpc.vpcId))).resources.RecoveryGroup?.action, |
| 495 | ).toBe("noop"); |
| 496 | yield* stack.destroy(); |
| 497 | yield* assertVpcGone(created.vpc.vpcId); |
| 498 | }).pipe(logLevel), |
| 499 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 500 | ); |
| 501 | |
| 502 | test.provider( |
| 503 | "reads a missing VPC as absent and destroys stale state idempotently", |
| 504 | (stack) => |
| 505 | Effect.gen(function* () { |
| 506 | yield* stack.destroy(); |
| 507 | const created = yield* stack.deploy( |
| 508 | Effect.gen(function* () { |
| 509 | const vpc = yield* Vpc("MissingVpc", { cidrBlock: "10.49.0.0/16" }); |
| 510 | const group = yield* DefaultSecurityGroup("MissingGroup", { |
| 511 | vpcId: vpc.vpcId, |
| 512 | ingress: [], |
| 513 | egress: [], |
| 514 | }); |
| 515 | return { vpc, group }; |
| 516 | }), |
| 517 | ); |
| 518 | yield* EC2.deleteVpc({ VpcId: created.vpc.vpcId }); |
| 519 | yield* assertVpcGone(created.vpc.vpcId); |
| 520 | const drift = yield* Drift.detect(stack).pipe(Effect.provide(stack.state)); |
| 521 | expect(drift.resources.MissingGroup?.action).toBe("missing"); |
| 522 | expect(drift.resources.MissingGroup?.attr).toBeUndefined(); |
| 523 | yield* Effect.gen(function* () { |
| 524 | const state = yield* yield* State; |
| 525 | yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "MissingGroup" }); |
| 526 | }).pipe(Effect.provide(stack.state)); |
| 527 | const cold = yield* stack.plan( |
| 528 | Effect.gen(function* () { |
| 529 | yield* Vpc("MissingVpc", { cidrBlock: "10.49.0.0/16" }); |
| 530 | return yield* DefaultSecurityGroup("MissingGroup", { |
| 531 | vpcId: created.vpc.vpcId, |
| 532 | ingress: [], |
| 533 | egress: [], |
| 534 | }); |
| 535 | }), |
| 536 | ); |
| 537 | expect(cold.resources.MissingGroup?.action).toBe("create"); |
| 538 | expect(cold.resources.MissingGroup?.state).toBeUndefined(); |
| 539 | yield* stack.destroy(); |
| 540 | yield* stack.destroy(); |
| 541 | yield* assertVpcGone(created.vpc.vpcId); |
| 542 | }).pipe(logLevel), |
| 543 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 544 | ); |
| 545 | |
| 546 | test.provider( |
| 547 | "replaces VPC identity while retaining the old group's last-applied rules", |
| 548 | (stack) => |
| 549 | Effect.gen(function* () { |
| 550 | yield* stack.destroy(); |
| 551 | const program = (useSecond: boolean, keepFirst = true) => |
| 552 | Effect.gen(function* () { |
| 553 | const first = keepFirst |
| 554 | ? yield* Vpc("FirstVpc", { cidrBlock: "10.50.0.0/16" }) |
| 555 | : undefined; |
| 556 | const second = yield* Vpc("SecondVpc", { cidrBlock: "10.51.0.0/16" }); |
| 557 | const group = yield* DefaultSecurityGroup("MovingGroup", { |
| 558 | vpcId: useSecond ? second.vpcId : first!.vpcId, |
| 559 | ingress: [{ ipProtocol: "tcp", fromPort: 443, toPort: 443, cidrIpv4: "10.50.0.0/16" }], |
| 560 | egress: [], |
| 561 | }); |
| 562 | return { first, second, group }; |
| 563 | }); |
| 564 | const created = yield* stack.deploy(program(false)); |
| 565 | const oldVpcId = created.first!.vpcId; |
| 566 | const oldRules = yield* readRules(created.group.groupId); |
| 567 | expect(oldRules).toHaveLength(1); |
| 568 | expect((yield* stack.plan(program(true))).resources.MovingGroup?.action).toBe("replace"); |
| 569 | const moved = yield* stack.deploy(program(true)); |
| 570 | expect(moved.group.groupId).not.toBe(created.group.groupId); |
| 571 | expect(moved.group.vpcId).toBe(created.second.vpcId); |
| 572 | expect((yield* findDefaultGroup(oldVpcId)).GroupId).toBe(created.group.groupId); |
| 573 | expect(yield* readRules(created.group.groupId)).toEqual(oldRules); |
| 574 | yield* expectRules( |
| 575 | moved.group.groupId, |
| 576 | [{ IpProtocol: "tcp", FromPort: 443, ToPort: 443, CidrIpv4: "10.50.0.0/16" }], |
| 577 | [], |
| 578 | ); |
| 579 | const newRules = yield* readRules(moved.group.groupId); |
| 580 | // Remove the old dependency only after its resource replacement is complete. |
| 581 | yield* stack.deploy(program(true, false)); |
| 582 | yield* assertVpcGone(oldVpcId); |
| 583 | expect(yield* readRules(moved.group.groupId)).toEqual(newRules); |
| 584 | expect((yield* stack.plan(program(true, false))).resources.MovingGroup?.action).toBe("noop"); |
| 585 | yield* stack.destroy(); |
| 586 | yield* assertVpcGone(created.second.vpcId); |
| 587 | }).pipe(logLevel), |
| 588 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 589 | ); |
| 590 | |
| 591 | for (const scenario of ["new destination VPC", "upstream VPC replacement"] as const) { |
| 592 | test.provider( |
| 593 | `propagates unresolved group identity to a real ENI during ${scenario}`, |
| 594 | (stack) => |
| 595 | Effect.gen(function* () { |
| 596 | yield* stack.destroy(); |
| 597 | const program = (switchVpc: boolean) => |
| 598 | Effect.gen(function* () { |
| 599 | const original = yield* Vpc("IdentityVpc", { |
| 600 | cidrBlock: |
| 601 | switchVpc && scenario === "upstream VPC replacement" |
| 602 | ? "10.53.0.0/16" |
| 603 | : "10.52.0.0/16", |
| 604 | }); |
| 605 | // Keep the original VPC declared while introducing its destination. |
| 606 | const destination = |
| 607 | switchVpc && scenario === "new destination VPC" |
| 608 | ? yield* Vpc("DestinationVpc", { cidrBlock: "10.53.0.0/16" }) |
| 609 | : original; |
| 610 | const group = yield* DefaultSecurityGroup("IdentityGroup", { |
| 611 | vpcId: destination.vpcId, |
| 612 | ingress: [ |
| 613 | { ipProtocol: "tcp", fromPort: 443, toPort: 443, cidrIpv4: "10.52.0.0/16" }, |
| 614 | ], |
| 615 | egress: [], |
| 616 | }); |
| 617 | const subnet = switchVpc |
| 618 | ? yield* Subnet("ConsumerSubnet", { |
| 619 | vpcId: destination.vpcId, |
| 620 | cidrBlock: "10.53.1.0/24", |
| 621 | }) |
| 622 | : undefined; |
| 623 | const eni = subnet |
| 624 | ? yield* NetworkInterface("ConsumerInterface", { |
| 625 | subnetId: subnet.subnetId, |
| 626 | securityGroupIds: [group.groupId], |
| 627 | }) |
| 628 | : undefined; |
| 629 | return { original, destination, group, subnet, eni }; |
| 630 | }); |
| 631 | const created = yield* stack.deploy(program(false)); |
| 632 | const oldRules = yield* readRules(created.group.groupId); |
| 633 | const plan = yield* stack.plan(program(true)); |
| 634 | expect(plan.resources.IdentityGroup?.action).toBe("replace"); |
| 635 | expect(plan.resources.IdentityVpc?.action).toBe( |
| 636 | scenario === "upstream VPC replacement" ? "replace" : "noop", |
| 637 | ); |
| 638 | if (scenario === "new destination VPC") { |
| 639 | expect(plan.resources.DestinationVpc?.action).toBe("create"); |
| 640 | } |
| 641 | expect(plan.resources.ConsumerInterface?.action).toBe("create"); |
| 642 | const moved = yield* stack.deploy(program(true)); |
| 643 | expect(moved.group.groupId).not.toBe(created.group.groupId); |
| 644 | expect(moved.destination.vpcId).not.toBe(created.destination.vpcId); |
| 645 | expect(moved.group.vpcId).toBe(moved.destination.vpcId); |
| 646 | const observed = (yield* EC2.describeNetworkInterfaces({ |
| 647 | NetworkInterfaceIds: [moved.eni!.networkInterfaceId], |
| 648 | })).NetworkInterfaces?.[0]; |
| 649 | expect(observed?.VpcId).toBe(moved.destination.vpcId); |
| 650 | expect(observed?.SubnetId).toBe(moved.subnet!.subnetId); |
| 651 | expect(observed?.Groups?.map((group) => group.GroupId)).toEqual([moved.group.groupId]); |
| 652 | if (scenario === "new destination VPC") { |
| 653 | expect((yield* findDefaultGroup(created.original.vpcId)).GroupId).toBe( |
| 654 | created.group.groupId, |
| 655 | ); |
| 656 | expect(yield* readRules(created.group.groupId)).toEqual(oldRules); |
| 657 | } else { |
| 658 | yield* assertVpcGone(created.original.vpcId); |
| 659 | } |
| 660 | const noop = yield* stack.plan(program(true)); |
| 661 | expect(noop.resources.IdentityGroup?.action).toBe("noop"); |
| 662 | expect(noop.resources.ConsumerInterface?.action).toBe("noop"); |
| 663 | yield* stack.destroy(); |
| 664 | yield* assertVpcGone(created.original.vpcId); |
| 665 | yield* assertVpcGone(moved.destination.vpcId); |
| 666 | }).pipe(logLevel), |
| 667 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 668 | ); |
| 669 | } |
| 670 | |
| 671 | test.provider( |
| 672 | "manages group and prefix-list sources and rejects invalid rules without writes", |
| 673 | (stack) => |
| 674 | Effect.gen(function* () { |
| 675 | yield* stack.destroy(); |
| 676 | const program = ( |
| 677 | description?: string, |
| 678 | directions?: { ingress: SecurityGroupRuleData[]; egress: SecurityGroupRuleData[] }, |
| 679 | ) => |
| 680 | Effect.gen(function* () { |
| 681 | const vpc = yield* Vpc("SourcesVpc", { cidrBlock: "10.54.0.0/16" }); |
| 682 | const peer = yield* SecurityGroup("SourcePeer", { vpcId: vpc.vpcId, egress: [] }); |
| 683 | const prefix = yield* PrefixList("SourcePrefix", { |
| 684 | maxEntries: 1, |
| 685 | entries: [{ cidr: "10.54.0.0/16" }], |
| 686 | }); |
| 687 | const rule = { |
| 688 | ipProtocol: "tcp", |
| 689 | fromPort: 443, |
| 690 | toPort: 443, |
| 691 | referencedGroupId: peer.groupId, |
| 692 | prefixListId: prefix.prefixListId, |
| 693 | description, |
| 694 | }; |
| 695 | const group = yield* DefaultSecurityGroup("SourcesGroup", { |
| 696 | vpcId: vpc.vpcId, |
| 697 | ingress: directions?.ingress ?? [rule], |
| 698 | egress: directions?.egress ?? [rule], |
| 699 | }); |
| 700 | return { vpc, peer, prefix, group }; |
| 701 | }); |
| 702 | const created = yield* stack.deploy(program("Before")); |
| 703 | const groupId = created.group.groupId; |
| 704 | yield* Effect.gen(function* () { |
| 705 | const initial = yield* readRules(groupId); |
| 706 | expect(initial).toHaveLength(4); |
| 707 | for (const isEgress of [false, true]) { |
| 708 | const rules = initial.filter((rule) => rule.IsEgress === isEgress); |
| 709 | expect(rules).toHaveLength(2); |
| 710 | expect(rules).toEqual( |
| 711 | expect.arrayContaining([ |
| 712 | expect.objectContaining({ |
| 713 | ReferencedGroupInfo: expect.objectContaining({ GroupId: created.peer.groupId }), |
| 714 | Description: "Before", |
| 715 | }), |
| 716 | expect.objectContaining({ |
| 717 | PrefixListId: created.prefix.prefixListId, |
| 718 | Description: "Before", |
| 719 | }), |
| 720 | ]), |
| 721 | ); |
| 722 | } |
| 723 | for (const description of ["After", undefined]) { |
| 724 | const observer = yield* observeRuleRequests; |
| 725 | yield* stack |
| 726 | .deploy(program(description)) |
| 727 | .pipe(Effect.provideService(HttpClient.HttpClient, observer.client)); |
| 728 | const rules = yield* readRules(groupId); |
| 729 | expect(rules).toHaveLength(4); |
| 730 | expect(rules.map((rule) => rule.SecurityGroupRuleId).sort()).toEqual( |
| 731 | initial.map((rule) => rule.SecurityGroupRuleId).sort(), |
| 732 | ); |
| 733 | for (const original of initial) { |
| 734 | const rule = rules.find( |
| 735 | (rule) => rule.SecurityGroupRuleId === original.SecurityGroupRuleId, |
| 736 | )!; |
| 737 | expect(rule.IsEgress).toBe(original.IsEgress); |
| 738 | expect(rule.ReferencedGroupInfo?.GroupId).toBe(original.ReferencedGroupInfo?.GroupId); |
| 739 | expect(rule.PrefixListId).toBe(original.PrefixListId); |
| 740 | expect(rule.Description ?? "").toBe(description ?? ""); |
| 741 | } |
| 742 | const writes = observer.requests.filter((request) => request.write); |
| 743 | expect(writes.length).toBeGreaterThan(0); |
| 744 | expect(writes.every((request) => request.action === "ModifySecurityGroupRules")).toBe( |
| 745 | true, |
| 746 | ); |
| 747 | expect([...new Set(writes.flatMap((request) => request.ruleIds))].sort()).toEqual( |
| 748 | initial.map((rule) => rule.SecurityGroupRuleId).sort(), |
| 749 | ); |
| 750 | } |
| 751 | const settled = yield* readRules(groupId); |
| 752 | const invalid: Array<{ rules: SecurityGroupRuleData[]; message: string }> = [ |
| 753 | { |
| 754 | rules: [ |
| 755 | { |
| 756 | ipProtocol: "tcp", |
| 757 | fromPort: 443, |
| 758 | toPort: 443, |
| 759 | cidrIpv4: "10.54.0.7/16", |
| 760 | description: "one", |
| 761 | }, |
| 762 | { |
| 763 | ipProtocol: "6", |
| 764 | fromPort: 443, |
| 765 | toPort: 443, |
| 766 | cidrIpv4: "10.54.0.0/16", |
| 767 | description: "two", |
| 768 | }, |
| 769 | ], |
| 770 | message: "Duplicate rules must have the same description.", |
| 771 | }, |
| 772 | { |
| 773 | rules: [{ ipProtocol: "tcp", fromPort: 443, toPort: 443 }], |
| 774 | message: "Every rule must specify a protocol and a source.", |
| 775 | }, |
| 776 | { |
| 777 | rules: [{ ipProtocol: "", cidrIpv4: "10.54.0.0/16" }], |
| 778 | message: "Every rule must specify a protocol and a source.", |
| 779 | }, |
| 780 | ]; |
| 781 | for (const { rules, message } of invalid) { |
| 782 | const observer = yield* observeRuleRequests; |
| 783 | const error = yield* stack |
| 784 | .deploy(program(undefined, { ingress: [], egress: rules })) |
| 785 | .pipe(Effect.provideService(HttpClient.HttpClient, observer.client), Effect.flip); |
| 786 | expect(error).toMatchObject({ _tag: "InvalidDefaultSecurityGroupRules", message }); |
| 787 | expect(observer.requests.filter((request) => request.write)).toEqual([]); |
| 788 | expect(yield* readRules(groupId)).toEqual(expect.arrayContaining(settled)); |
| 789 | expect(yield* readRules(groupId)).toHaveLength(settled.length); |
| 790 | } |
| 791 | expect((yield* stack.plan(program())).resources.SourcesGroup?.action).toBe("noop"); |
| 792 | }).pipe( |
| 793 | // Release source references even when an assertion fails before teardown. |
| 794 | Effect.ensuring( |
| 795 | stack.deploy(program(undefined, { ingress: [], egress: [] })).pipe(Effect.ignore), |
| 796 | ), |
| 797 | ); |
| 798 | expect(yield* readRules(groupId)).toEqual([]); |
| 799 | yield* stack.destroy(); |
| 800 | yield* assertVpcGone(created.vpc.vpcId); |
| 801 | const prefixGone = yield* EC2.describeManagedPrefixLists({ |
| 802 | PrefixListIds: [created.prefix.prefixListId], |
| 803 | }).pipe( |
| 804 | Effect.map((result) => |
| 805 | (result.PrefixLists ?? []).every((list) => list.State === "delete-complete"), |
| 806 | ), |
| 807 | Effect.catchTag("InvalidPrefixListID.NotFound", () => Effect.succeed(true)), |
| 808 | Effect.repeat({ until: Boolean, schedule: Schedule.spaced("1 second"), times: 8 }), |
| 809 | ); |
| 810 | expect(prefixGone).toBe(true); |
| 811 | }).pipe(logLevel), |
| 812 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 813 | ); |
| 814 | |
| 815 | test.provider( |
| 816 | "uses inline defaults on omission, property removal, and explicit undefined", |
| 817 | (stack) => |
| 818 | Effect.gen(function* () { |
| 819 | yield* stack.destroy(); |
| 820 | const program = (rules: Pick<DefaultSecurityGroupProps, "ingress" | "egress"> = {}) => |
| 821 | Effect.gen(function* () { |
| 822 | const vpc = yield* Vpc("DefaultsVpc", { cidrBlock: "10.55.0.0/16" }); |
| 823 | const group = yield* DefaultSecurityGroup("DefaultsGroup", { |
| 824 | vpcId: vpc.vpcId, |
| 825 | ...rules, |
| 826 | }); |
| 827 | return { vpc, group }; |
| 828 | }); |
| 829 | const created = yield* stack.deploy(program()); |
| 830 | const groupId = created.group.groupId; |
| 831 | expect((yield* findDefaultGroup(created.vpc.vpcId)).GroupId).toBe(groupId); |
| 832 | yield* expectRules(groupId, [], [{ IpProtocol: "-1", CidrIpv4: "0.0.0.0/0" }]); |
| 833 | expect(created.group.ingressRules).toEqual([]); |
| 834 | expect(created.group.egressRules).toHaveLength(1); |
| 835 | |
| 836 | const custom = [{ ipProtocol: "tcp", fromPort: 443, toPort: 443, cidrIpv4: "10.55.0.0/16" }]; |
| 837 | yield* stack.deploy(program({ ingress: custom, egress: custom })); |
| 838 | expect(yield* readRules(groupId)).toHaveLength(2); |
| 839 | yield* stack.deploy(program()); |
| 840 | yield* expectRules(groupId, [], [{ IpProtocol: "-1", CidrIpv4: "0.0.0.0/0" }]); |
| 841 | for (const defaults of [{}, { ingress: undefined, egress: undefined }]) { |
| 842 | yield* stack.deploy(program({ ingress: [], egress: [] })); |
| 843 | yield* expectRules(groupId, [], []); |
| 844 | const restored = yield* stack.deploy(program(defaults)); |
| 845 | expect(restored.group.groupId).toBe(groupId); |
| 846 | yield* expectRules(groupId, [], [{ IpProtocol: "-1", CidrIpv4: "0.0.0.0/0" }]); |
| 847 | } |
| 848 | const observer = yield* observeRuleRequests; |
| 849 | yield* Effect.gen(function* () { |
| 850 | expect((yield* stack.plan(program())).resources.DefaultsGroup?.action).toBe("noop"); |
| 851 | yield* stack.deploy(program()); |
| 852 | }).pipe(Effect.provideService(HttpClient.HttpClient, observer.client)); |
| 853 | expect(observer.requests.length).toBeGreaterThan(0); |
| 854 | expect(observer.requests.filter((request) => request.write)).toEqual([]); |
| 855 | yield* stack.destroy(); |
| 856 | yield* assertVpcGone(created.vpc.vpcId); |
| 857 | }).pipe(logLevel), |
| 858 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 859 | ); |
| 860 | |
| 861 | test.provider( |
| 862 | "repairs unchanged omitted defaults and adopts them without restoring AWS self-ingress", |
| 863 | (stack) => |
| 864 | Effect.gen(function* () { |
| 865 | yield* stack.destroy(); |
| 866 | const program = (vpcId?: VpcId) => |
| 867 | Effect.gen(function* () { |
| 868 | const vpc = yield* Vpc("DefaultDriftVpc", { cidrBlock: "10.56.0.0/16" }); |
| 869 | const group = yield* DefaultSecurityGroup("DefaultDriftGroup", { |
| 870 | vpcId: vpcId ?? vpc.vpcId, |
| 871 | }); |
| 872 | return { vpc, group }; |
| 873 | }); |
| 874 | const created = yield* stack.deploy(program()); |
| 875 | const groupId = created.group.groupId; |
| 876 | const state = yield* Effect.gen(function* () { |
| 877 | return yield* yield* State; |
| 878 | }).pipe(Effect.provide(stack.state)); |
| 879 | for (const adopt of [false, true]) { |
| 880 | const outbound = (yield* readRules(groupId)).filter((rule) => rule.IsEgress); |
| 881 | yield* EC2.revokeSecurityGroupEgress({ |
| 882 | GroupId: groupId, |
| 883 | SecurityGroupRuleIds: outbound.map((rule) => rule.SecurityGroupRuleId!), |
| 884 | }); |
| 885 | const self = yield* EC2.authorizeSecurityGroupIngress({ |
| 886 | GroupId: groupId, |
| 887 | IpPermissions: [{ IpProtocol: "-1", UserIdGroupPairs: [{ GroupId: groupId }] }], |
| 888 | }); |
| 889 | const rogue = yield* EC2.authorizeSecurityGroupEgress({ |
| 890 | GroupId: groupId, |
| 891 | IpPermissions: [ |
| 892 | { IpProtocol: "tcp", FromPort: 25, ToPort: 25, IpRanges: [{ CidrIp: "0.0.0.0/0" }] }, |
| 893 | ], |
| 894 | }); |
| 895 | const rogueIds = [...self.SecurityGroupRules!, ...rogue.SecurityGroupRules!].map( |
| 896 | (rule) => rule.SecurityGroupRuleId!, |
| 897 | ); |
| 898 | yield* waitForRules( |
| 899 | groupId, |
| 900 | (rules) => |
| 901 | rules.length === 2 && |
| 902 | rogueIds.every((id) => rules.some((rule) => rule.SecurityGroupRuleId === id)), |
| 903 | ); |
| 904 | if (adopt) { |
| 905 | yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "DefaultDriftGroup" }); |
| 906 | } else { |
| 907 | expect((yield* stack.plan(program())).resources.DefaultDriftGroup?.action).toBe("update"); |
| 908 | } |
| 909 | const repaired = yield* stack.deploy(program(adopt ? created.vpc.vpcId : undefined)); |
| 910 | expect(repaired.group.groupId).toBe(groupId); |
| 911 | yield* expectRules(groupId, [], [{ IpProtocol: "-1", CidrIpv4: "0.0.0.0/0" }]); |
| 912 | expect( |
| 913 | (yield* readRules(groupId)).some((rule) => rogueIds.includes(rule.SecurityGroupRuleId!)), |
| 914 | ).toBe(false); |
| 915 | } |
| 916 | expect((yield* stack.plan(program())).resources.DefaultDriftGroup?.action).toBe("noop"); |
| 917 | yield* stack.destroy(); |
| 918 | yield* assertVpcGone(created.vpc.vpcId); |
| 919 | }).pipe(logLevel), |
| 920 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 921 | ); |
| 922 | |
| 923 | for (const mode of ["omitted", "empty", "inline"] as const) { |
| 924 | test.provider( |
| 925 | `composes standalone ingress and egress with ${mode} inline rules`, |
| 926 | (stack) => |
| 927 | Effect.gen(function* () { |
| 928 | yield* stack.destroy(); |
| 929 | const program = ( |
| 930 | inlinePort?: number, |
| 931 | description = "Standalone", |
| 932 | replace = false, |
| 933 | standalone = true, |
| 934 | ) => |
| 935 | Effect.gen(function* () { |
| 936 | const vpc = yield* Vpc("CompositionVpc", { cidrBlock: "10.57.0.0/16" }); |
| 937 | const port = inlinePort ?? (mode === "inline" ? 443 : undefined); |
| 938 | const inline = |
| 939 | port === undefined |
| 940 | ? undefined |
| 941 | : [{ ipProtocol: "tcp", fromPort: port, toPort: port, cidrIpv4: "10.57.0.0/16" }]; |
| 942 | const group = yield* DefaultSecurityGroup("CompositionGroup", { |
| 943 | vpcId: vpc.vpcId, |
| 944 | ...(inline |
| 945 | ? { ingress: inline, egress: inline } |
| 946 | : mode === "empty" |
| 947 | ? { ingress: [], egress: [] } |
| 948 | : {}), |
| 949 | }); |
| 950 | const ingress = standalone |
| 951 | ? yield* SecurityGroupRule("CompositionIngress", { |
| 952 | group: group, |
| 953 | type: "ingress", |
| 954 | ipProtocol: "tcp", |
| 955 | fromPort: replace ? 5433 : 5432, |
| 956 | toPort: replace ? 5433 : 5432, |
| 957 | cidrIpv4: "10.57.0.0/16", |
| 958 | description, |
| 959 | }) |
| 960 | : undefined; |
| 961 | const egress = standalone |
| 962 | ? yield* SecurityGroupRule("CompositionEgress", { |
| 963 | group: group, |
| 964 | type: "egress", |
| 965 | ipProtocol: "udp", |
| 966 | fromPort: replace ? 123 : 53, |
| 967 | toPort: replace ? 123 : 53, |
| 968 | cidrIpv4: "10.57.0.0/16", |
| 969 | description, |
| 970 | }) |
| 971 | : undefined; |
| 972 | return { vpc, group, ingress, egress }; |
| 973 | }); |
| 974 | const created = yield* stack.deploy(program()); |
| 975 | const groupId = created.group.groupId; |
| 976 | const ownedIds = [ |
| 977 | created.ingress!.securityGroupRuleId, |
| 978 | created.egress!.securityGroupRuleId, |
| 979 | ]; |
| 980 | // #1968: the standalone rules created after the group are not drift. |
| 981 | const drift = yield* Drift.detect(stack).pipe(Effect.provide(stack.state)); |
| 982 | expect(drift.resources.CompositionGroup?.action).toBe("unchanged"); |
| 983 | const initial = yield* readRules(groupId); |
| 984 | expect(initial).toHaveLength(mode === "omitted" ? 3 : mode === "empty" ? 2 : 4); |
| 985 | expect(initial.filter((rule) => !rule.IsEgress)).toHaveLength(mode === "inline" ? 2 : 1); |
| 986 | expect(initial.filter((rule) => rule.IsEgress)).toHaveLength(mode === "empty" ? 1 : 2); |
| 987 | if (mode === "omitted") { |
| 988 | expect(initial.find((rule) => rule.IpProtocol === "-1")).toMatchObject({ |
| 989 | IsEgress: true, |
| 990 | CidrIpv4: "0.0.0.0/0", |
| 991 | }); |
| 992 | } |
| 993 | const observer = yield* observeRuleRequests; |
| 994 | yield* Effect.gen(function* () { |
| 995 | const plan = yield* stack.plan(program()); |
| 996 | expect(plan.resources.CompositionGroup?.action).toBe("noop"); |
| 997 | expect(plan.resources.CompositionIngress?.action).toBe("noop"); |
| 998 | expect(plan.resources.CompositionEgress?.action).toBe("noop"); |
| 999 | yield* stack.deploy(program()); |
| 1000 | }).pipe(Effect.provideService(HttpClient.HttpClient, observer.client)); |
| 1001 | expect(observer.requests.length).toBeGreaterThan(0); |
| 1002 | expect(observer.requests.filter((request) => request.write)).toEqual([]); |
| 1003 | |
| 1004 | if (mode === "inline") { |
| 1005 | const state = yield* Effect.gen(function* () { |
| 1006 | return yield* yield* State; |
| 1007 | }).pipe(Effect.provide(stack.state)); |
| 1008 | const rows = yield* Effect.forEach( |
| 1009 | ["CompositionIngress", "CompositionEgress"], |
| 1010 | Effect.fn(function* (fqn: string) { |
| 1011 | const key = { stack: stack.name, stage: stack.stage, fqn }; |
| 1012 | const row = yield* state.get(key); |
| 1013 | if ( |
| 1014 | !row || |
| 1015 | isActionState(row) || |
| 1016 | (row.status !== "created" && row.status !== "updated") |
| 1017 | ) { |
| 1018 | return yield* Effect.fail(new Error("Expected a persisted standalone rule")); |
| 1019 | } |
| 1020 | return { |
| 1021 | key, |
| 1022 | updating: { |
| 1023 | ...row, |
| 1024 | status: "updating" as const, |
| 1025 | old: { props: row.props, attr: row.attr, bindings: row.bindings }, |
| 1026 | }, |
| 1027 | }; |
| 1028 | }), |
| 1029 | ); |
| 1030 | // Planning recognizes old.attr even before a current snapshot is available. |
| 1031 | yield* Effect.gen(function* () { |
| 1032 | for (const { key, updating } of rows) { |
| 1033 | yield* state.set({ ...key, value: { ...updating, attr: undefined } }); |
| 1034 | } |
| 1035 | expect((yield* stack.plan(program())).resources.CompositionGroup?.action).toBe("noop"); |
| 1036 | }).pipe( |
| 1037 | Effect.ensuring( |
| 1038 | Effect.forEach(rows, ({ key, updating }) => |
| 1039 | state.set({ ...key, value: updating }), |
| 1040 | ).pipe(Effect.ignore), |
| 1041 | ), |
| 1042 | ); |
| 1043 | } |
| 1044 | const updated = yield* stack.deploy(program(8443)); |
| 1045 | expect([updated.ingress!.securityGroupRuleId, updated.egress!.securityGroupRuleId]).toEqual( |
| 1046 | ownedIds, |
| 1047 | ); |
| 1048 | const updatedRules = yield* readRules(groupId); |
| 1049 | expect(updatedRules).toHaveLength(4); |
| 1050 | expect(updatedRules.filter((rule) => rule.FromPort === 8443)).toHaveLength(2); |
| 1051 | expect( |
| 1052 | updatedRules.filter((rule) => ownedIds.some((id) => id === rule.SecurityGroupRuleId)), |
| 1053 | ).toEqual( |
| 1054 | expect.arrayContaining( |
| 1055 | initial.filter((rule) => ownedIds.some((id) => id === rule.SecurityGroupRuleId)), |
| 1056 | ), |
| 1057 | ); |
| 1058 | // Standalone rules belong to their SecurityGroupRule, not the group. |
| 1059 | expect( |
| 1060 | [...updated.group.ingressRules, ...updated.group.egressRules].filter((rule) => |
| 1061 | ownedIds.some((id) => id === rule.securityGroupRuleId), |
| 1062 | ), |
| 1063 | ).toEqual([]); |
| 1064 | |
| 1065 | const described = yield* stack.deploy(program(8443, "Updated standalone")); |
| 1066 | expect([ |
| 1067 | described.ingress!.securityGroupRuleId, |
| 1068 | described.egress!.securityGroupRuleId, |
| 1069 | ]).toEqual(ownedIds); |
| 1070 | const descriptions = yield* readRules(groupId); |
| 1071 | for (const id of ownedIds) { |
| 1072 | expect(descriptions.find((rule) => rule.SecurityGroupRuleId === id)?.Description).toBe( |
| 1073 | "Updated standalone", |
| 1074 | ); |
| 1075 | } |
| 1076 | const replaced = yield* stack.deploy(program(8443, "Updated standalone", true)); |
| 1077 | expect(replaced.ingress!.securityGroupRuleId).not.toBe(ownedIds[0]); |
| 1078 | expect(replaced.egress!.securityGroupRuleId).not.toBe(ownedIds[1]); |
| 1079 | const replacementRules = yield* readRules(groupId); |
| 1080 | expect(replacementRules).toHaveLength(4); |
| 1081 | expect( |
| 1082 | replacementRules.some((rule) => ownedIds.some((id) => id === rule.SecurityGroupRuleId)), |
| 1083 | ).toBe(false); |
| 1084 | expect( |
| 1085 | replacementRules.find( |
| 1086 | (rule) => rule.SecurityGroupRuleId === replaced.ingress!.securityGroupRuleId, |
| 1087 | ), |
| 1088 | ).toMatchObject({ IsEgress: false, FromPort: 5433, ToPort: 5433 }); |
| 1089 | expect( |
| 1090 | replacementRules.find( |
| 1091 | (rule) => rule.SecurityGroupRuleId === replaced.egress!.securityGroupRuleId, |
| 1092 | ), |
| 1093 | ).toMatchObject({ IsEgress: true, FromPort: 123, ToPort: 123 }); |
| 1094 | expect(replacementRules.filter((rule) => rule.FromPort === 8443)).toEqual( |
| 1095 | expect.arrayContaining(updatedRules.filter((rule) => rule.FromPort === 8443)), |
| 1096 | ); |
| 1097 | |
| 1098 | expect( |
| 1099 | (yield* stack.plan(program(8443, "Updated standalone", true, false))).resources |
| 1100 | .CompositionGroup?.action, |
| 1101 | ).toBe("update"); |
| 1102 | yield* stack.deploy(program(8443, "Updated standalone", true, false)); |
| 1103 | const final = yield* readRules(groupId); |
| 1104 | expect(final).toHaveLength(2); |
| 1105 | expect(final.every((rule) => rule.FromPort === 8443)).toBe(true); |
| 1106 | |
| 1107 | // New standalone records must wait for the existing manager's inline update. |
| 1108 | const recreated = yield* stack.deploy(program(9443, "Recreated")); |
| 1109 | expect(recreated.ingress!.securityGroupRuleId).not.toBe( |
| 1110 | replaced.ingress!.securityGroupRuleId, |
| 1111 | ); |
| 1112 | expect(recreated.egress!.securityGroupRuleId).not.toBe( |
| 1113 | replaced.egress!.securityGroupRuleId, |
| 1114 | ); |
| 1115 | const recreatedRules = yield* readRules(groupId); |
| 1116 | expect(recreatedRules).toHaveLength(4); |
| 1117 | expect(recreatedRules.filter((rule) => rule.FromPort === 9443)).toHaveLength(2); |
| 1118 | expect( |
| 1119 | recreatedRules.find( |
| 1120 | (rule) => rule.SecurityGroupRuleId === recreated.ingress!.securityGroupRuleId, |
| 1121 | ), |
| 1122 | ).toMatchObject({ IsEgress: false, FromPort: 5432, Description: "Recreated" }); |
| 1123 | expect( |
| 1124 | recreatedRules.find( |
| 1125 | (rule) => rule.SecurityGroupRuleId === recreated.egress!.securityGroupRuleId, |
| 1126 | ), |
| 1127 | ).toMatchObject({ IsEgress: true, FromPort: 53, Description: "Recreated" }); |
| 1128 | const noop = yield* observeRuleRequests; |
| 1129 | yield* stack |
| 1130 | .deploy(program(9443, "Recreated")) |
| 1131 | .pipe(Effect.provideService(HttpClient.HttpClient, noop.client)); |
| 1132 | expect(noop.requests.length).toBeGreaterThan(0); |
| 1133 | expect(noop.requests.filter((request) => request.write)).toEqual([]); |
| 1134 | yield* stack.destroy(); |
| 1135 | yield* assertVpcGone(created.vpc.vpcId); |
| 1136 | }).pipe(logLevel), |
| 1137 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 1138 | ); |
| 1139 | } |
| 1140 | |
| 1141 | test.provider( |
| 1142 | "preserves standalone replacement classification during inline updates and new rule creation", |
| 1143 | (stack) => |
| 1144 | Effect.gen(function* () { |
| 1145 | yield* stack.destroy(); |
| 1146 | const phases = [ |
| 1147 | { |
| 1148 | inlinePort: 443, |
| 1149 | direction: "ingress", |
| 1150 | directionPort: 5000, |
| 1151 | directionCidr: "10.59.1.0/24", |
| 1152 | protocol: "tcp", |
| 1153 | identityPort: 5432, |
| 1154 | identityCidr: "10.59.3.0/24", |
| 1155 | }, |
| 1156 | { |
| 1157 | inlinePort: 8443, |
| 1158 | direction: "egress", |
| 1159 | directionPort: 5001, |
| 1160 | directionCidr: "10.59.2.0/24", |
| 1161 | protocol: "udp", |
| 1162 | identityPort: 5353, |
| 1163 | identityCidr: "10.59.4.0/24", |
| 1164 | }, |
| 1165 | { |
| 1166 | inlinePort: 9443, |
| 1167 | direction: "ingress", |
| 1168 | directionPort: 5002, |
| 1169 | directionCidr: "10.59.6.0/24", |
| 1170 | protocol: "tcp", |
| 1171 | identityPort: 8444, |
| 1172 | identityCidr: "10.59.7.0/24", |
| 1173 | }, |
| 1174 | ] as const; |
| 1175 | const program = (phase: 0 | 1 | 2) => |
| 1176 | Effect.gen(function* () { |
| 1177 | const desired = phases[phase]; |
| 1178 | const vpc = yield* Vpc("ReplacementOrderingVpc", { cidrBlock: "10.59.0.0/16" }); |
| 1179 | const inline = [ |
| 1180 | { |
| 1181 | ipProtocol: "tcp", |
| 1182 | fromPort: desired.inlinePort, |
| 1183 | toPort: desired.inlinePort, |
| 1184 | cidrIpv4: "10.59.0.0/16", |
| 1185 | }, |
| 1186 | ]; |
| 1187 | const group = yield* DefaultSecurityGroup("ReplacementOrderingGroup", { |
| 1188 | vpcId: vpc.vpcId, |
| 1189 | ingress: inline, |
| 1190 | egress: inline, |
| 1191 | }); |
| 1192 | const direction = yield* SecurityGroupRule("DirectionReplacement", { |
| 1193 | group: group, |
| 1194 | type: desired.direction, |
| 1195 | ipProtocol: "tcp", |
| 1196 | fromPort: desired.directionPort, |
| 1197 | toPort: desired.directionPort, |
| 1198 | cidrIpv4: desired.directionCidr, |
| 1199 | }); |
| 1200 | const identity = yield* SecurityGroupRule("IdentityReplacement", { |
| 1201 | group: group, |
| 1202 | type: "egress", |
| 1203 | ipProtocol: desired.protocol, |
| 1204 | fromPort: desired.identityPort, |
| 1205 | toPort: desired.identityPort, |
| 1206 | cidrIpv4: desired.identityCidr, |
| 1207 | }); |
| 1208 | const added = |
| 1209 | phase === 0 |
| 1210 | ? undefined |
| 1211 | : yield* SecurityGroupRule("AddedDuringReplacement", { |
| 1212 | group: group, |
| 1213 | type: "ingress", |
| 1214 | ipProtocol: "udp", |
| 1215 | fromPort: 1234, |
| 1216 | toPort: 1234, |
| 1217 | cidrIpv4: "10.59.5.0/24", |
| 1218 | }); |
| 1219 | return { vpc, group, direction, identity, added }; |
| 1220 | }); |
| 1221 | let previous = yield* stack.deploy(program(0)); |
| 1222 | const groupId = previous.group.groupId; |
| 1223 | const vpcId = previous.vpc.vpcId; |
| 1224 | const initial = yield* readRules(groupId); |
| 1225 | expect(initial).toHaveLength(4); |
| 1226 | expect( |
| 1227 | initial.find((rule) => rule.SecurityGroupRuleId === previous.direction.securityGroupRuleId), |
| 1228 | ).toMatchObject({ |
| 1229 | GroupId: groupId, |
| 1230 | IsEgress: false, |
| 1231 | IpProtocol: "tcp", |
| 1232 | FromPort: 5000, |
| 1233 | ToPort: 5000, |
| 1234 | CidrIpv4: "10.59.1.0/24", |
| 1235 | }); |
| 1236 | expect( |
| 1237 | initial.find((rule) => rule.SecurityGroupRuleId === previous.identity.securityGroupRuleId), |
| 1238 | ).toMatchObject({ |
| 1239 | GroupId: groupId, |
| 1240 | IsEgress: true, |
| 1241 | IpProtocol: "tcp", |
| 1242 | FromPort: 5432, |
| 1243 | ToPort: 5432, |
| 1244 | CidrIpv4: "10.59.3.0/24", |
| 1245 | }); |
| 1246 | const retiredIds: string[] = []; |
| 1247 | for (const phase of [1, 2] as const) { |
| 1248 | const desired = phases[phase]; |
| 1249 | const plan = yield* stack.plan(program(phase)); |
| 1250 | expect(plan.resources.ReplacementOrderingGroup?.action).toBe("update"); |
| 1251 | expect(plan.resources.DirectionReplacement?.action).toBe("replace"); |
| 1252 | expect(plan.resources.IdentityReplacement?.action).toBe("replace"); |
| 1253 | expect(plan.resources.AddedDuringReplacement?.action).toBe(phase === 1 ? "create" : "noop"); |
| 1254 | retiredIds.push( |
| 1255 | previous.direction.securityGroupRuleId, |
| 1256 | previous.identity.securityGroupRuleId, |
| 1257 | ); |
| 1258 | const deployed = yield* stack.deploy(program(phase)); |
| 1259 | expect(deployed.group.groupId).toBe(groupId); |
| 1260 | expect(deployed.vpc.vpcId).toBe(vpcId); |
| 1261 | expect(deployed.direction.securityGroupRuleId).not.toBe( |
| 1262 | previous.direction.securityGroupRuleId, |
| 1263 | ); |
| 1264 | expect(deployed.identity.securityGroupRuleId).not.toBe( |
| 1265 | previous.identity.securityGroupRuleId, |
| 1266 | ); |
| 1267 | if (previous.added) { |
| 1268 | expect(deployed.added!.securityGroupRuleId).toBe(previous.added.securityGroupRuleId); |
| 1269 | } |
| 1270 | const currentIds = [ |
| 1271 | deployed.direction.securityGroupRuleId, |
| 1272 | deployed.identity.securityGroupRuleId, |
| 1273 | deployed.added!.securityGroupRuleId, |
| 1274 | ]; |
| 1275 | const observed = yield* waitForRules( |
| 1276 | groupId, |
| 1277 | (rules) => |
| 1278 | rules.length === 5 && |
| 1279 | !rules.some((rule) => retiredIds.includes(rule.SecurityGroupRuleId!)) && |
| 1280 | currentIds.every((id) => rules.some((rule) => rule.SecurityGroupRuleId === id)), |
| 1281 | ); |
| 1282 | expect( |
| 1283 | observed.find( |
| 1284 | (rule) => rule.SecurityGroupRuleId === deployed.direction.securityGroupRuleId, |
| 1285 | ), |
| 1286 | ).toMatchObject({ |
| 1287 | GroupId: groupId, |
| 1288 | IsEgress: desired.direction === "egress", |
| 1289 | IpProtocol: "tcp", |
| 1290 | FromPort: desired.directionPort, |
| 1291 | ToPort: desired.directionPort, |
| 1292 | CidrIpv4: desired.directionCidr, |
| 1293 | }); |
| 1294 | expect( |
| 1295 | observed.find( |
| 1296 | (rule) => rule.SecurityGroupRuleId === deployed.identity.securityGroupRuleId, |
| 1297 | ), |
| 1298 | ).toMatchObject({ |
| 1299 | GroupId: groupId, |
| 1300 | IsEgress: true, |
| 1301 | IpProtocol: desired.protocol, |
| 1302 | FromPort: desired.identityPort, |
| 1303 | ToPort: desired.identityPort, |
| 1304 | CidrIpv4: desired.identityCidr, |
| 1305 | }); |
| 1306 | expect( |
| 1307 | observed.find((rule) => rule.SecurityGroupRuleId === deployed.added!.securityGroupRuleId), |
| 1308 | ).toMatchObject({ |
| 1309 | GroupId: groupId, |
| 1310 | IsEgress: false, |
| 1311 | IpProtocol: "udp", |
| 1312 | FromPort: 1234, |
| 1313 | ToPort: 1234, |
| 1314 | CidrIpv4: "10.59.5.0/24", |
| 1315 | }); |
| 1316 | const inline = observed.filter( |
| 1317 | (rule) => !currentIds.some((id) => id === rule.SecurityGroupRuleId), |
| 1318 | ); |
| 1319 | expect(inline).toHaveLength(2); |
| 1320 | for (const isEgress of [false, true]) { |
| 1321 | expect(inline.find((rule) => rule.IsEgress === isEgress)).toMatchObject({ |
| 1322 | GroupId: groupId, |
| 1323 | IpProtocol: "tcp", |
| 1324 | FromPort: desired.inlinePort, |
| 1325 | ToPort: desired.inlinePort, |
| 1326 | CidrIpv4: "10.59.0.0/16", |
| 1327 | }); |
| 1328 | } |
| 1329 | previous = deployed; |
| 1330 | } |
| 1331 | const settled = yield* readRules(groupId); |
| 1332 | const observer = yield* observeRuleRequests; |
| 1333 | yield* Effect.gen(function* () { |
| 1334 | const plan = yield* stack.plan(program(2)); |
| 1335 | for (const fqn of [ |
| 1336 | "ReplacementOrderingGroup", |
| 1337 | "DirectionReplacement", |
| 1338 | "IdentityReplacement", |
| 1339 | "AddedDuringReplacement", |
| 1340 | ]) { |
| 1341 | expect(plan.resources[fqn]?.action).toBe("noop"); |
| 1342 | } |
| 1343 | yield* stack.deploy(program(2)); |
| 1344 | }).pipe(Effect.provideService(HttpClient.HttpClient, observer.client)); |
| 1345 | expect(observer.requests.length).toBeGreaterThan(0); |
| 1346 | expect(observer.requests.filter((request) => request.write)).toEqual([]); |
| 1347 | const final = yield* readRules(groupId); |
| 1348 | expect(final).toHaveLength(5); |
| 1349 | expect(final).toEqual(expect.arrayContaining(settled)); |
| 1350 | yield* stack.destroy(); |
| 1351 | yield* assertVpcGone(vpcId); |
| 1352 | }).pipe(logLevel), |
| 1353 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 1354 | ); |
| 1355 | |
| 1356 | test.provider( |
| 1357 | "rejects copied persisted-rule tags and cross-stack ownership in both directions", |
| 1358 | (stack) => |
| 1359 | Effect.gen(function* () { |
| 1360 | const foreign = Core.scratchStack( |
| 1361 | { providers: AWS.providers(), stage: stack.stage }, |
| 1362 | `${stack.name}-foreign`, |
| 1363 | "test/AWS/EC2/DefaultSecurityGroup.test.ts", |
| 1364 | ); |
| 1365 | yield* foreign.destroy(); |
| 1366 | yield* stack.destroy(); |
| 1367 | yield* Effect.gen(function* () { |
| 1368 | const program = Effect.gen(function* () { |
| 1369 | const vpc = yield* Vpc("OwnershipVpc", { cidrBlock: "10.58.0.0/16" }); |
| 1370 | const group = yield* DefaultSecurityGroup("OwnershipGroup", { |
| 1371 | vpcId: vpc.vpcId, |
| 1372 | ingress: [], |
| 1373 | egress: [], |
| 1374 | }); |
| 1375 | yield* SecurityGroupRule("TargetIngress", { |
| 1376 | group: group, |
| 1377 | type: "ingress", |
| 1378 | ipProtocol: "tcp", |
| 1379 | fromPort: 443, |
| 1380 | toPort: 443, |
| 1381 | cidrIpv4: "10.58.0.0/16", |
| 1382 | }); |
| 1383 | yield* SecurityGroupRule("TargetEgress", { |
| 1384 | group: group, |
| 1385 | type: "egress", |
| 1386 | ipProtocol: "udp", |
| 1387 | fromPort: 53, |
| 1388 | toPort: 53, |
| 1389 | cidrIpv4: "10.58.0.0/16", |
| 1390 | }); |
| 1391 | const peer = yield* SecurityGroup("OwnershipPeer", { vpcId: vpc.vpcId, egress: [] }); |
| 1392 | const ingress = yield* SecurityGroupRule("OwnedIngress", { |
| 1393 | groupId: peer.groupId, |
| 1394 | type: "ingress", |
| 1395 | ipProtocol: "tcp", |
| 1396 | fromPort: 5432, |
| 1397 | toPort: 5432, |
| 1398 | cidrIpv4: "10.58.0.0/16", |
| 1399 | }); |
| 1400 | const egress = yield* SecurityGroupRule("OwnedEgress", { |
| 1401 | groupId: peer.groupId, |
| 1402 | type: "egress", |
| 1403 | ipProtocol: "udp", |
| 1404 | fromPort: 53, |
| 1405 | toPort: 53, |
| 1406 | cidrIpv4: "10.58.0.0/16", |
| 1407 | }); |
| 1408 | return { vpc, group, peer, ingress, egress }; |
| 1409 | }); |
| 1410 | const created = yield* stack.deploy(program); |
| 1411 | const groupId = created.group.groupId; |
| 1412 | const peerRules = yield* readRules(created.peer.groupId); |
| 1413 | const targetRules = yield* readRules(groupId); |
| 1414 | expect(peerRules).toHaveLength(2); |
| 1415 | expect(targetRules).toHaveLength(2); |
| 1416 | const copiedIds: string[] = []; |
| 1417 | for (const isEgress of [false, true]) { |
| 1418 | const source = targetRules.find((rule) => !!rule.IsEgress === isEgress)!; |
| 1419 | expect(source.Tags?.some((tag) => tag.Key === "alchemy::id")).toBe(true); |
| 1420 | const authorize = isEgress |
| 1421 | ? EC2.authorizeSecurityGroupEgress |
| 1422 | : EC2.authorizeSecurityGroupIngress; |
| 1423 | const copied = yield* authorize({ |
| 1424 | GroupId: groupId, |
| 1425 | IpPermissions: [ |
| 1426 | { IpProtocol: "tcp", FromPort: 22, ToPort: 22, IpRanges: [{ CidrIp: "0.0.0.0/0" }] }, |
| 1427 | ], |
| 1428 | TagSpecifications: [{ ResourceType: "security-group-rule", Tags: source.Tags }], |
| 1429 | }); |
| 1430 | copiedIds.push(copied.SecurityGroupRules![0]!.SecurityGroupRuleId!); |
| 1431 | } |
| 1432 | // The same logical IDs in another stack do not delegate physical rules. |
| 1433 | const elsewhere = yield* foreign.deploy( |
| 1434 | Effect.gen(function* () { |
| 1435 | const ingress = yield* SecurityGroupRule("OwnedIngress", { |
| 1436 | groupId, |
| 1437 | type: "ingress", |
| 1438 | ipProtocol: "tcp", |
| 1439 | fromPort: 5433, |
| 1440 | toPort: 5433, |
| 1441 | cidrIpv4: "10.58.0.0/16", |
| 1442 | }); |
| 1443 | const egress = yield* SecurityGroupRule("OwnedEgress", { |
| 1444 | groupId, |
| 1445 | type: "egress", |
| 1446 | ipProtocol: "udp", |
| 1447 | fromPort: 123, |
| 1448 | toPort: 123, |
| 1449 | cidrIpv4: "10.58.0.0/16", |
| 1450 | }); |
| 1451 | return { ingress, egress }; |
| 1452 | }), |
| 1453 | ); |
| 1454 | const foreignState = yield* Effect.gen(function* () { |
| 1455 | return yield* yield* State; |
| 1456 | }).pipe(Effect.provide(foreign.state)); |
| 1457 | for (const [fqn, attrs] of [ |
| 1458 | ["OwnedIngress", elsewhere.ingress], |
| 1459 | ["OwnedEgress", elsewhere.egress], |
| 1460 | ] as const) { |
| 1461 | const row = yield* foreignState.get({ stack: foreign.name, stage: foreign.stage, fqn }); |
| 1462 | if (!row || isActionState(row)) { |
| 1463 | return yield* Effect.fail(new Error("Expected a persisted cross-stack rule")); |
| 1464 | } |
| 1465 | expect(row.attr?.securityGroupRuleId).toBe(attrs.securityGroupRuleId); |
| 1466 | expect(row.attr?.groupId).toBe(groupId); |
| 1467 | } |
| 1468 | const unownedIds = [ |
| 1469 | ...copiedIds, |
| 1470 | elsewhere.ingress.securityGroupRuleId, |
| 1471 | elsewhere.egress.securityGroupRuleId, |
| 1472 | ]; |
| 1473 | yield* waitForRules( |
| 1474 | groupId, |
| 1475 | (rules) => |
| 1476 | rules.length === 6 && |
| 1477 | unownedIds.every((id) => rules.some((rule) => rule.SecurityGroupRuleId === id)), |
| 1478 | ); |
| 1479 | expect((yield* stack.plan(program)).resources.OwnershipGroup?.action).toBe("update"); |
| 1480 | const repaired = yield* stack.deploy(program); |
| 1481 | expect(yield* readRules(groupId)).toHaveLength(2); |
| 1482 | expect(yield* readRules(groupId)).toEqual(expect.arrayContaining(targetRules)); |
| 1483 | // The remaining rules are TargetIngress/TargetEgress's, not the group's. |
| 1484 | expect(repaired.group.ingressRules).toEqual([]); |
| 1485 | expect(repaired.group.egressRules).toEqual([]); |
| 1486 | expect(yield* readRules(created.peer.groupId)).toEqual(expect.arrayContaining(peerRules)); |
| 1487 | expect(yield* readRules(created.peer.groupId)).toHaveLength(2); |
| 1488 | const observer = yield* observeRuleRequests; |
| 1489 | yield* Effect.gen(function* () { |
| 1490 | expect((yield* stack.plan(program)).resources.OwnershipGroup?.action).toBe("noop"); |
| 1491 | yield* stack.deploy(program); |
| 1492 | }).pipe(Effect.provideService(HttpClient.HttpClient, observer.client)); |
| 1493 | expect(observer.requests.length).toBeGreaterThan(0); |
| 1494 | expect(observer.requests.filter((request) => request.write)).toEqual([]); |
| 1495 | yield* foreign.destroy(); |
| 1496 | yield* stack.destroy(); |
| 1497 | yield* assertVpcGone(created.vpc.vpcId); |
| 1498 | }).pipe(Effect.ensuring(foreign.destroy().pipe(Effect.ignore))); |
| 1499 | }).pipe(logLevel), |
| 1500 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 1501 | ); |
| 1502 | |
| 1503 | for (const kind of ["default", "custom"] as const) { |
| 1504 | test.provider( |
| 1505 | `normalizes standalone input forms and ignores unrelated ${kind} group attributes`, |
| 1506 | (stack) => |
| 1507 | Effect.gen(function* () { |
| 1508 | yield* stack.destroy(); |
| 1509 | const program = ( |
| 1510 | form: |
| 1511 | | "id" |
| 1512 | | "resource" |
| 1513 | | "attributes" |
| 1514 | | "flatMap-id" |
| 1515 | | "flatMap-resource" |
| 1516 | | "both" |
| 1517 | | "neither", |
| 1518 | description = "Standalone", |
| 1519 | inlineDescription = "Inline", |
| 1520 | move = false, |
| 1521 | ) => |
| 1522 | Effect.gen(function* () { |
| 1523 | const vpc = yield* Vpc("InputFormsVpc", { cidrBlock: "10.60.0.0/16" }); |
| 1524 | const props = { |
| 1525 | vpcId: vpc.vpcId, |
| 1526 | ingress: [ |
| 1527 | { |
| 1528 | ipProtocol: "tcp", |
| 1529 | fromPort: 443, |
| 1530 | toPort: 443, |
| 1531 | cidrIpv4: "10.60.0.0/16", |
| 1532 | description: inlineDescription, |
| 1533 | }, |
| 1534 | ], |
| 1535 | egress: [], |
| 1536 | }; |
| 1537 | const group = |
| 1538 | kind === "default" |
| 1539 | ? yield* DefaultSecurityGroup("InputFormsGroup", props) |
| 1540 | : yield* SecurityGroup("InputFormsGroup", props); |
| 1541 | const peer = yield* SecurityGroup("InputFormsPeer", { |
| 1542 | vpcId: vpc.vpcId, |
| 1543 | ingress: [], |
| 1544 | egress: [], |
| 1545 | }); |
| 1546 | const targetGroup = move ? peer : group; |
| 1547 | const target = |
| 1548 | form === "id" |
| 1549 | ? { groupId: targetGroup.groupId } |
| 1550 | : form === "resource" |
| 1551 | ? { group: targetGroup } |
| 1552 | : form === "attributes" |
| 1553 | ? { group: { groupId: targetGroup.groupId } } |
| 1554 | : form === "flatMap-id" |
| 1555 | ? { groupId: vpc.vpcId.pipe(Output.flatMap(() => targetGroup.groupId)) } |
| 1556 | : form === "flatMap-resource" |
| 1557 | ? { group: vpc.vpcId.pipe(Output.flatMap(() => Output.of(targetGroup))) } |
| 1558 | : form === "both" |
| 1559 | ? { group: targetGroup, groupId: targetGroup.groupId } |
| 1560 | : {}; |
| 1561 | const rule = yield* SecurityGroupRule("InputFormsRule", { |
| 1562 | ...target, |
| 1563 | type: "egress", |
| 1564 | ipProtocol: "tcp", |
| 1565 | fromPort: 5432, |
| 1566 | toPort: 5432, |
| 1567 | cidrIpv4: "10.60.0.0/16", |
| 1568 | description, |
| 1569 | tags: { purpose: description }, |
| 1570 | }); |
| 1571 | return { vpc, group, peer, rule }; |
| 1572 | }); |
| 1573 | const created = yield* stack.deploy(program("id")); |
| 1574 | const groupId = created.group.groupId; |
| 1575 | const ruleId = created.rule.securityGroupRuleId; |
| 1576 | const initial = yield* readRules(groupId); |
| 1577 | for (const form of [ |
| 1578 | "flatMap-id", |
| 1579 | "id", |
| 1580 | "flatMap-resource", |
| 1581 | "resource", |
| 1582 | "attributes", |
| 1583 | "id", |
| 1584 | ] as const) { |
| 1585 | const observer = yield* observeRuleRequests; |
| 1586 | yield* Effect.gen(function* () { |
| 1587 | const plan = yield* stack.plan(program(form)); |
| 1588 | expect(plan.resources.InputFormsGroup?.action).toBe("noop"); |
| 1589 | expect(plan.resources.InputFormsRule?.action).toBe("noop"); |
| 1590 | const deployed = yield* stack.deploy(program(form)); |
| 1591 | expect(deployed.rule.securityGroupRuleId).toBe(ruleId); |
| 1592 | expect(deployed.rule.groupId).toBe(groupId); |
| 1593 | }).pipe(Effect.provideService(HttpClient.HttpClient, observer.client)); |
| 1594 | expect(observer.requests.length).toBeGreaterThan(0); |
| 1595 | expect(observer.requests.filter((request) => request.write)).toEqual([]); |
| 1596 | const rules = yield* readRules(groupId); |
| 1597 | expect(rules).toEqual(expect.arrayContaining(initial)); |
| 1598 | expect(rules).toHaveLength(2); |
| 1599 | expect(rules.find((rule) => rule.SecurityGroupRuleId === ruleId)).toMatchObject({ |
| 1600 | GroupId: groupId, |
| 1601 | SecurityGroupRuleId: ruleId, |
| 1602 | IsEgress: true, |
| 1603 | FromPort: 5432, |
| 1604 | ToPort: 5432, |
| 1605 | }); |
| 1606 | } |
| 1607 | for (const form of ["both", "neither"] as const) { |
| 1608 | const observer = yield* observeRuleRequests; |
| 1609 | const error = yield* stack |
| 1610 | .plan(program(form)) |
| 1611 | .pipe(Effect.provideService(HttpClient.HttpClient, observer.client), Effect.flip); |
| 1612 | expect(error).toMatchObject({ |
| 1613 | _tag: "InvalidSecurityGroupRuleGroup", |
| 1614 | message: "Specify exactly one of group or groupId.", |
| 1615 | }); |
| 1616 | expect(observer.requests.filter((request) => request.write)).toEqual([]); |
| 1617 | } |
| 1618 | expect( |
| 1619 | (yield* stack.plan(program("resource", "Updated"))).resources.InputFormsRule?.action, |
| 1620 | ).toBe("update"); |
| 1621 | const updated = yield* stack.deploy(program("resource", "Updated")); |
| 1622 | expect(updated.rule.securityGroupRuleId).toBe(ruleId); |
| 1623 | const observed = (yield* readRules(groupId)).find( |
| 1624 | (rule) => rule.SecurityGroupRuleId === ruleId, |
| 1625 | )!; |
| 1626 | expect(observed.Description).toBe("Updated"); |
| 1627 | expect(observed.Tags).toEqual( |
| 1628 | expect.arrayContaining([{ Key: "purpose", Value: "Updated" }]), |
| 1629 | ); |
| 1630 | const observer = yield* observeRuleRequests; |
| 1631 | yield* Effect.gen(function* () { |
| 1632 | const plan = yield* stack.plan(program("resource", "Updated", "Changed inline")); |
| 1633 | expect(plan.resources.InputFormsGroup?.action).toBe("update"); |
| 1634 | expect(plan.resources.InputFormsRule?.action).toBe("noop"); |
| 1635 | const deployed = yield* stack.deploy(program("resource", "Updated", "Changed inline")); |
| 1636 | expect(deployed.rule.securityGroupRuleId).toBe(ruleId); |
| 1637 | }).pipe(Effect.provideService(HttpClient.HttpClient, observer.client)); |
| 1638 | const writes = observer.requests.filter((request) => request.write); |
| 1639 | expect(writes.length).toBeGreaterThan(0); |
| 1640 | expect(writes.every((request) => request.action === "ModifySecurityGroupRules")).toBe(true); |
| 1641 | expect(writes.flatMap((request) => request.ruleIds)).not.toContain(ruleId); |
| 1642 | expect( |
| 1643 | (yield* readRules(groupId)).find((rule) => rule.SecurityGroupRuleId === ruleId), |
| 1644 | ).toEqual(observed); |
| 1645 | for (const form of ["resource", "id"] as const) { |
| 1646 | const noop = yield* observeRuleRequests; |
| 1647 | yield* Effect.gen(function* () { |
| 1648 | expect( |
| 1649 | (yield* stack.plan(program(form, "Updated", "Changed inline"))).resources |
| 1650 | .InputFormsRule?.action, |
| 1651 | ).toBe("noop"); |
| 1652 | yield* stack.deploy(program(form, "Updated", "Changed inline")); |
| 1653 | }).pipe(Effect.provideService(HttpClient.HttpClient, noop.client)); |
| 1654 | expect(noop.requests.length).toBeGreaterThan(0); |
| 1655 | expect(noop.requests.filter((request) => request.write)).toEqual([]); |
| 1656 | } |
| 1657 | // A current declaration targeting another group no longer delegates its old ID. |
| 1658 | for (const form of ["flatMap-id", "flatMap-resource"] as const) { |
| 1659 | const movePlan = yield* stack.plan(program(form, "Updated", "Changed inline", true)); |
| 1660 | expect(movePlan.resources.InputFormsGroup?.action).toBe("update"); |
| 1661 | expect(movePlan.resources.InputFormsRule?.action).toBe("replace"); |
| 1662 | } |
| 1663 | const moving = program( |
| 1664 | kind === "default" ? "resource" : "id", |
| 1665 | "Updated", |
| 1666 | "Changed inline", |
| 1667 | true, |
| 1668 | ); |
| 1669 | const movePlan = yield* stack.plan(moving); |
| 1670 | expect(movePlan.resources.InputFormsGroup?.action).toBe("update"); |
| 1671 | expect(movePlan.resources.InputFormsRule?.action).toBe("replace"); |
| 1672 | const moved = yield* stack.deploy(moving); |
| 1673 | expect(moved.rule.groupId).toBe(moved.peer.groupId); |
| 1674 | expect(moved.rule.securityGroupRuleId).not.toBe(ruleId); |
| 1675 | const remaining = yield* readRules(groupId); |
| 1676 | expect(remaining).toHaveLength(1); |
| 1677 | expect(remaining[0]?.FromPort).toBe(443); |
| 1678 | expect(remaining[0]?.Description).toBe("Changed inline"); |
| 1679 | const peerRules = yield* readRules(moved.peer.groupId); |
| 1680 | expect(peerRules).toHaveLength(1); |
| 1681 | expect(peerRules[0]).toMatchObject({ |
| 1682 | SecurityGroupRuleId: moved.rule.securityGroupRuleId, |
| 1683 | IsEgress: true, |
| 1684 | FromPort: 5432, |
| 1685 | ToPort: 5432, |
| 1686 | }); |
| 1687 | yield* stack.destroy(); |
| 1688 | yield* assertVpcGone(created.vpc.vpcId); |
| 1689 | }).pipe(logLevel), |
| 1690 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 1691 | ); |
| 1692 | } |
| 1693 | |
| 1694 | for (const scenario of ["new destination VPC", "upstream VPC replacement"] as const) { |
| 1695 | test.provider( |
| 1696 | `replaces whole-group standalone rules during ${scenario}`, |
| 1697 | (stack) => |
| 1698 | Effect.gen(function* () { |
| 1699 | yield* stack.destroy(); |
| 1700 | const program = (move: boolean) => |
| 1701 | Effect.gen(function* () { |
| 1702 | const original = yield* Vpc("RuleIdentityVpc", { |
| 1703 | cidrBlock: |
| 1704 | move && scenario === "upstream VPC replacement" ? "10.62.0.0/16" : "10.61.0.0/16", |
| 1705 | }); |
| 1706 | const destination = |
| 1707 | move && scenario === "new destination VPC" |
| 1708 | ? yield* Vpc("RuleDestinationVpc", { cidrBlock: "10.62.0.0/16" }) |
| 1709 | : original; |
| 1710 | const group = yield* DefaultSecurityGroup("RuleIdentityGroup", { |
| 1711 | vpcId: destination.vpcId, |
| 1712 | ingress: [], |
| 1713 | egress: [], |
| 1714 | }); |
| 1715 | const ingress = yield* SecurityGroupRule("MovingIngress", { |
| 1716 | group, |
| 1717 | type: "ingress", |
| 1718 | ipProtocol: "tcp", |
| 1719 | fromPort: 443, |
| 1720 | toPort: 443, |
| 1721 | cidrIpv4: "10.61.0.0/16", |
| 1722 | }); |
| 1723 | const egress = yield* SecurityGroupRule("MovingEgress", { |
| 1724 | group, |
| 1725 | type: "egress", |
| 1726 | ipProtocol: "udp", |
| 1727 | fromPort: 53, |
| 1728 | toPort: 53, |
| 1729 | cidrIpv4: "10.61.0.0/16", |
| 1730 | }); |
| 1731 | return { original, destination, group, ingress, egress }; |
| 1732 | }); |
| 1733 | const created = yield* stack.deploy(program(false)); |
| 1734 | const plan = yield* stack.plan(program(true)); |
| 1735 | for (const fqn of ["RuleIdentityGroup", "MovingIngress", "MovingEgress"]) { |
| 1736 | expect(plan.resources[fqn]?.action).toBe("replace"); |
| 1737 | } |
| 1738 | const moved = yield* stack.deploy(program(true)); |
| 1739 | expect(moved.group.groupId).not.toBe(created.group.groupId); |
| 1740 | expect(moved.ingress.securityGroupRuleId).not.toBe(created.ingress.securityGroupRuleId); |
| 1741 | expect(moved.egress.securityGroupRuleId).not.toBe(created.egress.securityGroupRuleId); |
| 1742 | expect(moved.ingress.groupId).toBe(moved.group.groupId); |
| 1743 | expect(moved.egress.groupId).toBe(moved.group.groupId); |
| 1744 | yield* expectRules( |
| 1745 | moved.group.groupId, |
| 1746 | [ |
| 1747 | { |
| 1748 | SecurityGroupRuleId: moved.ingress.securityGroupRuleId, |
| 1749 | IsEgress: false, |
| 1750 | IpProtocol: "tcp", |
| 1751 | FromPort: 443, |
| 1752 | ToPort: 443, |
| 1753 | CidrIpv4: "10.61.0.0/16", |
| 1754 | }, |
| 1755 | ], |
| 1756 | [ |
| 1757 | { |
| 1758 | SecurityGroupRuleId: moved.egress.securityGroupRuleId, |
| 1759 | IsEgress: true, |
| 1760 | IpProtocol: "udp", |
| 1761 | FromPort: 53, |
| 1762 | ToPort: 53, |
| 1763 | CidrIpv4: "10.61.0.0/16", |
| 1764 | }, |
| 1765 | ], |
| 1766 | ); |
| 1767 | if (scenario === "new destination VPC") { |
| 1768 | yield* expectRules(created.group.groupId, [], []); |
| 1769 | } else { |
| 1770 | yield* assertVpcGone(created.original.vpcId); |
| 1771 | } |
| 1772 | const noop = yield* stack.plan(program(true)); |
| 1773 | for (const fqn of ["RuleIdentityGroup", "MovingIngress", "MovingEgress"]) { |
| 1774 | expect(noop.resources[fqn]?.action).toBe("noop"); |
| 1775 | } |
| 1776 | yield* stack.destroy(); |
| 1777 | yield* assertVpcGone(created.original.vpcId); |
| 1778 | yield* assertVpcGone(moved.destination.vpcId); |
| 1779 | }).pipe(logLevel), |
| 1780 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 1781 | ); |
| 1782 | } |
| 1783 | |
| 1784 | const observeRuleRequests = Effect.gen(function* () { |
| 1785 | const client = yield* HttpClient.HttpClient; |
| 1786 | const requests: Array<{ action: string; write: boolean; ruleIds: string[] }> = []; |
| 1787 | return { |
| 1788 | requests, |
| 1789 | client: client.pipe( |
| 1790 | HttpClient.tapRequest((request) => |
| 1791 | Effect.sync(() => { |
| 1792 | if (request.body._tag !== "Uint8Array") return; |
| 1793 | const parameters = new URLSearchParams(new TextDecoder().decode(request.body.body)); |
| 1794 | const action = parameters.get("Action"); |
| 1795 | if ( |
| 1796 | !action || |
| 1797 | (!action.includes("SecurityGroup") && |
| 1798 | action !== "CreateTags" && |
| 1799 | action !== "DeleteTags") |
| 1800 | ) |
| 1801 | return; |
| 1802 | requests.push({ |
| 1803 | action, |
| 1804 | write: !action.startsWith("Describe"), |
| 1805 | ruleIds: [...parameters.entries()] |
| 1806 | .filter( |
| 1807 | ([key]) => |
| 1808 | key.endsWith(".SecurityGroupRuleId") || key.startsWith("SecurityGroupRuleId."), |
| 1809 | ) |
| 1810 | .map(([, value]) => value), |
| 1811 | }); |
| 1812 | }), |
| 1813 | ), |
| 1814 | ), |
| 1815 | }; |
| 1816 | }); |
| 1817 | |
| 1818 | const waitForRules = Effect.fn(function* ( |
| 1819 | groupId: string, |
| 1820 | matches: (rules: EC2.SecurityGroupRule[]) => boolean, |
| 1821 | ) { |
| 1822 | const rules = yield* readRules(groupId).pipe( |
| 1823 | Effect.repeat({ until: matches, schedule: Schedule.spaced("1 second"), times: 8 }), |
| 1824 | ); |
| 1825 | expect(matches(rules)).toBe(true); |
| 1826 | return rules; |
| 1827 | }); |
| 1828 | |
| 1829 | const findDefaultGroup = Effect.fn(function* (vpcId: string) { |
| 1830 | const group = yield* EC2.describeSecurityGroups({ |
| 1831 | Filters: [ |
| 1832 | { Name: "vpc-id", Values: [vpcId] }, |
| 1833 | { Name: "group-name", Values: ["default"] }, |
| 1834 | ], |
| 1835 | }).pipe( |
| 1836 | Effect.map((result) => result.SecurityGroups?.[0]), |
| 1837 | Effect.repeat({ |
| 1838 | until: (group) => !!group?.GroupId, |
| 1839 | schedule: Schedule.spaced("1 second"), |
| 1840 | times: 8, |
| 1841 | }), |
| 1842 | ); |
| 1843 | if (!group?.GroupId) { |
| 1844 | return yield* Effect.fail(new Error(`Default group for ${vpcId} was not found`)); |
| 1845 | } |
| 1846 | return group; |
| 1847 | }); |
| 1848 | |
| 1849 | const readRules = (groupId: string) => |
| 1850 | EC2.describeSecurityGroupRules.items({ Filters: [{ Name: "group-id", Values: [groupId] }] }).pipe( |
| 1851 | Stream.runCollect, |
| 1852 | Effect.map((rules) => Array.from(rules)), |
| 1853 | ); |
| 1854 | |
| 1855 | const expectRules = Effect.fn(function* ( |
| 1856 | groupId: string, |
| 1857 | ingress: Partial<EC2.SecurityGroupRule>[], |
| 1858 | egress: Partial<EC2.SecurityGroupRule>[], |
| 1859 | ) { |
| 1860 | const rules = yield* readRules(groupId); |
| 1861 | expect(rules.filter((rule) => !rule.IsEgress)).toEqual( |
| 1862 | ingress.map((rule) => expect.objectContaining(rule)), |
| 1863 | ); |
| 1864 | expect(rules.filter((rule) => rule.IsEgress)).toEqual( |
| 1865 | egress.map((rule) => expect.objectContaining(rule)), |
| 1866 | ); |
| 1867 | }); |