mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): serialize unique IPv6 allocation through VPC publication (#8536)
* fix(ec2): serialize unique IPv6 allocation through VPC publication
This commit is contained in:
1 parent
53ef87af1c
commit
0e86263ac0
3 files changed
+126
-10
No files matched your search
@@ -354,6 +354,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
// a concurrent allocation, which reads persisted resources to see what is taken, cannot pick
|
||||
// the same address in between. Taken after imageRegistryLock where a caller holds both.
|
||||
private final Object privateIpAllocationLock = new Object();
|
||||
private final Object amazonProvidedIpv6AllocationLock = new Object();
|
||||
/** Host part (1-254) of the last 203.0.113.0/24 address handed out; guarded by the allocation lock. */
|
||||
private int documentationHost;
|
||||
|
||||
@@ -6507,9 +6508,13 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
vpc.getCidrBlockAssociationSet().add(
|
||||
new VpcCidrBlockAssociation("vpc-cidr-assoc-" + randomHex(8), cidrBlock));
|
||||
if (amazonProvidedIpv6CidrBlock) {
|
||||
vpc.getIpv6CidrBlockAssociationSet().add(amazonProvidedIpv6Association(region));
|
||||
synchronized (amazonProvidedIpv6AllocationLock) {
|
||||
vpc.getIpv6CidrBlockAssociationSet().add(amazonProvidedIpv6Association(region));
|
||||
vpcs.put(key(region, vpcId), vpc);
|
||||
}
|
||||
} else {
|
||||
vpcs.put(key(region, vpcId), vpc);
|
||||
}
|
||||
vpcs.put(key(region, vpcId), vpc);
|
||||
declareVpcNetwork(region, vpcId, cidrBlock);
|
||||
|
||||
createDefaultSecurityGroup(region, vpcId, "sg-" + randomHex(17));
|
||||
@@ -6949,10 +6954,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
synchronized (attachmentTopologyLock(region)) {
|
||||
synchronized (lockFor(key(region, vpcId))) {
|
||||
Vpc vpc = getRequiredVpc(region, vpcId);
|
||||
VpcIpv6CidrBlockAssociation assoc = amazonProvidedIpv6Association(region);
|
||||
vpc.getIpv6CidrBlockAssociationSet().add(assoc);
|
||||
vpcs.put(key(region, vpcId), vpc);
|
||||
return assoc;
|
||||
synchronized (amazonProvidedIpv6AllocationLock) {
|
||||
VpcIpv6CidrBlockAssociation assoc = amazonProvidedIpv6Association(region);
|
||||
vpc.getIpv6CidrBlockAssociationSet().add(assoc);
|
||||
vpcs.put(key(region, vpcId), vpc);
|
||||
return assoc;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6966,11 +6973,37 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* fourth group folds into the "::".
|
||||
*/
|
||||
private VpcIpv6CidrBlockAssociation amazonProvidedIpv6Association(String region) {
|
||||
String block = CidrCanonicalizer.canonicalize(
|
||||
"2600:1f18:" + randomHex(4) + ":" + randomHex(2) + "00::/56").orElseThrow();
|
||||
// AWS gives each VPC its own Amazon-provided block, so a block another VPC holds, in any account of this
|
||||
// deployment, is drawn again.
|
||||
Set<String> held = new HashSet<>();
|
||||
for (Vpc vpc : vpcs instanceof AccountAwareStorageBackend<Vpc> aware
|
||||
? aware.scanAllAccounts() : vpcs.scan(k -> true)) {
|
||||
if (vpc.getIpv6CidrBlockAssociationSet() == null) {
|
||||
continue;
|
||||
}
|
||||
for (VpcIpv6CidrBlockAssociation association : vpc.getIpv6CidrBlockAssociationSet()) {
|
||||
held.add(association.getIpv6CidrBlock());
|
||||
}
|
||||
}
|
||||
String block = unusedAmazonProvidedIpv6Block(held, () -> CidrCanonicalizer.canonicalize(
|
||||
"2600:1f18:" + randomHex(4) + ":" + randomHex(2) + "00::/56").orElseThrow());
|
||||
return new VpcIpv6CidrBlockAssociation("vpc-cidr-assoc-" + randomHex(8), block, region);
|
||||
}
|
||||
|
||||
/**
|
||||
* The first drawn block no VPC holds. After 64 draws that are all held, the request is refused with
|
||||
* InsufficientCidrBlocks rather than handing out a block twice.
|
||||
*/
|
||||
static String unusedAmazonProvidedIpv6Block(Set<String> held, Supplier<String> draw) {
|
||||
for (int attempt = 0; attempt < 64; attempt++) {
|
||||
String block = draw.get();
|
||||
if (!held.contains(block)) {
|
||||
return block;
|
||||
}
|
||||
}
|
||||
throw new AwsException("InsufficientCidrBlocks", "No Amazon-provided IPv6 CIDR block is available.", 400);
|
||||
}
|
||||
|
||||
private boolean vpcHasAssociatedIpv6CidrBlock(Vpc vpc) {
|
||||
return vpc.getIpv6CidrBlockAssociationSet().stream()
|
||||
.anyMatch(assoc -> "associated".equalsIgnoreCase(assoc.getIpv6CidrBlockState()));
|
||||
@@ -6985,8 +7018,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
vpc.getCidrBlockAssociationSet().removeIf(a -> a.getAssociationId().equals(associationId));
|
||||
// The same operation disassociates either family; AWS takes one association id and
|
||||
// does not ask which set it belongs to.
|
||||
vpc.getIpv6CidrBlockAssociationSet().removeIf(a -> a.getAssociationId().equals(associationId));
|
||||
vpcs.put(key(region, vpc.getVpcId()), vpc);
|
||||
synchronized (amazonProvidedIpv6AllocationLock) {
|
||||
vpc.getIpv6CidrBlockAssociationSet().removeIf(a -> a.getAssociationId().equals(associationId));
|
||||
vpcs.put(key(region, vpc.getVpcId()), vpc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,6 +162,49 @@ import static org.mockito.Mockito.withSettings;
|
||||
|
||||
class Ec2ServiceTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"false, us-east-1", "true, us-east-1", "false, eu-west-1", "true, eu-west-1"})
|
||||
void ipv6AllocationWaitsForThePreviousVpcPublication(boolean associate, String secondRegion) throws Exception {
|
||||
AccountAwareStorageBackend<Vpc> vpcStore = spy(AccountAwareStorageBackend.<Vpc>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-vpcs.json", vpcStore)));
|
||||
Vpc existing = service.createVpc(secondRegion, "10.112.0.0/16", false);
|
||||
CountDownLatch publishing = new CountDownLatch(1);
|
||||
CountDownLatch resume = new CountDownLatch(1);
|
||||
doAnswer(call -> {
|
||||
Vpc publishingVpc = call.getArgument(1);
|
||||
if ("10.111.0.0/16".equals(publishingVpc.getCidrBlock())) {
|
||||
publishing.countDown();
|
||||
assertTrue(resume.await(15, TimeUnit.SECONDS));
|
||||
}
|
||||
return call.callRealMethod();
|
||||
}).when(vpcStore).put(anyString(), any(Vpc.class));
|
||||
AtomicReference<Thread> allocating = new AtomicReference<>();
|
||||
ExecutorService workers = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Future<Vpc> first = workers.submit(() -> service.createVpc("us-east-1", "10.111.0.0/16", false, true));
|
||||
assertTrue(publishing.await(5, TimeUnit.SECONDS));
|
||||
Future<String> second = workers.submit(() -> {
|
||||
allocating.set(Thread.currentThread());
|
||||
return associate
|
||||
? service.associateAmazonProvidedIpv6CidrBlock(secondRegion, existing.getVpcId()).getIpv6CidrBlock()
|
||||
: service.createVpc(secondRegion, "10.113.0.0/16", false, true)
|
||||
.getIpv6CidrBlockAssociationSet().getFirst().getIpv6CidrBlock();
|
||||
});
|
||||
awaitBlockedOrDone(allocating, second);
|
||||
assertFalse(second.isDone(), "A new allocation must wait until the previous block is visible in storage");
|
||||
resume.countDown();
|
||||
String firstBlock = first.get(5, TimeUnit.SECONDS).getIpv6CidrBlockAssociationSet().getFirst()
|
||||
.getIpv6CidrBlock();
|
||||
assertNotEquals(firstBlock, second.get(5, TimeUnit.SECONDS));
|
||||
} finally {
|
||||
resume.countDown();
|
||||
workers.shutdownNow();
|
||||
assertTrue(workers.awaitTermination(5, TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"false, aws:autoscaling:groupName", "true, aws:autoscaling:groupName",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.floci.aws.services.ec2;
|
||||
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -7,6 +8,10 @@ import org.junit.jupiter.api.Test;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
@@ -15,6 +20,7 @@ import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.matchesRegex;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* CreateVpc ignored AmazonProvidedIpv6CidrBlock and no response carried an
|
||||
@@ -195,4 +201,36 @@ class Ec2VpcIpv6CidrBlockIntegrationTest {
|
||||
}
|
||||
return String.join(":", Arrays.copyOf(groups, kept)) + "::/56";
|
||||
}
|
||||
|
||||
/** AWS gives each VPC its own Amazon-provided IPv6 block; no two VPCs here share one either. */
|
||||
@Test
|
||||
void amazonProvidedBlocksAreDistinctAcrossVpcs() {
|
||||
Set<String> blocks = new HashSet<>();
|
||||
for (int i = 0; i < 40; i++) {
|
||||
String block = given()
|
||||
.formParam("Action", "CreateVpc")
|
||||
.formParam("CidrBlock", "10.120." + i + ".0/24")
|
||||
.formParam("AmazonProvidedIpv6CidrBlock", "true")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("CreateVpcResponse.vpc.ipv6CidrBlockAssociationSet.item.ipv6CidrBlock");
|
||||
assertEquals(true, blocks.add(block), "block handed out twice: " + block);
|
||||
}
|
||||
}
|
||||
|
||||
/** A drawn block another VPC holds is drawn again; a run of held draws is refused, never handed out twice. */
|
||||
@Test
|
||||
void aHeldBlockIsDrawnAgainAndExhaustionIsRefused() {
|
||||
Set<String> held = Set.of("2600:1f18:1:100::/56", "2600:1f18:2:200::/56");
|
||||
Iterator<String> draws = List.of("2600:1f18:1:100::/56", "2600:1f18:2:200::/56",
|
||||
"2600:1f18:3:300::/56").iterator();
|
||||
assertEquals("2600:1f18:3:300::/56", Ec2Service.unusedAmazonProvidedIpv6Block(held, draws::next));
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> Ec2Service.unusedAmazonProvidedIpv6Block(held, () -> "2600:1f18:1:100::/56"));
|
||||
assertEquals("InsufficientCidrBlocks", refused.getErrorCode());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user