diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md index e6006163cf..48e27efdde 100644 --- a/docs/services/cloudformation.md +++ b/docs/services/cloudformation.md @@ -162,7 +162,7 @@ cross-resource references. | ElastiCache | `CacheCluster` (starts a real container; modifying an existing cluster in place is not supported), `SubnetGroup`, `User` (Tags are ignored), `UserGroup` (Tags are ignored), `ServerlessCache` (GetAtt uses the dotted Endpoint.* and ReaderEndpoint.* form) | | Redshift | `Cluster` (single-node container; Port and non-dev DBName ignored; ManageMasterPassword unsupported), `ClusterParameterGroup`, `ClusterSubnetGroup`, `ClusterSecurityGroup` (accepted; no EC2-Classic security group model) | | Redshift Serverless | `Namespace` (ManageAdminPassword is accepted and not applied; GetAtt uses the dotted Namespace.* form), `Workgroup` (starts a real container; one workgroup per namespace; capacity, VPC and price-performance settings are stored only; snapshot restore properties are ignored; GetAtt uses the dotted Workgroup.* form) | -| EC2 | `VPC` (deleted on stack teardown; a VPC with dependents outside the stack, including anything still holding a network interface in its subnets (an instance that is not terminated, a NAT gateway that is not deleted, a standalone interface, an interface endpoint), fails the delete with `DependencyViolation`, as on AWS), `Subnet`, `SecurityGroup` (inline `SecurityGroupIngress`/`SecurityGroupEgress` supported), `SecurityGroupIngress`, `SecurityGroupEgress`, `InternetGateway`, `RouteTable`, `SubnetRouteTableAssociation`, `Route`, `NatGateway`, `EIP`, `Instance`, `LaunchTemplate`, `VPCGatewayAttachment`, `VPCEndpoint`, `NetworkAcl`, `NetworkAclEntry`, `SubnetNetworkAclAssociation`, `FlowLog` | +| EC2 | `VPC` (deleted on stack teardown; a VPC with dependents outside the stack, including anything still holding a network interface in its subnets (an instance that is not terminated, a NAT gateway that is not deleted or failed, a standalone interface, an interface endpoint), fails the delete with `DependencyViolation`, as on AWS), `Subnet`, `SecurityGroup` (inline `SecurityGroupIngress`/`SecurityGroupEgress` supported), `SecurityGroupIngress`, `SecurityGroupEgress`, `InternetGateway`, `RouteTable`, `SubnetRouteTableAssociation`, `Route`, `NatGateway`, `EIP`, `Instance`, `LaunchTemplate`, `VPCGatewayAttachment`, `VPCEndpoint`, `NetworkAcl`, `NetworkAclEntry`, `SubnetNetworkAclAssociation`, `FlowLog` | | Elastic Load Balancing v2 | `LoadBalancer`, `TargetGroup`, `Listener`, `ListenerRule` | | Auto Scaling | `LaunchConfiguration`, `AutoScalingGroup`, `LifecycleHook`, `ScalingPolicy` | | Application Auto Scaling | `ScalableTarget` (`ScheduledActions` are accepted and not run; Floci has no scheduled scaling), `ScalingPolicy` | diff --git a/docs/services/ec2.md b/docs/services/ec2.md index 49df196569..f6e3b72dc8 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -222,7 +222,7 @@ Floci seeds the following resources on first use in each region so Terraform, th |--------|-------------| | CreateVpc | Creates a VPC with the requested CIDR block. `InstanceTenancy` accepts `default` or `dedicated`; `host` is rejected with `InvalidParameterValue`, as on AWS. | | DescribeVpcs | Lists or returns stored VPCs. | -| DeleteVpc | Deletes a VPC from the local EC2 store, together with its default security group and rules, main route table and default network ACL. Fails with `DependencyViolation` while the VPC still has a subnet, a security group, route table or network ACL other than those defaults, a VPC endpoint, an attached internet gateway, a transit gateway attachment, or anything still holding a network interface in one of its subnets: an instance that is not terminated, a NAT gateway that is not deleted, a standalone network interface, or an interface endpoint. | +| DeleteVpc | Deletes a VPC from the local EC2 store, together with its default security group and rules, main route table and default network ACL. Fails with `DependencyViolation` while the VPC still has a subnet, a security group, route table or network ACL other than those defaults, a VPC endpoint, an attached internet gateway, a transit gateway attachment, or anything still holding a network interface in one of its subnets: an instance that is not terminated, a NAT gateway that is not deleted or failed, a standalone network interface, or an interface endpoint. | | ModifyVpcAttribute | Updates supported VPC attributes. | | DescribeVpcAttribute | Returns a supported VPC attribute. | | DescribeVpcEndpointServices | Lists common AWS interface endpoint services and S3 (gateway and interface) in every availability zone, named per partition (see [Partitions](../configuration/partitions.md)). A `ServiceName` filter is echoed back as asked. | @@ -247,7 +247,7 @@ gateway lifecycles. |--------|-------------| | CreateSubnet | Creates a subnet in a VPC. | | DescribeSubnets | Lists or returns stored subnets. | -| DeleteSubnet | Deletes a subnet from the local EC2 store. Fails with `DependencyViolation` while the subnet still has a transit gateway attachment or anything holding a network interface in it: an instance that is not terminated, a NAT gateway that is not deleted, a standalone network interface, or an interface endpoint. | +| DeleteSubnet | Deletes a subnet from the local EC2 store. Fails with `DependencyViolation` while the subnet still has a transit gateway attachment or anything holding a network interface in it: an instance that is not terminated, a NAT gateway that is not deleted or failed, a standalone network interface, or an interface endpoint. | | ModifySubnetAttribute | Updates supported subnet attributes. | ### Security Groups diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisioner.java index 57da848c80..ec9efd8a8d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisioner.java @@ -573,6 +573,11 @@ public class Ec2NetworkCfnProvisioner implements CfnResourceProvisioner { && connectivityType.equalsIgnoreCase(existing.getConnectivityType() == null ? "public" : existing.getConnectivityType()); NatGateway nat = reuse ? existing : ec2Service.createNatGateway(ctx.region(), subnetId, allocationId, connectivityType, tagList(props, ctx)); + if ("failed".equals(nat.getState())) { + // The resource fails and its failed gateway is not left behind. + ec2Service.deleteNatGateway(ctx.region(), nat.getNatGatewayId()); + throw new AwsException(nat.getFailureCode(), nat.getFailureMessage(), 400); + } if (reuse) { CfnRollback.guarded(r, ctx, false, () -> { }, () -> undoInPlace(r), () -> applyTags(r, true, nat.getNatGatewayId(), existing.getTags(), props, ctx)); } 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 c1b0f2b1d8..628ba99af1 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 @@ -5117,6 +5117,8 @@ public class Ec2QueryHandler { .elem("subnetId", natGateway.getSubnetId()) .elem("vpcId", natGateway.getVpcId()) .elem("state", natGateway.getState()) + .elem("failureCode", natGateway.getFailureCode()) + .elem("failureMessage", natGateway.getFailureMessage()) .elem("connectivityType", natGateway.getConnectivityType()); if (natGateway.getCreateTime() != null) { xml.elem("createTime", ISO_FMT.format(natGateway.getCreateTime())); 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 8506c5bded..1b8a272437 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 @@ -4790,7 +4790,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab * it the VPC: an instance, a NAT gateway, a standalone network interface, or an interface or * Gateway Load Balancer endpoint. A {@code shutting-down} instance still holds its interface and * a {@code deleting} NAT gateway still holds its address, so only {@code terminated} and - * {@code deleted} release the subnet; a standalone interface is removed from the store on + * {@code deleted} release the subnet. A {@code failed} NAT gateway was never created and holds + * no interface, so it does not count either; a standalone interface is removed from the store on * delete, so its presence alone counts. AWS answers {@code DependencyViolation} until then. */ private void requireNoSubnetResidents(String region, Set subnetIds, String message) { @@ -4804,7 +4805,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab || !"terminated".equals(instance.getState().getName()))) || natGateways.scan(k -> k.startsWith(regionPrefix)).stream() .anyMatch(natGateway -> subnetIds.contains(natGateway.getSubnetId()) - && !"deleted".equals(natGateway.getState())) + && !"deleted".equals(natGateway.getState()) + && !"failed".equals(natGateway.getState())) || networkInterfaces.scan(k -> k.startsWith(regionPrefix)).stream() .anyMatch(ni -> subnetIds.contains(ni.getSubnetId())) || vpcEndpoints.scan(k -> k.startsWith(regionPrefix)).stream() @@ -5340,7 +5342,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab *

Split out so that everything reporting an endpoint's interfaces -- the ids on * the wire, the objects flow-log attribution reads -- comes from one place and * cannot disagree. The alternative, deriving ids independently from the same - * {@link #endpointEniId}, looks equivalent and is not: this method skips a subnet + * {@link #eniIdFromSeed}, looks equivalent and is not: this method skips a subnet * whose record has gone, and a second derivation that forgot to would report an * interface the first one denies exists. Congruence by construction beats congruence * by inspection, and the two had in fact already diverged. @@ -5372,7 +5374,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab continue; } NetworkInterface ni = new NetworkInterface(); - ni.setNetworkInterfaceId(endpointEniId(endpoint.getVpcEndpointId(), subnetId)); + ni.setNetworkInterfaceId(eniIdFromSeed(endpoint.getVpcEndpointId() + "|" + subnetId)); ni.setSubnetId(subnetId); ni.setVpcId(endpoint.getVpcId()); ni.setAvailabilityZone(subnet.getAvailabilityZone()); @@ -5435,9 +5437,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab return result; } - private static String endpointEniId(String endpointId, String subnetId) { - String hex = java.util.UUID.nameUUIDFromBytes( - (endpointId + "|" + subnetId).getBytes(StandardCharsets.UTF_8)) + /** A stable interface id derived from {@code seed}, for interfaces Floci projects rather than stores. */ + private static String eniIdFromSeed(String seed) { + String hex = UUID.nameUUIDFromBytes(seed.getBytes(StandardCharsets.UTF_8)) .toString().replace("-", ""); return "eni-" + hex.substring(0, 17); } @@ -5453,7 +5455,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab *

Mapped over {@link #endpointNetworkInterfacesOf(VpcEndpoint)} rather than derived * separately, so the ids on the wire come BY CONSTRUCTION from the same derivation * flow-log attribution uses, and no endpoint can be described two ways. An earlier - * version of this method called {@link #endpointEniId} itself and looked equivalent; + * version of this method called {@link #eniIdFromSeed} itself and looked equivalent; * it was not, because it lacked that method's skip of a subnet whose record has gone, * and the two were measured reporting different sets after a subnet was deleted out * from under a live endpoint. @@ -9365,7 +9367,36 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab natGateway.setCreateTime(Instant.now()); natGateway.setRegion(region); synchronized (privateIpAllocationLock) { - natGateway.getNatGatewayAddresses().add(natGatewayAddress(region, subnetId, allocationId)); + // Checked under the same lock that writes the association below, so two gateways + // cannot both see the allocation free. As on AWS, an allocation already held by an + // instance or another gateway does not fail the call: the gateway is created in state + // failed, with no interface, and the allocation is left with its holder. + if (isSet(allocationId)) { + Address held = getRequiredAddress(region, allocationId); + // The gateway scan covers gateways persisted before the Address record was marked. + if (isSet(held.getAssociationId()) || isSet(held.getInstanceId()) + || isHeldByNatGateway(region, allocationId)) { + natGateway.setState("failed"); + natGateway.setFailureCode("Resource.AlreadyAssociated"); + natGateway.setFailureMessage("Elastic IP address " + allocationId + " is already associated"); + } + } + if (!"failed".equals(natGateway.getState())) { + NatGatewayAddress gatewayAddress = + natGatewayAddress(region, natGateway.getNatGatewayId(), subnetId, allocationId); + natGateway.getNatGatewayAddresses().add(gatewayAddress); + // The Elastic IP is now held by the gateway's interface, so DescribeAddresses has to + // say so: an allocation that looks free is one Terraform and the console would offer + // to reuse. No instanceId, a NAT gateway is not an instance. + if (isSet(allocationId)) { + addresses.get(key(region, allocationId)).ifPresent(eip -> { + eip.setAssociationId(gatewayAddress.getAssociationId()); + eip.setNetworkInterfaceId(gatewayAddress.getNetworkInterfaceId()); + eip.setPrivateIpAddress(gatewayAddress.getPrivateIp()); + addresses.put(key(region, allocationId), eip); + }); + } + } if (natGatewayTags != null && !natGatewayTags.isEmpty()) { natGateway.setTags(new ArrayList<>(natGatewayTags)); tags.put(natGateway.getNatGatewayId(), new ArrayList<>(natGatewayTags)); @@ -9382,9 +9413,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab * network_interface_id), and Gruntwork's VPC modules re-export nat_gateway_public_ips, so an * address carrying only an allocation id propagates empty values into dependent modules. */ - private NatGatewayAddress natGatewayAddress(String region, String subnetId, String allocationId) { + private NatGatewayAddress natGatewayAddress(String region, String natGatewayId, String subnetId, + String allocationId) { NatGatewayAddress address = new NatGatewayAddress(); - address.setNetworkInterfaceId("eni-" + randomHex(17)); + address.setNetworkInterfaceId(eniIdFromSeed("nat|" + natGatewayId)); address.setPrivateIp(assignPrivateIp(region, subnetId)); if (isSet(allocationId)) { address.setAllocationId(allocationId); @@ -9397,6 +9429,51 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab return address; } + /** + * The interfaces a NAT gateway owns, one per stored address, built from that address so the id + * DescribeNatGateways publishes is the id DescribeNetworkInterfaces resolves. Empty when the + * gateway's subnet record is gone, as for an endpoint. + */ + private List natNetworkInterfacesOf(NatGateway natGateway, String ownerId) { + String subnetKey = key(natGateway.getRegion(), natGateway.getSubnetId()); + Subnet subnet = (subnets instanceof AccountAwareStorageBackend aware + ? aware.getForAccount(ownerId, subnetKey).or(() -> aware.getLegacyForAccount(ownerId, subnetKey)) + : subnets.get(subnetKey)).orElse(null); + if (subnet == null) { + return List.of(); + } + List result = new ArrayList<>(); + for (NatGatewayAddress address : natGateway.getNatGatewayAddresses()) { + if (address.getNetworkInterfaceId() == null) { + continue; + } + NetworkInterface ni = new NetworkInterface(); + ni.setNetworkInterfaceId(address.getNetworkInterfaceId()); + ni.setSubnetId(natGateway.getSubnetId()); + ni.setVpcId(natGateway.getVpcId()); + ni.setAvailabilityZone(subnet.getAvailabilityZone()); + ni.setDescription("Interface for NAT Gateway " + natGateway.getNatGatewayId()); + ni.setInterfaceType("nat_gateway"); + ni.setRequesterManaged(true); + ni.setOwnerId(ownerId); + ni.setPrivateIpAddress(address.getPrivateIp()); + NetworkInterfacePrivateIpAddress primaryIp = new NetworkInterfacePrivateIpAddress(); + primaryIp.setPrivateIpAddress(address.getPrivateIp()); + primaryIp.setPrimary(true); + if (address.getPublicIp() != null) { + NetworkInterfaceAssociation assoc = new NetworkInterfaceAssociation(); + assoc.setPublicIp(address.getPublicIp()); + assoc.setAllocationId(address.getAllocationId()); + assoc.setAssociationId(address.getAssociationId()); + assoc.setIpOwnerId(ownerId); + primaryIp.setAssociation(assoc); + } + ni.getPrivateIpAddresses().add(primaryIp); + result.add(ni); + } + return result; + } + public List describeNatGateways(String region, List natGatewayIds, Map> filters) { ensureDefaultResources(region); @@ -9416,9 +9493,28 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab public NatGateway deleteNatGateway(String region, String natGatewayId) { ensureDefaultResources(region); NatGateway natGateway = getRequiredNatGateway(region, natGatewayId); - natGateway.setState("deleted"); - natGateways.delete(key(region, natGatewayId)); - tags.delete(natGatewayId); + // Hand the Elastic IP back, but only if the association is still the gateway's own. Under + // the lock createNatGateway takes, so a concurrent create cannot have its association wiped. + synchronized (privateIpAllocationLock) { + natGateway.setState("deleted"); + for (NatGatewayAddress gatewayAddress : natGateway.getNatGatewayAddresses()) { + if (!isSet(gatewayAddress.getAllocationId())) { + continue; + } + addresses.get(key(region, gatewayAddress.getAllocationId())) + .filter(eip -> gatewayAddress.getAssociationId() != null + && gatewayAddress.getAssociationId().equals(eip.getAssociationId())) + .ifPresent(eip -> { + eip.setAssociationId(null); + eip.setNetworkInterfaceId(null); + eip.setPrivateIpAddress(null); + addresses.put(key(region, eip.getAllocationId()), eip); + }); + } + natGateways.delete(key(region, natGatewayId)); + tags.delete(natGatewayId); + natGateway.getNatGatewayAddresses().forEach(a -> tags.delete(a.getNetworkInterfaceId())); + } return natGateway; } @@ -9447,16 +9543,26 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab public Address associateAddress(String region, String allocationId, String instanceId) { ensureDefaultResources(region); - Address addr = getRequiredAddress(region, allocationId); + // Under the lock createNatGateway and deleteNatGateway take, so the gateway check and the + // write cannot interleave with a gateway claiming or releasing the same allocation. + synchronized (privateIpAllocationLock) { + Address addr = getRequiredAddress(region, allocationId); + // A NAT gateway holds its Elastic IP until DeleteNatGateway; reassociation would leave + // the gateway claiming an address an instance now holds. + if (isHeldByNatGateway(region, allocationId)) { + throw new AwsException("Resource.AlreadyAssociated", + "Elastic IP address " + allocationId + " is already associated with a NAT gateway.", 400); + } - if (addr.getAssociationId() != null) { - disassociateAddress(region, addr.getAssociationId()); + if (addr.getAssociationId() != null) { + disassociateAddress(region, addr.getAssociationId()); + } + addr.setInstanceId(instanceId); + addr.setAssociationId("eipassoc-" + randomHex(17)); + pointAddressAtInstance(addr, region, instanceId); + addresses.put(key(region, allocationId), addr); + return addr; } - addr.setInstanceId(instanceId); - addr.setAssociationId("eipassoc-" + randomHex(17)); - pointAddressAtInstance(addr, region, instanceId); - addresses.put(key(region, allocationId), addr); - return addr; } /** @@ -9546,38 +9652,93 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab public void disassociateAddress(String region, String associationId) { ensureDefaultResources(region); - for (Address addr : addresses.scan(k -> true)) { - if (addr.getRegion().equals(region) && associationId.equals(addr.getAssociationId())) { - addr.setInstanceId(null); - addr.setAssociationId(null); - updateStoredAddressAssociation(addr); - addr.setNetworkInterfaceId(null); - addr.setPrivateIpAddress(null); - // Hand the allocation back its allocated address: with no instance behind it, - // there is nothing reachable left for it to stand for. - if (addr.getAllocatedPublicIp() != null) { - addr.setPublicIp(addr.getAllocatedPublicIp()); + // Under the lock NAT create and delete take, so the gateway check and the clear see the + // same association. + synchronized (privateIpAllocationLock) { + for (Address addr : addresses.scan(k -> true)) { + if (addr.getRegion().equals(region) && associationId.equals(addr.getAssociationId())) { + // A NAT gateway holds its Elastic IP for its whole life and is released by + // DeleteNatGateway; clearing only the allocation would leave the gateway and its + // interface still claiming the address. + if (isNatGatewayAssociation(region, associationId)) { + throw new AwsException("InvalidParameterValue", "The association " + associationId + + " belongs to a NAT gateway and cannot be disassociated; delete the NAT gateway.", + 400); + } + addr.setInstanceId(null); + addr.setAssociationId(null); + updateStoredAddressAssociation(addr); + addr.setNetworkInterfaceId(null); + addr.setPrivateIpAddress(null); + // Hand the allocation back its allocated address: with no instance behind it, + // there is nothing reachable left for it to stand for. + if (addr.getAllocatedPublicIp() != null) { + addr.setPublicIp(addr.getAllocatedPublicIp()); + } + addresses.put(key(region, addr.getAllocationId()), addr); + return; } - addresses.put(key(region, addr.getAllocationId()), addr); - return; } } } + /** + * The caller's gateways, including unmigrated pre-account-isolation records when the caller is + * the default account (they belong to it); never another account's. + */ + private List callerNatGateways() { + return natGatewaysOf(callerAccountId()); + } + + private List natGatewaysOf(String account) { + if (natGateways instanceof AccountAwareStorageBackend aware) { + List own = aware.scanForAccount(account, k -> true); + if (!account.equals(defaultAccountId)) { + return own; + } + List all = new ArrayList<>(own); + all.addAll(aware.scanUnscopedLegacy(g -> true)); + return all; + } + return natGateways.scan(k -> true); + } + + private boolean isHeldByNatGateway(String region, String allocationId) { + return callerNatGateways().stream() + .filter(g -> region.equals(g.getRegion()) + && !"deleted".equals(g.getState()) && !"failed".equals(g.getState())) + .anyMatch(g -> allocationId.equals(g.getAllocationId()) + || g.getNatGatewayAddresses().stream().anyMatch(a -> allocationId.equals(a.getAllocationId()))); + } + + private boolean isNatGatewayAssociation(String region, String associationId) { + return callerNatGateways().stream() + .filter(g -> region.equals(g.getRegion())) + .flatMap(g -> g.getNatGatewayAddresses().stream()) + .anyMatch(a -> associationId.equals(a.getAssociationId())); + } + public void releaseAddress(String region, String allocationId) { ensureDefaultResources(region); - Address address = getRequiredAddress(region, allocationId); - if (address.getAssociationId() != null) { - NetworkInterface ni = networkInterfaces.get(key(region, address.getNetworkInterfaceId())).orElse(null); - String vpcId = ni != null ? ni.getVpcId() - : getRequiredInstance(region, address.getInstanceId()).getVpcId(); - if (!getRequiredVpc(region, vpcId).isDefault()) { + synchronized (privateIpAllocationLock) { + Address address = getRequiredAddress(region, allocationId); + // A NAT gateway's Elastic IP is released by DeleteNatGateway, never out from under it. + if (isHeldByNatGateway(region, allocationId)) { throw new AwsException("InvalidIPAddress.InUse", "Address " + address.getPublicIp() + " is in use.", 400); } - disassociateAddress(region, address.getAssociationId()); + if (address.getAssociationId() != null) { + NetworkInterface ni = networkInterfaces.get(key(region, address.getNetworkInterfaceId())).orElse(null); + String vpcId = ni != null ? ni.getVpcId() + : getRequiredInstance(region, address.getInstanceId()).getVpcId(); + if (!getRequiredVpc(region, vpcId).isDefault()) { + throw new AwsException("InvalidIPAddress.InUse", + "Address " + address.getPublicIp() + " is in use.", 400); + } + disassociateAddress(region, address.getAssociationId()); + } + addresses.delete(key(region, allocationId)); } - addresses.delete(key(region, allocationId)); } private void releaseInterfaceAddresses(String region, String networkInterfaceId, boolean deleted) { @@ -10872,6 +11033,31 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab result.add(endpointNi); } + // The ENI a NAT gateway owns. DescribeNatGateways publishes its id (aws_nat_gateway's + // network_interface_id), so it has to resolve here too. Unlike the endpoint arm above this + // one honours the explicit account: gateways and their subnets are read for safeAccount. + for (NatGateway natGateway : natGatewaysOf(safeAccount)) { + if (!region.equals(natGateway.getRegion())) { + continue; + } + for (NetworkInterface natNi : natNetworkInterfacesOf(natGateway, safeAccount)) { + String natEniId = natNi.getNetworkInterfaceId(); + if (foundIds.contains(natEniId)) { + continue; + } + if (!networkInterfaceIds.isEmpty() && !networkInterfaceIds.contains(natEniId)) { + continue; + } + foundIds.add(natEniId); + natNi.getTagSet().addAll((tags instanceof AccountAwareStorageBackend> aware + ? aware.getForAccount(safeAccount, natEniId) : tags.get(natEniId)).orElse(List.of())); + if (!matchesFilters(natNi, filters, region)) { + continue; + } + result.add(natNi); + } + } + // Phase 6: validate requested IDs exist for (String id : networkInterfaceIds) { if (!foundIds.contains(id)) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/NatGateway.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/NatGateway.java index a3aa6388cc..a5b6922b5f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/NatGateway.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/NatGateway.java @@ -17,6 +17,8 @@ public class NatGateway { private String allocationId; private String state = "available"; private String connectivityType = "public"; + private String failureCode; + private String failureMessage; private Instant createTime; private String region; private List natGatewayAddresses = new ArrayList<>(); @@ -39,6 +41,12 @@ public class NatGateway { public String getState() { return state; } public void setState(String state) { this.state = state; } + public String getFailureCode() { return failureCode; } + public void setFailureCode(String failureCode) { this.failureCode = failureCode; } + + public String getFailureMessage() { return failureMessage; } + public void setFailureMessage(String failureMessage) { this.failureMessage = failureMessage; } + public String getConnectivityType() { return connectivityType; } public void setConnectivityType(String connectivityType) { this.connectivityType = connectivityType; } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationEc2NetworkingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationEc2NetworkingIntegrationTest.java index 09d9e1d806..051ac841e0 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationEc2NetworkingIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationEc2NetworkingIntegrationTest.java @@ -86,6 +86,28 @@ class CloudFormationEc2NetworkingIntegrationTest { ec2("DeleteVpc", Map.of("VpcId", vpcId)); } + /** + * CreateNatGateway with an Elastic IP another gateway holds succeeds on AWS and leaves the + * gateway failed; CloudFormation must still fail the resource rather than report it created. + */ + @Test + void aNatGatewayGivenAnAlreadyHeldElasticIpFailsTheStack() { + String vpcId = between(ec2("CreateVpc", Map.of("CidrBlock", "10.42.0.0/16")), "", ""); + String nat = "{\"Type\": \"AWS::EC2::NatGateway\", %s\"Properties\": {\"SubnetId\": {\"Ref\": \"Subnet\"}," + + " \"AllocationId\": {\"Fn::GetAtt\": [\"Eip\", \"AllocationId\"]}}}"; + cloudFormation("ec2-nat-shared-eip-it", "CreateStack", """ + {"Resources": { + "Subnet": {"Type": "AWS::EC2::Subnet", "Properties": {"VpcId": "%s", "CidrBlock": "10.42.0.0/24"}}, + "Eip": {"Type": "AWS::EC2::EIP", "Properties": {"Domain": "vpc"}}, + "NatA": %s, + "NatB": %s + }} + """.formatted(vpcId, nat.formatted(""), nat.formatted("\"DependsOn\": \"NatA\", "))); + + describeStacks("ec2-nat-shared-eip-it", "ROLLBACK_COMPLETE"); + assertFalse(ec2("DescribeNatGateways").contains(vpcId), "no gateway, failed or not, is left behind"); + } + private static String subnetXml(String subnetId) { return ec2("DescribeSubnets", Map.of("SubnetId.1", subnetId)); } @@ -187,14 +209,27 @@ class CloudFormationEc2NetworkingIntegrationTest { assertFalse(retagged.contains("tier"), "the dropped tag must be removed"); // CidrBlock is createOnly: the public subnet is replaced, and with it the NAT gateway and the - // association that name it; the gateway, EIP and route tables stay. - cloudFormation("UpdateStack", template(vpcId, "10.40.2.0/24")); + // association that name it; the gateway and route tables stay. The replacement gateway gets a + // new EIP: CloudFormation creates it before deleting the old one, and on AWS a gateway given + // an Elastic IP the displaced gateway still holds is created failed. The old EIP stays in + // the template for a second reason: Floci's update cleanup deletes removed resources before + // the old copies of replaced ones (AWS deletes in reverse dependency order), so dropping the + // EIP while replacing its gateway would try to release it while the old gateway still holds + // it. Keeping NatEip sidesteps that; the cleanup order is a separate issue. The old EIP is + // free once the displaced gateway is gone. oldEip is NatEip: out is NatEipB's outputs below. + cloudFormation("UpdateStack", template(vpcId, "10.40.2.0/24").replace("NatEip", "NatEipB") + .replace("\"NatEipB\": {\"Type\"", + "\"NatEip\": {\"Type\": \"AWS::EC2::EIP\", \"Properties\": {\"Domain\": \"vpc\"}},\n" + + " \"NatEipB\": {\"Type\"")); Map replaced = XmlParser.extractPairs(describeStacks("UPDATE_COMPLETE"), "Outputs", "OutputKey", "OutputValue"); assertFalse(out.get("SubnetId").equals(replaced.get("SubnetId")), "a changed CIDR must replace the subnet"); assertFalse(out.get("NatId").equals(replaced.get("NatId")), "the NAT gateway follows its subnet"); assertFalse(out.get("AssocId").equals(replaced.get("AssocId")), "the association follows its subnet"); assertEquals(out.get("IgwId"), replaced.get("IgwId")); - assertEquals(out.get("EipAllocationId"), replaced.get("EipAllocationId")); + assertFalse(out.get("EipAllocationId").equals(replaced.get("EipAllocationId")), "the gateway takes the new EIP"); + assertFalse(ec2("DescribeAddresses", Map.of("AllocationId.1", out.get("EipAllocationId"))).contains(""), + "the displaced gateway's EIP must be free"); + String oldEip = out.get("EipAllocationId"); assertEquals(out.get("PublicRtId"), replaced.get("PublicRtId")); assertEquals(out.get("PrivateRtId"), replaced.get("PrivateRtId")); assertFalse(ec2("DescribeSubnets").contains(out.get("SubnetId")), "the displaced subnet must be gone"); @@ -206,7 +241,7 @@ class CloudFormationEc2NetworkingIntegrationTest { CfnStackWaits.awaitStackDeleted(STACK); for (String id : List.of(out.get("SubnetId"), out.get("PublicRtId"), out.get("PrivateRtId"), - out.get("AssocId"), out.get("NatId"), out.get("EipAllocationId"), out.get("IgwId"))) { + out.get("AssocId"), out.get("NatId"), out.get("EipAllocationId"), oldEip, out.get("IgwId"))) { String everything = ec2("DescribeSubnets") + ec2("DescribeRouteTables") + ec2("DescribeNatGateways") + ec2("DescribeAddresses") + ec2("DescribeInternetGateways"); assertFalse(everything.contains(id), "stack delete must remove " + id); diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2NatGatewayAddressIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2NatGatewayAddressIntegrationTest.java index 035057041b..c765e7f2a5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2NatGatewayAddressIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2NatGatewayAddressIntegrationTest.java @@ -89,6 +89,105 @@ class Ec2NatGatewayAddressIntegrationTest { startsWith("eni-")); } + /** + * The interface id DescribeNatGateways publishes has to resolve through + * DescribeNetworkInterfaces, and the Elastic IP the gateway holds has to read as associated + * through DescribeAddresses. Deleting the gateway releases both. + */ + @Test + void aGatewaysInterfaceResolvesAndItsElasticIpReadsAsAssociated() { + String subnetId = subnetId(); + String allocationId = given() + .formParam("Action", "AllocateAddress") + .formParam("Domain", "vpc") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("AllocateAddressResponse.allocationId"); + + String natGatewayId = given() + .formParam("Action", "CreateNatGateway") + .formParam("SubnetId", subnetId) + .formParam("AllocationId", allocationId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("CreateNatGatewayResponse.natGateway.natGatewayId"); + + io.restassured.response.Response described = given() + .formParam("Action", "DescribeNatGateways") + .formParam("NatGatewayId.1", natGatewayId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200).extract().response(); + String prefix = "DescribeNatGatewaysResponse.natGatewaySet.item."; + String eniId = described.path(prefix + "natGatewayAddressSet.item.networkInterfaceId"); + String privateIp = described.path(prefix + "natGatewayAddressSet.item.privateIp"); + String associationId = described.path(prefix + "natGatewayAddressSet.item.associationId"); + String vpcId = described.path(prefix + "vpcId"); + + // Twice: the id must be stable, not re-minted per read. + for (int i = 0; i < 2; i++) { + given() + .formParam("Action", "DescribeNetworkInterfaces") + .formParam("NetworkInterfaceId.1", eniId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then() + .statusCode(200) + .body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.networkInterfaceId", + equalTo(eniId)) + .body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.interfaceType", + equalTo("nat_gateway")) + .body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.subnetId", + equalTo(subnetId)) + .body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.vpcId", + equalTo(vpcId)) + .body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.privateIpAddress", + equalTo(privateIp)) + .body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item" + + ".privateIpAddressesSet.item.association.allocationId", equalTo(allocationId)); + } + + given() + .formParam("Action", "DescribeAddresses") + .formParam("AllocationId.1", allocationId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then() + .statusCode(200) + .body("DescribeAddressesResponse.addressesSet.item.associationId", equalTo(associationId)) + .body("DescribeAddressesResponse.addressesSet.item.networkInterfaceId", equalTo(eniId)) + .body("DescribeAddressesResponse.addressesSet.item.privateIpAddress", equalTo(privateIp)); + + given() + .formParam("Action", "DeleteNatGateway") + .formParam("NatGatewayId", natGatewayId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200); + + given() + .formParam("Action", "DescribeNetworkInterfaces") + .formParam("NetworkInterfaceId.1", eniId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then() + .statusCode(400) + .body("Response.Errors.Error.Code", equalTo("InvalidNetworkInterfaceID.NotFound")); + + given() + .formParam("Action", "DescribeAddresses") + .formParam("AllocationId.1", allocationId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then() + .statusCode(200) + .body("DescribeAddressesResponse.addressesSet.item.allocationId", equalTo(allocationId)) + .body(not(containsString(""))) + .body(not(containsString(""))); + } + @Test void aPrivateGatewayHasAPrivateAddressAndNoElasticIp() { given() @@ -134,4 +233,164 @@ class Ec2NatGatewayAddressIntegrationTest { .statusCode(400) .body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination")); } + + private String allocate() { + return given() + .formParam("Action", "AllocateAddress") + .formParam("Domain", "vpc") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("AllocateAddressResponse.allocationId"); + } + + private io.restassured.response.Response createNat(String allocationId) { + return given() + .formParam("Action", "CreateNatGateway") + .formParam("SubnetId", subnetId()) + .formParam("AllocationId", allocationId) + .header("Authorization", AUTH_HEADER) + .when().post("/"); + } + + /** + * As on AWS, CreateNatGateway with an Elastic IP another gateway or an instance holds + * succeeds, but the gateway is failed: no interface, and the holder keeps the address. + */ + @Test + void aGatewayGivenAnAssociatedElasticIpIsCreatedFailed() { + String heldByGateway = allocate(); + createNat(heldByGateway).then().statusCode(200); + String failedId = createNat(heldByGateway).then().statusCode(200) + .body("CreateNatGatewayResponse.natGateway.state", equalTo("failed")) + .extract().path("CreateNatGatewayResponse.natGateway.natGatewayId"); + given() + .formParam("Action", "DescribeNatGateways") + .formParam("NatGatewayId.1", failedId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .body("DescribeNatGatewaysResponse.natGatewaySet.item.state", equalTo("failed")) + .body("DescribeNatGatewaysResponse.natGatewaySet.item.failureCode", + equalTo("Resource.AlreadyAssociated")) + .body("DescribeNatGatewaysResponse.natGatewaySet.item.failureMessage", + equalTo("Elastic IP address " + heldByGateway + " is already associated")) + .body(not(containsString(""))); + + String heldByInstance = allocate(); + given() + .formParam("Action", "AssociateAddress") + .formParam("AllocationId", heldByInstance) + .formParam("InstanceId", "i-0123456789abcdef0") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200); + createNat(heldByInstance).then().statusCode(200) + .body("CreateNatGatewayResponse.natGateway.state", equalTo("failed")) + .body("CreateNatGatewayResponse.natGateway.failureCode", equalTo("Resource.AlreadyAssociated")); + String instanceAssociation = given() + .formParam("Action", "DescribeAddresses") + .formParam("AllocationId.1", heldByInstance) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .body("DescribeAddressesResponse.addressesSet.item.instanceId", equalTo("i-0123456789abcdef0")) + .extract().path("DescribeAddressesResponse.addressesSet.item.associationId"); + // The failed gateway does not hold the address: once the instance lets go it can be released. + given() + .formParam("Action", "DisassociateAddress") + .formParam("AssociationId", instanceAssociation) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200); + given() + .formParam("Action", "ReleaseAddress") + .formParam("AllocationId", heldByInstance) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200); + + // The failed gateway claimed nothing, and deleting it leaves the first gateway's association alone. + String held = given() + .formParam("Action", "DescribeAddresses") + .formParam("AllocationId.1", heldByGateway) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .body("DescribeAddressesResponse.addressesSet.item.associationId", startsWith("eipassoc-")) + .extract().path("DescribeAddressesResponse.addressesSet.item.associationId"); + given() + .formParam("Action", "DeleteNatGateway") + .formParam("NatGatewayId", failedId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200); + given() + .formParam("Action", "DescribeAddresses") + .formParam("AllocationId.1", heldByGateway) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .body("DescribeAddressesResponse.addressesSet.item.associationId", equalTo(held)); + } + + /** The association a gateway publishes is released by deleting the gateway, not by disassociating. */ + @Test + void aGatewaysElasticIpCannotBeDisassociated() { + String allocationId = allocate(); + String natGatewayId = createNat(allocationId).then().statusCode(200) + .extract().path("CreateNatGatewayResponse.natGateway.natGatewayId"); + String associationId = given() + .formParam("Action", "DescribeAddresses") + .formParam("AllocationId.1", allocationId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("DescribeAddressesResponse.addressesSet.item.associationId"); + + given() + .formParam("Action", "DisassociateAddress") + .formParam("AssociationId", associationId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(400) + .body("Response.Errors.Error.Code", equalTo("InvalidParameterValue")); + + given() + .formParam("Action", "DescribeAddresses") + .formParam("AllocationId.1", allocationId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .body("DescribeAddressesResponse.addressesSet.item.associationId", equalTo(associationId)); + + // Nor released out from under the gateway: AWS answers InvalidIPAddress.InUse. + given() + .formParam("Action", "ReleaseAddress") + .formParam("AllocationId", allocationId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(400) + .body("Response.Errors.Error.Code", equalTo("InvalidIPAddress.InUse")); + given() + .formParam("Action", "DescribeAddresses") + .formParam("AllocationId.1", allocationId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .body("DescribeAddressesResponse.addressesSet.item.associationId", equalTo(associationId)); + + given() + .formParam("Action", "DeleteNatGateway") + .formParam("NatGatewayId", natGatewayId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200); + given() + .formParam("Action", "ReleaseAddress") + .formParam("AllocationId", allocationId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200); + } } 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 fed2017657..e80310edf3 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 @@ -6,6 +6,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RequestContext; 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; @@ -24,6 +25,7 @@ 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.NetworkInterface; import io.github.hectorvent.floci.services.ec2.model.NetworkInterfaceAssociation; import io.github.hectorvent.floci.services.ec2.model.NetworkInterfaceListResult; @@ -463,6 +465,54 @@ class Ec2ServiceTest { .noneMatch(subnet -> subnetId.equals(subnet.getSubnetId()))); } + /** + * A failed NAT gateway was never created and holds no interface, so it does not pin its subnet. + * The gateway fails because its Elastic IP is already held by a gateway in the default VPC. + */ + @Test + void deleteSubnetIgnoresAFailedNatGateway() { + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), + mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(), + new InMemoryStorageFactory()); + String region = "us-east-1"; + String defaultSubnetId = service.describeSubnets(region, List.of(), Map.of()).getFirst().getSubnetId(); + String allocationId = service.allocateAddress(region).getAllocationId(); + service.createNatGateway(region, defaultSubnetId, allocationId, null, null); + String vpcId = service.createVpc(region, "10.85.0.0/16", false).getVpcId(); + String subnetId = service.createSubnet(region, vpcId, "10.85.1.0/24", "us-east-1a").getSubnetId(); + NatGateway failed = service.createNatGateway(region, subnetId, allocationId, null, null); + assertEquals("failed", failed.getState()); + + service.deleteSubnet(region, subnetId); + assertTrue(service.describeSubnets(region, List.of(), Map.of()).stream() + .noneMatch(subnet -> subnetId.equals(subnet.getSubnetId()))); + } + + /** DeleteVpc checks the same subnet residents, so a failed NAT gateway does not pin the VPC either. */ + @Test + void deleteVpcIgnoresAFailedNatGatewayInOneOfItsSubnets() { + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), + mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(), + new InMemoryStorageFactory()); + String region = "us-east-1"; + String defaultSubnetId = service.describeSubnets(region, List.of(), Map.of()).getFirst().getSubnetId(); + String allocationId = service.allocateAddress(region).getAllocationId(); + service.createNatGateway(region, defaultSubnetId, allocationId, null, null); + String vpcId = service.createVpc(region, "10.86.0.0/16", false).getVpcId(); + String subnetId = service.createSubnet(region, vpcId, "10.86.1.0/24", "us-east-1a").getSubnetId(); + NatGateway failed = service.createNatGateway(region, subnetId, allocationId, null, null); + assertEquals("failed", failed.getState()); + + // DeleteVpc refuses while any subnet remains, so the resident check runs in DeleteSubnet; + // after that the failed gateway must not keep the VPC either. + service.deleteSubnet(region, subnetId); + service.deleteVpc(region, vpcId); + assertTrue(service.describeVpcs(region, List.of(), Map.of()).stream() + .noneMatch(vpc -> vpcId.equals(vpc.getVpcId()))); + } + @Test void deleteSubnetFailsUntilItsStandaloneNetworkInterfaceIsDeleted() { Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), @@ -1117,6 +1167,216 @@ class Ec2ServiceTest { .anyMatch(n -> eni.getNetworkInterfaceId().equals(n.getNetworkInterfaceId()))); } + /** + * The NAT arm of DescribeNetworkInterfaces reads gateways for the account it was asked about. + * A gateway made under the default account resolves for that account and is NotFound for + * another one named explicitly. + */ + @Test + void natGatewayInterfaceResolvesOnlyForTheAccountThatOwnsTheGateway() { + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), + new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory()); + String subnetId = service.describeSubnets("us-east-1", List.of(), Map.of()) + .getFirst().getSubnetId(); + String eniId = service.createNatGateway("us-east-1", subnetId, null, "private", null) + .getNatGatewayAddresses().getFirst().getNetworkInterfaceId(); + + String owner = service.callerAccountId(); + assertEquals(eniId, service.describeNetworkInterfaces(owner, "us-east-1", List.of(eniId), Map.of()) + .networkInterfaces().getFirst().getNetworkInterfaceId()); + AwsException e = assertThrows(AwsException.class, () -> service.describeNetworkInterfaces( + "999999999999", "us-east-1", List.of(eniId), Map.of())); + assertEquals("InvalidNetworkInterfaceID.NotFound", e.getErrorCode()); + } + + /** + * A gateway persisted before Address records were marked has an allocation that looks free. + * The gateway still holds it, so a second gateway is created failed and claims nothing. + */ + @Test + void natGatewayGivenAnElasticIpHeldByALegacyGatewayWithAnUnmarkedAddressIsFailed() { + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), + new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory()); + String subnetId = service.describeSubnets("us-east-1", List.of(), Map.of()).getFirst().getSubnetId(); + String allocationId = service.allocateAddress("us-east-1").getAllocationId(); + service.createNatGateway("us-east-1", subnetId, allocationId, null, null); + // Simulate the pre-change record: in-memory storage hands back the stored object. + Address legacy = service.describeAddresses("us-east-1", List.of(allocationId), Map.of()).getFirst(); + legacy.setAssociationId(null); + legacy.setNetworkInterfaceId(null); + legacy.setPrivateIpAddress(null); + + NatGateway failed = service.createNatGateway("us-east-1", subnetId, allocationId, null, null); + assertEquals("failed", failed.getState()); + assertEquals("Resource.AlreadyAssociated", failed.getFailureCode()); + assertTrue(failed.getNatGatewayAddresses().isEmpty()); + assertNull(service.describeAddresses("us-east-1", List.of(allocationId), Map.of()) + .getFirst().getAssociationId(), "a failed gateway must not claim the allocation"); + } + + /** AssociateAddress refuses an allocation a NAT gateway holds; the gateway keeps it. */ + @Test + void associateAddressRefusesANatGatewaysElasticIp() { + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), + new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory()); + String subnetId = service.describeSubnets("us-east-1", List.of(), Map.of()).getFirst().getSubnetId(); + String allocationId = service.allocateAddress("us-east-1").getAllocationId(); + NatGateway nat = service.createNatGateway("us-east-1", subnetId, allocationId, null, null); + String gatewayAssociation = nat.getNatGatewayAddresses().getFirst().getAssociationId(); + + AwsException e = assertThrows(AwsException.class, + () -> service.associateAddress("us-east-1", allocationId, "i-0123456789abcdef0")); + assertEquals("Resource.AlreadyAssociated", e.getErrorCode()); + + Address after = service.describeAddresses("us-east-1", List.of(allocationId), Map.of()).getFirst(); + assertEquals(gatewayAssociation, after.getAssociationId()); + assertNull(after.getInstanceId()); + service.deleteNatGateway("us-east-1", nat.getNatGatewayId()); + assertNull(service.describeAddresses("us-east-1", List.of(allocationId), Map.of()) + .getFirst().getAssociationId(), "delete must still release the gateway's Elastic IP"); + } + + /** + * A gateway persisted before account isolation (unprefixed key) still holds its Elastic IP: + * a second gateway is created failed, and ReleaseAddress answers InvalidIPAddress.InUse. + */ + @Test + void aLegacyUnprefixedGatewaysElasticIpCannotBeTakenOrReleased() { + InMemoryStorage rawGateways = new InMemoryStorage<>(); + Map> stores = Map.of("ec2-nat-gateways.json", + new AccountAwareStorageBackend<>(rawGateways, null, "000000000000")); + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), + new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory(stores)); + String subnetId = service.describeSubnets("us-east-1", List.of(), Map.of()).getFirst().getSubnetId(); + String allocationId = service.allocateAddress("us-east-1").getAllocationId(); + NatGateway legacy = new NatGateway(); + legacy.setNatGatewayId("nat-0legacy000000000"); + legacy.setSubnetId(subnetId); + legacy.setAllocationId(allocationId); + legacy.setState("available"); + legacy.setRegion("us-east-1"); + rawGateways.put("us-east-1::nat-0legacy000000000", legacy); + + NatGateway failed = service.createNatGateway("us-east-1", subnetId, allocationId, null, null); + assertEquals("failed", failed.getState()); + assertEquals("Resource.AlreadyAssociated", failed.getFailureCode()); + AwsException e = assertThrows(AwsException.class, + () -> service.releaseAddress("us-east-1", allocationId)); + assertEquals("InvalidIPAddress.InUse", e.getErrorCode()); + assertEquals(1, service.describeAddresses("us-east-1", List.of(allocationId), Map.of()).size()); + } + + /** A legacy unprefixed gateway in a legacy unprefixed subnet resolves its interface for the default account. */ + @Test + void aLegacyUnprefixedGatewaysInterfaceResolvesForTheDefaultAccount() { + InMemoryStorage rawGateways = new InMemoryStorage<>(); + InMemoryStorage rawSubnets = new InMemoryStorage<>(); + Map> stores = Map.of( + "ec2-nat-gateways.json", new AccountAwareStorageBackend<>(rawGateways, null, "000000000000"), + "ec2-subnets.json", new AccountAwareStorageBackend<>(rawSubnets, null, "000000000000")); + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), + new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory(stores)); + service.describeSubnets("us-east-1", List.of(), Map.of()); + Subnet subnet = new Subnet(); + subnet.setSubnetId("subnet-0legacy00000000"); + subnet.setVpcId("vpc-0legacy000000000"); + subnet.setAvailabilityZone("us-east-1a"); + subnet.setRegion("us-east-1"); + rawSubnets.put("us-east-1::subnet-0legacy00000000", subnet); + NatGateway legacy = new NatGateway(); + legacy.setNatGatewayId("nat-0legacy000000000"); + legacy.setSubnetId("subnet-0legacy00000000"); + legacy.setVpcId("vpc-0legacy000000000"); + legacy.setRegion("us-east-1"); + NatGatewayAddress address = new NatGatewayAddress(); + address.setNetworkInterfaceId("eni-0legacy000000000"); + address.setPrivateIp("10.0.0.5"); + legacy.getNatGatewayAddresses().add(address); + rawGateways.put("us-east-1::nat-0legacy000000000", legacy); + + NetworkInterface ni = service.describeNetworkInterfaces(service.callerAccountId(), "us-east-1", + List.of("eni-0legacy000000000"), Map.of()).networkInterfaces().getFirst(); + assertEquals("nat_gateway", ni.getInterfaceType()); + assertEquals("us-east-1a", ni.getAvailabilityZone()); + AwsException e = assertThrows(AwsException.class, () -> service.describeNetworkInterfaces( + "999999999999", "us-east-1", List.of("eni-0legacy000000000"), Map.of())); + assertEquals("InvalidNetworkInterfaceID.NotFound", e.getErrorCode()); + } + + /** Tags on a NAT gateway's interface read back through DescribeNetworkInterfaces and go with the gateway. */ + @Test + void aNatGatewayInterfacesTagsReadBackAndAreDeletedWithTheGateway() { + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), + new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory()); + String subnetId = service.describeSubnets("us-east-1", List.of(), Map.of()).getFirst().getSubnetId(); + NatGateway nat = service.createNatGateway("us-east-1", subnetId, null, "private", null); + String eniId = nat.getNatGatewayAddresses().getFirst().getNetworkInterfaceId(); + service.createTags("us-east-1", List.of(eniId), List.of(new Tag("Name", "nat-eni"))); + + NetworkInterface ni = service.describeNetworkInterfaces(service.callerAccountId(), "us-east-1", + List.of(eniId), Map.of()).networkInterfaces().getFirst(); + assertEquals("nat-eni", ni.getTagSet().getFirst().getValue()); + + service.deleteNatGateway("us-east-1", nat.getNatGatewayId()); + assertTrue(service.describeTags("us-east-1", Map.of("resource-id", List.of(eniId))).isEmpty()); + } + + /** DisassociateAddress checks and clears under the lock NAT create and delete take. */ + @Test + void disassociateAddressWaitsForTheAllocationLock() throws Exception { + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), + new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory()); + String allocationId = service.allocateAddress("us-east-1").getAllocationId(); + String associationId = service.associateAddress("us-east-1", allocationId, "i-0123456789abcdef0") + .getAssociationId(); + Field field = Ec2Service.class.getDeclaredField("privateIpAllocationLock"); + field.setAccessible(true); + Object lock = field.get(service); + + Thread disassociator = new Thread(() -> service.disassociateAddress("us-east-1", associationId)); + synchronized (lock) { + disassociator.start(); + disassociator.join(500); + assertTrue(disassociator.isAlive(), "disassociate must wait for the allocation lock"); + } + disassociator.join(5000); + assertFalse(disassociator.isAlive()); + assertNull(service.describeAddresses("us-east-1", List.of(allocationId), Map.of()) + .getFirst().getAssociationId()); + } + + /** DeleteNatGateway releases the Elastic IP under the lock CreateNatGateway takes. */ + @Test + void deleteNatGatewayReleasesTheElasticIpUnderTheAllocationLock() throws Exception { + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), + new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory()); + String subnetId = service.describeSubnets("us-east-1", List.of(), Map.of()).getFirst().getSubnetId(); + String allocationId = service.allocateAddress("us-east-1").getAllocationId(); + String natId = service.createNatGateway("us-east-1", subnetId, allocationId, null, null).getNatGatewayId(); + Field field = Ec2Service.class.getDeclaredField("privateIpAllocationLock"); + field.setAccessible(true); + Object lock = field.get(service); + + Thread deleter = new Thread(() -> service.deleteNatGateway("us-east-1", natId)); + synchronized (lock) { + deleter.start(); + deleter.join(500); + assertTrue(deleter.isAlive(), "delete must wait for the allocation lock"); + } + deleter.join(5000); + assertFalse(deleter.isAlive()); + assertNull(service.describeAddresses("us-east-1", List.of(allocationId), Map.of()) + .getFirst().getAssociationId()); + } + @Test void runInstancesRequiresImageIdInsteadOfDefaulting() { Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), diff --git a/tools/docs/cfn_resource_types.yaml b/tools/docs/cfn_resource_types.yaml index 9cd4c00316..53b0d21dc6 100644 --- a/tools/docs/cfn_resource_types.yaml +++ b/tools/docs/cfn_resource_types.yaml @@ -177,7 +177,7 @@ notes: AWS::Lambda::EventSourceMapping: 'SQS, Kinesis, DynamoDB Streams; `ParallelizationFactor` and `TumblingWindowInSeconds` are ignored' AWS::Lambda::Alias: also what SAM's `AutoPublishAlias` expands into AWS::Lambda::EventInvokeConfig: also what a SAM function's `EventInvokeConfig` expands into - AWS::EC2::VPC: 'deleted on stack teardown; a VPC with dependents outside the stack, including anything still holding a network interface in its subnets (an instance that is not terminated, a NAT gateway that is not deleted, a standalone interface, an interface endpoint), fails the delete with `DependencyViolation`, as on AWS' + AWS::EC2::VPC: 'deleted on stack teardown; a VPC with dependents outside the stack, including anything still holding a network interface in its subnets (an instance that is not terminated, a NAT gateway that is not deleted or failed, a standalone interface, an interface endpoint), fails the delete with `DependencyViolation`, as on AWS' AWS::EC2::SecurityGroup: inline `SecurityGroupIngress`/`SecurityGroupEgress` supported AWS::ApplicationAutoScaling::ScalableTarget: '`ScheduledActions` are accepted and not run; Floci has no scheduled scaling' AWS::RDS::DBInstance: starts a real container