mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): reject subnet IPv4 prefixes outside /16 to /28 (#5439)
* fix(ec2): reject subnet IPv4 prefixes outside /16 to /28 * test(ec2): store the /30 endpoint subnet directly and quote the guide's subnet wording CreateSubnet now refuses prefixes outside /16 to /28, so the interface endpoint test seeds its /30 subnet through the subnet store, as the stored-endpoint test does. Quote the VPC guide's subnet sentence rather than its VPC block sentence.
This commit is contained in:
1 parent
22c4c32e3a
commit
b1bb016b6e
2 files changed
+65
-4
No files matched your search
@@ -5625,6 +5625,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
+ "IPv6 CIDR block. VPC " + vpcId + " has none.", 400);
|
||||
}
|
||||
|
||||
rejectOutOfRangeSubnetPrefix(cidrBlock);
|
||||
|
||||
String zoneName = resolveSubnetZoneName(region, availabilityZone, availabilityZoneId);
|
||||
|
||||
String subnetId = "subnet-" + randomHex(8);
|
||||
@@ -5687,6 +5689,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The VPC User Guide limits an IPv4 subnet to a prefix between /16 and /28 ("The allowed IPv4
|
||||
* CIDR block size for a subnet is between a /28 netmask and /16 netmask."), and AWS answers
|
||||
* anything else with InvalidSubnet.Range. Requests with no IPv4 CIDR, or one that does not parse, are left to the
|
||||
* checks that already handle them.
|
||||
*/
|
||||
private void rejectOutOfRangeSubnetPrefix(String cidrBlock) {
|
||||
int[] range = cidrRange(cidrBlock);
|
||||
if (range == null) {
|
||||
return;
|
||||
}
|
||||
int prefix = range[1];
|
||||
if (prefix < 16 || prefix > 28) {
|
||||
throw new AwsException("InvalidSubnet.Range", "The CIDR '" + cidrBlock + "' is invalid.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS refuses a subnet whose IPv4 CIDR overlaps another subnet's in the same VPC. CreateSubnet
|
||||
* carries no idempotency token, so this check is also what stops an SDK transport retry after
|
||||
|
||||
@@ -718,6 +718,40 @@ class Ec2ServiceTest {
|
||||
.filter(s -> vpc.getVpcId().equals(s.getVpcId())).count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSubnetRejectsIpv4PrefixOutsideSlash16ToSlash28() {
|
||||
// VPC User Guide: "The allowed IPv4 CIDR block size for a subnet is between a /28 netmask and /16 netmask."
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
String vpcId = service.createVpc("us-east-1", "10.0.0.0/8", false).getVpcId();
|
||||
|
||||
for (String cidr : List.of("10.0.0.0/15", "10.2.0.0/29", "10.2.0.16/30", "10.2.0.32/32")) {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.createSubnet("us-east-1", vpcId, cidr, "us-east-1a"));
|
||||
assertEquals("InvalidSubnet.Range", error.getErrorCode());
|
||||
assertEquals("The CIDR '" + cidr + "' is invalid.", error.getMessage());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
}
|
||||
assertEquals(0, service.describeSubnets("us-east-1", List.of(), Map.of()).stream()
|
||||
.filter(s -> vpcId.equals(s.getVpcId())).count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSubnetAcceptsTheSlash16AndSlash28PrefixBounds() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
String vpcId = service.createVpc("us-east-1", "10.0.0.0/8", false).getVpcId();
|
||||
|
||||
assertEquals("10.1.0.0/16", service.createSubnet("us-east-1", vpcId, "10.1.0.0/16", "us-east-1a")
|
||||
.getCidrBlock());
|
||||
assertEquals("10.2.0.0/28", service.createSubnet("us-east-1", vpcId, "10.2.0.0/28", "us-east-1a")
|
||||
.getCidrBlock());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createVpcAndCreateSubnetStoreTheCidrBlockInCanonicalForm() {
|
||||
// AWS CreateVpc/CreateSubnet: "We modify the specified CIDR block to its canonical form; for
|
||||
@@ -1579,12 +1613,20 @@ class Ec2ServiceTest {
|
||||
|
||||
@Test
|
||||
void interfaceEndpointIsRefusedInASubnetWithNoAssignableAddress() {
|
||||
// CreateSubnet refuses a /30, so the subnet is written straight into the subnet store.
|
||||
AccountAwareStorageBackend<Subnet> subnetStore = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory(Map.of("ec2-subnets.json", subnetStore)));
|
||||
String vpcId = service.createVpc("us-east-1", "10.64.0.0/16", false).getVpcId();
|
||||
String tiny = service.createSubnet("us-east-1", vpcId, "10.64.1.0/30", "us-east-1a").getSubnetId();
|
||||
Subnet tinySubnet = new Subnet();
|
||||
tinySubnet.setSubnetId("subnet-0000000000000031");
|
||||
tinySubnet.setVpcId(vpcId);
|
||||
tinySubnet.setRegion("us-east-1");
|
||||
tinySubnet.setCidrBlock("10.64.1.0/30");
|
||||
tinySubnet.setAvailabilityZone("us-east-1a");
|
||||
subnetStore.put("us-east-1::" + tinySubnet.getSubnetId(), tinySubnet);
|
||||
String tiny = tinySubnet.getSubnetId();
|
||||
String roomy = service.createSubnet("us-east-1", vpcId, "10.64.2.0/28", "us-east-1b").getSubnetId();
|
||||
|
||||
AwsException createError = assertThrows(AwsException.class, () ->
|
||||
|
||||
Reference in new issue
Block a user