Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): keep Ipv6CidrBlock and IcmpTypeCode on network ACL entries (#6680)
lex009e14ffe92a19
diff --git a/docs/services/ec2.md b/docs/services/ec2.mdindex 54587c7585..cc4a089ae1 100644--- a/docs/services/ec2.md+++ b/docs/services/ec2.md@@ -537,8 +537,8 @@ matching how AWS itself responds.| CreateNetworkAcl | Creates a network ACL in a VPC, with `network-acl` tags from `TagSpecification`. || DescribeNetworkAcls | Lists or returns stored network ACLs. || DeleteNetworkAcl | Deletes a network ACL from the local EC2 store. |-| CreateNetworkAclEntry | Adds an entry to a network ACL. |-| ReplaceNetworkAclEntry | Replaces an entry in a network ACL. |+| CreateNetworkAclEntry | Adds an entry to a network ACL. The entry keeps whichever of `CidrBlock` or `Ipv6CidrBlock` was supplied, and `DescribeNetworkAcls` returns it under the same name. `IcmpTypeCode` is kept for ICMP (`1`) and ICMPv6 (`58`) entries only. |+| ReplaceNetworkAclEntry | Replaces an entry in a network ACL, including its `CidrBlock` or `Ipv6CidrBlock` and, for ICMP and ICMPv6 entries, its `IcmpTypeCode`. || DeleteNetworkAclEntry | Removes an entry from a network ACL. || ReplaceNetworkAclAssociation | Replaces the network ACL associated with a subnet. |diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkAclCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkAclCfnProvisioner.javaindex 9ca164313b..478f1c67da 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkAclCfnProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkAclCfnProvisioner.java@@ -100,12 +100,16 @@ public class Ec2NetworkAclCfnProvisioner implements CfnResourceProvisioner {String toValue = ctx.resolveOptional(portRange, "To");Integer from = fromValue != null ? Integer.valueOf(fromValue) : null;Integer to = toValue != null ? Integer.valueOf(toValue) : null;+ JsonNode icmp = props != null && props.hasNonNull("Icmp") ? props.get("Icmp") : null;String protocol = ctx.resolveOptional(props, "Protocol");ec2Service.createNetworkAclEntry(ctx.region(), aclId, ruleNumber,protocol != null ? protocol : "-1",ctx.resolveOptional(props, "RuleAction"),egress,ctx.resolveOptional(props, "CidrBlock"),+ ctx.resolveOptional(props, "Ipv6CidrBlock"),+ ctx.resolveOptionalInt(icmp, "Type"),+ ctx.resolveOptionalInt(icmp, "Code"),// RuleNumber, Egress and NetworkAclId are the createOnly key, so re-provisioning// the same key is an update. replace=false would raise NetworkAclEntryAlreadyExists// on every stack update.diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.javaindex e0db2e5860..4dad37aa93 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java@@ -4356,6 +4356,8 @@ public class Ec2QueryHandler {private Response handleNetworkAclEntry(MultivaluedMap<String, String> p, String region, String action) {String fromStr = p.getFirst("PortRange.From");String toStr = p.getFirst("PortRange.To");+ String icmpType = p.getFirst("Icmp.Type");+ String icmpCode = p.getFirst("Icmp.Code");service.createNetworkAclEntry(region,p.getFirst("NetworkAclId"),Integer.parseInt(p.getFirst("RuleNumber")),@@ -4363,6 +4365,9 @@ public class Ec2QueryHandler {p.getFirst("RuleAction"),Boolean.parseBoolean(p.getFirst("Egress")),p.getFirst("CidrBlock"),+ p.getFirst("Ipv6CidrBlock"),+ icmpType != null ? Integer.valueOf(icmpType) : null,+ icmpCode != null ? Integer.valueOf(icmpCode) : null,fromStr != null ? Integer.valueOf(fromStr) : null,toStr != null ? Integer.valueOf(toStr) : null,"ReplaceNetworkAclEntry".equals(action));@@ -5475,7 +5480,14 @@ public class Ec2QueryHandler {.elem("protocol", e.getProtocol()).elem("ruleAction", e.getRuleAction()).elem("egress", String.valueOf(e.isEgress()))- .elem("cidrBlock", e.getCidrBlock());+ .elem("cidrBlock", e.getCidrBlock())+ .elem("ipv6CidrBlock", e.getIpv6CidrBlock());+ if (e.getIcmpType() != null || e.getIcmpCode() != null) {+ xml.start("icmpTypeCode")+ .elem("code", e.getIcmpCode() != null ? String.valueOf(e.getIcmpCode()) : null)+ .elem("type", e.getIcmpType() != null ? String.valueOf(e.getIcmpType()) : null)+ .end("icmpTypeCode");+ }if (e.getPortRangeFrom() != null || e.getPortRangeTo() != null) {xml.start("portRange").elem("from", String.valueOf(e.getPortRangeFrom()))diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.javaindex d0a583b107..bc2415d3b5 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java@@ -1659,7 +1659,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab}public void createNetworkAclEntry(String region, String networkAclId, int ruleNumber, String protocol,- String ruleAction, boolean egress, String cidrBlock, Integer from, Integer to,+ String ruleAction, boolean egress, String cidrBlock, String ipv6CidrBlock,+ Integer icmpType, Integer icmpCode, Integer from, Integer to,boolean replace) {synchronized (lockFor(key(region, networkAclId))) {NetworkAcl acl = getRequiredNetworkAcl(region, networkAclId);@@ -1672,6 +1673,13 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, ResettabList<NetworkAclEntry> next = new ArrayList<>(acl.getEntries());next.removeIf(e -> e.getRuleNumber() == ruleNumber && e.isEgress() == egress);NetworkAclEntry entry = naclEntry(ruleNumber, protocol, ruleAction, egress, cidrBlock);+ entry.setIpv6CidrBlock(ipv6CidrBlock);+ // The model documents an ICMP type and code as having no effect on any other protocol,+ // so only ICMP and ICMPv6 entries keep one for DescribeNetworkAcls to return.+ if (isIcmpProtocol(protocol)) {+ entry.setIcmpType(icmpType);+ entry.setIcmpCode(icmpCode);+ }entry.setPortRangeFrom(from);entry.setPortRangeTo(to);next.add(entry);@@ -1680,6 +1688,13 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab}}+ private static boolean isIcmpProtocol(String protocol) {+ return protocol != null && switch (protocol.toLowerCase(Locale.ROOT)) {+ case "1", "58", "icmp", "icmpv6" -> true;+ default -> false;+ };+ }+public void deleteNetworkAclEntry(String region, String networkAclId, int ruleNumber, boolean egress) {synchronized (lockFor(key(region, networkAclId))) {NetworkAcl acl = getRequiredNetworkAcl(region, networkAclId);diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAclEntry.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAclEntry.javaindex e9d79afa90..6a75883160 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAclEntry.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAclEntry.java@@ -12,6 +12,9 @@ public class NetworkAclEntry {private String ruleAction;private boolean egress;private String cidrBlock;+ private String ipv6CidrBlock;+ private Integer icmpType;+ private Integer icmpCode;private Integer portRangeFrom;private Integer portRangeTo;@@ -32,6 +35,15 @@ public class NetworkAclEntry {public String getCidrBlock() { return cidrBlock; }public void setCidrBlock(String cidrBlock) { this.cidrBlock = cidrBlock; }+ public String getIpv6CidrBlock() { return ipv6CidrBlock; }+ public void setIpv6CidrBlock(String ipv6CidrBlock) { this.ipv6CidrBlock = ipv6CidrBlock; }++ public Integer getIcmpType() { return icmpType; }+ public void setIcmpType(Integer icmpType) { this.icmpType = icmpType; }++ public Integer getIcmpCode() { return icmpCode; }+ public void setIcmpCode(Integer icmpCode) { this.icmpCode = icmpCode; }+public Integer getPortRangeFrom() { return portRangeFrom; }public void setPortRangeFrom(Integer portRangeFrom) { this.portRangeFrom = portRangeFrom; }diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkAclCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkAclCfnProvisionerTest.javaindex 88ca2eb633..d676ea9888 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkAclCfnProvisionerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkAclCfnProvisionerTest.java@@ -39,6 +39,7 @@ class Ec2NetworkAclCfnProvisionerTest {private ProvisionContext ctx() {// The engine is a collaborator; scalars resolve to their text and {"Ref": "X"} nodes// resolve to "resolved-X", enough to prove every property goes through the engine.+ // {"Ref": "AWS::NoValue"} resolves to "", as it does in the real engine.CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);when(engine.resolve(any())).thenAnswer(inv -> {JsonNode node = inv.getArgument(0);@@ -46,7 +47,8 @@ class Ec2NetworkAclCfnProvisionerTest {return null;}if (node.isObject() && node.has("Ref")) {- return "resolved-" + node.get("Ref").asText();+ String ref = node.get("Ref").asText();+ return "AWS::NoValue".equals(ref) ? "" : "resolved-" + ref;}return node.asText();});@@ -122,7 +124,7 @@ class Ec2NetworkAclCfnProvisionerTest {provisioner.provision(r, props, new ProvisionContext(engine, "us-east-1", "000000000000", "my-stack"));verify(ec2).createNetworkAclEntry("us-east-1", "acl-abc", 150, "6", "deny", true,- "0.0.0.0/0", 22, 22, true);+ "0.0.0.0/0", null, null, null, 22, 22, true);assertEquals("acl-abc|150|egress", r.getPhysicalId());}@@ -134,10 +136,63 @@ class Ec2NetworkAclCfnProvisionerTest {provisioner.provision(r, props, ctx());verify(ec2).createNetworkAclEntry("us-east-1", "acl-abc", 100, "-1", null, false,- null, null, null, true);+ null, null, null, null, null, null, true);assertEquals("acl-abc|100|ingress", r.getPhysicalId());}+ @Test+ void aclEntryPassesIpv6CidrBlockThrough() {+ StackResource r = resource("AWS::EC2::NetworkAclEntry", "Entry");+ ObjectNode props = mapper.createObjectNode()+ .put("NetworkAclId", "acl-abc")+ .put("RuleNumber", 101)+ .put("Protocol", "-1")+ .put("RuleAction", "allow")+ .put("Ipv6CidrBlock", "::/0");++ provisioner.provision(r, props, ctx());++ verify(ec2).createNetworkAclEntry("us-east-1", "acl-abc", 101, "-1", "allow", false,+ null, "::/0", null, null, null, null, true);+ }++ @Test+ void aclEntryPassesIcmpTypeAndCodeThrough() {+ StackResource r = resource("AWS::EC2::NetworkAclEntry", "Entry");+ ObjectNode props = mapper.createObjectNode()+ .put("NetworkAclId", "acl-abc")+ .put("RuleNumber", 110)+ .put("Protocol", "1")+ .put("RuleAction", "allow")+ .put("CidrBlock", "0.0.0.0/0");+ props.set("Icmp", mapper.createObjectNode().put("Type", 8).put("Code", -1));++ provisioner.provision(r, props, ctx());++ verify(ec2).createNetworkAclEntry("us-east-1", "acl-abc", 110, "1", "allow", false,+ "0.0.0.0/0", null, 8, -1, null, null, true);+ }++ @Test+ void aclEntryOmitsIcmpTypeAndCodeThatResolveToNoValue() {+ StackResource r = resource("AWS::EC2::NetworkAclEntry", "Entry");+ ObjectNode props = mapper.createObjectNode()+ .put("NetworkAclId", "acl-abc")+ .put("RuleNumber", 120)+ .put("Protocol", "1")+ .put("RuleAction", "allow")+ .put("CidrBlock", "0.0.0.0/0");+ ObjectNode icmp = mapper.createObjectNode();+ icmp.set("Type", mapper.createObjectNode().put("Ref", "AWS::NoValue"));+ icmp.set("Code", mapper.createObjectNode().put("Ref", "AWS::NoValue"));+ props.set("Icmp", icmp);++ provisioner.provision(r, props, ctx());++ verify(ec2).createNetworkAclEntry("us-east-1", "acl-abc", 120, "1", "allow", false,+ "0.0.0.0/0", null, null, null, null, null, true);+ }+/*** Id is the type's primaryIdentifier and its only schema readOnlyProperty. It went unset for a* long time because the sibling NetworkAcl arm sets one too, and the coverage scan read thediff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.javaindex 9a0cdad3b7..25829b1b5b 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java@@ -5125,6 +5125,149 @@ class Ec2IntegrationTest {.body("Response.Errors.Error.Code", equalTo("DependencyViolation"));}+ private String newNetworkAcl(String vpcId) {+ return given()+ .formParam("Action", "CreateNetworkAcl")+ .formParam("VpcId", vpcId)+ .header("Authorization", AUTH_HEADER)+ .when().post("/")+ .then().statusCode(200)+ .extract().path("CreateNetworkAclResponse.networkAcl.networkAclId");+ }++ private void putNetworkAclEntry(String action, String aclId, int ruleNumber, boolean egress,+ String cidrParam, String cidr) {+ given()+ .formParam("Action", action)+ .formParam("NetworkAclId", aclId)+ .formParam("RuleNumber", String.valueOf(ruleNumber))+ .formParam("Protocol", "-1")+ .formParam("RuleAction", "allow")+ .formParam("Egress", String.valueOf(egress))+ .formParam(cidrParam, cidr)+ .header("Authorization", AUTH_HEADER)+ .when().post("/")+ .then().statusCode(200)+ .body(action + "Response.return", equalTo("true"));+ }++ private XmlPath describeNetworkAcl(String aclId) {+ return new XmlPath(given()+ .formParam("Action", "DescribeNetworkAcls")+ .formParam("NetworkAclId.1", aclId)+ .header("Authorization", AUTH_HEADER)+ .when().post("/")+ .then().statusCode(200)+ .extract().asString());+ }++ private static String naclEntryPath(int ruleNumber, boolean egress) {+ return "DescribeNetworkAclsResponse.networkAclSet.item.entrySet.item.find { it.ruleNumber == '"+ + ruleNumber + "' && it.egress == '" + egress + "' }";+ }++ // An entry created with only Ipv6CidrBlock reads back with that range under ipv6CidrBlock.+ @Test+ @Order(314)+ void createNetworkAclEntryKeepsIpv6CidrBlock() {+ String aclId = newNetworkAcl(newVpc("10.34.0.0/16"));+ putNetworkAclEntry("CreateNetworkAclEntry", aclId, 100, false, "CidrBlock", "0.0.0.0/0");+ putNetworkAclEntry("CreateNetworkAclEntry", aclId, 101, false, "Ipv6CidrBlock", "::/0");+ putNetworkAclEntry("CreateNetworkAclEntry", aclId, 101, true, "Ipv6CidrBlock", "::/0");++ XmlPath acl = describeNetworkAcl(aclId);++ assertEquals("0.0.0.0/0", acl.getString(naclEntryPath(100, false) + ".cidrBlock"));+ assertEquals(0, acl.getInt(naclEntryPath(100, false) + ".ipv6CidrBlock.size()"));+ for (boolean egress : new boolean[] {false, true}) {+ assertEquals("::/0", acl.getString(naclEntryPath(101, egress) + ".ipv6CidrBlock"));+ assertEquals(0, acl.getInt(naclEntryPath(101, egress) + ".cidrBlock.size()"));+ }+ }++ @Test+ @Order(314)+ void replaceNetworkAclEntrySwitchesToIpv6CidrBlock() {+ String aclId = newNetworkAcl(newVpc("10.35.0.0/16"));+ putNetworkAclEntry("CreateNetworkAclEntry", aclId, 120, false, "CidrBlock", "10.0.0.0/8");+ putNetworkAclEntry("ReplaceNetworkAclEntry", aclId, 120, false, "Ipv6CidrBlock", "2001:db8::/32");++ XmlPath acl = describeNetworkAcl(aclId);++ assertEquals("2001:db8::/32", acl.getString(naclEntryPath(120, false) + ".ipv6CidrBlock"));+ assertEquals(0, acl.getInt(naclEntryPath(120, false) + ".cidrBlock.size()"));+ }++ // The default ACL's IPv6 rule reads back as IPv6, and only ICMP entries carry an icmpTypeCode.+ @Test+ @Order(314)+ void defaultNetworkAclIpv6RuleReadsBackWithoutIcmpTypeCode() {+ String vpc = newVpc("10.42.0.0/16");+ String aclId = given()+ .formParam("Action", "DescribeNetworkAcls")+ .formParam("Filter.1.Name", "vpc-id")+ .formParam("Filter.1.Value.1", vpc)+ .formParam("Filter.2.Name", "default")+ .formParam("Filter.2.Value.1", "true")+ .header("Authorization", AUTH_HEADER)+ .when().post("/")+ .then().statusCode(200)+ .extract().path("DescribeNetworkAclsResponse.networkAclSet.item.networkAclId");+ for (boolean egress : new boolean[] {false, true}) {+ given()+ .formParam("Action", "CreateNetworkAclEntry")+ .formParam("NetworkAclId", aclId)+ .formParam("RuleNumber", "101")+ .formParam("Protocol", "-1")+ .formParam("RuleAction", "allow")+ .formParam("Egress", String.valueOf(egress))+ .formParam("Ipv6CidrBlock", "::/0")+ .formParam("PortRange.From", "0")+ .formParam("PortRange.To", "0")+ .header("Authorization", AUTH_HEADER)+ .when().post("/")+ .then().statusCode(200);+ }++ XmlPath acl = describeNetworkAcl(aclId);++ for (boolean egress : new boolean[] {false, true}) {+ assertEquals("0.0.0.0/0", acl.getString(naclEntryPath(100, egress) + ".cidrBlock"));+ assertEquals(0, acl.getInt(naclEntryPath(100, egress) + ".ipv6CidrBlock.size()"));+ assertEquals("::/0", acl.getString(naclEntryPath(101, egress) + ".ipv6CidrBlock"));+ assertEquals(0, acl.getInt(naclEntryPath(101, egress) + ".cidrBlock.size()"));+ }+ assertEquals(0, acl.getInt(+ "DescribeNetworkAclsResponse.networkAclSet.item.entrySet.item.icmpTypeCode.size()"));+ }++ @Test+ @Order(314)+ void networkAclEntryKeepsIcmpTypeCodeOnlyForIcmp() {+ String aclId = newNetworkAcl(newVpc("10.43.0.0/16"));+ for (String[] rule : new String[][] {{"110", "1"}, {"120", "6"}}) {+ given()+ .formParam("Action", "CreateNetworkAclEntry")+ .formParam("NetworkAclId", aclId)+ .formParam("RuleNumber", rule[0])+ .formParam("Protocol", rule[1])+ .formParam("RuleAction", "allow")+ .formParam("Egress", "false")+ .formParam("CidrBlock", "0.0.0.0/0")+ .formParam("Icmp.Type", "8")+ .formParam("Icmp.Code", "-1")+ .header("Authorization", AUTH_HEADER)+ .when().post("/")+ .then().statusCode(200);+ }++ XmlPath acl = describeNetworkAcl(aclId);++ assertEquals("8", acl.getString(naclEntryPath(110, false) + ".icmpTypeCode.type"));+ assertEquals("-1", acl.getString(naclEntryPath(110, false) + ".icmpTypeCode.code"));+ assertEquals(0, acl.getInt(naclEntryPath(120, false) + ".icmpTypeCode.size()"));+ }+@Test@Order(315)void describePrefixListsReturnsManagedS3() {diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceConcurrencyTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceConcurrencyTest.javaindex b9206b4dfb..51c9b9b83f 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceConcurrencyTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceConcurrencyTest.java@@ -127,7 +127,7 @@ class Ec2ServiceConcurrencyTest {try {runRace(i -> {service.createNetworkAclEntry(region, aclId, 100 + i, "6", "allow", false,- "10.0." + i + ".0/24", 80, 80, false);+ "10.0." + i + ".0/24", null, null, null, 80, 80, false);return "entry-" + i;});} finally {