feat(ec2): create and list network interface permissions (#8891)

This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-19 00:28:03 -04:00
1 parent e3b7fd286c
commit 7c2018ffd6
8 files changed
+475 -1

No files matched your search

@@ -0,0 +1,151 @@
package io.floci.aws.services.ec2;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static io.floci.aws.testing.Ec2Requests.ec2;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Network interface permissions: the public CreateNetworkInterfacePermission an interface's owner
* calls, and DescribeNetworkInterfacePermissions, which lists them to the owner.
*
* <p>Twelve-digit access key ids resolve to distinct accounts, as in the other cross-account tests.
*/
@QuarkusTest
class Ec2NetworkInterfacePermissionIntegrationTest {
private static final String OWNER = "000000003511";
private static final String GRANTEE = "000000003512";
private static final String SERVICE = "000000003513";
private static final String OTHER = "000000003514";
/**
* The owner's interface, created on first use: QuarkusTest points RestAssured at the application
* only for a test method, so a {@code @BeforeAll} could not reach it.
*/
private static String eniId;
private static String eni() {
if (eniId == null) {
eniId = createTheOwnersInterface();
}
return eniId;
}
private static String createTheOwnersInterface() {
String vpcId = ec2("DescribeVpcs", OWNER, "us-east-1")
.formParam("Filter.1.Name", "is-default").formParam("Filter.1.Value.1", "true")
.when().post("/")
.then().statusCode(200)
.extract().path("DescribeVpcsResponse.vpcSet.item[0].vpcId");
String subnetId = ec2("DescribeSubnets", OWNER, "us-east-1")
.formParam("Filter.1.Name", "vpc-id").formParam("Filter.1.Value.1", vpcId)
.when().post("/")
.then().statusCode(200)
.extract().path("DescribeSubnetsResponse.subnetSet.item[0].subnetId");
return ec2("CreateNetworkInterface", OWNER, "us-east-1")
.formParam("SubnetId", subnetId)
.formParam("Description", "ES permissions-domain")
.when().post("/")
.then().statusCode(200)
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
}
@Test
void theOwnerGrantsAnotherAccountInstanceAttachAndSeesIt() {
ec2("CreateNetworkInterfacePermission", OWNER, "us-east-1")
.formParam("NetworkInterfaceId", eni())
.formParam("AwsAccountId", GRANTEE)
.formParam("Permission", "INSTANCE-ATTACH")
.when().post("/")
.then().statusCode(200)
.body("CreateNetworkInterfacePermissionResponse.interfacePermission.awsAccountId", equalTo(GRANTEE));
ec2("DescribeNetworkInterfacePermissions", OWNER, "us-east-1")
.formParam("Filter.1.Name", "network-interface-permission.network-interface-id")
.formParam("Filter.1.Value.1", eni())
.formParam("Filter.2.Name", "network-interface-permission.aws-account-id")
.formParam("Filter.2.Value.1", GRANTEE)
.when().post("/")
.then().statusCode(200)
.body("DescribeNetworkInterfacePermissionsResponse.networkInterfacePermissions.item.size()", equalTo(1))
.body("DescribeNetworkInterfacePermissionsResponse.networkInterfacePermissions.item"
+ ".networkInterfacePermissionId", startsWith("eni-perm-"))
.body("DescribeNetworkInterfacePermissionsResponse.networkInterfacePermissions.item.permission",
equalTo("INSTANCE-ATTACH"))
.body("DescribeNetworkInterfacePermissionsResponse.networkInterfacePermissions.item"
+ ".permissionState.state", equalTo("granted"));
}
@Test
void theOwnerGrantsAPermissionOnceAndOnlyTheOwnerCan() {
String first = ec2("CreateNetworkInterfacePermission", OWNER, "us-east-1")
.formParam("NetworkInterfaceId", eni())
.formParam("AwsAccountId", SERVICE)
.formParam("Permission", "INSTANCE-ATTACH")
.when().post("/")
.then().statusCode(200)
.body("CreateNetworkInterfacePermissionResponse.interfacePermission.networkInterfaceId",
equalTo(eni()))
.body("CreateNetworkInterfacePermissionResponse.interfacePermission.awsAccountId", equalTo(SERVICE))
.body("CreateNetworkInterfacePermissionResponse.interfacePermission.permissionState.state",
equalTo("granted"))
.extract().path("CreateNetworkInterfacePermissionResponse.interfacePermission"
+ ".networkInterfacePermissionId");
String again = ec2("CreateNetworkInterfacePermission", OWNER, "us-east-1")
.formParam("NetworkInterfaceId", eni())
.formParam("AwsAccountId", SERVICE)
.formParam("Permission", "INSTANCE-ATTACH")
.when().post("/")
.then().statusCode(200)
.extract().path("CreateNetworkInterfacePermissionResponse.interfacePermission"
+ ".networkInterfacePermissionId");
assertEquals(first, again);
ec2("DescribeNetworkInterfacePermissions", OWNER, "us-east-1")
.formParam("NetworkInterfacePermissionId.1", first)
.when().post("/")
.then().statusCode(200)
.body("DescribeNetworkInterfacePermissionsResponse.networkInterfacePermissions.item.awsAccountId",
equalTo(SERVICE));
ec2("CreateNetworkInterfacePermission", GRANTEE, "us-east-1")
.formParam("NetworkInterfaceId", eni())
.formParam("AwsAccountId", OTHER)
.formParam("Permission", "INSTANCE-ATTACH")
.when().post("/")
.then().statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidNetworkInterfaceID.NotFound"));
}
@Test
void aPermissionRequestOutsideTheModelIsRefused() {
ec2("CreateNetworkInterfacePermission", OWNER, "us-east-1")
.formParam("NetworkInterfaceId", eni())
.formParam("AwsAccountId", GRANTEE)
.formParam("Permission", "INSTANCE-DETACH")
.when().post("/")
.then().statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
ec2("CreateNetworkInterfacePermission", OWNER, "us-east-1")
.formParam("NetworkInterfaceId", eni())
.formParam("AwsAccountId", GRANTEE)
.formParam("Permission", "EIP-ASSOCIATE")
.when().post("/")
.then().statusCode(400)
.body("Response.Errors.Error.Code", equalTo("UnsupportedOperation"));
ec2("DescribeNetworkInterfacePermissions", OWNER, "us-east-1")
.formParam("NetworkInterfacePermissionId.1", "eni-perm-0123456789abcdef0")
.when().post("/")
.then().statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidNetworkInterfacePermissionID.NotFound"));
ec2("DescribeNetworkInterfacePermissions", OWNER, "us-east-1")
.formParam("NetworkInterfacePermissionId.1", "perm-0123")
.when().post("/")
.then().statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidNetworkInterfacePermissionID.Malformed"));
}
}
@@ -34,6 +34,7 @@ import io.floci.aws.services.ec2.model.NatGatewayAddress;
import io.floci.aws.services.ec2.model.NetworkAcl;
import io.floci.aws.services.ec2.model.NetworkInterface;
import io.floci.aws.services.ec2.model.NetworkInterfaceAssociation;
import io.floci.aws.services.ec2.model.NetworkInterfaceAttachment;
import io.floci.aws.services.ec2.model.NetworkInterfaceListResult;
import io.floci.aws.services.ec2.model.NetworkInterfacePrivateIpAddress;
import io.floci.aws.services.ec2.model.Placement;
@@ -125,6 +126,7 @@ import static io.floci.aws.services.ec2.Ec2KeyPairFixtures.ED25519_KEY;
import static io.floci.aws.services.ec2.Ec2KeyPairFixtures.RSA_FINGERPRINT;
import static io.floci.aws.services.ec2.Ec2KeyPairFixtures.RSA_KEY;
import static io.floci.aws.services.ec2.Ec2KeyPairFixtures.TRUNCATED_RSA_KEY;
import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore;
import static io.floci.aws.testing.ThreadStates.awaitBlockedOrDone;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -663,6 +665,48 @@ class Ec2ServiceTest {
List.of(data), false, null, null);
}
@ParameterizedTest
@ValueSource(strings = {"grant-before-detach", "detach-before-grant", "grant-before-describe"})
void interfacePermissionOperationsShareAttachmentSynchronization(String order) throws Exception {
String region = "us-east-1";
ParkingStore<NetworkInterface> interfaces = new ParkingStore<>();
AccountAwareStorageBackend<NetworkInterface> interfaceStore =
new AccountAwareStorageBackend<>(interfaces, null, "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-network-interfaces.json", interfaceStore)));
String subnet = publicSubnet(service, region, "10.243.0.0/24");
NetworkInterface eni = service.createNetworkInterface(region, subnet, null, "10.243.0.10",
List.of(), List.of(), List.of());
Instance instance = service.runInstances(region, "ami-1234567890abcdef0", "t3.micro",
1, 1, null, List.of(), subnet, null, List.of(), null, null, null).getInstances().getFirst();
instance.setState(InstanceState.running());
NetworkInterfaceAttachment attachment = service.attachNetworkInterface(region,
eni.getNetworkInterfaceId(), instance.getInstanceId(), 1);
Runnable grant = () -> service.createNetworkInterfacePermission(region, eni.getNetworkInterfaceId(),
"111111111111", null, Ec2Service.INSTANCE_ATTACH);
Runnable detach = () -> service.detachNetworkInterface(region, attachment.getAttachmentId(), false);
Runnable describe = () -> assertEquals(1, service.describeNetworkInterfacePermissions(
region, List.of(), List.of(), null, null).permissions().size());
interfaces.parkNextWrite();
switch (order) {
case "grant-before-detach" -> assertOperationWaitsForParkedStore(interfaces, grant, detach);
case "detach-before-grant" -> assertOperationWaitsForParkedStore(interfaces, detach, grant);
case "grant-before-describe" -> assertOperationWaitsForParkedStore(interfaces, grant, describe);
default -> throw new IllegalArgumentException(order);
}
assertEquals(1, service.describeNetworkInterfacePermissions(
region, List.of(), List.of(), null, null).permissions().size());
if (!"grant-before-describe".equals(order)) {
assertNull(eni.getAttachment());
assertTrue(instance.getNetworkInterfaces().stream()
.noneMatch(value -> eni.getNetworkInterfaceId().equals(value.getNetworkInterfaceId())));
}
}
@Test
void subnetAvailableAddressesTrackAllocationsAndRelease() {
AccountAwareStorageBackend<NetworkInterface> interfaceStore =