fix(ec2): validate DescribeVpcs ids at the query boundary (#4482)

* fix(ec2): validate DescribeVpcs ids at the query boundary

* fix(ec2): preserve DescribeVpcs filter error precedence

* fix(ec2): remove obsolete shared VPC lookup catches
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-26 16:32:43 -05:00
1 parent d658c75a41
commit 5ace4281f3
10 files changed
+144 -32

No files matched your search

@@ -1,13 +1,11 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Vpc;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.util.List;
import java.util.Map;
@@ -20,8 +18,6 @@ import java.util.Set;
@ApplicationScoped
public class Ec2VpcCfnProvisioner implements CfnResourceProvisioner {
private static final Logger LOG = Logger.getLogger(Ec2VpcCfnProvisioner.class);
private final Ec2Service ec2Service;
@Inject
@@ -69,15 +65,8 @@ public class Ec2VpcCfnProvisioner implements CfnResourceProvisioner {
if (priorPhysicalId == null || priorPhysicalId.isBlank()) {
return null;
}
Vpc existing;
try {
existing = ec2Service.describeVpcs(region, List.of(priorPhysicalId), Map.of())
.stream().findFirst().orElse(null);
} catch (AwsException notFound) {
// Expected when the VPC was deleted out of band since the prior update.
LOG.debugv(notFound, "No existing VPC {0} found on file, falling back to create", priorPhysicalId);
return null;
}
Vpc existing = ec2Service.describeVpcs(region, List.of(priorPhysicalId), Map.of())
.stream().findFirst().orElse(null);
if (existing == null) {
return null;
}
@@ -1531,7 +1531,12 @@ public class Ec2QueryHandler {
private Response handleDescribeVpcs(MultivaluedMap<String, String> p, String region) {
List<String> ids = getList(p, "VpcId");
Map<String, List<String>> filters = getFilters(p);
// Resolve the lookup first so unsupported filters retain their error precedence.
List<Vpc> vpcs = service.describeVpcs(region, ids, filters);
// Shared service callers decide their own missing-resource behavior, as with subnets.
for (String vpcId : ids) {
service.requireVpc(region, vpcId);
}
XmlBuilder xml = new XmlBuilder()
.start("DescribeVpcsResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -4043,11 +4043,6 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
// unsupported filter matches every VPC instead of narrowing anything. Real EC2 rejects the
// name outright, which is the difference between a wrong result and an error.
requireSupportedFilters(filters, VPC_FILTERS);
if (!vpcIds.isEmpty()) {
for (String id : vpcIds) {
getRequiredVpc(region, id);
}
}
return vpcs.scan(k -> true).stream()
.filter(v -> v.getRegion().equals(region))
.filter(v -> vpcIds.isEmpty() || vpcIds.contains(v.getVpcId()))
@@ -454,15 +454,9 @@ public class EcsTaskMetadataController {
if (subnet.getVpcId() == null || region == null) {
return null;
}
try {
return ec2Service.describeVpcs(region, List.of(subnet.getVpcId()), Map.of()).stream()
.findFirst()
.orElse(null);
} catch (AwsException e) {
LOG.debugv("Subnet {0} references a VPC that is gone: {1}",
subnet.getSubnetId(), e.getMessage());
return null;
}
return ec2Service.describeVpcs(region, List.of(subnet.getVpcId()), Map.of()).stream()
.findFirst()
.orElse(null);
}
private static String taskSubnetId(EcsTask task) {
@@ -25,7 +25,6 @@ import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension;
import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricDatum;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
import io.github.hectorvent.floci.services.ec2.model.Vpc;
import io.github.hectorvent.floci.services.rds.container.AutoPauseListener;
import io.github.hectorvent.floci.services.rds.container.RdsContainerHandle;
import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;
@@ -8389,8 +8388,8 @@ public class RdsService implements Resettable, ResourceProvider {
}
private void validateVpcHasIpv6CidrBlock(String vpcId, String region) {
List<Vpc> vpcs = ec2Service.describeVpcs(region, List.of(vpcId), Map.of());
boolean hasIpv6 = !vpcs.isEmpty() && vpcs.get(0).getIpv6CidrBlockAssociationSet().stream()
boolean hasIpv6 = ec2Service.describeVpcs(region, List.of(vpcId), Map.of()).stream()
.flatMap(vpc -> vpc.getIpv6CidrBlockAssociationSet().stream())
.anyMatch(assoc -> "associated".equalsIgnoreCase(assoc.getIpv6CidrBlockState()));
if (!hasIpv6) {
throw new AwsException("InvalidParameterValue",