diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2RouteTargetTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2RouteTargetTest.java new file mode 100644 index 0000000000..08b0a64e82 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2RouteTargetTest.java @@ -0,0 +1,192 @@ +package com.floci.test; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.ec2.Ec2Client; +import software.amazon.awssdk.services.ec2.model.CreateRouteRequest; +import software.amazon.awssdk.services.ec2.model.CreateRouteTableRequest; +import software.amazon.awssdk.services.ec2.model.CreateVpcRequest; +import software.amazon.awssdk.services.ec2.model.DeleteRouteRequest; +import software.amazon.awssdk.services.ec2.model.DeleteRouteTableRequest; +import software.amazon.awssdk.services.ec2.model.DeleteVpcRequest; +import software.amazon.awssdk.services.ec2.model.DescribeRouteTablesRequest; +import software.amazon.awssdk.services.ec2.model.Ec2Exception; +import software.amazon.awssdk.services.ec2.model.Filter; +import software.amazon.awssdk.services.ec2.model.ReplaceRouteRequest; +import software.amazon.awssdk.services.ec2.model.Route; +import software.amazon.awssdk.services.ec2.model.RouteTable; +import software.amazon.awssdk.services.ec2.model.RunInstancesRequest; +import software.amazon.awssdk.services.ec2.model.TerminateInstancesRequest; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class Ec2RouteTargetTest { + + private static final String CIDR_INST = "10.150.1.0/24"; + private static final String CIDR_ENI = "10.150.2.0/24"; + private static final String CIDR_REPLACE = "10.150.3.0/24"; + private static final String ENI_ID = "eni-0123456789abcdef0"; + private static final String REPLACEMENT_ENI_ID = "eni-0987654321fedcba0"; + + @Test + @DisplayName("CreateRoute, ReplaceRoute and DescribeRouteTables with instance and network-interface targets") + void routeTargetsWithSdk() { + try (Ec2Client ec2 = TestFixtures.ec2Client()) { + String vpcId = ec2.createVpc(CreateVpcRequest.builder() + .cidrBlock("10.150.0.0/16") + .build()) + .vpc().vpcId(); + + String routeTableId = ec2.createRouteTable(CreateRouteTableRequest.builder() + .vpcId(vpcId) + .build()) + .routeTable().routeTableId(); + + String instanceId = ec2.runInstances(RunInstancesRequest.builder() + .imageId("ami-12345678") + .minCount(1) + .maxCount(1) + .build()) + .instances().get(0).instanceId(); + + try { + // 1. Create route targeting instanceId + ec2.createRoute(CreateRouteRequest.builder() + .routeTableId(routeTableId) + .destinationCidrBlock(CIDR_INST) + .instanceId(instanceId) + .build()); + + // 2. Create route targeting networkInterfaceId + ec2.createRoute(CreateRouteRequest.builder() + .routeTableId(routeTableId) + .destinationCidrBlock(CIDR_ENI) + .networkInterfaceId(ENI_ID) + .build()); + + // 3. Describe route table and verify targets are present on Route objects + List tables = ec2.describeRouteTables(DescribeRouteTablesRequest.builder() + .routeTableIds(routeTableId) + .build()) + .routeTables(); + assertThat(tables).hasSize(1); + RouteTable table = tables.get(0); + + Route instanceRoute = table.routes().stream() + .filter(r -> CIDR_INST.equals(r.destinationCidrBlock())) + .findFirst() + .orElseThrow(); + assertThat(instanceRoute.instanceId()).isEqualTo(instanceId); + assertThat(instanceRoute.instanceOwnerId()).isNotNull(); + assertThat(instanceRoute.instanceOwnerId()).isEqualTo(table.ownerId()); + assertThat(instanceRoute.networkInterfaceId()).isNull(); + + Route eniRoute = table.routes().stream() + .filter(r -> CIDR_ENI.equals(r.destinationCidrBlock())) + .findFirst() + .orElseThrow(); + assertThat(eniRoute.networkInterfaceId()).isEqualTo(ENI_ID); + assertThat(eniRoute.instanceId()).isNull(); + assertThat(eniRoute.instanceOwnerId()).isNull(); + + // 4. DescribeRouteTables filters + List filteredByInstance = ec2.describeRouteTables(DescribeRouteTablesRequest.builder() + .filters(Filter.builder().name("route.instance-id").values(instanceId).build()) + .build()) + .routeTables(); + assertThat(filteredByInstance).anyMatch(rt -> routeTableId.equals(rt.routeTableId())); + + List filteredByEni = ec2.describeRouteTables(DescribeRouteTablesRequest.builder() + .filters(Filter.builder().name("route.network-interface-id").values(ENI_ID).build()) + .build()) + .routeTables(); + assertThat(filteredByEni).anyMatch(rt -> routeTableId.equals(rt.routeTableId())); + + // 5. ReplaceRoute: replace instance target with network interface target + ec2.createRoute(CreateRouteRequest.builder() + .routeTableId(routeTableId) + .destinationCidrBlock(CIDR_REPLACE) + .instanceId(instanceId) + .build()); + + ec2.replaceRoute(ReplaceRouteRequest.builder() + .routeTableId(routeTableId) + .destinationCidrBlock(CIDR_REPLACE) + .networkInterfaceId(REPLACEMENT_ENI_ID) + .build()); + + RouteTable tableAfterReplace = ec2.describeRouteTables(DescribeRouteTablesRequest.builder() + .routeTableIds(routeTableId) + .build()) + .routeTables().get(0); + Route replacedRoute = tableAfterReplace.routes().stream() + .filter(r -> CIDR_REPLACE.equals(r.destinationCidrBlock())) + .findFirst() + .orElseThrow(); + assertThat(replacedRoute.networkInterfaceId()).isEqualTo(REPLACEMENT_ENI_ID); + assertThat(replacedRoute.instanceId()).isNull(); + assertThat(replacedRoute.instanceOwnerId()).isNull(); + + // 6. Non-existent instance target rejected + assertThatThrownBy(() -> ec2.createRoute(CreateRouteRequest.builder() + .routeTableId(routeTableId) + .destinationCidrBlock("10.150.88.0/24") + .instanceId("i-doesnotexist") + .build())) + .isInstanceOfSatisfying(Ec2Exception.class, ex -> { + assertThat(ex.statusCode()).isEqualTo(400); + assertThat(ex.awsErrorDetails().errorCode()).isEqualTo("InvalidInstanceID.NotFound"); + }); + + assertThatThrownBy(() -> ec2.replaceRoute(ReplaceRouteRequest.builder() + .routeTableId(routeTableId) + .destinationCidrBlock(CIDR_REPLACE) + .instanceId("i-doesnotexist") + .build())) + .isInstanceOfSatisfying(Ec2Exception.class, ex -> { + assertThat(ex.statusCode()).isEqualTo(400); + assertThat(ex.awsErrorDetails().errorCode()).isEqualTo("InvalidInstanceID.NotFound"); + }); + + // 7. Error validation: combining targets must fail + assertThatThrownBy(() -> ec2.createRoute(CreateRouteRequest.builder() + .routeTableId(routeTableId) + .destinationCidrBlock("10.150.99.0/24") + .instanceId(instanceId) + .networkInterfaceId(ENI_ID) + .build())) + .isInstanceOfSatisfying(Ec2Exception.class, ex -> { + assertThat(ex.statusCode()).isEqualTo(400); + assertThat(ex.awsErrorDetails().errorCode()).isEqualTo("InvalidParameterCombination"); + }); + + // 8. Delete route + ec2.deleteRoute(DeleteRouteRequest.builder() + .routeTableId(routeTableId) + .destinationCidrBlock(CIDR_INST) + .build()); + + RouteTable tableAfterDelete = ec2.describeRouteTables(DescribeRouteTablesRequest.builder() + .routeTableIds(routeTableId) + .build()) + .routeTables().get(0); + assertThat(tableAfterDelete.routes().stream() + .noneMatch(r -> CIDR_INST.equals(r.destinationCidrBlock()))).isTrue(); + + } finally { + ec2.terminateInstances(TerminateInstancesRequest.builder() + .instanceIds(instanceId) + .build()); + ec2.deleteRouteTable(DeleteRouteTableRequest.builder() + .routeTableId(routeTableId) + .build()); + ec2.deleteVpc(DeleteVpcRequest.builder() + .vpcId(vpcId) + .build()); + } + } + } +} diff --git a/docs/services/ec2.md b/docs/services/ec2.md index 067afdc48c..222e4e6399 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -311,8 +311,8 @@ that publish those images. | DeleteRouteTable | Deletes a route table from the local EC2 store. | | AssociateRouteTable | Associates a route table with a subnet. | | DisassociateRouteTable | Removes a route table association. | -| CreateRoute | Adds a route to a route table. Accepts `VpcPeeringConnectionId` as a target (alongside `GatewayId`/`NatGatewayId`/`EgressOnlyInternetGatewayId`) and reports it back on `DescribeRouteTables`. | -| ReplaceRoute | Replaces the target of an existing route. | +| CreateRoute | Adds a route to a route table. Accepts `InstanceId`, `NetworkInterfaceId`, and `VpcPeeringConnectionId` as targets (alongside `GatewayId`/`NatGatewayId`/`EgressOnlyInternetGatewayId`) and reports them back on `DescribeRouteTables`. | +| ReplaceRoute | Replaces the target of an existing route, accepting `InstanceId`, `NetworkInterfaceId`, and `VpcPeeringConnectionId`. | | DeleteRoute | Removes a route from a route table. | ### VPC Peering Connections diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java index e819bbecb0..dbd28fbdb6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java @@ -32,8 +32,8 @@ public class Ec2QueryHandler { /** ReplaceRoute targets AWS accepts that a stored {@code Route} cannot represent here. */ private static final List UNSUPPORTED_ROUTE_TARGETS = List.of( - "CarrierGatewayId", "CoreNetworkArn", "EgressOnlyInternetGatewayId", "InstanceId", - "LocalGatewayId", "NetworkInterfaceId", "OdbNetworkArn", + "CarrierGatewayId", "CoreNetworkArn", "EgressOnlyInternetGatewayId", + "LocalGatewayId", "OdbNetworkArn", "TransitGatewayId", "VpcEndpointId"); /** The gateway id a route table's built-in route carries (see Ec2Service#createRouteTable). */ @@ -3644,7 +3644,10 @@ public class Ec2QueryHandler { // egress route is not silently rewritten into a targetless one. String eigwId = p.getFirst("EgressOnlyInternetGatewayId"); String pcxId = p.getFirst("VpcPeeringConnectionId"); - service.createRoute(region, rtId, dest, destIpv6, destPrefixList, gwId, natGwId, eigwId, pcxId); + String instanceId = p.getFirst("InstanceId"); + String networkInterfaceId = p.getFirst("NetworkInterfaceId"); + service.createRoute(region, rtId, dest, destIpv6, destPrefixList, gwId, natGwId, eigwId, pcxId, + instanceId, networkInterfaceId); return booleanResponse("CreateRoute"); } @@ -3665,16 +3668,19 @@ public class Ec2QueryHandler { String gwId = p.getFirst("GatewayId"); String natGwId = p.getFirst("NatGatewayId"); String pcxId = p.getFirst("VpcPeeringConnectionId"); + String instanceId = p.getFirst("InstanceId"); + String networkInterfaceId = p.getFirst("NetworkInterfaceId"); // Resetting a route to the local target is expressible: `local` is the gateway id the // route table's built-in route already carries, so it needs no new field on Route. if (Boolean.parseBoolean(p.getFirst("LocalTarget"))) { - if (gwId != null || natGwId != null || pcxId != null) { + if (gwId != null || natGwId != null || pcxId != null || instanceId != null || networkInterfaceId != null) { throw new AwsException("InvalidParameterCombination", "ReplaceRoute takes exactly one target.", 400); } gwId = LOCAL_GATEWAY_ID; } service.replaceRoute(region, rtId, dest, destIpv6, destPrefixList, gwId, natGwId, pcxId, + instanceId, networkInterfaceId, Boolean.parseBoolean(p.getFirst("DryRun"))); return booleanResponse("ReplaceRoute"); } @@ -4733,7 +4739,10 @@ public class Ec2QueryHandler { .elem("destinationIpv6CidrBlock", r.getDestinationIpv6CidrBlock()) .elem("destinationPrefixListId", r.getDestinationPrefixListId()) .elem("gatewayId", r.getGatewayId()) + .elem("instanceId", r.getInstanceId()) + .elem("instanceOwnerId", r.getInstanceOwnerId()) .elem("natGatewayId", r.getNatGatewayId()) + .elem("networkInterfaceId", r.getNetworkInterfaceId()) .elem("egressOnlyInternetGatewayId", r.getEgressOnlyInternetGatewayId()) .elem("vpcPeeringConnectionId", r.getVpcPeeringConnectionId()) .elem("state", r.getState()) 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 726fc27294..65137e5ae4 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 @@ -1,5 +1,99 @@ package io.github.hectorvent.floci.services.ec2; +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsArnUtils; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.AwsPartition; +import io.github.hectorvent.floci.core.common.AwsRegions; +import io.github.hectorvent.floci.core.common.CidrCanonicalizer; +import io.github.hectorvent.floci.core.common.ContainerTeardown; +import io.github.hectorvent.floci.core.common.RequestContext; +import io.github.hectorvent.floci.core.common.RequestScopes; +import io.github.hectorvent.floci.core.resource.ExplorerResource; +import io.github.hectorvent.floci.core.resource.ResourceProvider; +import io.github.hectorvent.floci.core.resource.SupportedResourceType; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.ec2.model.Address; +import io.github.hectorvent.floci.services.ec2.model.BlockDeviceMapping; +import io.github.hectorvent.floci.services.ec2.model.CapacityReservation; +import io.github.hectorvent.floci.services.ec2.model.EbsBlockDevice; +import io.github.hectorvent.floci.services.ec2.model.GroupIdentifier; +import io.github.hectorvent.floci.services.ec2.model.IamInstanceProfileAssociation; +import io.github.hectorvent.floci.services.ec2.model.Image; +import io.github.hectorvent.floci.services.ec2.model.Instance; +import io.github.hectorvent.floci.services.ec2.model.InstanceCreditSpecification; +import io.github.hectorvent.floci.services.ec2.model.InstanceCreditSpecificationListResult; +import io.github.hectorvent.floci.services.ec2.model.InstanceNetworkInterface; +import io.github.hectorvent.floci.services.ec2.model.InstanceState; +import io.github.hectorvent.floci.services.ec2.model.InternetGateway; +import io.github.hectorvent.floci.services.ec2.model.InternetGatewayAttachment; +import io.github.hectorvent.floci.services.ec2.model.IpPermission; +import io.github.hectorvent.floci.services.ec2.model.IpRange; +import io.github.hectorvent.floci.services.ec2.model.Ipv6Range; +import io.github.hectorvent.floci.services.ec2.model.KeyPair; +import io.github.hectorvent.floci.services.ec2.model.LaunchSpecification; +import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate; +import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData; +import io.github.hectorvent.floci.services.ec2.model.ManagedPrefixList; +import io.github.hectorvent.floci.services.ec2.model.NatGateway; +import io.github.hectorvent.floci.services.ec2.model.NatGatewayAddress; +import io.github.hectorvent.floci.services.ec2.model.NetworkAcl; +import io.github.hectorvent.floci.services.ec2.model.NetworkAclAssociation; +import io.github.hectorvent.floci.services.ec2.model.NetworkAclEntry; +import io.github.hectorvent.floci.services.ec2.model.NetworkInterface; +import io.github.hectorvent.floci.services.ec2.model.NetworkInterfaceAssociation; +import io.github.hectorvent.floci.services.ec2.model.NetworkInterfaceAttachment; +import io.github.hectorvent.floci.services.ec2.model.NetworkInterfaceListResult; +import io.github.hectorvent.floci.services.ec2.model.NetworkInterfacePrivateIpAddress; +import io.github.hectorvent.floci.services.ec2.model.Placement; +import io.github.hectorvent.floci.services.ec2.model.PrefixList; +import io.github.hectorvent.floci.services.ec2.model.PrefixListEntry; +import io.github.hectorvent.floci.services.ec2.model.PrefixListId; +import io.github.hectorvent.floci.services.ec2.model.ReferencedSecurityGroup; +import io.github.hectorvent.floci.services.ec2.model.Reservation; +import io.github.hectorvent.floci.services.ec2.model.Route; +import io.github.hectorvent.floci.services.ec2.model.RouteTable; +import io.github.hectorvent.floci.services.ec2.model.RouteTableAssociation; +import io.github.hectorvent.floci.services.ec2.model.SecurityGroup; +import io.github.hectorvent.floci.services.ec2.model.SecurityGroupRule; +import io.github.hectorvent.floci.services.ec2.model.Snapshot; +import io.github.hectorvent.floci.services.ec2.model.SpotInstanceRequest; +import io.github.hectorvent.floci.services.ec2.model.Subnet; +import io.github.hectorvent.floci.services.ec2.model.Tag; +import io.github.hectorvent.floci.services.ec2.model.TransitGateway; +import io.github.hectorvent.floci.services.ec2.model.TransitGatewayOptions; +import io.github.hectorvent.floci.services.ec2.model.TransitGatewayRoute; +import io.github.hectorvent.floci.services.ec2.model.TransitGatewayRouteTable; +import io.github.hectorvent.floci.services.ec2.model.TransitGatewayRouteTablePropagation; +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.UserIdGroupPair; +import io.github.hectorvent.floci.services.ec2.model.Volume; +import io.github.hectorvent.floci.services.ec2.model.VolumeAttachment; +import io.github.hectorvent.floci.services.ec2.model.VolumeModification; +import io.github.hectorvent.floci.services.ec2.model.Vpc; +import io.github.hectorvent.floci.services.ec2.model.VpcCidrBlockAssociation; +import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint; +import io.github.hectorvent.floci.services.ec2.model.VpcEndpointDnsEntry; +import io.github.hectorvent.floci.services.ec2.model.VpcEndpointSubnetConfiguration; +import io.github.hectorvent.floci.services.ec2.model.VpcIpv6CidrBlockAssociation; +import io.github.hectorvent.floci.services.ec2.model.VpcPeeringConnection; +import io.github.hectorvent.floci.services.ec2.model.VpcPeeringConnectionStateReason; +import io.github.hectorvent.floci.services.ec2.model.VpcPeeringConnectionVpcInfo; +import io.github.hectorvent.floci.services.ec2.net.VpcNetworkManager; +import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager; +import io.github.hectorvent.floci.services.iam.IamService; +import io.github.hectorvent.floci.services.iam.model.InstanceProfile; +import jakarta.annotation.PostConstruct; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.context.ContextNotActiveException; +import jakarta.inject.Inject; +import org.jboss.logging.Logger; + import java.nio.charset.StandardCharsets; import java.time.Duration; import java.time.Instant; @@ -28,101 +122,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.jboss.logging.Logger; - -import com.fasterxml.jackson.core.type.TypeReference; -import io.github.hectorvent.floci.config.EmulatorConfig; -import io.github.hectorvent.floci.core.common.RequestContext; -import io.github.hectorvent.floci.core.common.RequestScopes; -import io.github.hectorvent.floci.core.common.AwsArnUtils; -import io.github.hectorvent.floci.core.common.AwsException; -import io.github.hectorvent.floci.core.common.AwsPartition; -import io.github.hectorvent.floci.core.common.AwsRegions; -import io.github.hectorvent.floci.core.common.CidrCanonicalizer; -import io.github.hectorvent.floci.core.common.ContainerTeardown; -import io.github.hectorvent.floci.core.resource.ExplorerResource; -import io.github.hectorvent.floci.core.resource.ResourceProvider; -import io.github.hectorvent.floci.core.resource.SupportedResourceType; -import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; -import io.github.hectorvent.floci.core.storage.InMemoryStorage; -import io.github.hectorvent.floci.core.storage.StorageBackend; -import io.github.hectorvent.floci.core.storage.StorageFactory; -import io.github.hectorvent.floci.services.ec2.model.Address; -import io.github.hectorvent.floci.services.ec2.model.BlockDeviceMapping; -import io.github.hectorvent.floci.services.ec2.model.CapacityReservation; -import io.github.hectorvent.floci.services.ec2.model.EbsBlockDevice; -import io.github.hectorvent.floci.services.ec2.model.GroupIdentifier; -import io.github.hectorvent.floci.services.ec2.model.IamInstanceProfileAssociation; -import io.github.hectorvent.floci.services.ec2.net.VpcNetworkManager; -import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager; -import io.github.hectorvent.floci.services.ec2.model.Image; -import io.github.hectorvent.floci.services.ec2.model.Instance; -import io.github.hectorvent.floci.services.ec2.model.InstanceCreditSpecification; -import io.github.hectorvent.floci.services.ec2.model.InstanceCreditSpecificationListResult; -import io.github.hectorvent.floci.services.ec2.model.InstanceNetworkInterface; -import io.github.hectorvent.floci.services.ec2.model.InstanceState; -import io.github.hectorvent.floci.services.ec2.model.NetworkInterface; -import io.github.hectorvent.floci.services.ec2.model.NetworkInterfaceAssociation; -import io.github.hectorvent.floci.services.ec2.model.NetworkInterfaceAttachment; -import io.github.hectorvent.floci.services.ec2.model.NetworkInterfaceListResult; -import io.github.hectorvent.floci.services.ec2.model.NetworkInterfacePrivateIpAddress; -import io.github.hectorvent.floci.services.ec2.model.InternetGateway; -import io.github.hectorvent.floci.services.ec2.model.InternetGatewayAttachment; -import io.github.hectorvent.floci.services.ec2.model.IpPermission; -import io.github.hectorvent.floci.services.ec2.model.IpRange; -import io.github.hectorvent.floci.services.ec2.model.Ipv6Range; -import io.github.hectorvent.floci.services.ec2.model.KeyPair; -import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate; -import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData; -import io.github.hectorvent.floci.services.ec2.model.ManagedPrefixList; -import io.github.hectorvent.floci.services.ec2.model.NatGateway; -import io.github.hectorvent.floci.services.ec2.model.NatGatewayAddress; -import io.github.hectorvent.floci.services.ec2.model.NetworkAcl; -import io.github.hectorvent.floci.services.ec2.model.NetworkAclAssociation; -import io.github.hectorvent.floci.services.ec2.model.NetworkAclEntry; -import io.github.hectorvent.floci.services.ec2.model.PrefixList; -import io.github.hectorvent.floci.services.ec2.model.PrefixListId; -import io.github.hectorvent.floci.services.ec2.model.PrefixListEntry; -import io.github.hectorvent.floci.services.ec2.model.Placement; -import io.github.hectorvent.floci.services.ec2.model.ReferencedSecurityGroup; -import io.github.hectorvent.floci.services.ec2.model.Reservation; -import io.github.hectorvent.floci.services.ec2.model.Route; -import io.github.hectorvent.floci.services.ec2.model.RouteTable; -import io.github.hectorvent.floci.services.ec2.model.RouteTableAssociation; -import io.github.hectorvent.floci.services.ec2.model.SecurityGroup; -import io.github.hectorvent.floci.services.ec2.model.SecurityGroupRule; -import io.github.hectorvent.floci.services.ec2.model.Snapshot; -import io.github.hectorvent.floci.services.ec2.model.Subnet; -import io.github.hectorvent.floci.services.ec2.model.Tag; -import io.github.hectorvent.floci.services.ec2.model.TransitGateway; -import io.github.hectorvent.floci.services.ec2.model.TransitGatewayOptions; -import io.github.hectorvent.floci.services.ec2.model.TransitGatewayRoute; -import io.github.hectorvent.floci.services.ec2.model.TransitGatewayRouteTable; -import io.github.hectorvent.floci.services.ec2.model.TransitGatewayRouteTablePropagation; -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.UserIdGroupPair; -import io.github.hectorvent.floci.services.ec2.model.Volume; -import io.github.hectorvent.floci.services.ec2.model.VolumeAttachment; -import io.github.hectorvent.floci.services.ec2.model.VolumeModification; -import io.github.hectorvent.floci.services.ec2.model.Vpc; -import io.github.hectorvent.floci.services.ec2.model.VpcCidrBlockAssociation; -import io.github.hectorvent.floci.services.ec2.model.VpcIpv6CidrBlockAssociation; -import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint; -import io.github.hectorvent.floci.services.ec2.model.VpcEndpointDnsEntry; -import io.github.hectorvent.floci.services.ec2.model.VpcEndpointSubnetConfiguration; -import io.github.hectorvent.floci.services.ec2.model.VpcPeeringConnection; -import io.github.hectorvent.floci.services.ec2.model.VpcPeeringConnectionStateReason; -import io.github.hectorvent.floci.services.ec2.model.VpcPeeringConnectionVpcInfo; -import io.github.hectorvent.floci.services.iam.IamService; -import io.github.hectorvent.floci.services.iam.model.InstanceProfile; -import jakarta.annotation.PostConstruct; -import io.github.hectorvent.floci.services.ec2.model.LaunchSpecification; -import io.github.hectorvent.floci.services.ec2.model.SpotInstanceRequest; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.enterprise.context.ContextNotActiveException; -import jakarta.inject.Inject; - @ApplicationScoped public class Ec2Service implements ContainerTeardown, ResourceProvider { @@ -7308,13 +7307,17 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { * to would be a behaviour change beyond this fix. */ private static void requireEgressOnlyGatewayIsTheOnlyTarget(String gatewayId, String natGatewayId, - String egressOnlyInternetGatewayId) { + String egressOnlyInternetGatewayId, + String vpcPeeringConnectionId, + String instanceId, + String networkInterfaceId) { if (!isSet(egressOnlyInternetGatewayId)) { return; } - if (isSet(gatewayId) || isSet(natGatewayId)) { + if (isSet(gatewayId) || isSet(natGatewayId) || isSet(vpcPeeringConnectionId) + || isSet(instanceId) || isSet(networkInterfaceId)) { throw new AwsException("InvalidParameterCombination", - "EgressOnlyInternetGatewayId cannot be combined with GatewayId or NatGatewayId; " + "EgressOnlyInternetGatewayId cannot be combined with another target; " + "a route takes one target.", 400); } } @@ -7327,14 +7330,55 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { */ private static void requireVpcPeeringConnectionIsTheOnlyTarget(String gatewayId, String natGatewayId, String egressOnlyInternetGatewayId, - String vpcPeeringConnectionId) { + String vpcPeeringConnectionId, + String instanceId, + String networkInterfaceId) { if (!isSet(vpcPeeringConnectionId)) { return; } - if (isSet(gatewayId) || isSet(natGatewayId) || isSet(egressOnlyInternetGatewayId)) { + if (isSet(gatewayId) || isSet(natGatewayId) || isSet(egressOnlyInternetGatewayId) + || isSet(instanceId) || isSet(networkInterfaceId)) { throw new AwsException("InvalidParameterCombination", - "VpcPeeringConnectionId cannot be combined with GatewayId, NatGatewayId or " - + "EgressOnlyInternetGatewayId; a route takes one target.", 400); + "VpcPeeringConnectionId cannot be combined with GatewayId, NatGatewayId, " + + "EgressOnlyInternetGatewayId, InstanceId or NetworkInterfaceId; a route takes one target.", 400); + } + } + + private String resolveInstanceOwnerId(String region, String instanceId) { + if (!isSet(instanceId)) { + return null; + } + getRequiredInstance(region, instanceId); + return callerAccountId(); + } + + private static void requireInstanceIsTheOnlyTarget(String gatewayId, String natGatewayId, + String egressOnlyInternetGatewayId, + String vpcPeeringConnectionId, + String networkInterfaceId, + String instanceId) { + if (!isSet(instanceId)) { + return; + } + if (isSet(gatewayId) || isSet(natGatewayId) || isSet(egressOnlyInternetGatewayId) + || isSet(vpcPeeringConnectionId) || isSet(networkInterfaceId)) { + throw new AwsException("InvalidParameterCombination", + "InstanceId cannot be combined with another target; a route takes one target.", 400); + } + } + + private static void requireNetworkInterfaceIsTheOnlyTarget(String gatewayId, String natGatewayId, + String egressOnlyInternetGatewayId, + String vpcPeeringConnectionId, + String instanceId, + String networkInterfaceId) { + if (!isSet(networkInterfaceId)) { + return; + } + if (isSet(gatewayId) || isSet(natGatewayId) || isSet(egressOnlyInternetGatewayId) + || isSet(vpcPeeringConnectionId) || isSet(instanceId)) { + throw new AwsException("InvalidParameterCombination", + "NetworkInterfaceId cannot be combined with another target; a route takes one target.", 400); } } @@ -7342,11 +7386,26 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { String destinationIpv6CidrBlock, String destinationPrefixListId, String gatewayId, String natGatewayId, String egressOnlyInternetGatewayId, String vpcPeeringConnectionId) { + createRoute(region, routeTableId, destinationCidrBlock, destinationIpv6CidrBlock, + destinationPrefixListId, gatewayId, natGatewayId, egressOnlyInternetGatewayId, + vpcPeeringConnectionId, null, null); + } + + public void createRoute(String region, String routeTableId, String destinationCidrBlock, + String destinationIpv6CidrBlock, String destinationPrefixListId, + String gatewayId, String natGatewayId, + String egressOnlyInternetGatewayId, String vpcPeeringConnectionId, + String instanceId, String networkInterfaceId) { requireExactlyOneDestination("CreateRoute", destinationCidrBlock, destinationIpv6CidrBlock, destinationPrefixListId); - requireEgressOnlyGatewayIsTheOnlyTarget(gatewayId, natGatewayId, egressOnlyInternetGatewayId); + requireEgressOnlyGatewayIsTheOnlyTarget(gatewayId, natGatewayId, egressOnlyInternetGatewayId, + vpcPeeringConnectionId, instanceId, networkInterfaceId); requireVpcPeeringConnectionIsTheOnlyTarget(gatewayId, natGatewayId, egressOnlyInternetGatewayId, - vpcPeeringConnectionId); + vpcPeeringConnectionId, instanceId, networkInterfaceId); + requireInstanceIsTheOnlyTarget(gatewayId, natGatewayId, egressOnlyInternetGatewayId, + vpcPeeringConnectionId, networkInterfaceId, instanceId); + requireNetworkInterfaceIsTheOnlyTarget(gatewayId, natGatewayId, egressOnlyInternetGatewayId, + vpcPeeringConnectionId, instanceId, networkInterfaceId); final String canonicalDestinationCidrBlock = canonicalizeIpv4Cidr(destinationCidrBlock); ensureDefaultResources(region); synchronized (lockFor(key(region, routeTableId))) { @@ -7364,12 +7423,16 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { destinationPrefixListId) + " already exists", 400); } + String effectiveInstanceOwnerId = resolveInstanceOwnerId(region, instanceId); Route route = new Route(canonicalDestinationCidrBlock, gatewayId, "CreateRoute"); route.setDestinationIpv6CidrBlock(destinationIpv6CidrBlock); route.setDestinationPrefixListId(destinationPrefixListId); route.setNatGatewayId(natGatewayId); route.setEgressOnlyInternetGatewayId(egressOnlyInternetGatewayId); route.setVpcPeeringConnectionId(vpcPeeringConnectionId); + route.setInstanceId(instanceId); + route.setInstanceOwnerId(effectiveInstanceOwnerId); + route.setNetworkInterfaceId(networkInterfaceId); next.add(route); current.setRoutes(next); routeTables.put(key(region, routeTableId), current); @@ -7380,25 +7443,39 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { String destinationIpv6CidrBlock, String destinationPrefixListId, String gatewayId, String natGatewayId, String vpcPeeringConnectionId) { replaceRoute(region, routeTableId, destinationCidrBlock, destinationIpv6CidrBlock, - destinationPrefixListId, gatewayId, natGatewayId, vpcPeeringConnectionId, false); + destinationPrefixListId, gatewayId, natGatewayId, vpcPeeringConnectionId, + null, null, false); } public void replaceRoute(String region, String routeTableId, String destinationCidrBlock, String destinationIpv6CidrBlock, String destinationPrefixListId, String gatewayId, String natGatewayId, String vpcPeeringConnectionId, boolean dryRun) { + replaceRoute(region, routeTableId, destinationCidrBlock, destinationIpv6CidrBlock, + destinationPrefixListId, gatewayId, natGatewayId, vpcPeeringConnectionId, + null, null, dryRun); + } + + public void replaceRoute(String region, String routeTableId, String destinationCidrBlock, + String destinationIpv6CidrBlock, String destinationPrefixListId, + String gatewayId, String natGatewayId, String vpcPeeringConnectionId, + String instanceId, String networkInterfaceId, + boolean dryRun) { requireExactlyOneDestination("ReplaceRoute", destinationCidrBlock, destinationIpv6CidrBlock, destinationPrefixListId); // AWS takes exactly one target. Rejecting both-or-neither also keeps the targets this - // emulator cannot model (transit gateway, network interface, ...) from silently clearing + // emulator cannot model (transit gateway, ...) from silently clearing // the route and reporting success. boolean hasGateway = isSet(gatewayId); boolean hasNatGateway = isSet(natGatewayId); boolean hasPeeringConnection = isSet(vpcPeeringConnectionId); - if ((hasGateway ? 1 : 0) + (hasNatGateway ? 1 : 0) + (hasPeeringConnection ? 1 : 0) != 1) { + boolean hasInstance = isSet(instanceId); + boolean hasNetworkInterface = isSet(networkInterfaceId); + if ((hasGateway ? 1 : 0) + (hasNatGateway ? 1 : 0) + (hasPeeringConnection ? 1 : 0) + + (hasInstance ? 1 : 0) + (hasNetworkInterface ? 1 : 0) != 1) { throw new AwsException("InvalidParameterCombination", - "ReplaceRoute takes exactly one target, and only GatewayId, NatGatewayId or " - + "VpcPeeringConnectionId is supported.", 400); + "ReplaceRoute takes exactly one target, and only GatewayId, NatGatewayId, " + + "VpcPeeringConnectionId, InstanceId or NetworkInterfaceId is supported.", 400); } final String canonicalDestinationCidrBlock = canonicalizeIpv4Cidr(destinationCidrBlock); @@ -7416,6 +7493,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { destinationPrefixListId) + " does not exist", 400)); + String effectiveInstanceOwnerId = hasInstance + ? resolveInstanceOwnerId(region, instanceId) + : null; + if (dryRun) { throw new AwsException("DryRunOperation", "Request would have succeeded, but DryRun flag is set.", 412); @@ -7428,6 +7509,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { replacement.setDestinationPrefixListId(destinationPrefixListId); replacement.setNatGatewayId(hasNatGateway ? natGatewayId : null); replacement.setVpcPeeringConnectionId(hasPeeringConnection ? vpcPeeringConnectionId : null); + replacement.setInstanceId(hasInstance ? instanceId : null); + replacement.setInstanceOwnerId(effectiveInstanceOwnerId); + replacement.setNetworkInterfaceId(hasNetworkInterface ? networkInterfaceId : null); next.set(next.indexOf(existing), replacement); current.setRoutes(next); routeTables.put(key(region, routeTableId), current); @@ -7604,6 +7688,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { return findAnyVpcEntry(region, vpcId).map(AccountAwareStorageBackend.OwnedEntry::account); } + public List describeVpcPeeringConnections(String region, List ids, Map> filters) { ensureDefaultResources(region); @@ -8332,6 +8417,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { case "route.destination-prefix-list-id" -> rt.getRoutes().stream() .anyMatch(r -> r.getDestinationPrefixListId() != null && matchesValue(values, r.getDestinationPrefixListId())); + case "route.instance-id" -> rt.getRoutes().stream() + .anyMatch(r -> r.getInstanceId() != null && matchesValue(values, r.getInstanceId())); + case "route.network-interface-id" -> rt.getRoutes().stream() + .anyMatch(r -> r.getNetworkInterfaceId() != null && matchesValue(values, r.getNetworkInterfaceId())); default -> true; }; } diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Route.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Route.java index c0e8ec416a..2542fda931 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Route.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Route.java @@ -12,7 +12,10 @@ public class Route { private String destinationIpv6CidrBlock; private String destinationPrefixListId; private String gatewayId; + private String instanceId; + private String instanceOwnerId; private String natGatewayId; + private String networkInterfaceId; private String egressOnlyInternetGatewayId; private String vpcPeeringConnectionId; private String state = "active"; @@ -61,9 +64,18 @@ public class Route { public String getGatewayId() { return gatewayId; } public void setGatewayId(String gatewayId) { this.gatewayId = gatewayId; } + public String getInstanceId() { return instanceId; } + public void setInstanceId(String instanceId) { this.instanceId = instanceId; } + + public String getInstanceOwnerId() { return instanceOwnerId; } + public void setInstanceOwnerId(String instanceOwnerId) { this.instanceOwnerId = instanceOwnerId; } + public String getNatGatewayId() { return natGatewayId; } public void setNatGatewayId(String natGatewayId) { this.natGatewayId = natGatewayId; } + public String getNetworkInterfaceId() { return networkInterfaceId; } + public void setNetworkInterfaceId(String networkInterfaceId) { this.networkInterfaceId = networkInterfaceId; } + public String getEgressOnlyInternetGatewayId() { return egressOnlyInternetGatewayId; } public void setEgressOnlyInternetGatewayId(String egressOnlyInternetGatewayId) { this.egressOnlyInternetGatewayId = egressOnlyInternetGatewayId; } diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2RouteTargetIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2RouteTargetIntegrationTest.java new file mode 100644 index 0000000000..b3601ce69d --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2RouteTargetIntegrationTest.java @@ -0,0 +1,297 @@ +package io.github.hectorvent.floci.services.ec2; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ExtractableResponse; +import io.restassured.response.Response; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.xml.HasXPath.hasXPath; + +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class Ec2RouteTargetIntegrationTest { + + private static final String AUTH_HEADER = + "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request"; + + private static final String CIDR_INST = "10.10.1.0/24"; + private static final String CIDR_ENI = "10.10.2.0/24"; + private static final String CIDR_REPLACE = "10.10.3.0/24"; + private static final String ENI_ID = "eni-0123456789abcdef0"; + + private static String routeTableId; + private static String routeTableOwnerId; + private static String instanceId; + + private static RequestSpecification ec2() { + return given().header("Authorization", AUTH_HEADER); + } + + @Test + @Order(1) + void setUpRouteTableAndInstance() { + String vpcId = ec2() + .formParam("Action", "CreateVpc") + .formParam("CidrBlock", "10.10.0.0/16") + .when() + .post("/") + .then() + .statusCode(200) + .extract().path("CreateVpcResponse.vpc.vpcId"); + + ExtractableResponse response = ec2() + .formParam("Action", "CreateRouteTable") + .formParam("VpcId", vpcId) + .when() + .post("/") + .then() + .statusCode(200) + .extract(); + + routeTableId = response.path("CreateRouteTableResponse.routeTable.routeTableId"); + routeTableOwnerId = response.path("CreateRouteTableResponse.routeTable.ownerId"); + + instanceId = ec2() + .formParam("Action", "RunInstances") + .formParam("ImageId", "ami-12345678") + .formParam("MinCount", "1") + .formParam("MaxCount", "1") + .when() + .post("/") + .then() + .statusCode(200) + .extract().path("RunInstancesResponse.instancesSet.item.instanceId"); + } + + @Test + @Order(2) + void createRouteWithInstanceIdAndDescribe() { + ec2() + .formParam("Action", "CreateRoute") + .formParam("RouteTableId", routeTableId) + .formParam("DestinationCidrBlock", CIDR_INST) + .formParam("InstanceId", instanceId) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("/*[local-name()='CreateRouteResponse']/*[local-name()='return']", equalTo("true"))); + + ec2() + .formParam("Action", "DescribeRouteTables") + .formParam("RouteTableId.1", routeTableId) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("//*[local-name()='routeSet']/*[local-name()='item'][*[local-name()='destinationCidrBlock']='" + CIDR_INST + "']/*[local-name()='instanceId']", equalTo(instanceId))) + .body(hasXPath("//*[local-name()='routeSet']/*[local-name()='item'][*[local-name()='destinationCidrBlock']='" + CIDR_INST + "']/*[local-name()='instanceOwnerId']", equalTo(routeTableOwnerId))); + } + + @Test + @Order(3) + void createRouteRejectsNonExistentInstanceId() { + ec2() + .formParam("Action", "CreateRoute") + .formParam("RouteTableId", routeTableId) + .formParam("DestinationCidrBlock", "10.10.88.0/24") + .formParam("InstanceId", "i-doesnotexist") + .when() + .post("/") + .then() + .statusCode(400) + .body(containsString("InvalidInstanceID.NotFound")); + } + + @Test + @Order(4) + void createRouteWithNetworkInterfaceIdAndDescribe() { + ec2() + .formParam("Action", "CreateRoute") + .formParam("RouteTableId", routeTableId) + .formParam("DestinationCidrBlock", CIDR_ENI) + .formParam("NetworkInterfaceId", ENI_ID) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("/*[local-name()='CreateRouteResponse']/*[local-name()='return']", equalTo("true"))); + + ec2() + .formParam("Action", "DescribeRouteTables") + .formParam("RouteTableId.1", routeTableId) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("//*[local-name()='routeSet']/*[local-name()='item'][*[local-name()='destinationCidrBlock']='" + CIDR_ENI + "']/*[local-name()='networkInterfaceId']", equalTo(ENI_ID))) + .body(hasXPath("count(//*[local-name()='routeSet']/*[local-name()='item'][*[local-name()='destinationCidrBlock']='" + CIDR_ENI + "']/*[local-name()='instanceOwnerId'])", equalTo("0"))); + } + + @Test + @Order(5) + void createRouteRejectsMultipleTargets() { + ec2() + .formParam("Action", "CreateRoute") + .formParam("RouteTableId", routeTableId) + .formParam("DestinationCidrBlock", "10.10.99.0/24") + .formParam("InstanceId", instanceId) + .formParam("NetworkInterfaceId", ENI_ID) + .when() + .post("/") + .then() + .statusCode(400) + .body(containsString("InvalidParameterCombination")); + + ec2() + .formParam("Action", "CreateRoute") + .formParam("RouteTableId", routeTableId) + .formParam("DestinationCidrBlock", "10.10.99.0/24") + .formParam("InstanceId", instanceId) + .formParam("GatewayId", "igw-12345678") + .when() + .post("/") + .then() + .statusCode(400) + .body(containsString("InvalidParameterCombination")); + } + + @Test + @Order(6) + void replaceRouteWithInstanceAndNetworkInterface() { + ec2() + .formParam("Action", "CreateRoute") + .formParam("RouteTableId", routeTableId) + .formParam("DestinationCidrBlock", CIDR_REPLACE) + .formParam("GatewayId", "igw-replace-init") + .when() + .post("/") + .then() + .statusCode(200); + + ec2() + .formParam("Action", "ReplaceRoute") + .formParam("RouteTableId", routeTableId) + .formParam("DestinationCidrBlock", CIDR_REPLACE) + .formParam("InstanceId", instanceId) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("/*[local-name()='ReplaceRouteResponse']/*[local-name()='return']", equalTo("true"))); + + ec2() + .formParam("Action", "DescribeRouteTables") + .formParam("RouteTableId.1", routeTableId) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("//*[local-name()='routeSet']/*[local-name()='item'][*[local-name()='destinationCidrBlock']='" + CIDR_REPLACE + "']/*[local-name()='instanceId']", equalTo(instanceId))) + .body(hasXPath("//*[local-name()='routeSet']/*[local-name()='item'][*[local-name()='destinationCidrBlock']='" + CIDR_REPLACE + "']/*[local-name()='instanceOwnerId']", equalTo(routeTableOwnerId))); + + ec2() + .formParam("Action", "ReplaceRoute") + .formParam("RouteTableId", routeTableId) + .formParam("DestinationCidrBlock", CIDR_REPLACE) + .formParam("NetworkInterfaceId", ENI_ID) + .when() + .post("/") + .then() + .statusCode(200); + + ec2() + .formParam("Action", "DescribeRouteTables") + .formParam("RouteTableId.1", routeTableId) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("//*[local-name()='routeSet']/*[local-name()='item'][*[local-name()='destinationCidrBlock']='" + CIDR_REPLACE + "']/*[local-name()='networkInterfaceId']", equalTo(ENI_ID))) + .body(hasXPath("count(//*[local-name()='routeSet']/*[local-name()='item'][*[local-name()='destinationCidrBlock']='" + CIDR_REPLACE + "']/*[local-name()='instanceOwnerId'])", equalTo("0"))); + } + + @Test + @Order(7) + void replaceRouteRejectsNonExistentInstanceId() { + ec2() + .formParam("Action", "ReplaceRoute") + .formParam("RouteTableId", routeTableId) + .formParam("DestinationCidrBlock", CIDR_REPLACE) + .formParam("InstanceId", "i-doesnotexist") + .when() + .post("/") + .then() + .statusCode(400) + .body(containsString("InvalidInstanceID.NotFound")); + } + + @Test + @Order(8) + void replaceRouteRejectsMultipleTargets() { + ec2() + .formParam("Action", "ReplaceRoute") + .formParam("RouteTableId", routeTableId) + .formParam("DestinationCidrBlock", CIDR_REPLACE) + .formParam("InstanceId", instanceId) + .formParam("NetworkInterfaceId", ENI_ID) + .when() + .post("/") + .then() + .statusCode(400) + .body(containsString("InvalidParameterCombination")); + } + + @Test + @Order(9) + void filterRouteTablesByTarget() { + ec2() + .formParam("Action", "DescribeRouteTables") + .formParam("Filter.1.Name", "route.instance-id") + .formParam("Filter.1.Value.1", instanceId) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("count(//*[local-name()='routeTableSet']/*[local-name()='item'][*[local-name()='routeTableId']='" + routeTableId + "'])", equalTo("1"))); + + ec2() + .formParam("Action", "DescribeRouteTables") + .formParam("Filter.1.Name", "route.network-interface-id") + .formParam("Filter.1.Value.1", ENI_ID) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("count(//*[local-name()='routeTableSet']/*[local-name()='item'][*[local-name()='routeTableId']='" + routeTableId + "'])", equalTo("1"))); + } + + @Test + @Order(10) + void deleteRouteCleansUp() { + ec2() + .formParam("Action", "DeleteRoute") + .formParam("RouteTableId", routeTableId) + .formParam("DestinationCidrBlock", CIDR_INST) + .when() + .post("/") + .then() + .statusCode(200); + + ec2() + .formParam("Action", "DescribeRouteTables") + .formParam("RouteTableId.1", routeTableId) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("count(//*[local-name()='routeSet']/*[local-name()='item'][*[local-name()='destinationCidrBlock']='" + CIDR_INST + "'])", equalTo("0"))); + } +}