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 { SecurityGroup, SecurityGroupRule, Vpc } from "@alchemy.run/aws/EC2"; |
| 3 | import type { SecurityGroupProps, SecurityGroupRuleData } from "@alchemy.run/aws/EC2/SecurityGroup"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 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 | const describeRules = (groupId: string) => |
| 18 | EC2.describeSecurityGroupRules({ |
| 19 | Filters: [{ Name: "group-id", Values: [groupId] }], |
| 20 | }).pipe(Effect.map((result) => result.SecurityGroupRules ?? [])); |
| 21 | |
| 22 | test.provider( |
| 23 | "list enumerates the deployed Security Group", |
| 24 | (stack) => |
| 25 | Effect.gen(function* () { |
| 26 | yield* stack.destroy(); |
| 27 | |
| 28 | const { vpc, sg } = yield* stack.deploy( |
| 29 | Effect.gen(function* () { |
| 30 | const vpc = yield* Vpc("ListSgVpc", { |
| 31 | cidrBlock: "10.0.0.0/16", |
| 32 | }); |
| 33 | const sg = yield* SecurityGroup("ListSg", { |
| 34 | vpcId: vpc.vpcId, |
| 35 | }); |
| 36 | return { vpc, sg }; |
| 37 | }), |
| 38 | ); |
| 39 | |
| 40 | const provider = yield* Provider.findProvider(SecurityGroup); |
| 41 | const all = yield* provider.list(); |
| 42 | |
| 43 | expect(all.some((x) => x.groupId === sg.groupId)).toBe(true); |
| 44 | |
| 45 | yield* stack.destroy(); |
| 46 | |
| 47 | yield* assertSecurityGroupGone(sg.groupId); |
| 48 | yield* assertVpcGone(vpc.vpcId); |
| 49 | }).pipe(logLevel), |
| 50 | { tags: ["provider:aws", "provider:aws:ec2", "live"] }, |
| 51 | ); |
| 52 | |
| 53 | test.provider( |
| 54 | "reconciles inline rules without taking over standalone rules", |
| 55 | (stack) => |
| 56 | Effect.gen(function* () { |
| 57 | yield* stack.destroy(); |
| 58 | |
| 59 | const makeStack = (inlinePort: number, standalone = true) => |
| 60 | Effect.gen(function* () { |
| 61 | const vpc = yield* Vpc("MixedRulesVpc", { |
| 62 | cidrBlock: "10.0.0.0/16", |
| 63 | }); |
| 64 | const sg = yield* SecurityGroup("MixedRulesSg", { |
| 65 | vpcId: vpc.vpcId, |
| 66 | ingress: [ |
| 67 | { |
| 68 | ipProtocol: "6", |
| 69 | fromPort: inlinePort, |
| 70 | toPort: inlinePort, |
| 71 | cidrIpv4: "10.0.0.7/16", |
| 72 | }, |
| 73 | { |
| 74 | ipProtocol: "58", |
| 75 | fromPort: -1, |
| 76 | toPort: -1, |
| 77 | cidrIpv6: "2001:0db8:0:0:0:0:0:5/64", |
| 78 | }, |
| 79 | ], |
| 80 | egress: [ |
| 81 | { |
| 82 | ipProtocol: "6", |
| 83 | fromPort: inlinePort, |
| 84 | toPort: inlinePort, |
| 85 | cidrIpv4: "10.0.0.7/16", |
| 86 | }, |
| 87 | ], |
| 88 | }); |
| 89 | const ingressRule = standalone |
| 90 | ? yield* SecurityGroupRule("StandaloneIngress", { |
| 91 | groupId: sg.groupId, |
| 92 | type: "ingress", |
| 93 | ipProtocol: "tcp", |
| 94 | fromPort: 5432, |
| 95 | toPort: 5432, |
| 96 | cidrIpv4: "10.0.0.0/16", |
| 97 | }) |
| 98 | : undefined; |
| 99 | const egressRule = standalone |
| 100 | ? yield* SecurityGroupRule("StandaloneEgress", { |
| 101 | groupId: sg.groupId, |
| 102 | type: "egress", |
| 103 | ipProtocol: "udp", |
| 104 | fromPort: 53, |
| 105 | toPort: 53, |
| 106 | cidrIpv4: "10.0.0.0/16", |
| 107 | }) |
| 108 | : undefined; |
| 109 | return { egressRule, ingressRule, sg, vpc }; |
| 110 | }); |
| 111 | |
| 112 | const deployed = yield* stack.deploy(makeStack(443)); |
| 113 | const declared = (yield* describeRules(deployed.sg.groupId)).find( |
| 114 | (rule) => rule.SecurityGroupRuleId === deployed.ingressRule?.securityGroupRuleId, |
| 115 | )!; |
| 116 | // Even copied ownership tags do not delegate an undeclared physical rule. |
| 117 | const rogue = yield* EC2.authorizeSecurityGroupIngress({ |
| 118 | GroupId: deployed.sg.groupId, |
| 119 | IpPermissions: [ |
| 120 | { |
| 121 | IpProtocol: "tcp", |
| 122 | FromPort: 22, |
| 123 | ToPort: 22, |
| 124 | IpRanges: [{ CidrIp: "0.0.0.0/0" }], |
| 125 | }, |
| 126 | ], |
| 127 | TagSpecifications: [{ ResourceType: "security-group-rule", Tags: declared.Tags }], |
| 128 | }); |
| 129 | const rogueId = rogue.SecurityGroupRules![0]!.SecurityGroupRuleId!; |
| 130 | const stale = yield* EC2.authorizeSecurityGroupEgress({ |
| 131 | GroupId: deployed.sg.groupId, |
| 132 | IpPermissions: [ |
| 133 | { |
| 134 | IpProtocol: "tcp", |
| 135 | FromPort: 25, |
| 136 | ToPort: 25, |
| 137 | IpRanges: [{ CidrIp: "0.0.0.0/0" }], |
| 138 | }, |
| 139 | ], |
| 140 | TagSpecifications: [ |
| 141 | { |
| 142 | ResourceType: "security-group-rule", |
| 143 | Tags: [{ Key: "alchemy::id", Value: "RemovedRule" }], |
| 144 | }, |
| 145 | ], |
| 146 | }); |
| 147 | const staleId = stale.SecurityGroupRules![0]!.SecurityGroupRuleId!; |
| 148 | expect( |
| 149 | (yield* describeRules(deployed.sg.groupId).pipe( |
| 150 | Effect.repeat({ |
| 151 | until: (rules) => |
| 152 | rules.some((rule) => rule.SecurityGroupRuleId === rogueId) && |
| 153 | rules.some((rule) => rule.SecurityGroupRuleId === staleId), |
| 154 | schedule: Schedule.spaced("1 second"), |
| 155 | times: 8, |
| 156 | }), |
| 157 | )).some((rule) => rule.SecurityGroupRuleId === rogueId), |
| 158 | ).toBe(true); |
| 159 | expect((yield* stack.plan(makeStack(443))).resources.MixedRulesSg?.action).toBe("update"); |
| 160 | yield* stack.deploy(makeStack(443)); |
| 161 | const repaired = yield* describeRules(deployed.sg.groupId); |
| 162 | expect(repaired.some((rule) => [rogueId, staleId].includes(rule.SecurityGroupRuleId!))).toBe( |
| 163 | false, |
| 164 | ); |
| 165 | expect( |
| 166 | repaired.some( |
| 167 | (rule) => rule.SecurityGroupRuleId === deployed.ingressRule?.securityGroupRuleId, |
| 168 | ), |
| 169 | ).toBe(true); |
| 170 | expect( |
| 171 | repaired.some( |
| 172 | (rule) => rule.SecurityGroupRuleId === deployed.egressRule?.securityGroupRuleId, |
| 173 | ), |
| 174 | ).toBe(true); |
| 175 | expect((yield* stack.plan(makeStack(443))).resources.MixedRulesSg?.action).toBe("noop"); |
| 176 | const updated = yield* stack.deploy(makeStack(8443)); |
| 177 | |
| 178 | expect(updated.ingressRule?.securityGroupRuleId).toEqual( |
| 179 | deployed.ingressRule?.securityGroupRuleId, |
| 180 | ); |
| 181 | expect(updated.egressRule?.securityGroupRuleId).toEqual( |
| 182 | deployed.egressRule?.securityGroupRuleId, |
| 183 | ); |
| 184 | |
| 185 | const updatedRules = yield* describeRules(updated.sg.groupId); |
| 186 | expect( |
| 187 | updatedRules.some( |
| 188 | (rule) => |
| 189 | rule.SecurityGroupRuleId === updated.ingressRule?.securityGroupRuleId && |
| 190 | rule.Tags?.some((tag) => tag.Key === "alchemy::id"), |
| 191 | ), |
| 192 | ).toBe(true); |
| 193 | expect( |
| 194 | updatedRules.some( |
| 195 | (rule) => |
| 196 | rule.SecurityGroupRuleId === updated.egressRule?.securityGroupRuleId && |
| 197 | rule.Tags?.some((tag) => tag.Key === "alchemy::id"), |
| 198 | ), |
| 199 | ).toBe(true); |
| 200 | expect( |
| 201 | updatedRules.filter((rule) => rule.FromPort === 8443 && rule.ToPort === 8443), |
| 202 | ).toHaveLength(2); |
| 203 | expect(updatedRules.some((rule) => rule.FromPort === 443)).toBe(false); |
| 204 | |
| 205 | yield* stack.deploy(makeStack(8443, false)); |
| 206 | const finalRules = yield* describeRules(updated.sg.groupId); |
| 207 | expect( |
| 208 | finalRules.some( |
| 209 | (rule) => |
| 210 | rule.SecurityGroupRuleId === updated.ingressRule?.securityGroupRuleId || |
| 211 | rule.SecurityGroupRuleId === updated.egressRule?.securityGroupRuleId, |
| 212 | ), |
| 213 | ).toBe(false); |
| 214 | expect( |
| 215 | finalRules.filter((rule) => rule.FromPort === 8443 && rule.ToPort === 8443), |
| 216 | ).toHaveLength(2); |
| 217 | |
| 218 | yield* stack.destroy(); |
| 219 | yield* assertSecurityGroupGone(updated.sg.groupId); |
| 220 | yield* assertVpcGone(updated.vpc.vpcId); |
| 221 | }).pipe(logLevel), |
| 222 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 223 | ); |
| 224 | |
| 225 | test.provider( |
| 226 | "creates dual-stack permissions and keeps unchanged rules stable", |
| 227 | (stack) => |
| 228 | Effect.gen(function* () { |
| 229 | yield* stack.destroy(); |
| 230 | const program = Effect.gen(function* () { |
| 231 | const vpc = yield* Vpc("DualStackVpc", { cidrBlock: "10.0.0.0/16" }); |
| 232 | const permission = { |
| 233 | ipProtocol: "6", |
| 234 | fromPort: 443, |
| 235 | toPort: 443, |
| 236 | cidrIpv4: "10.0.0.7/16", |
| 237 | cidrIpv6: "2001:0db8:0:0:0:0:0:5/64", |
| 238 | description: "dual-stack HTTPS", |
| 239 | }; |
| 240 | const sg = yield* SecurityGroup("DualStackSg", { |
| 241 | vpcId: vpc.vpcId, |
| 242 | ingress: [permission, { ipProtocol: "58", cidrIpv6: "2001:db8::/64" }], |
| 243 | egress: [permission], |
| 244 | }); |
| 245 | return { sg, vpc }; |
| 246 | }); |
| 247 | const { sg, vpc } = yield* stack.deploy(program); |
| 248 | const rules = yield* describeRules(sg.groupId); |
| 249 | expect(rules).toHaveLength(5); |
| 250 | for (const isEgress of [false, true]) { |
| 251 | const https = rules.filter( |
| 252 | (rule) => rule.IsEgress === isEgress && rule.IpProtocol === "tcp", |
| 253 | ); |
| 254 | expect(https).toHaveLength(2); |
| 255 | expect(https.some((rule) => rule.CidrIpv4 === "10.0.0.0/16")).toBe(true); |
| 256 | expect(https.some((rule) => rule.CidrIpv6 === "2001:db8::/64")).toBe(true); |
| 257 | } |
| 258 | expect((yield* stack.plan(program)).resources.DualStackSg?.action).toBe("noop"); |
| 259 | yield* stack.deploy(program); |
| 260 | expect( |
| 261 | (yield* describeRules(sg.groupId)).map((rule) => rule.SecurityGroupRuleId).sort(), |
| 262 | ).toEqual(rules.map((rule) => rule.SecurityGroupRuleId).sort()); |
| 263 | yield* stack.destroy(); |
| 264 | yield* assertSecurityGroupGone(sg.groupId); |
| 265 | yield* assertVpcGone(vpc.vpcId); |
| 266 | }), |
| 267 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 268 | ); |
| 269 | |
| 270 | const securityGroupStack = (props: { egress?: [] }) => |
| 271 | Effect.gen(function* () { |
| 272 | const vpc = yield* Vpc("EmptyEgressVpc", { |
| 273 | cidrBlock: "10.0.0.0/16", |
| 274 | }); |
| 275 | const sg = yield* SecurityGroup("EmptyEgressSg", { |
| 276 | vpcId: vpc.vpcId, |
| 277 | ...props, |
| 278 | }); |
| 279 | return { sg, vpc }; |
| 280 | }); |
| 281 | |
| 282 | const describeEgress = (groupId: string) => |
| 283 | EC2.describeSecurityGroupRules({ |
| 284 | Filters: [{ Name: "group-id", Values: [groupId] }], |
| 285 | }).pipe( |
| 286 | Effect.map((result) => (result.SecurityGroupRules ?? []).filter((rule) => rule.IsEgress)), |
| 287 | ); |
| 288 | |
| 289 | test.provider( |
| 290 | "distinguishes explicit empty egress from omitted egress", |
| 291 | (stack) => |
| 292 | Effect.gen(function* () { |
| 293 | yield* stack.destroy(); |
| 294 | |
| 295 | // Creating with explicitly empty egress removes AWS's default allow-all rule. |
| 296 | const noOutbound = yield* stack.deploy(securityGroupStack({ egress: [] })); |
| 297 | expect(yield* describeEgress(noOutbound.sg.groupId)).toEqual([]); |
| 298 | |
| 299 | // Redeploying the same configuration must keep outbound access disabled. |
| 300 | const stillNoOutbound = yield* stack.deploy(securityGroupStack({ egress: [] })); |
| 301 | expect(yield* describeEgress(stillNoOutbound.sg.groupId)).toEqual([]); |
| 302 | |
| 303 | // Omitting the egress property restores default allow-all IPv4 access. |
| 304 | const defaultOutbound = yield* stack.deploy(securityGroupStack({})); |
| 305 | const defaultEgress = yield* describeEgress(defaultOutbound.sg.groupId); |
| 306 | expect(defaultEgress).toHaveLength(1); |
| 307 | expect(defaultEgress[0]?.IpProtocol).toEqual("-1"); |
| 308 | expect(defaultEgress[0]?.CidrIpv4).toEqual("0.0.0.0/0"); |
| 309 | |
| 310 | // Switching back to explicitly empty egress removes allow-all again. |
| 311 | const outboundDisabledAgain = yield* stack.deploy(securityGroupStack({ egress: [] })); |
| 312 | expect(yield* describeEgress(outboundDisabledAgain.sg.groupId)).toEqual([]); |
| 313 | |
| 314 | yield* stack.destroy(); |
| 315 | yield* assertSecurityGroupGone(outboundDisabledAgain.sg.groupId); |
| 316 | yield* assertVpcGone(outboundDisabledAgain.vpc.vpcId); |
| 317 | }).pipe(logLevel), |
| 318 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 319 | ); |
| 320 | |
| 321 | test.provider( |
| 322 | "creates and updates explicitly empty egress", |
| 323 | (stack) => |
| 324 | Effect.gen(function* () { |
| 325 | yield* stack.destroy(); |
| 326 | const deploy = (egress: SecurityGroupRuleData[]) => |
| 327 | stack.deploy( |
| 328 | Effect.gen(function* () { |
| 329 | const vpc = yield* Vpc("EmptyEgressVpc", { |
| 330 | cidrBlock: "10.0.0.0/16", |
| 331 | }); |
| 332 | const sg = yield* SecurityGroup("EmptyEgressSg", { |
| 333 | vpcId: vpc.vpcId, |
| 334 | egress, |
| 335 | }); |
| 336 | return { vpc, sg }; |
| 337 | }), |
| 338 | ); |
| 339 | const created = yield* deploy([]); |
| 340 | const initial = yield* EC2.describeSecurityGroupRules({ |
| 341 | Filters: [{ Name: "group-id", Values: [created.sg.groupId] }], |
| 342 | }); |
| 343 | expect(initial.SecurityGroupRules).toEqual([]); |
| 344 | const configured = yield* deploy([ |
| 345 | { |
| 346 | ipProtocol: "tcp", |
| 347 | fromPort: 443, |
| 348 | toPort: 443, |
| 349 | cidrIpv4: "0.0.0.0/0", |
| 350 | }, |
| 351 | ]); |
| 352 | expect(configured.sg.groupId).toBe(created.sg.groupId); |
| 353 | const configuredRules = yield* EC2.describeSecurityGroupRules({ |
| 354 | Filters: [{ Name: "group-id", Values: [created.sg.groupId] }], |
| 355 | }); |
| 356 | expect(configuredRules.SecurityGroupRules).toEqual([ |
| 357 | expect.objectContaining({ |
| 358 | IsEgress: true, |
| 359 | IpProtocol: "tcp", |
| 360 | FromPort: 443, |
| 361 | ToPort: 443, |
| 362 | CidrIpv4: "0.0.0.0/0", |
| 363 | }), |
| 364 | ]); |
| 365 | const updated = yield* deploy([]); |
| 366 | expect(updated.sg.groupId).toBe(created.sg.groupId); |
| 367 | const final = yield* EC2.describeSecurityGroupRules({ |
| 368 | Filters: [{ Name: "group-id", Values: [created.sg.groupId] }], |
| 369 | }); |
| 370 | expect(final.SecurityGroupRules).toEqual([]); |
| 371 | yield* stack.destroy(); |
| 372 | yield* assertSecurityGroupGone(created.sg.groupId); |
| 373 | yield* assertVpcGone(created.vpc.vpcId); |
| 374 | }).pipe(logLevel), |
| 375 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120000 }, |
| 376 | ); |
| 377 | |
| 378 | test.provider( |
| 379 | "repairs inline drift without replacing unchanged rules", |
| 380 | (stack) => |
| 381 | Effect.gen(function* () { |
| 382 | yield* stack.destroy(); |
| 383 | const deploy = () => |
| 384 | stack.deploy( |
| 385 | Effect.gen(function* () { |
| 386 | const vpc = yield* Vpc("InlineDriftVpc", { |
| 387 | cidrBlock: "10.0.0.0/16", |
| 388 | }); |
| 389 | const sg = yield* SecurityGroup("InlineDriftSg", { |
| 390 | vpcId: vpc.vpcId, |
| 391 | ingress: [ |
| 392 | { |
| 393 | ipProtocol: "tcp", |
| 394 | fromPort: 443, |
| 395 | toPort: 443, |
| 396 | cidrIpv4: "10.0.0.0/16", |
| 397 | }, |
| 398 | ], |
| 399 | egress: [], |
| 400 | }); |
| 401 | return { vpc, sg }; |
| 402 | }), |
| 403 | ); |
| 404 | const created = yield* deploy(); |
| 405 | const originalRuleId = created.sg.ingressRules?.[0]?.securityGroupRuleId; |
| 406 | yield* EC2.authorizeSecurityGroupIngress({ |
| 407 | GroupId: created.sg.groupId, |
| 408 | IpPermissions: [ |
| 409 | { |
| 410 | IpProtocol: "tcp", |
| 411 | FromPort: 22, |
| 412 | ToPort: 22, |
| 413 | IpRanges: [{ CidrIp: "10.0.0.0/16" }], |
| 414 | }, |
| 415 | ], |
| 416 | }); |
| 417 | const updated = yield* deploy(); |
| 418 | expect(updated.sg.groupId).toBe(created.sg.groupId); |
| 419 | const rules = yield* EC2.describeSecurityGroupRules({ |
| 420 | Filters: [{ Name: "group-id", Values: [created.sg.groupId] }], |
| 421 | }); |
| 422 | expect(rules.SecurityGroupRules).toEqual([ |
| 423 | expect.objectContaining({ |
| 424 | SecurityGroupRuleId: originalRuleId, |
| 425 | FromPort: 443, |
| 426 | }), |
| 427 | ]); |
| 428 | yield* stack.destroy(); |
| 429 | yield* assertSecurityGroupGone(created.sg.groupId); |
| 430 | yield* assertVpcGone(created.vpc.vpcId); |
| 431 | }).pipe(logLevel), |
| 432 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120000 }, |
| 433 | ); |
| 434 | |
| 435 | test.provider( |
| 436 | "omitted directions restore defaults while empty directions disable traffic", |
| 437 | (stack) => |
| 438 | Effect.gen(function* () { |
| 439 | yield* stack.destroy(); |
| 440 | const makeStack = ( |
| 441 | directions: Pick<SecurityGroupProps, "ingress" | "egress">, |
| 442 | label: string, |
| 443 | ) => |
| 444 | Effect.gen(function* () { |
| 445 | const vpc = yield* Vpc("DirectionsVpc", { cidrBlock: "10.0.0.0/16" }); |
| 446 | const sg = yield* SecurityGroup("DirectionsSg", { |
| 447 | vpcId: vpc.vpcId, |
| 448 | ...directions, |
| 449 | tags: { Label: label }, |
| 450 | }); |
| 451 | return { vpc, sg }; |
| 452 | }); |
| 453 | const rule: SecurityGroupRuleData = { |
| 454 | ipProtocol: "tcp", |
| 455 | fromPort: 443, |
| 456 | toPort: 443, |
| 457 | cidrIpv4: "10.0.0.0/16", |
| 458 | }; |
| 459 | const created = yield* stack.deploy( |
| 460 | makeStack({ ingress: [rule], egress: [rule] }, "managed"), |
| 461 | ); |
| 462 | const readRules = () => |
| 463 | EC2.describeSecurityGroupRules({ |
| 464 | Filters: [{ Name: "group-id", Values: [created.sg.groupId] }], |
| 465 | }); |
| 466 | const initial = yield* readRules(); |
| 467 | expect(initial.SecurityGroupRules).toHaveLength(2); |
| 468 | const defaults = yield* stack.deploy(makeStack({}, "defaults")); |
| 469 | expect(defaults.sg.groupId).toBe(created.sg.groupId); |
| 470 | const defaultRules = yield* readRules(); |
| 471 | expect(defaultRules.SecurityGroupRules).toEqual([ |
| 472 | expect.objectContaining({ |
| 473 | IsEgress: true, |
| 474 | IpProtocol: "-1", |
| 475 | CidrIpv4: "0.0.0.0/0", |
| 476 | }), |
| 477 | ]); |
| 478 | |
| 479 | const ingressEmpty = yield* stack.deploy(makeStack({ ingress: [] }, "ingress-empty")); |
| 480 | expect(ingressEmpty.sg.groupId).toBe(created.sg.groupId); |
| 481 | const egressOnly = yield* readRules(); |
| 482 | expect(egressOnly.SecurityGroupRules).toEqual(defaultRules.SecurityGroupRules); |
| 483 | |
| 484 | const emptyStack = makeStack({ egress: [] }, "egress-empty"); |
| 485 | const empty = yield* stack.deploy(emptyStack); |
| 486 | expect(empty.sg.groupId).toBe(created.sg.groupId); |
| 487 | expect((yield* readRules()).SecurityGroupRules).toEqual([]); |
| 488 | const plan = yield* stack.plan(emptyStack); |
| 489 | expect(plan.resources.DirectionsSg).toMatchObject({ action: "noop" }); |
| 490 | yield* stack.deploy(emptyStack); |
| 491 | expect((yield* readRules()).SecurityGroupRules).toEqual([]); |
| 492 | yield* stack.destroy(); |
| 493 | yield* assertSecurityGroupGone(created.sg.groupId); |
| 494 | yield* assertVpcGone(created.vpc.vpcId); |
| 495 | }).pipe(logLevel), |
| 496 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120000 }, |
| 497 | ); |
| 498 | |
| 499 | test.provider( |
| 500 | "updates and clears inline descriptions without replacing ingress or egress rule IDs", |
| 501 | (stack) => |
| 502 | Effect.gen(function* () { |
| 503 | yield* stack.destroy(); |
| 504 | const makeStack = (description: string | undefined) => |
| 505 | Effect.gen(function* () { |
| 506 | const vpc = yield* Vpc("DescriptionsVpc", { |
| 507 | cidrBlock: "10.0.0.0/16", |
| 508 | }); |
| 509 | const rule: SecurityGroupRuleData = { |
| 510 | ipProtocol: "tcp", |
| 511 | fromPort: 443, |
| 512 | toPort: 443, |
| 513 | cidrIpv4: "10.0.0.0/16", |
| 514 | description, |
| 515 | }; |
| 516 | const sg = yield* SecurityGroup("DescriptionsSg", { |
| 517 | vpcId: vpc.vpcId, |
| 518 | ingress: [rule], |
| 519 | egress: [rule], |
| 520 | }); |
| 521 | return { vpc, sg }; |
| 522 | }); |
| 523 | const created = yield* stack.deploy(makeStack("before")); |
| 524 | const readRules = () => |
| 525 | EC2.describeSecurityGroupRules({ |
| 526 | Filters: [{ Name: "group-id", Values: [created.sg.groupId] }], |
| 527 | }); |
| 528 | const initial = yield* readRules(); |
| 529 | expect(initial.SecurityGroupRules).toHaveLength(2); |
| 530 | for (const rule of initial.SecurityGroupRules ?? []) { |
| 531 | expect(rule.SecurityGroupRuleId).toMatch(/^sgr-/); |
| 532 | expect(rule.Description).toBe("before"); |
| 533 | } |
| 534 | for (const description of ["after", "", undefined]) { |
| 535 | const updated = yield* stack.deploy(makeStack(description)); |
| 536 | expect(updated.sg.groupId).toBe(created.sg.groupId); |
| 537 | expect(updated.sg.groupArn).toBe(created.sg.groupArn); |
| 538 | expect(updated.sg.ownerId).toBe(created.sg.ownerId); |
| 539 | const observed = yield* readRules(); |
| 540 | expect(observed.SecurityGroupRules).toHaveLength(2); |
| 541 | for (const original of initial.SecurityGroupRules ?? []) { |
| 542 | const rule = observed.SecurityGroupRules?.find( |
| 543 | (rule) => rule.SecurityGroupRuleId === original.SecurityGroupRuleId, |
| 544 | ); |
| 545 | expect(rule).toBeDefined(); |
| 546 | expect(rule?.IsEgress).toBe(original.IsEgress); |
| 547 | expect(rule?.Description ?? "").toBe(description ?? ""); |
| 548 | } |
| 549 | } |
| 550 | const plan = yield* stack.plan(makeStack(undefined)); |
| 551 | expect(plan.resources.DescriptionsSg).toMatchObject({ action: "noop" }); |
| 552 | yield* stack.destroy(); |
| 553 | yield* assertSecurityGroupGone(created.sg.groupId); |
| 554 | yield* assertVpcGone(created.vpc.vpcId); |
| 555 | }).pipe(logLevel), |
| 556 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120000 }, |
| 557 | ); |
| 558 | |
| 559 | test.provider( |
| 560 | "updates only changed inline rules and repairs a missing rule before a noop deploy", |
| 561 | (stack) => |
| 562 | Effect.gen(function* () { |
| 563 | yield* stack.destroy(); |
| 564 | const retained: SecurityGroupRuleData = { |
| 565 | ipProtocol: "tcp", |
| 566 | fromPort: 443, |
| 567 | toPort: 443, |
| 568 | cidrIpv4: "10.0.0.0/16", |
| 569 | }; |
| 570 | const before: SecurityGroupRuleData = { |
| 571 | ipProtocol: "tcp", |
| 572 | fromPort: 22, |
| 573 | toPort: 22, |
| 574 | cidrIpv4: "10.0.0.0/16", |
| 575 | }; |
| 576 | const after: SecurityGroupRuleData = { |
| 577 | ipProtocol: "udp", |
| 578 | fromPort: 53, |
| 579 | toPort: 53, |
| 580 | cidrIpv4: "10.1.0.0/16", |
| 581 | }; |
| 582 | const makeStack = (rule: SecurityGroupRuleData) => |
| 583 | Effect.gen(function* () { |
| 584 | const vpc = yield* Vpc("RuleDeltaVpc", { cidrBlock: "10.0.0.0/16" }); |
| 585 | const sg = yield* SecurityGroup("RuleDeltaSg", { |
| 586 | vpcId: vpc.vpcId, |
| 587 | ingress: [retained, rule], |
| 588 | egress: [], |
| 589 | }); |
| 590 | return { vpc, sg }; |
| 591 | }); |
| 592 | const created = yield* stack.deploy(makeStack(before)); |
| 593 | const readRules = () => |
| 594 | EC2.describeSecurityGroupRules({ |
| 595 | Filters: [{ Name: "group-id", Values: [created.sg.groupId] }], |
| 596 | }); |
| 597 | const initial = yield* readRules(); |
| 598 | expect(initial.SecurityGroupRules).toHaveLength(2); |
| 599 | const retainedRule = initial.SecurityGroupRules?.find((rule) => rule.FromPort === 443); |
| 600 | const removedRule = initial.SecurityGroupRules?.find((rule) => rule.FromPort === 22); |
| 601 | expect(retainedRule?.SecurityGroupRuleId).toMatch(/^sgr-/); |
| 602 | expect(removedRule?.SecurityGroupRuleId).toMatch(/^sgr-/); |
| 603 | const desiredStack = makeStack(after); |
| 604 | const updatePlan = yield* stack.plan(desiredStack); |
| 605 | expect(updatePlan.resources.RuleDeltaSg).toMatchObject({ |
| 606 | action: "update", |
| 607 | }); |
| 608 | const updated = yield* stack.deploy(desiredStack); |
| 609 | expect(updated.sg.groupId).toBe(created.sg.groupId); |
| 610 | expect(updated.sg.groupArn).toBe(created.sg.groupArn); |
| 611 | expect(updated.sg.ownerId).toBe(created.sg.ownerId); |
| 612 | const changed = yield* readRules(); |
| 613 | expect(changed.SecurityGroupRules).toHaveLength(2); |
| 614 | expect(changed.SecurityGroupRules).toEqual( |
| 615 | expect.arrayContaining([ |
| 616 | retainedRule, |
| 617 | expect.objectContaining({ |
| 618 | IsEgress: false, |
| 619 | IpProtocol: "udp", |
| 620 | FromPort: 53, |
| 621 | ToPort: 53, |
| 622 | CidrIpv4: "10.1.0.0/16", |
| 623 | }), |
| 624 | ]), |
| 625 | ); |
| 626 | expect( |
| 627 | changed.SecurityGroupRules?.some( |
| 628 | (rule) => rule.SecurityGroupRuleId === removedRule?.SecurityGroupRuleId, |
| 629 | ), |
| 630 | ).toBe(false); |
| 631 | const addedRule = changed.SecurityGroupRules?.find((rule) => rule.FromPort === 53); |
| 632 | expect(addedRule?.SecurityGroupRuleId).toMatch(/^sgr-/); |
| 633 | yield* EC2.revokeSecurityGroupIngress({ |
| 634 | GroupId: created.sg.groupId, |
| 635 | SecurityGroupRuleIds: [addedRule!.SecurityGroupRuleId!], |
| 636 | }); |
| 637 | const drifted = yield* readRules().pipe( |
| 638 | Effect.repeat({ |
| 639 | schedule: Schedule.spaced("2 seconds"), |
| 640 | times: 8, |
| 641 | until: (response) => |
| 642 | response.SecurityGroupRules?.length === 1 && |
| 643 | response.SecurityGroupRules[0]?.SecurityGroupRuleId === |
| 644 | retainedRule?.SecurityGroupRuleId, |
| 645 | }), |
| 646 | ); |
| 647 | expect(drifted.SecurityGroupRules).toEqual([retainedRule]); |
| 648 | const repairPlan = yield* stack.plan(desiredStack); |
| 649 | expect(repairPlan.resources.RuleDeltaSg).toMatchObject({ |
| 650 | action: "update", |
| 651 | }); |
| 652 | const repaired = yield* stack.deploy(desiredStack); |
| 653 | expect(repaired.sg.groupId).toBe(created.sg.groupId); |
| 654 | const restored = yield* readRules(); |
| 655 | expect(restored.SecurityGroupRules).toHaveLength(2); |
| 656 | expect(restored.SecurityGroupRules).toEqual( |
| 657 | expect.arrayContaining([ |
| 658 | retainedRule, |
| 659 | expect.objectContaining({ |
| 660 | IsEgress: false, |
| 661 | IpProtocol: "udp", |
| 662 | FromPort: 53, |
| 663 | ToPort: 53, |
| 664 | CidrIpv4: "10.1.0.0/16", |
| 665 | }), |
| 666 | ]), |
| 667 | ); |
| 668 | expect( |
| 669 | restored.SecurityGroupRules?.some( |
| 670 | (rule) => rule.SecurityGroupRuleId === addedRule?.SecurityGroupRuleId, |
| 671 | ), |
| 672 | ).toBe(false); |
| 673 | const noopPlan = yield* stack.plan(desiredStack); |
| 674 | expect(noopPlan.resources.RuleDeltaSg).toMatchObject({ action: "noop" }); |
| 675 | yield* stack.deploy(desiredStack); |
| 676 | const unchanged = yield* readRules(); |
| 677 | expect(unchanged.SecurityGroupRules).toHaveLength(2); |
| 678 | expect(unchanged.SecurityGroupRules).toEqual( |
| 679 | expect.arrayContaining(restored.SecurityGroupRules ?? []), |
| 680 | ); |
| 681 | yield* stack.destroy(); |
| 682 | yield* assertSecurityGroupGone(created.sg.groupId); |
| 683 | yield* assertVpcGone(created.vpc.vpcId); |
| 684 | }).pipe(logLevel), |
| 685 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120000 }, |
| 686 | ); |
| 687 | |
| 688 | test.provider( |
| 689 | "recreates a deleted group and updates its standalone rule dependency", |
| 690 | (stack) => |
| 691 | Effect.gen(function* () { |
| 692 | yield* stack.destroy(); |
| 693 | const resources = Effect.gen(function* () { |
| 694 | const vpc = yield* Vpc("MissingGroupVpc", { cidrBlock: "10.0.0.0/16" }); |
| 695 | const sg = yield* SecurityGroup("MissingGroupSg", { |
| 696 | vpcId: vpc.vpcId, |
| 697 | egress: [], |
| 698 | }); |
| 699 | const rule = yield* SecurityGroupRule("MissingGroupRule", { |
| 700 | groupId: sg.groupId, |
| 701 | type: "ingress", |
| 702 | ipProtocol: "tcp", |
| 703 | fromPort: 443, |
| 704 | toPort: 443, |
| 705 | cidrIpv4: "10.0.0.0/16", |
| 706 | }); |
| 707 | return { vpc, sg, rule }; |
| 708 | }); |
| 709 | const created = yield* stack.deploy(resources); |
| 710 | yield* EC2.deleteSecurityGroup({ GroupId: created.sg.groupId }); |
| 711 | yield* assertSecurityGroupGone(created.sg.groupId); |
| 712 | const repaired = yield* stack.deploy(resources); |
| 713 | expect(repaired.sg.groupId).not.toBe(created.sg.groupId); |
| 714 | expect(repaired.rule.groupId).toBe(repaired.sg.groupId); |
| 715 | expect(repaired.rule.securityGroupRuleId).not.toBe(created.rule.securityGroupRuleId); |
| 716 | const observed = yield* EC2.describeSecurityGroupRules({ |
| 717 | Filters: [{ Name: "group-id", Values: [repaired.sg.groupId] }], |
| 718 | }); |
| 719 | expect(observed.SecurityGroupRules).toEqual([ |
| 720 | expect.objectContaining({ |
| 721 | SecurityGroupRuleId: repaired.rule.securityGroupRuleId, |
| 722 | IsEgress: false, |
| 723 | FromPort: 443, |
| 724 | }), |
| 725 | ]); |
| 726 | const plan = yield* stack.plan(resources); |
| 727 | expect(plan.resources.MissingGroupSg).toMatchObject({ action: "noop" }); |
| 728 | yield* stack.destroy(); |
| 729 | yield* assertSecurityGroupGone(repaired.sg.groupId); |
| 730 | yield* assertVpcGone(repaired.vpc.vpcId); |
| 731 | }).pipe(logLevel), |
| 732 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120000 }, |
| 733 | ); |
| 734 | |
| 735 | test.provider( |
| 736 | "canonical inline rules retain IDs across equivalent updates and noop deploys", |
| 737 | (stack) => |
| 738 | Effect.gen(function* () { |
| 739 | yield* stack.destroy(); |
| 740 | const supplied: SecurityGroupRuleData[] = [ |
| 741 | { |
| 742 | ipProtocol: "6", |
| 743 | fromPort: 443, |
| 744 | toPort: 443, |
| 745 | cidrIpv4: "100.68.0.18/18", |
| 746 | cidrIpv6: "2001:DB8:0:0:ABCD:0123:4567:89ab/64", |
| 747 | description: "", |
| 748 | }, |
| 749 | { |
| 750 | ipProtocol: "17", |
| 751 | fromPort: 53, |
| 752 | toPort: 53, |
| 753 | cidrIpv4: "10.0.0.1/16", |
| 754 | }, |
| 755 | { |
| 756 | ipProtocol: "1", |
| 757 | fromPort: 8, |
| 758 | toPort: -1, |
| 759 | cidrIpv4: "10.0.0.1/16", |
| 760 | }, |
| 761 | { |
| 762 | ipProtocol: "-1", |
| 763 | fromPort: 0, |
| 764 | toPort: 65535, |
| 765 | cidrIpv4: "10.1.0.1/16", |
| 766 | }, |
| 767 | { |
| 768 | ipProtocol: "50", |
| 769 | fromPort: 0, |
| 770 | toPort: 65535, |
| 771 | cidrIpv4: "10.2.0.1/16", |
| 772 | }, |
| 773 | { ipProtocol: "icmpv6", cidrIpv6: "::/0" }, |
| 774 | ]; |
| 775 | const canonical: SecurityGroupRuleData[] = [ |
| 776 | { |
| 777 | ipProtocol: "58", |
| 778 | fromPort: -1, |
| 779 | toPort: -1, |
| 780 | cidrIpv6: "::/0", |
| 781 | }, |
| 782 | { ipProtocol: "50", cidrIpv4: "10.2.0.0/16" }, |
| 783 | { ipProtocol: "-1", cidrIpv4: "10.1.0.0/16" }, |
| 784 | { |
| 785 | ipProtocol: "icmp", |
| 786 | fromPort: 8, |
| 787 | toPort: -1, |
| 788 | cidrIpv4: "10.0.0.0/16", |
| 789 | }, |
| 790 | { |
| 791 | ipProtocol: "udp", |
| 792 | fromPort: 53, |
| 793 | toPort: 53, |
| 794 | cidrIpv4: "10.0.0.0/16", |
| 795 | }, |
| 796 | { |
| 797 | ipProtocol: "tcp", |
| 798 | fromPort: 443, |
| 799 | toPort: 443, |
| 800 | cidrIpv6: "2001:db8::/64", |
| 801 | }, |
| 802 | { |
| 803 | ipProtocol: "tcp", |
| 804 | fromPort: 443, |
| 805 | toPort: 443, |
| 806 | cidrIpv4: "100.68.0.0/18", |
| 807 | }, |
| 808 | ]; |
| 809 | const makeStack = (ingress: SecurityGroupRuleData[], label: string) => |
| 810 | Effect.gen(function* () { |
| 811 | const vpc = yield* Vpc("CanonicalVpc", { cidrBlock: "10.0.0.0/16" }); |
| 812 | const sg = yield* SecurityGroup("CanonicalSg", { |
| 813 | vpcId: vpc.vpcId, |
| 814 | ingress, |
| 815 | egress: [], |
| 816 | tags: { Label: label }, |
| 817 | }); |
| 818 | return { vpc, sg }; |
| 819 | }); |
| 820 | const created = yield* stack.deploy(makeStack(supplied, "before")); |
| 821 | const readRules = () => |
| 822 | EC2.describeSecurityGroupRules({ |
| 823 | Filters: [{ Name: "group-id", Values: [created.sg.groupId] }], |
| 824 | }); |
| 825 | const initial = yield* readRules(); |
| 826 | expect(initial.SecurityGroupRules).toHaveLength(7); |
| 827 | for (const rule of canonical) { |
| 828 | expect(initial.SecurityGroupRules).toEqual( |
| 829 | expect.arrayContaining([ |
| 830 | expect.objectContaining({ |
| 831 | IsEgress: false, |
| 832 | IpProtocol: |
| 833 | rule.ipProtocol === "58" ? expect.stringMatching(/^(58|icmpv6)$/) : rule.ipProtocol, |
| 834 | ...(rule.fromPort === undefined ? {} : { FromPort: rule.fromPort }), |
| 835 | ...(rule.toPort === undefined ? {} : { ToPort: rule.toPort }), |
| 836 | ...(rule.cidrIpv4 === undefined ? {} : { CidrIpv4: rule.cidrIpv4 }), |
| 837 | ...(rule.cidrIpv6 === undefined ? {} : { CidrIpv6: rule.cidrIpv6 }), |
| 838 | }), |
| 839 | ]), |
| 840 | ); |
| 841 | } |
| 842 | const originalPlan = yield* stack.plan(makeStack(supplied, "before")); |
| 843 | expect(originalPlan.resources.CanonicalSg).toMatchObject({ |
| 844 | action: "noop", |
| 845 | }); |
| 846 | |
| 847 | const equivalentStack = makeStack(canonical, "after"); |
| 848 | const updated = yield* stack.deploy(equivalentStack); |
| 849 | expect(updated.sg.groupId).toBe(created.sg.groupId); |
| 850 | const equivalent = yield* readRules(); |
| 851 | expect(equivalent.SecurityGroupRules).toHaveLength(7); |
| 852 | expect(equivalent.SecurityGroupRules).toEqual( |
| 853 | expect.arrayContaining(initial.SecurityGroupRules ?? []), |
| 854 | ); |
| 855 | const plan = yield* stack.plan(equivalentStack); |
| 856 | expect(plan.resources.CanonicalSg).toMatchObject({ action: "noop" }); |
| 857 | yield* stack.deploy(equivalentStack); |
| 858 | const unchanged = yield* readRules(); |
| 859 | expect(unchanged.SecurityGroupRules).toHaveLength(7); |
| 860 | expect(unchanged.SecurityGroupRules).toEqual( |
| 861 | expect.arrayContaining(initial.SecurityGroupRules ?? []), |
| 862 | ); |
| 863 | yield* stack.destroy(); |
| 864 | yield* assertSecurityGroupGone(created.sg.groupId); |
| 865 | yield* assertVpcGone(created.vpc.vpcId); |
| 866 | }).pipe(logLevel), |
| 867 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120000 }, |
| 868 | ); |