mirror of
https://github.com/floci-io/floci.git
synced 2026-12-05 19:18:01 -05:00
fix(lambda): hold the subnets a function's VpcConfig names (#8609)
On AWS a function's Hyperplane network interfaces sit in the subnets its VpcConfig names, and EC2 refuses DeleteSubnet, and so DeleteVpc, while one is there. Floci's Lambda never told EC2 a function used a subnet, so DeleteSubnet succeeded under a VPC-configured function and left it naming a subnet that was gone. - LambdaService is a VpcDependencyProvider: a function of the caller's account, or a published version of one, holds each subnet its VpcConfig names and the VPC it is configured in (LambdaFunctionStore.listWithVersionsForAccount, which also reads a record stored before accounts were kept apart, unprefixed until it is next read). - CreateFunction and UpdateFunctionConfiguration save a function that names subnets under EC2's subnet lock, so a DeleteSubnet either sees it or runs before it is stored. A VpcConfig the request set is resolved again under the lock, so a subnet deleted since the request was validated is unknown, as one never created is, and the function infers no VPC from it. ECS awsvpc tasks already hold their subnet through the network interface each task is given, which DeleteSubnet counts. LambdaVpcResourceValidationIntegrationTest has DeleteSubnet refused under a function, then under its published version once $LATEST moves to another subnet, and allowed once the version is deleted, and a subnet deleted while a function is created leaves it no inferred VPC. LambdaFunctionStoreAccountScopeTest covers the unprefixed record. ec2.md and lambda.md say so.
This commit is contained in:
1 parent
3d6f59c064
commit
94fcf5cbf3
6 files changed
+159
-6
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package io.floci.aws.services.lambda;
|
||||
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.services.lambda.model.LambdaFunction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -80,6 +81,28 @@ class LambdaFunctionStoreAccountScopeTest {
|
||||
assertEquals(2, store.listAll().size());
|
||||
}
|
||||
|
||||
/**
|
||||
* A record stored before accounts were kept apart stays unprefixed until it is next read, and still holds the
|
||||
* subnets its VpcConfig names, so the account's listing of functions and versions finds it.
|
||||
*/
|
||||
@Test
|
||||
void listWithVersionsForAccountIncludesARecordStoredBeforeAccountsWereKeptApart() {
|
||||
InMemoryStorage<String, LambdaFunction> raw = new InMemoryStorage<>();
|
||||
LambdaFunctionStore store = new LambdaFunctionStore(
|
||||
new AccountAwareStorageBackend<>(raw, null, DEFAULT_ACCOUNT));
|
||||
store.saveForAccount(OTHER_ACCOUNT, REGION, function("current-fn", OTHER_ACCOUNT));
|
||||
LambdaFunction legacyVersion = function("legacy-fn", OTHER_ACCOUNT);
|
||||
legacyVersion.setVersion("1");
|
||||
raw.put("lambda::" + REGION + "::legacy-fn::1", legacyVersion);
|
||||
raw.put("lambda::" + REGION + "::legacy-default-fn::$LATEST", function("legacy-default-fn", DEFAULT_ACCOUNT));
|
||||
raw.put("lambda::eu-west-1::legacy-elsewhere-fn::$LATEST", function("legacy-elsewhere-fn", OTHER_ACCOUNT));
|
||||
|
||||
assertEquals(List.of("current-fn", "legacy-fn"), store.listWithVersionsForAccount(OTHER_ACCOUNT, REGION)
|
||||
.stream().map(LambdaFunction::getFunctionName).sorted().toList());
|
||||
assertEquals(List.of("legacy-default-fn"), store.listWithVersionsForAccount(DEFAULT_ACCOUNT, REGION)
|
||||
.stream().map(LambdaFunction::getFunctionName).toList());
|
||||
}
|
||||
|
||||
private LambdaFunctionStore accountAwareStore() {
|
||||
return new LambdaFunctionStore(AccountAwareStorageBackend.<LambdaFunction>inMemory(DEFAULT_ACCOUNT));
|
||||
}
|
||||
|
||||
+60
-1
@@ -1,8 +1,10 @@
|
||||
package io.floci.aws.services.lambda;
|
||||
|
||||
import io.floci.aws.services.ec2.Ec2Service;
|
||||
import io.floci.aws.testing.LambdaCodeFixtures;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.mockito.InjectSpy;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -14,6 +16,7 @@ import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.core.SdkBytes;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
|
||||
import software.amazon.awssdk.services.iam.IamClient;
|
||||
import software.amazon.awssdk.services.lambda.LambdaClient;
|
||||
import software.amazon.awssdk.services.lambda.model.FunctionCode;
|
||||
@@ -35,6 +38,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
|
||||
@QuarkusTest
|
||||
class LambdaVpcResourceValidationIntegrationTest {
|
||||
@@ -52,6 +58,9 @@ class LambdaVpcResourceValidationIntegrationTest {
|
||||
@TestHTTPResource("/")
|
||||
URI endpoint;
|
||||
|
||||
@InjectSpy
|
||||
Ec2Service ec2Service;
|
||||
|
||||
@BeforeEach
|
||||
void createDeclaredNetworksAndRole() throws Exception {
|
||||
StaticCredentialsProvider credentials = StaticCredentialsProvider.create(
|
||||
@@ -205,11 +214,61 @@ class LambdaVpcResourceValidationIntegrationTest {
|
||||
&& (current.vpcId() == null || current.vpcId().isEmpty())));
|
||||
}
|
||||
|
||||
/**
|
||||
* A function, and a published version of it, hold the subnets their VpcConfig names, as their network interfaces
|
||||
* do on AWS: DeleteSubnet is refused until neither names the subnet.
|
||||
*/
|
||||
@Test
|
||||
void aFunctionAndItsVersionsHoldTheSubnetsTheirVpcConfigNames() {
|
||||
createFunction(first.config());
|
||||
assertEquals("DependencyViolation", subnetDeletionError(first.subnet()));
|
||||
|
||||
String version = lambda.publishVersion(r -> r.functionName(name)).version();
|
||||
lambda.updateFunctionConfiguration(r -> r.functionName(name).vpcConfig(second.config()));
|
||||
assertEquals("DependencyViolation", subnetDeletionError(first.subnet()), "the version still names it");
|
||||
assertEquals("DependencyViolation", subnetDeletionError(second.subnet()));
|
||||
|
||||
lambda.deleteFunction(r -> r.functionName(name).qualifier(version));
|
||||
ec2.deleteSubnet(r -> r.subnetId(first.subnet()));
|
||||
}
|
||||
|
||||
/**
|
||||
* A DeleteSubnet that runs after CreateFunction validated its subnets but before it stored the function sees no
|
||||
* function naming the subnet and deletes it. The function then infers no VPC from it, as from a subnet never
|
||||
* created, rather than storing the VPC of a subnet that is gone.
|
||||
*/
|
||||
@Test
|
||||
void aSubnetDeletedWhileAFunctionIsCreatedLeavesItNoInferredVpc() {
|
||||
doAnswer(invocation -> {
|
||||
ec2.deleteSubnet(r -> r.subnetId(first.subnet()));
|
||||
return invocation.callRealMethod();
|
||||
}).when(ec2Service).runWithSubnetReferencesHeld(anyString(), any(Runnable.class));
|
||||
|
||||
createFunction(first.config());
|
||||
|
||||
assertEquals(List.of(first.subnet()), configuration().vpcConfig().subnetIds());
|
||||
assertNull(configuration().vpcConfig().vpcId());
|
||||
}
|
||||
|
||||
private String subnetDeletionError(String subnet) {
|
||||
return assertThrows(Ec2Exception.class, () -> ec2.deleteSubnet(r -> r.subnetId(subnet)))
|
||||
.awsErrorDetails().errorCode();
|
||||
}
|
||||
|
||||
private Network network(String prefix) {
|
||||
String vpc = ec2.createVpc(r -> r.cidrBlock(prefix + ".0.0/16")).vpc().vpcId();
|
||||
cleanup.add(() -> ec2.deleteVpc(r -> r.vpcId(vpc)));
|
||||
String subnet = ec2.createSubnet(r -> r.vpcId(vpc).cidrBlock(prefix + ".1.0/24")).subnet().subnetId();
|
||||
cleanup.add(() -> ec2.deleteSubnet(r -> r.subnetId(subnet)));
|
||||
cleanup.add(() -> {
|
||||
try {
|
||||
ec2.deleteSubnet(r -> r.subnetId(subnet));
|
||||
} catch (Ec2Exception e) {
|
||||
// A test may have deleted it already, once nothing held it.
|
||||
if (!"InvalidSubnetID.NotFound".equals(e.awsErrorDetails().errorCode())) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
});
|
||||
String group = ec2.createSecurityGroup(
|
||||
r -> r.vpcId(vpc).groupName(name).description("Declared Lambda network")).groupId();
|
||||
cleanup.add(() -> ec2.deleteSecurityGroup(r -> r.groupId(group)));
|
||||
|
||||
Reference in new issue
Block a user