Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(ec2): resolve interface endpoint fallback address collisions (#5347)

Salil Chincholikar84dbed14afb0
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.java
index 4ff3bab678..05a43dea0b 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
@@ -5138,6 +5138,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
String region = endpoint.getRegion();
List<NetworkInterface> result = new ArrayList<>();
+ List<NetworkInterface> fallbackInterfaces = new ArrayList<>();
+ Set<Long> usedAddresses = new HashSet<>();
for (String subnetId : endpoint.getSubnetIds()) {
Subnet subnet = subnets.get(key(region, subnetId)).orElse(null);
if (subnet == null) {
@@ -5170,9 +5172,20 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
.ifPresent(sg -> group.setGroupName(sg.getGroupName()));
ni.getGroups().add(group);
}
- ni.setPrivateIpAddress(endpointPrivateIp(subnet, endpoint, subnetId));
+ String address = endpointPrivateIp(subnet, endpoint, subnetId);
+ ni.setPrivateIpAddress(address);
+ if (address == null) {
+ fallbackInterfaces.add(ni);
+ } else {
+ usedAddresses.add(Ipv4Cidrs.addressValue(address));
+ }
result.add(ni);
}
+ // Reserve real addresses first and resolve collisions in subnet order, independent of request order.
+ fallbackInterfaces.sort(Comparator.comparing(NetworkInterface::getSubnetId));
+ for (NetworkInterface ni : fallbackInterfaces) {
+ ni.setPrivateIpAddress(endpointFallbackPrivateIp(endpoint, ni.getSubnetId(), usedAddresses));
+ }
return result;
}
@@ -5305,29 +5318,29 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
}
String cidr = subnet.getCidrBlock();
- String baseIp = cidr != null ? cidr.split("/")[0] : "172.31.0.0";
- String[] parts = baseIp.split("\\.");
- // A subnet's CidrBlock is not guaranteed to be dotted IPv4. CreateSubnet stores
- // whatever it is given without validating the family, and an IPv6-only subnet has
- // no IPv4 CIDR at all, so this can be "2001:db8::" or anything else -- one element,
- // and parts[1] then throws. That used to surface only on the flow-log scheduler;
- // DescribeVpcEndpoints now derives interfaces on the request path, which would turn
- // an odd subnet into a failed EC2 response rather than a degraded address.
- // Falls back to the same default the null case already uses, so "no usable IPv4"
- // has one behaviour rather than two.
- int host = 200 + Math.floorMod(endpoint.getVpcEndpointId().hashCode(), 50);
- if (parts.length < 4) {
- // The third octet comes from the SUBNET, not a constant. On the IPv4 path each
- // subnet supplies its own distinct network, and that is the only thing making
- // one endpoint's interfaces distinct -- the host octet is derived from the
- // endpoint and is therefore the same for all of them. A constant fallback threw
- // that away and gave every non-IPv4 subnet on an endpoint the same address,
- // trading a crash for a silent collision.
- return "172.31." + Math.floorMod(subnetId.hashCode(), 256) + "." + host;
+ if (!Ipv4Cidrs.isIpv4(cidr)) {
+ return null;
}
+ String[] parts = cidr.split("/")[0].split("\\.");
+ int host = 200 + Math.floorMod(endpoint.getVpcEndpointId().hashCode(), 50);
return parts[0] + "." + parts[1] + "." + parts[2] + "." + host;
}
+ private static String endpointFallbackPrivateIp(VpcEndpoint endpoint, String subnetId,
+ Set<Long> usedAddresses) {
+ int network = Math.floorMod(subnetId.hashCode(), 256);
+ int hostOffset = Math.floorMod(endpoint.getVpcEndpointId().hashCode(), 50);
+ for (int offset = 0; offset < 256 * 50; offset++) {
+ String address = "172.31." + Math.floorMod(network + offset, 256) + "."
+ + (200 + Math.floorMod(hostOffset + offset / 256, 50));
+ if (usedAddresses.add(Ipv4Cidrs.addressValue(address))) {
+ return address;
+ }
+ }
+ throw new AwsException("InsufficientFreeAddressesInSubnet",
+ "No unused fallback IPv4 address is available for endpoint " + endpoint.getVpcEndpointId(), 400);
+ }
+
private VpcEndpoint getRequiredVpcEndpoint(String region, String endpointId) {
VpcEndpoint endpoint = vpcEndpoints.get(key(region, endpointId)).orElse(null);
if (endpoint == null) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ipv4Cidrs.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ipv4Cidrs.java
index 5a24acb0db..6cfa5e64fd 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ipv4Cidrs.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ipv4Cidrs.java
@@ -61,6 +61,10 @@ public final class Ipv4Cidrs {
}
}
+ static long addressValue(String address) {
+ return parse(address + "/32")[0];
+ }
+
/**
* The first /{@code netmask} block inside any of the {@code provisioned}
* CIDRs (in order) that overlaps none of the {@code occupied} CIDRs, or
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
index d5158c0c6e..e831d09775 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
@@ -1366,6 +1366,93 @@ class Ec2ServiceTest {
"endpoints are regional");
}
+ @Test
+ void endpointFallbackAddressesResolveSubnetHashCollisions() {
+ EndpointAddressFixture fixture = endpointAddressFixture("2001:db8:1::/64", "2001:db8:2::/64");
+ String firstSubnet = "subnet-00000000";
+ String secondSubnet = "subnet-00000088";
+ assertEquals(Math.floorMod(firstSubnet.hashCode(), 256), Math.floorMod(secondSubnet.hashCode(), 256));
+
+ Map<String, String> addresses = endpointAddressesBySubnet(fixture.service());
+ assertEquals(2, addresses.size());
+ assertNotEquals(addresses.get(firstSubnet), addresses.get(secondSubnet));
+ assertEquals(addresses, endpointAddressesBySubnet(fixture.service()));
+
+ fixture.endpoint().setSubnetIds(List.of(secondSubnet, firstSubnet));
+ assertEquals(addresses, endpointAddressesBySubnet(fixture.service()),
+ "reordering subnets must not change their fallback addresses");
+ }
+
+ @Test
+ void endpointFallbackAddressesAreDistinctForSubnetsWithNoIpv4Cidr() {
+ EndpointAddressFixture fixture = endpointAddressFixture(null, null);
+
+ Map<String, String> addresses = endpointAddressesBySubnet(fixture.service());
+ assertEquals(2, addresses.size());
+ assertNotEquals(addresses.get("subnet-00000000"), addresses.get("subnet-00000088"));
+ assertEquals(addresses, endpointAddressesBySubnet(fixture.service()));
+ }
+
+ @Test
+ void endpointFallbackAddressesDoNotReusePinnedOrIpv4DerivedAddresses() {
+ for (boolean pinned : new boolean[]{false, true}) {
+ EndpointAddressFixture fixture = endpointAddressFixture("2001:db8:1::/64", "172.31.112.0/24");
+ int host = 200 + Math.floorMod(fixture.endpoint().getVpcEndpointId().hashCode(), 50);
+ String fixedAddress = "172.31.112." + host;
+ if (pinned) {
+ fixture.endpoint().setSubnetConfigurations(List.of(
+ new VpcEndpointSubnetConfiguration("subnet-00000088", fixedAddress, null)));
+ }
+
+ Map<String, String> addresses = endpointAddressesBySubnet(fixture.service());
+ assertEquals(fixedAddress, addresses.get("subnet-00000088"));
+ assertNotEquals(fixedAddress, addresses.get("subnet-00000000"));
+ assertEquals(addresses, endpointAddressesBySubnet(fixture.service()));
+ }
+ }
+
+ @Test
+ void endpointFallbackAddressesDoNotReusePinnedAddressesWithLeadingZeros() {
+ EndpointAddressFixture fixture = endpointAddressFixture("2001:db8:1::/64", "172.31.112.0/24");
+ int host = 200 + Math.floorMod(fixture.endpoint().getVpcEndpointId().hashCode(), 50);
+ String pinnedAddress = "172.31.112.0" + host;
+ fixture.service().modifyVpcEndpoint("us-east-1", fixture.endpoint().getVpcEndpointId(),
+ List.of(), List.of(), List.of(), List.of(), List.of(), List.of(), null, null, null,
+ List.of(new VpcEndpointSubnetConfiguration("subnet-00000088", pinnedAddress, null)));
+
+ Map<String, String> addresses = endpointAddressesBySubnet(fixture.service());
+ assertEquals(pinnedAddress, addresses.get("subnet-00000088"));
+ assertNotEquals("172.31.112." + host, addresses.get("subnet-00000000"));
+ assertEquals(addresses, endpointAddressesBySubnet(fixture.service()));
+ }
+
+ private static EndpointAddressFixture endpointAddressFixture(String firstCidr, String secondCidr) {
+ 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(Map.of("ec2-subnets.json", subnetStore)));
+ String region = "us-east-1";
+ String vpcId = service.createVpc(region, "172.31.0.0/16", false).getVpcId();
+ String[] subnetIds = {"subnet-00000000", "subnet-00000088"};
+ String[] cidrs = {firstCidr, secondCidr};
+ for (int i = 0; i < subnetIds.length; i++) {
+ Subnet subnet = new Subnet();
+ subnet.setSubnetId(subnetIds[i]);
+ subnet.setVpcId(vpcId);
+ subnet.setRegion(region);
+ subnet.setCidrBlock(cidrs[i]);
+ subnet.setAvailabilityZone(region + (i == 0 ? "a" : "b"));
+ subnetStore.put(region + "::" + subnetIds[i], subnet);
+ }
+ VpcEndpoint endpoint = service.createVpcEndpoint(region, vpcId,
+ "com.amazonaws.us-east-1.ec2", "Interface",
+ List.of(), List.of(subnetIds), List.of(), null, null, List.of());
+ return new EndpointAddressFixture(service, endpoint);
+ }
+
+ private record EndpointAddressFixture(Ec2Service service, VpcEndpoint endpoint) {
+ }
+
@Test
void subnetConfigurationPinsTheEndpointInterfaceAddress() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),