mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): reject VPC IPv4 prefixes outside /16 to /28 (#5495)
* fix(ec2): reject VPC IPv4 prefixes outside /16 to /28 CreateVpc and AssociateVpcCidrBlock took any IPv4 prefix, while the VPC User Guide allows only /16 to /28 for VPC blocks and AWS answers anything else with InvalidVpc.Range. The EKS test that needs a VPC block covering both default service CIDRs now widens a valid VPC in the store instead. * refactor(ec2): share one prefix check, seed /8 VPC in subnet tests
This commit is contained in:
1 parent
255ad96168
commit
162ac6dea7
3 files changed
+86
-15
No files matched your search
@@ -4658,6 +4658,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
boolean amazonProvidedIpv6CidrBlock, String instanceTenancy) {
|
||||
// AWS stores the CIDR in canonical form: "100.68.0.18/18" becomes "100.68.0.0/18".
|
||||
String cidrBlock = canonicalizeIpv4Cidr(requestedCidrBlock);
|
||||
rejectOutOfRangePrefix(cidrBlock, "InvalidVpc.Range");
|
||||
ensureDefaultResources(region);
|
||||
String vpcId = "vpc-" + randomHex(8);
|
||||
Vpc vpc = new Vpc();
|
||||
@@ -4864,6 +4865,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
|
||||
public VpcCidrBlockAssociation associateVpcCidrBlock(String region, String vpcId, String cidrBlock) {
|
||||
rejectOutOfRangePrefix(cidrBlock, "InvalidVpc.Range");
|
||||
ensureDefaultResources(region);
|
||||
synchronized (attachmentTopologyLock(region)) {
|
||||
synchronized (lockFor(key(region, vpcId))) {
|
||||
@@ -5684,7 +5686,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
+ "IPv6 CIDR block. VPC " + vpcId + " has none.", 400);
|
||||
}
|
||||
|
||||
rejectOutOfRangeSubnetPrefix(cidrBlock);
|
||||
rejectOutOfRangePrefix(cidrBlock, "InvalidSubnet.Range");
|
||||
|
||||
String zoneName = resolveSubnetZoneName(region, availabilityZone, availabilityZoneId);
|
||||
|
||||
@@ -5749,19 +5751,20 @@ 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.
|
||||
* The VPC User Guide limits an IPv4 VPC block, primary or secondary, and an IPv4 subnet to a
|
||||
* prefix between /16 and /28 ("The allowed block size is between a /28 netmask and /16
|
||||
* netmask."), and AWS answers anything else with InvalidVpc.Range or InvalidSubnet.Range, which
|
||||
* the caller passes as {@code errorCode}. 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) {
|
||||
private void rejectOutOfRangePrefix(String cidrBlock, String errorCode) {
|
||||
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);
|
||||
throw new AwsException(errorCode, "The CIDR '" + cidrBlock + "' is invalid.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -767,11 +767,12 @@ class Ec2ServiceTest {
|
||||
@Test
|
||||
void createSubnetRejectsIpv4PrefixOutsideSlash16ToSlash28() {
|
||||
// VPC User Guide: "The allowed IPv4 CIDR block size for a subnet is between a /28 netmask and /16 netmask."
|
||||
AccountAwareStorageBackend<Vpc> vpcStore = 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());
|
||||
String vpcId = service.createVpc("us-east-1", "10.0.0.0/8", false).getVpcId();
|
||||
new InMemoryStorageFactory(Map.of("ec2-vpcs.json", vpcStore)));
|
||||
String vpcId = slashEightVpc(service, vpcStore);
|
||||
|
||||
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,
|
||||
@@ -786,11 +787,12 @@ class Ec2ServiceTest {
|
||||
|
||||
@Test
|
||||
void createSubnetAcceptsTheSlash16AndSlash28PrefixBounds() {
|
||||
AccountAwareStorageBackend<Vpc> vpcStore = 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());
|
||||
String vpcId = service.createVpc("us-east-1", "10.0.0.0/8", false).getVpcId();
|
||||
new InMemoryStorageFactory(Map.of("ec2-vpcs.json", vpcStore)));
|
||||
String vpcId = slashEightVpc(service, vpcStore);
|
||||
|
||||
assertEquals("10.1.0.0/16", service.createSubnet("us-east-1", vpcId, "10.1.0.0/16", "us-east-1a")
|
||||
.getCidrBlock());
|
||||
@@ -798,6 +800,14 @@ class Ec2ServiceTest {
|
||||
.getCidrBlock());
|
||||
}
|
||||
|
||||
/** CreateVpc refuses a /8, so the VPC is created as a /16 and widened to 10.0.0.0/8 in the store. */
|
||||
private static String slashEightVpc(Ec2Service service, AccountAwareStorageBackend<Vpc> vpcStore) {
|
||||
Vpc vpc = service.createVpc("us-east-1", "10.0.0.0/16", false);
|
||||
vpc.setCidrBlock("10.0.0.0/8");
|
||||
vpcStore.put("us-east-1::" + vpc.getVpcId(), vpc);
|
||||
return vpc.getVpcId();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createVpcAndCreateSubnetStoreTheCidrBlockInCanonicalForm() {
|
||||
// AWS CreateVpc/CreateSubnet: "We modify the specified CIDR block to its canonical form; for
|
||||
@@ -904,6 +914,48 @@ class Ec2ServiceTest {
|
||||
() -> service.createSubnet("us-east-1", vpcId, "192.168.2.0/24", "us-east-1a")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createVpcAndAssociateVpcCidrBlockRejectIpv4PrefixOutsideSlash16ToSlash28() {
|
||||
// VPC User Guide: "The allowed block size 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());
|
||||
long vpcsBefore = service.describeVpcs("us-east-1", List.of(), Map.of()).size();
|
||||
String vpcId = service.createVpc("us-east-1", "10.0.0.0/16", false).getVpcId();
|
||||
|
||||
for (String cidr : List.of("0.0.0.0/0", "10.0.0.0/8", "10.0.0.0/15", "10.0.0.0/29", "10.0.0.0/32")) {
|
||||
AwsException createError = assertThrows(AwsException.class,
|
||||
() -> service.createVpc("us-east-1", cidr, false));
|
||||
assertEquals("InvalidVpc.Range", createError.getErrorCode());
|
||||
assertEquals("The CIDR '" + cidr + "' is invalid.", createError.getMessage());
|
||||
assertEquals(400, createError.getHttpStatus());
|
||||
|
||||
AwsException associateError = assertThrows(AwsException.class,
|
||||
() -> service.associateVpcCidrBlock("us-east-1", vpcId, cidr));
|
||||
assertEquals("InvalidVpc.Range", associateError.getErrorCode());
|
||||
assertEquals(400, associateError.getHttpStatus());
|
||||
}
|
||||
assertEquals(vpcsBefore + 1, service.describeVpcs("us-east-1", List.of(), Map.of()).size());
|
||||
assertEquals(1, service.describeVpcs("us-east-1", List.of(vpcId), Map.of()).getFirst()
|
||||
.getCidrBlockAssociationSet().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createVpcAndAssociateVpcCidrBlockAcceptTheSlash16AndSlash28PrefixBounds() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
|
||||
|
||||
assertEquals("10.1.0.0/16", service.createVpc("us-east-1", "10.1.0.0/16", false).getCidrBlock());
|
||||
Vpc smallest = service.createVpc("us-east-1", "10.2.0.0/28", false);
|
||||
assertEquals("10.2.0.0/28", smallest.getCidrBlock());
|
||||
// Secondary blocks stay in 10.0.0.0/8, the range AWS lets a 10.x VPC add from.
|
||||
assertEquals("10.3.0.0/16", service.associateVpcCidrBlock("us-east-1", smallest.getVpcId(),
|
||||
"10.3.0.0/16").getCidrBlock());
|
||||
assertEquals("10.4.0.0/28", service.associateVpcCidrBlock("us-east-1", smallest.getVpcId(),
|
||||
"10.4.0.0/28").getCidrBlock());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cidrAssociationEditsWaitForSubnetContainmentValidation() throws Exception {
|
||||
for (boolean remove : List.of(false, true)) {
|
||||
|
||||
@@ -21,6 +21,7 @@ import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
|
||||
import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Tag;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Vpc;
|
||||
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
|
||||
import io.github.hectorvent.floci.services.eks.model.Cluster;
|
||||
import io.github.hectorvent.floci.services.eks.model.ClusterIdentity;
|
||||
@@ -49,6 +50,7 @@ import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -169,6 +171,10 @@ class EksServiceTest {
|
||||
}
|
||||
|
||||
private Ec2Service realEc2Service() {
|
||||
return realEc2Service(new HashMap<>());
|
||||
}
|
||||
|
||||
private Ec2Service realEc2Service(Map<String, AccountAwareStorageBackend<?>> stores) {
|
||||
EmulatorConfig ec2Config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.Ec2ServiceConfig ec2ServiceConfig = mock(EmulatorConfig.Ec2ServiceConfig.class);
|
||||
@@ -181,7 +187,9 @@ class EksServiceTest {
|
||||
@Override
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return AccountAwareStorageBackend.inMemory("000000000000");
|
||||
AccountAwareStorageBackend<V> store = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
stores.put(fileName, store);
|
||||
return store;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -2227,10 +2235,18 @@ class EksServiceTest {
|
||||
|
||||
@Test
|
||||
void createClusterWithBothDefaultServiceCidrsOverlappingVpcThrowsInvalidParameterException() {
|
||||
Ec2Service ec2 = realEc2Service();
|
||||
// No VPC block CreateVpc accepts (/16 to /28) covers both defaults, so the VPC is created
|
||||
// normally and its block is then widened in the store, as state stored before that check is.
|
||||
Map<String, AccountAwareStorageBackend<?>> ec2Stores = new HashMap<>();
|
||||
Ec2Service ec2 = realEc2Service(ec2Stores);
|
||||
ec2.ensureDefaultResources("us-east-1");
|
||||
String vpcId = ec2.createVpc("us-east-1", "0.0.0.0/0", false).getVpcId();
|
||||
String subnetId = ec2.createSubnet("us-east-1", vpcId, "0.0.0.0/24", "us-east-1a").getSubnetId();
|
||||
Vpc vpc = ec2.createVpc("us-east-1", "10.100.0.0/16", false);
|
||||
String vpcId = vpc.getVpcId();
|
||||
String subnetId = ec2.createSubnet("us-east-1", vpcId, "10.100.0.0/24", "us-east-1a").getSubnetId();
|
||||
vpc.setCidrBlock("0.0.0.0/0");
|
||||
@SuppressWarnings("unchecked")
|
||||
AccountAwareStorageBackend<Vpc> vpcStore = (AccountAwareStorageBackend<Vpc>) ec2Stores.get("ec2-vpcs.json");
|
||||
vpcStore.put("us-east-1::" + vpcId, vpc);
|
||||
|
||||
EksService service = new EksService(storageFactory, testConfig(), regionResolver, null, ec2,
|
||||
new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class),
|
||||
|
||||
Reference in new issue
Block a user