Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): write Amazon-provided VPC IPv6 blocks in canonical form (#6692)
Jackson Pricec6927cca9638
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 bc2415d3b5..da563bba3e 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@@ -6599,10 +6599,13 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab* Allocates a /56 the way an Amazon-provided association looks on the wire. AWS hands out a* block from its own pool with no say from the caller, so the exact prefix is not something a* client can predict or assert on, what matters is that one is returned at all, that it is a- * well-formed /56, and that it comes back unchanged on every later read.+ * well-formed /56 in canonical text, and that it comes back unchanged on every later read.+ * A random group can start with a zero digit, which canonical text drops, and an all-zero+ * fourth group folds into the "::".*/private VpcIpv6CidrBlockAssociation amazonProvidedIpv6Association(String region) {- String block = "2600:1f18:" + randomHex(4) + ":" + randomHex(2) + "00::/56";+ String block = CidrCanonicalizer.canonicalize(+ "2600:1f18:" + randomHex(4) + ":" + randomHex(2) + "00::/56").orElseThrow();return new VpcIpv6CidrBlockAssociation("vpc-cidr-assoc-" + randomHex(8), block, region);}diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcEndpointNetworkInterfaceIdsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcEndpointNetworkInterfaceIdsIntegrationTest.javaindex 71e723f4e3..b4da1777a2 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcEndpointNetworkInterfaceIdsIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcEndpointNetworkInterfaceIdsIntegrationTest.java@@ -7,6 +7,7 @@ import io.restassured.specification.RequestSpecification;import jakarta.inject.Inject;import org.junit.jupiter.api.Test;+import java.net.InetAddress;import java.util.ArrayList;import java.util.List;@@ -78,11 +79,18 @@ class Ec2VpcEndpointNetworkInterfaceIdsIntegrationTest {"VpcId", vpcId, "AmazonProvidedIpv6CidrBlock", "true");}+ /**+ * The /64 at {@code offset} inside the VPC's /56. It is worked out from the address bytes+ * rather than the text, because compressed text drops a zero fourth group.+ */private String ipv6SubnetCidr(String block, int offset) {- String network = block.substring(0, block.indexOf("::"));- int separator = network.lastIndexOf(':');- int subnet = Integer.parseInt(network.substring(separator + 1), 16) + offset;- return network.substring(0, separator + 1) + Integer.toHexString(subnet) + "::/64";+ byte[] address = InetAddress.ofLiteral(block.substring(0, block.indexOf('/'))).getAddress();+ address[7] = (byte) (address[7] + offset);+ StringBuilder cidr = new StringBuilder();+ for (int i = 0; i < 8; i += 2) {+ cidr.append(Integer.toHexString(((address[i] & 0xff) << 8) | (address[i + 1] & 0xff))).append(':');+ }+ return cidr.append(":/64").toString();}private XmlPath describeEndpoint(String endpointId) {diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcIpv6CidrBlockIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcIpv6CidrBlockIntegrationTest.javaindex 6f28a85954..66165e5a8e 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcIpv6CidrBlockIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcIpv6CidrBlockIntegrationTest.java@@ -1,14 +1,20 @@package io.github.hectorvent.floci.services.ec2;import io.quarkus.test.junit.QuarkusTest;+import jakarta.inject.Inject;import org.junit.jupiter.api.Test;+import java.net.InetAddress;+import java.net.UnknownHostException;+import java.util.Arrays;+import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.containsString;import static org.hamcrest.Matchers.emptyOrNullString;import static org.hamcrest.Matchers.equalTo;import static org.hamcrest.Matchers.matchesRegex;import static org.hamcrest.Matchers.not;+import static org.junit.jupiter.api.Assertions.assertEquals;/*** CreateVpc ignored AmazonProvidedIpv6CidrBlock and no response carried an@@ -27,8 +33,11 @@ class Ec2VpcIpv6CidrBlockIntegrationTest {/** A /56 out of Amazon's pool, in the notation the API returns. */private static final String IPV6_CIDR = "[0-9a-f:]+::/56";+ @Inject+ Ec2Service service;+@Test- void createVpcWithAnAmazonProvidedBlockReturnsOneAndKeepsIt() {+ void createVpcWithAnAmazonProvidedBlockReturnsOneAndKeepsIt() throws UnknownHostException {String vpcId = given().formParam("Action", "CreateVpc").formParam("CidrBlock", "10.91.0.0/16")@@ -59,6 +68,7 @@ class Ec2VpcIpv6CidrBlockIntegrationTest {.body("DescribeVpcsResponse.vpcSet.item.ipv6CidrBlockAssociationSet.item.ipv6CidrBlock",matchesRegex(IPV6_CIDR)).extract().path("DescribeVpcsResponse.vpcSet.item.ipv6CidrBlockAssociationSet.item.ipv6CidrBlock");+ assertEquals(canonicalText(created), created);given().formParam("Action", "DescribeVpcs")@@ -72,6 +82,26 @@ class Ec2VpcIpv6CidrBlockIntegrationTest {equalTo(created));}+ /**+ * CreateVpc and AssociateVpcCidrBlock draw their block from one generator, and it comes back in+ * RFC 5952 text: {@code 2600:1f18:a3f:1200::/56}, not {@code 2600:1f18:0a3f:1200::/56}, and+ * {@code 2600:1f18:a3f::/56}, not {@code 2600:1f18:a3f:0::/56}. A random group starts with a+ * zero digit in about one block in eight, but the fourth group is all zeros in only one in 256,+ * so the test draws 4096 blocks in process: the chance that none has an all-zero fourth group+ * is about one in ten million. They are all drawn on one VPC, so the test costs the same+ * however many resources the app already holds. That leans on Floci not enforcing AWS's quota+ * of five IPv6 blocks per VPC (adjustable to 50); if it ever does, spread the draws over VPCs.+ */+ @Test+ void amazonProvidedBlocksAreWrittenInCanonicalForm() throws UnknownHostException {+ String vpcId = service.createVpc("us-east-1", "10.97.0.0/16", false).getVpcId();+ for (int i = 0; i < 4096; i++) {+ String block = service.associateAmazonProvidedIpv6CidrBlock("us-east-1", vpcId).getIpv6CidrBlock();+ assertEquals(canonicalText(block), block);+ }+ service.deleteVpc("us-east-1", vpcId);+ }+@Testvoid aVpcThatDidNotAskForOneHasNoIpv6Block() {given()@@ -149,4 +179,20 @@ class Ec2VpcIpv6CidrBlockIntegrationTest {.body(not(containsString("<ipv6CidrBlock>"))).body("DescribeVpcsResponse.vpcSet.item.cidrBlock", not(emptyOrNullString()));}++ /**+ * The RFC 5952 text of the /56 that {@code block} names. Every byte past the seventh is zero+ * in a /56, so its text is the first four groups without leading zeros, less any zero groups+ * at their end, then "::": the zero groups that end the address are always the longest run,+ * the one RFC 5952 compresses.+ */+ private static String canonicalText(String block) throws UnknownHostException {+ byte[] network = Arrays.copyOf(InetAddress.ofLiteral(block.split("/")[0]).getAddress(), 7);+ String[] groups = InetAddress.getByAddress(Arrays.copyOf(network, 16)).getHostAddress().split(":");+ int kept = 4;+ while (kept > 0 && groups[kept - 1].equals("0")) {+ kept--;+ }+ return String.join(":", Arrays.copyOf(groups, kept)) + "::/56";+ }}