mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(ec2): normalize subnet IPv6 CIDRs and expose zone network borders (#6387)
Normalize subnet IPv6 responses and expose regional availability-zone network borders. Keep the read-only regional test at a unique execution order. Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
0acb8cd44c
commit
5b24703815
8 files changed
+101
-16
No files matched your search
@@ -229,6 +229,8 @@ class Ec2Tests {
|
||||
void describeAvailabilityZones() {
|
||||
DescribeAvailabilityZonesResponse resp = ec2.describeAvailabilityZones();
|
||||
assertThat(resp.availabilityZones()).hasSize(3);
|
||||
assertThat(resp.availabilityZones()).allSatisfy(zone ->
|
||||
assertThat(zone.networkBorderGroup()).isEqualTo(TestFixtures.region().id()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -58,6 +58,9 @@ python3.9 -m venv .venv-legacy
|
||||
FLOCI_ENDPOINT=http://localhost:4566 .venv-legacy/bin/python -m pytest legacy/
|
||||
```
|
||||
|
||||
`tests/test_ec2_subnet_ipv6.py` checks that expanded IPv6 subnet CIDRs are
|
||||
stored and returned in canonical form through Boto 3.
|
||||
|
||||
## Legacy Query GET
|
||||
|
||||
`legacy/test_elb_query_get.py` checks that public Boto 2 Query GET reads and
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
"""Subnet IPv6 CIDRs remain canonical across SDK create and describe calls."""
|
||||
|
||||
import ipaddress
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize("host_bits", [0, 0xABCD])
|
||||
def test_subnet_ipv6_cidr_is_canonical(ec2_client, host_bits):
|
||||
vpc = ec2_client.create_vpc(
|
||||
CidrBlock="10.94.0.0/16", AmazonProvidedIpv6CidrBlock=True)["Vpc"]
|
||||
subnet_id = None
|
||||
try:
|
||||
vpc_network = ipaddress.IPv6Network(
|
||||
vpc["Ipv6CidrBlockAssociationSet"][0]["Ipv6CidrBlock"])
|
||||
network = next(vpc_network.subnets(new_prefix=64))
|
||||
expanded = (network.network_address + host_bits).exploded.upper() + "/64"
|
||||
subnet = ec2_client.create_subnet(
|
||||
VpcId=vpc["VpcId"], CidrBlock="10.94.1.0/24",
|
||||
Ipv6CidrBlock=expanded)["Subnet"]
|
||||
subnet_id = subnet["SubnetId"]
|
||||
assert subnet["Ipv6CidrBlockAssociationSet"][0]["Ipv6CidrBlock"] == str(network)
|
||||
described = ec2_client.describe_subnets(SubnetIds=[subnet_id])["Subnets"]
|
||||
assert len(described) == 1
|
||||
assert described[0]["Ipv6CidrBlockAssociationSet"] == subnet["Ipv6CidrBlockAssociationSet"]
|
||||
assert described[0]["CidrBlock"] == "10.94.1.0/24"
|
||||
finally:
|
||||
if subnet_id is not None:
|
||||
ec2_client.delete_subnet(SubnetId=subnet_id)
|
||||
ec2_client.delete_vpc(VpcId=vpc["VpcId"])
|
||||
@@ -833,6 +833,9 @@ allocated address.
|
||||
| DescribeRegions | Returns the regions known to the local EC2 service. |
|
||||
| DescribeAccountAttributes | Returns local account-level EC2 attributes. |
|
||||
|
||||
The modeled zones are standard Availability Zones. Each reports `NetworkBorderGroup` as its
|
||||
Region, such as `eu-west-1`. Local Zones and Wavelength Zones are not modeled.
|
||||
|
||||
A zone id has AWS's shape, a short region prefix and `-az<n>`: `use1-az1` in `us-east-1`, `apne1-az1`
|
||||
in `ap-northeast-1`, `usgw1-az1` in `us-gov-west-1`. On AWS the pairing of a zone name with a zone id
|
||||
differs per account; Floci always pairs zone `a` with `az1`, `b` with `az2`, and so on. Subnets, VPC
|
||||
|
||||
@@ -4582,6 +4582,7 @@ public class Ec2QueryHandler {
|
||||
.elem("zoneName", az.get("zoneName"))
|
||||
.elem("zoneState", az.get("state"))
|
||||
.elem("regionName", az.get("regionName"))
|
||||
.elem("networkBorderGroup", az.get("networkBorderGroup"))
|
||||
.elem("zoneId", az.get("zoneId"))
|
||||
.elem("groupName", az.get("groupName"))
|
||||
.elem("groupLongName", az.get("groupLongName"))
|
||||
|
||||
@@ -6994,8 +6994,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
subnet.setRegion(region);
|
||||
subnet.setSubnetArn(AwsArnUtils.Arn.of("ec2", region, ownerAccountId, "subnet/" + subnetId).toString());
|
||||
if (ipv6CidrBlock != null && !ipv6CidrBlock.isBlank()) {
|
||||
String canonicalIpv6CidrBlock = CidrCanonicalizer.canonicalize(ipv6CidrBlock)
|
||||
.filter(cidr -> cidr.contains(":")).orElse(ipv6CidrBlock);
|
||||
subnet.getIpv6CidrBlockAssociationSet().add(new VpcIpv6CidrBlockAssociation(
|
||||
"subnet-cidr-assoc-" + randomHex(17), ipv6CidrBlock, null));
|
||||
"subnet-cidr-assoc-" + randomHex(17), canonicalIpv6CidrBlock, null));
|
||||
}
|
||||
// The conflict scan and the store must be one step under the VPC's lock, or two
|
||||
// overlapping creates in flight together both pass the scan before either is stored.
|
||||
@@ -10636,8 +10638,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* makes the CreateSubnet overlap check see the real network. In every case it is also why the
|
||||
* Describe calls echo back the canonical form: the stored resource never holds anything else.
|
||||
*
|
||||
* <p>Only IPv4 blocks are rewritten. DestinationIpv6CidrBlock and the IPv6 blocks of VPCs and
|
||||
* subnets are left untouched, as is DestinationPrefixListId, which is an opaque ID, not a CIDR.
|
||||
* <p>This helper rewrites only IPv4 blocks. IPv6 values and DestinationPrefixListId, which is
|
||||
* an opaque ID rather than a CIDR, are returned unchanged.
|
||||
* A null block (an IPv6-only subnet) is returned as is.
|
||||
*
|
||||
* <p>A block that is not a well-formed "IPv4/prefix" is returned unchanged: this method does
|
||||
@@ -11964,6 +11966,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
az.put("zoneName", zoneName);
|
||||
az.put("state", "available");
|
||||
az.put("regionName", region);
|
||||
az.put("networkBorderGroup", region);
|
||||
az.put("zoneId", AvailabilityZoneIds.forZoneName(region, zoneName));
|
||||
az.put("zoneType", "availability-zone");
|
||||
az.put("optInStatus", "opt-in-not-required");
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -160,10 +161,34 @@ class Ec2IntegrationTest {
|
||||
.statusCode(200)
|
||||
.contentType("application/xml")
|
||||
.body("DescribeAvailabilityZonesResponse.availabilityZoneInfo.item.size()", equalTo(3))
|
||||
.body("DescribeAvailabilityZonesResponse.availabilityZoneInfo.item.networkBorderGroup",
|
||||
everyItem(equalTo("us-east-1")))
|
||||
.body("DescribeAvailabilityZonesResponse.availabilityZoneInfo.item[0].zoneName",
|
||||
startsWith("us-east-1"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"eu-west-1", "us-gov-west-1", "cn-north-1"})
|
||||
@Order(337)
|
||||
void availabilityZoneBorderGroupUsesTheRequestedRegion(String region) {
|
||||
given()
|
||||
.formParam("Action", "DescribeAvailabilityZones")
|
||||
.formParam("Version", "2016-11-15")
|
||||
.formParam("ZoneName.1", region + "b")
|
||||
.header("Authorization", AUTH_HEADER.replace("us-east-1", region))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeAvailabilityZonesResponse.availabilityZoneInfo.item.size()", equalTo(1))
|
||||
.body("DescribeAvailabilityZonesResponse.availabilityZoneInfo.item[0].networkBorderGroup",
|
||||
equalTo(region))
|
||||
.body("DescribeAvailabilityZonesResponse.availabilityZoneInfo.item[0].zoneName",
|
||||
equalTo(region + "b"))
|
||||
.body("DescribeAvailabilityZonesResponse.availabilityZoneInfo.item[0].zoneType",
|
||||
equalTo("availability-zone"));
|
||||
}
|
||||
|
||||
// DescribeAvailabilityZones parameter validation and filtering
|
||||
@Test
|
||||
@Order(5)
|
||||
|
||||
+31
-13
@@ -1,28 +1,38 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* CreateSubnet accepted an Ipv6CidrBlock parameter and silently dropped it: the response always
|
||||
* carried an empty ipv6CidrBlockAssociationSet, so a Terraform aws_subnet with an ipv6_cidr_block
|
||||
* never converged the same way the VPC-level case fixed in
|
||||
* {@link Ec2VpcIpv6CidrBlockIntegrationTest} needed fixing.
|
||||
*/
|
||||
/** Subnet IPv6 associations retain canonical CIDRs across creation and describe calls. */
|
||||
@QuarkusTest
|
||||
class Ec2SubnetIpv6CidrBlockIntegrationTest {
|
||||
|
||||
private static final String AUTH_HEADER =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
|
||||
|
||||
@Test
|
||||
void createSubnetWithAnIpv6CidrBlockReturnsItAndKeepsIt() {
|
||||
String vpcId = given()
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"::/64, false",
|
||||
":0000:0000:0000:0000/64, false",
|
||||
":0000:0000:0000:0000/64, true",
|
||||
":0:0:0:abcd/64, false"
|
||||
})
|
||||
void createSubnetReturnsAndKeepsCanonicalIpv6Cidr(String suffix, boolean uppercase)
|
||||
throws UnknownHostException {
|
||||
Response vpc = given()
|
||||
.formParam("Action", "CreateVpc")
|
||||
.formParam("CidrBlock", "10.94.0.0/16")
|
||||
.formParam("AmazonProvidedIpv6CidrBlock", "true")
|
||||
@@ -31,20 +41,28 @@ class Ec2SubnetIpv6CidrBlockIntegrationTest {
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("CreateVpcResponse.vpc.vpcId");
|
||||
.extract().response();
|
||||
String vpcId = vpc.path("CreateVpcResponse.vpc.vpcId");
|
||||
String vpcCidr = vpc.path("CreateVpcResponse.vpc.ipv6CidrBlockAssociationSet.item.ipv6CidrBlock");
|
||||
byte[] address = InetAddress.ofLiteral(vpcCidr.split("/")[0]).getAddress();
|
||||
address[7] = 1; // Select subnet 1 within the allocated /56, with a nonzero fourth group.
|
||||
String prefix = String.join(":", Arrays.copyOf(
|
||||
InetAddress.getByAddress(address).getHostAddress().split(":"), 4));
|
||||
String expectedCidr = prefix + "::/64";
|
||||
String requestedCidr = uppercase ? (prefix + suffix).toUpperCase(Locale.ROOT) : prefix + suffix;
|
||||
|
||||
String subnetId = given()
|
||||
.formParam("Action", "CreateSubnet")
|
||||
.formParam("VpcId", vpcId)
|
||||
.formParam("CidrBlock", "10.94.1.0/24")
|
||||
.formParam("Ipv6CidrBlock", "2600:1f18:1::/64")
|
||||
.formParam("Ipv6CidrBlock", requestedCidr)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("CreateSubnetResponse.subnet.ipv6CidrBlockAssociationSet.item.ipv6CidrBlock",
|
||||
equalTo("2600:1f18:1::/64"))
|
||||
equalTo(expectedCidr))
|
||||
.body("CreateSubnetResponse.subnet.ipv6CidrBlockAssociationSet.item.ipv6CidrBlockState.state",
|
||||
equalTo("associated"))
|
||||
.extract().path("CreateSubnetResponse.subnet.subnetId");
|
||||
@@ -58,7 +76,7 @@ class Ec2SubnetIpv6CidrBlockIntegrationTest {
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeSubnetsResponse.subnetSet.item.ipv6CidrBlockAssociationSet.item.ipv6CidrBlock",
|
||||
equalTo("2600:1f18:1::/64"));
|
||||
equalTo(expectedCidr));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user