mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): store and validate CreateVpcEndpoint SubnetConfiguration (#3491)
* fix(ec2): store and validate CreateVpcEndpoint SubnetConfiguration CreateVpcEndpoint accepted SubnetConfiguration.N and dropped it, so an interface endpoint took floci's own synthesized address in each subnet rather than the one the caller pinned. Terraform's aws_vpc_endpoint sends the subnet_configuration block on create and reads it back by following the endpoint to its network interfaces, so every plan after the apply saw an address it never asked for and proposed replacing the endpoint. SubnetConfiguration is request-only in ec2/2016-11-15. No output shape carries it, so the addresses are stored on the endpoint and consumed when its interfaces are built, and DescribeVpcEndpoints echoes nothing new. Ec2QueryHandler parses the flattened SubnetConfiguration.N list for CreateVpcEndpoint and for ModifyVpcEndpoint, which takes the same parameter. Ec2Service validates each entry against the named subnet, rejecting an IPv4 address outside its CIDR with InvalidParameterValue, and keeps the pinned address on the endpoint's interface in that subnet. A subnet named only through its configuration joins the endpoint's subnet set. Ipv6 is stored as given, since floci's subnets carry no IPv6 CIDR to validate against and its network interfaces have no IPv6 field yet. Adds Ec2VpcEndpointSubnetConfigurationIntegrationTest over the query wire and three Ec2ServiceTest cases for the create, modify and rejection paths. * fix(ec2): reject the subnet-reserved addresses in SubnetConfiguration validateSubnetConfigurations only checked that SubnetConfiguration.Ipv4 parsed and fell inside the named subnet's CIDR. For a 10.61.1.0/24 subnet that let 10.61.1.0, .1, .2, .3 and .255 through, and each became the private address of the endpoint's network interface. AWS keeps those five for itself, so none of them can be assigned to a resource and no real endpoint could ever report one. The rule is in ec2/2016-11-15 itself, in the CreateSubnet documentation: "Amazon Web Services reserves both the first four and the last IPv4 address in each subnet's CIDR block. They're not available for your use." CreateSecondarySubnet says the same for secondary subnets. No constraint key carries it, and no shape in the model states it for SubnetConfiguration. Ipv4Cidrs.isSubnetReserved is new; the EC2 service had no reserved-address helper to reuse. VpcNetworkManager.firstHostOffset looks close but is not the same rule: it reserves ten addresses of Docker-network headroom at the bottom of a block and nothing at the top, for allocation readability rather than AWS semantics. Small blocks need no special case. A /28, the smallest subnet AWS accepts, keeps 11 of its 16 addresses and a /29 keeps three. Both ends are clamped so they cannot cross in a block of four addresses or fewer, where every address is reserved. The check sits in the shared validator, so CreateVpcEndpoint and ModifyVpcEndpoint both refuse a reserved address with the InvalidParameterValue the containment check already raises. Tests cover all five reserved addresses and one ordinary host address on the create path, the modify path, a /28 subnet, and the query wire. --------- Co-authored-by: lex00 <artigues.dev@gmail.com>
This commit is contained in:
1 parent
fe40e06938
commit
5d0b0b44b1
9 files changed
+650
-12
No files matched your search
@@ -43,6 +43,7 @@ import io.github.hectorvent.floci.services.ec2.model.TransitGatewayVpcAttachment
|
||||
import io.github.hectorvent.floci.services.ec2.model.TransitGatewayVpcAttachmentOptions;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Vpc;
|
||||
import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint;
|
||||
import io.github.hectorvent.floci.services.ec2.model.VpcEndpointSubnetConfiguration;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Volume;
|
||||
import io.github.hectorvent.floci.services.ec2.model.VolumeAttachment;
|
||||
import org.bouncycastle.openssl.PEMKeyPair;
|
||||
@@ -53,6 +54,7 @@ import org.junit.jupiter.api.Test;
|
||||
import java.io.StringReader;
|
||||
import java.security.interfaces.RSAPrivateCrtKey;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -748,6 +750,165 @@ class Ec2ServiceTest {
|
||||
"endpoints are regional");
|
||||
}
|
||||
|
||||
@Test
|
||||
void subnetConfigurationPinsTheEndpointInterfaceAddress() {
|
||||
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.60.0.0/16", false).getVpcId();
|
||||
String pinned = service.createSubnet("us-east-1", vpcId, "10.60.1.0/24", "us-east-1a").getSubnetId();
|
||||
String unpinned = service.createSubnet("us-east-1", vpcId, "10.60.2.0/24", "us-east-1b").getSubnetId();
|
||||
|
||||
VpcEndpoint endpoint = service.createVpcEndpoint("us-east-1", vpcId,
|
||||
"com.amazonaws.us-east-1.ecs", "Interface",
|
||||
List.of(), List.of(pinned, unpinned), List.of(), null, null, List.of(),
|
||||
List.of(new VpcEndpointSubnetConfiguration(pinned, "10.60.1.10", "2600:1f18::10")));
|
||||
|
||||
Map<String, String> addresses = endpointAddressesBySubnet(service);
|
||||
assertEquals("10.60.1.10", addresses.get(pinned),
|
||||
"the pinned address must be the one the interface reports");
|
||||
assertNotEquals("10.60.2.10", addresses.get(unpinned),
|
||||
"a subnet with no configuration keeps the synthesized address");
|
||||
assertEquals(addresses, endpointAddressesBySubnet(service),
|
||||
"a second read must answer with the same addresses");
|
||||
assertEquals("2600:1f18::10", endpoint.getSubnetConfigurations().getFirst().getIpv6(),
|
||||
"Ipv6 is stored even though no floci interface field carries it yet");
|
||||
}
|
||||
|
||||
@Test
|
||||
void subnetConfigurationRejectsAnAddressOutsideTheSubnet() {
|
||||
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.61.0.0/16", false).getVpcId();
|
||||
String subnetId = service.createSubnet("us-east-1", vpcId, "10.61.1.0/24", "us-east-1a").getSubnetId();
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () ->
|
||||
service.createVpcEndpoint("us-east-1", vpcId, "com.amazonaws.us-east-1.ecs", "Interface",
|
||||
List.of(), List.of(subnetId), List.of(), null, null, List.of(),
|
||||
List.of(new VpcEndpointSubnetConfiguration(subnetId, "10.61.9.10", null))));
|
||||
assertEquals("InvalidParameterValue", error.getErrorCode());
|
||||
assertTrue(service.describeVpcEndpoints("us-east-1", List.of(), Map.of()).isEmpty(),
|
||||
"a rejected configuration must not leave an endpoint behind");
|
||||
}
|
||||
|
||||
@Test
|
||||
void subnetConfigurationRejectsTheAddressesAwsReservesInTheSubnet() {
|
||||
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.63.0.0/16", false).getVpcId();
|
||||
String subnetId = service.createSubnet("us-east-1", vpcId, "10.63.1.0/24", "us-east-1a").getSubnetId();
|
||||
|
||||
for (String reserved : List.of("10.63.1.0", "10.63.1.1", "10.63.1.2", "10.63.1.3", "10.63.1.255")) {
|
||||
AwsException error = assertThrows(AwsException.class, () ->
|
||||
service.createVpcEndpoint("us-east-1", vpcId, "com.amazonaws.us-east-1.ecs", "Interface",
|
||||
List.of(), List.of(subnetId), List.of(), null, null, List.of(),
|
||||
List.of(new VpcEndpointSubnetConfiguration(subnetId, reserved, null))), reserved);
|
||||
assertEquals("InvalidParameterValue", error.getErrorCode(), reserved);
|
||||
assertTrue(error.getMessage().contains(reserved), reserved);
|
||||
}
|
||||
assertTrue(service.describeVpcEndpoints("us-east-1", List.of(), Map.of()).isEmpty(),
|
||||
"none of the reserved addresses may leave an endpoint behind");
|
||||
|
||||
VpcEndpoint endpoint = service.createVpcEndpoint("us-east-1", vpcId,
|
||||
"com.amazonaws.us-east-1.ecs", "Interface",
|
||||
List.of(), List.of(subnetId), List.of(), null, null, List.of(),
|
||||
List.of(new VpcEndpointSubnetConfiguration(subnetId, "10.63.1.4", null)));
|
||||
assertEquals("10.63.1.4", endpointAddressesBySubnet(service).get(subnetId),
|
||||
"the first host address above the reserved four is assignable");
|
||||
assertNotNull(endpoint.getVpcEndpointId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void subnetConfigurationKeepsTheUsableHostsOfASmallSubnet() {
|
||||
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.64.0.0/16", false).getVpcId();
|
||||
// /28 is the smallest subnet AWS accepts, and five of its sixteen addresses are reserved.
|
||||
String subnetId = service.createSubnet("us-east-1", vpcId, "10.64.1.16/28", "us-east-1a").getSubnetId();
|
||||
|
||||
for (String reserved : List.of("10.64.1.16", "10.64.1.17", "10.64.1.18", "10.64.1.19", "10.64.1.31")) {
|
||||
AwsException error = assertThrows(AwsException.class, () ->
|
||||
service.createVpcEndpoint("us-east-1", vpcId, "com.amazonaws.us-east-1.ecs", "Interface",
|
||||
List.of(), List.of(subnetId), List.of(), null, null, List.of(),
|
||||
List.of(new VpcEndpointSubnetConfiguration(subnetId, reserved, null))), reserved);
|
||||
assertEquals("InvalidParameterValue", error.getErrorCode(), reserved);
|
||||
}
|
||||
|
||||
service.createVpcEndpoint("us-east-1", vpcId, "com.amazonaws.us-east-1.ecs", "Interface",
|
||||
List.of(), List.of(subnetId), List.of(), null, null, List.of(),
|
||||
List.of(new VpcEndpointSubnetConfiguration(subnetId, "10.64.1.30", null)));
|
||||
assertEquals("10.64.1.30", endpointAddressesBySubnet(service).get(subnetId),
|
||||
"the address below the broadcast address is still a host address");
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyVpcEndpointRejectsAReservedSubnetConfigurationAddress() {
|
||||
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.65.0.0/16", false).getVpcId();
|
||||
String subnetId = service.createSubnet("us-east-1", vpcId, "10.65.1.0/24", "us-east-1a").getSubnetId();
|
||||
VpcEndpoint endpoint = service.createVpcEndpoint("us-east-1", vpcId,
|
||||
"com.amazonaws.us-east-1.ecs", "Interface",
|
||||
List.of(), List.of(subnetId), List.of(), null, null, List.of(),
|
||||
List.of(new VpcEndpointSubnetConfiguration(subnetId, "10.65.1.10", null)));
|
||||
|
||||
for (String reserved : List.of("10.65.1.0", "10.65.1.1", "10.65.1.2", "10.65.1.3", "10.65.1.255")) {
|
||||
AwsException error = assertThrows(AwsException.class, () ->
|
||||
service.modifyVpcEndpoint("us-east-1", endpoint.getVpcEndpointId(),
|
||||
List.of(), List.of(), List.of(), List.of(), List.of(), List.of(), null, null, null,
|
||||
List.of(new VpcEndpointSubnetConfiguration(subnetId, reserved, null))), reserved);
|
||||
assertEquals("InvalidParameterValue", error.getErrorCode(), reserved);
|
||||
}
|
||||
assertEquals("10.65.1.10", endpointAddressesBySubnet(service).get(subnetId),
|
||||
"a rejected modify must leave the pinned address untouched");
|
||||
|
||||
service.modifyVpcEndpoint("us-east-1", endpoint.getVpcEndpointId(),
|
||||
List.of(), List.of(), List.of(), List.of(), List.of(), List.of(), null, null, null,
|
||||
List.of(new VpcEndpointSubnetConfiguration(subnetId, "10.65.1.254", null)));
|
||||
assertEquals("10.65.1.254", endpointAddressesBySubnet(service).get(subnetId),
|
||||
"an ordinary host address still goes through");
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyVpcEndpointReplacesTheConfigurationForOneSubnet() {
|
||||
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.62.0.0/16", false).getVpcId();
|
||||
String subnetId = service.createSubnet("us-east-1", vpcId, "10.62.1.0/24", "us-east-1a").getSubnetId();
|
||||
VpcEndpoint endpoint = service.createVpcEndpoint("us-east-1", vpcId,
|
||||
"com.amazonaws.us-east-1.ecs", "Interface",
|
||||
List.of(), List.of(subnetId), List.of(), null, null, List.of(),
|
||||
List.of(new VpcEndpointSubnetConfiguration(subnetId, "10.62.1.10", null)));
|
||||
|
||||
service.modifyVpcEndpoint("us-east-1", endpoint.getVpcEndpointId(),
|
||||
List.of(), List.of(), List.of(), List.of(), List.of(), List.of(), null, null, null,
|
||||
List.of(new VpcEndpointSubnetConfiguration(subnetId, "10.62.1.40", null)));
|
||||
|
||||
assertEquals(1, service.describeVpcEndpoints("us-east-1", List.of(endpoint.getVpcEndpointId()), Map.of())
|
||||
.getFirst().getSubnetConfigurations().size(),
|
||||
"a second configuration for the same subnet replaces the first");
|
||||
assertEquals("10.62.1.40", endpointAddressesBySubnet(service).get(subnetId));
|
||||
}
|
||||
|
||||
private static Map<String, String> endpointAddressesBySubnet(Ec2Service service) {
|
||||
Map<String, String> addresses = new HashMap<>();
|
||||
for (NetworkInterface eni : service.endpointNetworkInterfaces("us-east-1")) {
|
||||
addresses.put(eni.getSubnetId(), eni.getPrivateIpAddress());
|
||||
}
|
||||
return addresses;
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyInstanceGroupsReassignsSecurityGroupsOnInstanceAndEni() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
|
||||
+240
@@ -0,0 +1,240 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.services.ec2.model.NetworkInterface;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.xml.HasXPath.hasXPath;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* CreateVpcEndpoint and ModifyVpcEndpoint accept {@code SubnetConfiguration.N}, the per-subnet
|
||||
* IPv4 and IPv6 addresses an interface endpoint takes on its network interfaces. Terraform's
|
||||
* aws_vpc_endpoint sends the block on create and reads it back by following the endpoint to its
|
||||
* interfaces, so an emulator that drops the parameter answers with an address the configuration
|
||||
* never asked for and the endpoint replans as a replacement on every plan.
|
||||
*
|
||||
* <p>{@code SubnetConfiguration} is request-only in ec2/2016-11-15, so DescribeVpcEndpoints never
|
||||
* echoes it. What the wire shows is the subnet set; the addresses themselves are asserted on the
|
||||
* endpoint's interfaces.
|
||||
*
|
||||
* @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_SubnetConfiguration.html">SubnetConfiguration</a>
|
||||
*/
|
||||
@QuarkusTest
|
||||
class Ec2VpcEndpointSubnetConfigurationIntegrationTest {
|
||||
|
||||
@Inject
|
||||
Ec2Service service;
|
||||
|
||||
private static final String AUTH_HEADER =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
|
||||
|
||||
private String ec2Value(String action, String element, String... formParams) {
|
||||
RequestSpecification req = given().formParam("Action", action)
|
||||
.header("Authorization", AUTH_HEADER);
|
||||
for (int i = 0; i < formParams.length; i += 2) {
|
||||
req = req.formParam(formParams[i], formParams[i + 1]);
|
||||
}
|
||||
return req.when().post("/").then().statusCode(200).extract().path(element);
|
||||
}
|
||||
|
||||
private String createVpc(String cidr) {
|
||||
return ec2Value("CreateVpc", "CreateVpcResponse.vpc.vpcId", "CidrBlock", cidr);
|
||||
}
|
||||
|
||||
private String createSubnet(String vpcId, String cidr, String availabilityZone) {
|
||||
return ec2Value("CreateSubnet", "CreateSubnetResponse.subnet.subnetId",
|
||||
"VpcId", vpcId, "CidrBlock", cidr, "AvailabilityZone", availabilityZone);
|
||||
}
|
||||
|
||||
private Map<String, String> endpointAddressesBySubnet() {
|
||||
Map<String, String> addresses = new HashMap<>();
|
||||
for (NetworkInterface eni : service.endpointNetworkInterfaces("us-east-1")) {
|
||||
addresses.put(eni.getSubnetId(), eni.getPrivateIpAddress());
|
||||
}
|
||||
return addresses;
|
||||
}
|
||||
|
||||
@Test
|
||||
void createPinsTheAddressInEverySubnetItNames() {
|
||||
String vpcId = createVpc("10.70.0.0/16");
|
||||
String subnetA = createSubnet(vpcId, "10.70.1.0/24", "us-east-1a");
|
||||
String subnetB = createSubnet(vpcId, "10.70.2.0/24", "us-east-1b");
|
||||
|
||||
String endpointId = ec2Value("CreateVpcEndpoint",
|
||||
"CreateVpcEndpointResponse.vpcEndpoint.vpcEndpointId",
|
||||
"VpcId", vpcId,
|
||||
"ServiceName", "com.amazonaws.us-east-1.ecs",
|
||||
"VpcEndpointType", "Interface",
|
||||
"SubnetId.1", subnetA,
|
||||
"SubnetId.2", subnetB,
|
||||
"SubnetConfiguration.1.SubnetId", subnetA,
|
||||
"SubnetConfiguration.1.Ipv4", "10.70.1.10",
|
||||
"SubnetConfiguration.2.SubnetId", subnetB,
|
||||
"SubnetConfiguration.2.Ipv4", "10.70.2.10");
|
||||
|
||||
// Two reads, the way an apply followed by a plan reads the endpoint back. Both must
|
||||
// answer with the address the request pinned.
|
||||
for (int round = 0; round < 2; round++) {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpoints")
|
||||
.formParam("VpcEndpointId.1", endpointId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.contentType("application/xml")
|
||||
.body("DescribeVpcEndpointsResponse.vpcEndpointSet.item.subnetIdSet.item",
|
||||
hasItem(subnetA))
|
||||
.body("DescribeVpcEndpointsResponse.vpcEndpointSet.item.subnetIdSet.item",
|
||||
hasItem(subnetB));
|
||||
|
||||
Map<String, String> addresses = endpointAddressesBySubnet();
|
||||
assertEquals("10.70.1.10", addresses.get(subnetA), "round " + round);
|
||||
assertEquals("10.70.2.10", addresses.get(subnetB), "round " + round);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSubnetIsNamedThroughItsConfigurationAlone() {
|
||||
String vpcId = createVpc("10.71.0.0/16");
|
||||
String subnetId = createSubnet(vpcId, "10.71.1.0/24", "us-east-1a");
|
||||
|
||||
String endpointId = ec2Value("CreateVpcEndpoint",
|
||||
"CreateVpcEndpointResponse.vpcEndpoint.vpcEndpointId",
|
||||
"VpcId", vpcId,
|
||||
"ServiceName", "com.amazonaws.us-east-1.ecr.api",
|
||||
"VpcEndpointType", "Interface",
|
||||
"SubnetConfiguration.1.SubnetId", subnetId,
|
||||
"SubnetConfiguration.1.Ipv4", "10.71.1.20");
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpoints")
|
||||
.formParam("VpcEndpointId.1", endpointId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(hasXPath("//*[local-name()='subnetIdSet']/*[local-name()='item']", equalTo(subnetId)));
|
||||
|
||||
assertEquals("10.71.1.20", endpointAddressesBySubnet().get(subnetId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEndpointWithoutTheParameterKeepsItsSynthesizedAddress() {
|
||||
String vpcId = createVpc("10.72.0.0/16");
|
||||
String subnetId = createSubnet(vpcId, "10.72.1.0/24", "us-east-1a");
|
||||
|
||||
ec2Value("CreateVpcEndpoint", "CreateVpcEndpointResponse.vpcEndpoint.vpcEndpointId",
|
||||
"VpcId", vpcId,
|
||||
"ServiceName", "com.amazonaws.us-east-1.rds",
|
||||
"VpcEndpointType", "Interface",
|
||||
"SubnetId.1", subnetId);
|
||||
|
||||
String address = endpointAddressesBySubnet().get(subnetId);
|
||||
assertEquals("10.72.1.", address.substring(0, address.lastIndexOf('.') + 1),
|
||||
"the synthesized address still comes from the subnet's own range");
|
||||
assertEquals(address, endpointAddressesBySubnet().get(subnetId),
|
||||
"and it is stable across reads");
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyRewritesThePinnedAddress() {
|
||||
String vpcId = createVpc("10.73.0.0/16");
|
||||
String subnetId = createSubnet(vpcId, "10.73.1.0/24", "us-east-1a");
|
||||
|
||||
String endpointId = ec2Value("CreateVpcEndpoint",
|
||||
"CreateVpcEndpointResponse.vpcEndpoint.vpcEndpointId",
|
||||
"VpcId", vpcId,
|
||||
"ServiceName", "com.amazonaws.us-east-1.ecs",
|
||||
"VpcEndpointType", "Interface",
|
||||
"SubnetId.1", subnetId,
|
||||
"SubnetConfiguration.1.SubnetId", subnetId,
|
||||
"SubnetConfiguration.1.Ipv4", "10.73.1.10");
|
||||
|
||||
given()
|
||||
.formParam("Action", "ModifyVpcEndpoint")
|
||||
.formParam("VpcEndpointId", endpointId)
|
||||
.formParam("SubnetConfiguration.1.SubnetId", subnetId)
|
||||
.formParam("SubnetConfiguration.1.Ipv4", "10.73.1.44")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(hasXPath("//*[local-name()='ModifyVpcEndpointResponse']/*[local-name()='return']",
|
||||
equalTo("true")));
|
||||
|
||||
assertEquals("10.73.1.44", endpointAddressesBySubnet().get(subnetId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAddressOutsideTheSubnetIsRejected() {
|
||||
String vpcId = createVpc("10.74.0.0/16");
|
||||
String subnetId = createSubnet(vpcId, "10.74.1.0/24", "us-east-1a");
|
||||
|
||||
given()
|
||||
.formParam("Action", "CreateVpcEndpoint")
|
||||
.formParam("VpcId", vpcId)
|
||||
.formParam("ServiceName", "com.amazonaws.us-east-1.ecs")
|
||||
.formParam("VpcEndpointType", "Interface")
|
||||
.formParam("SubnetId.1", subnetId)
|
||||
.formParam("SubnetConfiguration.1.SubnetId", subnetId)
|
||||
.formParam("SubnetConfiguration.1.Ipv4", "10.74.9.10")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
|
||||
|
||||
List<String> subnets = service.describeVpcEndpoints("us-east-1", List.of(), Map.of()).stream()
|
||||
.filter(endpoint -> vpcId.equals(endpoint.getVpcId()))
|
||||
.flatMap(endpoint -> endpoint.getSubnetIds().stream())
|
||||
.toList();
|
||||
assertEquals(List.of(), subnets, "the rejected request must not have created an endpoint");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theSubnetReservedAddressesAreRejected() {
|
||||
String vpcId = createVpc("10.75.0.0/16");
|
||||
String subnetId = createSubnet(vpcId, "10.75.1.0/24", "us-east-1a");
|
||||
|
||||
for (String reserved : List.of("10.75.1.0", "10.75.1.1", "10.75.1.2", "10.75.1.3", "10.75.1.255")) {
|
||||
given()
|
||||
.formParam("Action", "CreateVpcEndpoint")
|
||||
.formParam("VpcId", vpcId)
|
||||
.formParam("ServiceName", "com.amazonaws.us-east-1.ecs")
|
||||
.formParam("VpcEndpointType", "Interface")
|
||||
.formParam("SubnetId.1", subnetId)
|
||||
.formParam("SubnetConfiguration.1.SubnetId", subnetId)
|
||||
.formParam("SubnetConfiguration.1.Ipv4", reserved)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
|
||||
}
|
||||
|
||||
ec2Value("CreateVpcEndpoint", "CreateVpcEndpointResponse.vpcEndpoint.vpcEndpointId",
|
||||
"VpcId", vpcId, "ServiceName", "com.amazonaws.us-east-1.ecs",
|
||||
"VpcEndpointType", "Interface", "SubnetId.1", subnetId,
|
||||
"SubnetConfiguration.1.SubnetId", subnetId,
|
||||
"SubnetConfiguration.1.Ipv4", "10.75.1.4");
|
||||
|
||||
assertEquals("10.75.1.4", endpointAddressesBySubnet().get(subnetId),
|
||||
"the first address above the reserved four is assignable");
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,37 @@ class Ipv4CidrsTest {
|
||||
assertFalse(Ipv4Cidrs.isIpv4("2600:1f18::/56"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isSubnetReservedCoversTheFirstFourAddressesAndTheLast() {
|
||||
for (String reserved : List.of("10.0.1.0", "10.0.1.1", "10.0.1.2", "10.0.1.3", "10.0.1.255")) {
|
||||
assertTrue(Ipv4Cidrs.isSubnetReserved("10.0.1.0/24", reserved + "/32"), reserved);
|
||||
}
|
||||
assertFalse(Ipv4Cidrs.isSubnetReserved("10.0.1.0/24", "10.0.1.4/32"));
|
||||
assertFalse(Ipv4Cidrs.isSubnetReserved("10.0.1.0/24", "10.0.1.254/32"));
|
||||
assertFalse(Ipv4Cidrs.isSubnetReserved("10.0.1.0/24", "10.0.2.1/32"),
|
||||
"an address outside the block is not reserved by it");
|
||||
}
|
||||
|
||||
@Test
|
||||
void isSubnetReservedLeavesTheHostsOfASmallSubnetAlone() {
|
||||
// A /28 is the smallest subnet AWS accepts: five reserved, eleven usable.
|
||||
assertTrue(Ipv4Cidrs.isSubnetReserved("10.0.1.16/28", "10.0.1.19/32"));
|
||||
assertTrue(Ipv4Cidrs.isSubnetReserved("10.0.1.16/28", "10.0.1.31/32"));
|
||||
assertFalse(Ipv4Cidrs.isSubnetReserved("10.0.1.16/28", "10.0.1.20/32"));
|
||||
assertFalse(Ipv4Cidrs.isSubnetReserved("10.0.1.16/28", "10.0.1.30/32"));
|
||||
// A /29 keeps three hosts between the two reserved ends.
|
||||
assertFalse(Ipv4Cidrs.isSubnetReserved("10.0.1.8/29", "10.0.1.12/32"));
|
||||
assertTrue(Ipv4Cidrs.isSubnetReserved("10.0.1.8/29", "10.0.1.15/32"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isSubnetReservedClaimsEveryAddressOfABlockTooSmallToHoldAHost() {
|
||||
for (String address : List.of("10.0.1.0", "10.0.1.1", "10.0.1.2", "10.0.1.3")) {
|
||||
assertTrue(Ipv4Cidrs.isSubnetReserved("10.0.1.0/30", address + "/32"), address);
|
||||
}
|
||||
assertTrue(Ipv4Cidrs.isSubnetReserved("10.0.1.0/32", "10.0.1.0/32"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void firstFreeBlockSkipsOccupiedSpaceInOrder() {
|
||||
assertEquals("10.0.1.0/24",
|
||||
|
||||
Reference in new issue
Block a user