feat(ec2): add Network ACL support (#1473)

Implements CreateNetworkAcl, DescribeNetworkAcls (with vpc-id / default /
association.subnet-id filters), CreateNetworkAclEntry, ReplaceNetworkAclEntry,
DeleteNetworkAclEntry, ReplaceNetworkAclAssociation, and DeleteNetworkAcl.
Every VPC is provisioned with a default NACL and each subnet is associated with
it, so aws_network_acl and aws_default_network_acl work via Terraform.

Adds Ec2IntegrationTest coverage and documents the new actions.
This commit is contained in:
seyi idowu authored and GitHub committed 2026-06-22 22:57:57 -05:00
1 parent 7b3c5b230d
commit 2301db53c7
9 files changed
+604 -1

No files matched your search

@@ -161,6 +161,9 @@ public class AwsQueryController {
"AttachInternetGateway", "DetachInternetGateway",
"CreateRouteTable", "DescribeRouteTables", "DeleteRouteTable",
"AssociateRouteTable", "DisassociateRouteTable", "CreateRoute", "DeleteRoute",
"CreateNetworkAcl", "DescribeNetworkAcls", "DeleteNetworkAcl",
"CreateNetworkAclEntry", "ReplaceNetworkAclEntry", "DeleteNetworkAclEntry",
"ReplaceNetworkAclAssociation",
"CreateNatGateway", "DescribeNatGateways", "DeleteNatGateway",
"AllocateAddress", "AssociateAddress", "DisassociateAddress", "ReleaseAddress", "DescribeAddresses",
"DescribeAddressesAttribute",
@@ -109,6 +109,14 @@ public class Ec2QueryHandler {
case "DisassociateRouteTable" -> handleDisassociateRouteTable(params, region);
case "CreateRoute" -> handleCreateRoute(params, region);
case "DeleteRoute" -> handleDeleteRoute(params, region);
// Network ACLs
case "CreateNetworkAcl" -> handleCreateNetworkAcl(params, region);
case "DescribeNetworkAcls" -> handleDescribeNetworkAcls(params, region);
case "DeleteNetworkAcl" -> handleDeleteNetworkAcl(params, region);
case "CreateNetworkAclEntry" -> handleNetworkAclEntry(params, region, "CreateNetworkAclEntry");
case "ReplaceNetworkAclEntry" -> handleNetworkAclEntry(params, region, "ReplaceNetworkAclEntry");
case "DeleteNetworkAclEntry" -> handleDeleteNetworkAclEntry(params, region);
case "ReplaceNetworkAclAssociation" -> handleReplaceNetworkAclAssociation(params, region);
// NAT Gateways
case "CreateNatGateway" -> handleCreateNatGateway(params, region);
case "DescribeNatGateways" -> handleDescribeNatGateways(params, region);
@@ -1173,6 +1181,73 @@ public class Ec2QueryHandler {
return booleanResponse("DeleteRoute");
}
// ─── Network ACL handlers ─────────────────────────────────────────────────
private Response handleCreateNetworkAcl(MultivaluedMap<String, String> p, String region) {
NetworkAcl acl = service.createNetworkAcl(region, p.getFirst("VpcId"));
XmlBuilder xml = new XmlBuilder()
.start("CreateNetworkAclResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
.start("networkAcl").raw(networkAclXml(acl)).end("networkAcl")
.end("CreateNetworkAclResponse");
return xmlResponse(xml.build());
}
private Response handleDescribeNetworkAcls(MultivaluedMap<String, String> p, String region) {
List<String> ids = getList(p, "NetworkAclId");
Map<String, List<String>> filters = getFilters(p);
List<NetworkAcl> acls = service.describeNetworkAcls(region, ids, filters);
XmlBuilder xml = new XmlBuilder()
.start("DescribeNetworkAclsResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
.start("networkAclSet");
for (NetworkAcl acl : acls) {
xml.start("item").raw(networkAclXml(acl)).end("item");
}
xml.end("networkAclSet").end("DescribeNetworkAclsResponse");
return xmlResponse(xml.build());
}
private Response handleDeleteNetworkAcl(MultivaluedMap<String, String> p, String region) {
service.deleteNetworkAcl(region, p.getFirst("NetworkAclId"));
return booleanResponse("DeleteNetworkAcl");
}
private Response handleNetworkAclEntry(MultivaluedMap<String, String> p, String region, String action) {
String fromStr = p.getFirst("PortRange.From");
String toStr = p.getFirst("PortRange.To");
service.createNetworkAclEntry(region,
p.getFirst("NetworkAclId"),
Integer.parseInt(p.getFirst("RuleNumber")),
p.getFirst("Protocol"),
p.getFirst("RuleAction"),
Boolean.parseBoolean(p.getFirst("Egress")),
p.getFirst("CidrBlock"),
fromStr != null ? Integer.valueOf(fromStr) : null,
toStr != null ? Integer.valueOf(toStr) : null,
"ReplaceNetworkAclEntry".equals(action));
return booleanResponse(action);
}
private Response handleDeleteNetworkAclEntry(MultivaluedMap<String, String> p, String region) {
service.deleteNetworkAclEntry(region,
p.getFirst("NetworkAclId"),
Integer.parseInt(p.getFirst("RuleNumber")),
Boolean.parseBoolean(p.getFirst("Egress")));
return booleanResponse("DeleteNetworkAclEntry");
}
private Response handleReplaceNetworkAclAssociation(MultivaluedMap<String, String> p, String region) {
NetworkAclAssociation assoc = service.replaceNetworkAclAssociation(region,
p.getFirst("AssociationId"), p.getFirst("NetworkAclId"));
XmlBuilder xml = new XmlBuilder()
.start("ReplaceNetworkAclAssociationResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
.elem("newAssociationId", assoc.getNetworkAclAssociationId())
.end("ReplaceNetworkAclAssociationResponse");
return xmlResponse(xml.build());
}
// ─── NAT Gateway handlers ─────────────────────────────────────────────────
private Response handleCreateNatGateway(MultivaluedMap<String, String> p, String region) {
@@ -1846,6 +1921,41 @@ public class Ec2QueryHandler {
return xml.build();
}
private String networkAclXml(NetworkAcl acl) {
XmlBuilder xml = new XmlBuilder()
.elem("networkAclId", acl.getNetworkAclId())
.elem("vpcId", acl.getVpcId())
.elem("default", String.valueOf(acl.isDefault()))
.elem("ownerId", acl.getOwnerId())
.start("entrySet");
for (NetworkAclEntry e : acl.getEntries()) {
xml.start("item")
.elem("ruleNumber", String.valueOf(e.getRuleNumber()))
.elem("protocol", e.getProtocol())
.elem("ruleAction", e.getRuleAction())
.elem("egress", String.valueOf(e.isEgress()))
.elem("cidrBlock", e.getCidrBlock());
if (e.getPortRangeFrom() != null || e.getPortRangeTo() != null) {
xml.start("portRange")
.elem("from", String.valueOf(e.getPortRangeFrom()))
.elem("to", String.valueOf(e.getPortRangeTo()))
.end("portRange");
}
xml.end("item");
}
xml.end("entrySet").start("associationSet");
for (NetworkAclAssociation a : acl.getAssociations()) {
xml.start("item")
.elem("networkAclAssociationId", a.getNetworkAclAssociationId())
.elem("networkAclId", a.getNetworkAclId())
.elem("subnetId", a.getSubnetId())
.end("item");
}
xml.end("associationSet")
.raw(tagSetXml(acl.getTags()));
return xml.build();
}
private String natGatewayXml(NatGateway natGateway) {
XmlBuilder xml = new XmlBuilder()
.elem("natGatewayId", natGateway.getNatGatewayId())
@@ -47,6 +47,9 @@ 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.NatGateway;
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.Placement;
import io.github.hectorvent.floci.services.ec2.model.Reservation;
import io.github.hectorvent.floci.services.ec2.model.Route;
@@ -97,6 +100,7 @@ public class Ec2Service {
private final StorageBackend<String, VpcEndpoint> vpcEndpoints;
private final StorageBackend<String, NatGateway> natGateways;
private final StorageBackend<String, SpotInstanceRequest> spotInstanceRequests;
private final StorageBackend<String, NetworkAcl> networkAcls;
// resourceId → List<Tag>
private final StorageBackend<String, List<Tag>> tags;
private final Set<String> seededRegions = ConcurrentHashMap.newKeySet();
@@ -122,6 +126,7 @@ public class Ec2Service {
storageFactory.create("ec2", "ec2-vpc-endpoints.json", new TypeReference<Map<String, VpcEndpoint>>() {}),
storageFactory.create("ec2", "ec2-nat-gateways.json", new TypeReference<Map<String, NatGateway>>() {}),
storageFactory.create("ec2", "ec2-spot-instance-requests.json", new TypeReference<Map<String, SpotInstanceRequest>>() {}),
storageFactory.create("ec2", "ec2-network-acls.json", new TypeReference<Map<String, NetworkAcl>>() {}),
storageFactory.create("ec2", "ec2-tags.json", new TypeReference<Map<String, List<Tag>>>() {}));
}
@@ -142,6 +147,7 @@ public class Ec2Service {
StorageBackend<String, VpcEndpoint> vpcEndpoints,
StorageBackend<String, NatGateway> natGateways,
StorageBackend<String, SpotInstanceRequest> spotInstanceRequests,
StorageBackend<String, NetworkAcl> networkAcls,
StorageBackend<String, List<Tag>> tags) {
this.accountId = config.defaultAccountId();
this.config = config;
@@ -162,6 +168,7 @@ public class Ec2Service {
this.vpcEndpoints = vpcEndpoints;
this.natGateways = natGateways;
this.spotInstanceRequests = spotInstanceRequests;
this.networkAcls = networkAcls;
this.tags = tags;
}
@@ -245,6 +252,20 @@ public class Ec2Service {
createDefaultSecurityGroup(region, vpcId, "sg-default");
// Default NACL, with the default subnets associated to it.
String defaultAclId = createDefaultNetworkAcl(region, vpcId, "acl-default");
NetworkAcl defaultAcl = networkAcls.get(key(region, defaultAclId)).orElse(null);
if (defaultAcl != null) {
for (String subnetId : subnetIds) {
NetworkAclAssociation assoc = new NetworkAclAssociation();
assoc.setNetworkAclAssociationId("aclassoc-" + subnetId);
assoc.setNetworkAclId(defaultAclId);
assoc.setSubnetId(subnetId);
defaultAcl.getAssociations().add(assoc);
}
networkAcls.put(key(region, defaultAclId), defaultAcl);
}
// Default internet gateway
String igwId = "igw-default";
InternetGateway igw = new InternetGateway();
@@ -298,6 +319,153 @@ public class Ec2Service {
return routeTableId;
}
private NetworkAclEntry naclEntry(int ruleNumber, String protocol, String action, boolean egress, String cidr) {
NetworkAclEntry entry = new NetworkAclEntry();
entry.setRuleNumber(ruleNumber);
entry.setProtocol(protocol);
entry.setRuleAction(action);
entry.setEgress(egress);
entry.setCidrBlock(cidr);
return entry;
}
// The default NACL allows all traffic (rule 100) and ends with the implicit deny (32767),
// for both ingress and egress — matching what AWS provisions with every VPC.
private String createDefaultNetworkAcl(String region, String vpcId, String networkAclId) {
NetworkAcl acl = new NetworkAcl();
acl.setNetworkAclId(networkAclId);
acl.setVpcId(vpcId);
acl.setOwnerId(accountId);
acl.setRegion(region);
acl.setDefault(true);
acl.getEntries().add(naclEntry(100, "-1", "allow", false, "0.0.0.0/0"));
acl.getEntries().add(naclEntry(32767, "-1", "deny", false, "0.0.0.0/0"));
acl.getEntries().add(naclEntry(100, "-1", "allow", true, "0.0.0.0/0"));
acl.getEntries().add(naclEntry(32767, "-1", "deny", true, "0.0.0.0/0"));
networkAcls.put(key(region, networkAclId), acl);
return networkAclId;
}
private NetworkAcl findDefaultNetworkAcl(String region, String vpcId) {
return networkAcls.scan(k -> true).stream()
.filter(a -> region.equals(a.getRegion()) && vpcId.equals(a.getVpcId()) && a.isDefault())
.findFirst().orElse(null);
}
private NetworkAcl getRequiredNetworkAcl(String region, String networkAclId) {
return networkAcls.get(key(region, networkAclId)).orElseThrow(() ->
new AwsException("InvalidNetworkAclID.NotFound",
"The network ACL ID '" + networkAclId + "' does not exist", 400));
}
// A brand-new custom NACL starts closed: only the implicit deny rules, no allows.
public NetworkAcl createNetworkAcl(String region, String vpcId) {
ensureDefaultResources(region);
getRequiredVpc(region, vpcId);
String networkAclId = "acl-" + randomHex(17);
NetworkAcl acl = new NetworkAcl();
acl.setNetworkAclId(networkAclId);
acl.setVpcId(vpcId);
acl.setOwnerId(accountId);
acl.setRegion(region);
acl.setDefault(false);
acl.getEntries().add(naclEntry(32767, "-1", "deny", false, "0.0.0.0/0"));
acl.getEntries().add(naclEntry(32767, "-1", "deny", true, "0.0.0.0/0"));
networkAcls.put(key(region, networkAclId), acl);
return acl;
}
public List<NetworkAcl> describeNetworkAcls(String region, List<String> ids, Map<String, List<String>> filters) {
ensureDefaultResources(region);
return networkAcls.scan(k -> true).stream()
.filter(a -> region.equals(a.getRegion()))
.filter(a -> ids.isEmpty() || ids.contains(a.getNetworkAclId()))
.filter(a -> matchesNetworkAclFilters(a, filters))
.collect(Collectors.toList());
}
private boolean matchesNetworkAclFilters(NetworkAcl acl, Map<String, List<String>> filters) {
for (Map.Entry<String, List<String>> f : filters.entrySet()) {
List<String> values = f.getValue();
boolean matches = switch (f.getKey()) {
case "network-acl-id" -> values.contains(acl.getNetworkAclId());
case "vpc-id" -> values.contains(acl.getVpcId());
case "default" -> values.contains(String.valueOf(acl.isDefault()));
case "association.subnet-id" ->
acl.getAssociations().stream().anyMatch(a -> values.contains(a.getSubnetId()));
case "association.network-acl-association-id" ->
acl.getAssociations().stream().anyMatch(a -> values.contains(a.getNetworkAclAssociationId()));
default -> true;
};
if (!matches) {
return false;
}
}
return true;
}
public synchronized void createNetworkAclEntry(String region, String networkAclId, int ruleNumber, String protocol,
String ruleAction, boolean egress, String cidrBlock, Integer from, Integer to,
boolean replace) {
NetworkAcl acl = getRequiredNetworkAcl(region, networkAclId);
boolean exists = acl.getEntries().stream()
.anyMatch(e -> e.getRuleNumber() == ruleNumber && e.isEgress() == egress);
if (!replace && exists) {
throw new AwsException("NetworkAclEntryAlreadyExists",
"The network acl entry identified by " + ruleNumber + " already exists.", 400);
}
acl.getEntries().removeIf(e -> e.getRuleNumber() == ruleNumber && e.isEgress() == egress);
NetworkAclEntry entry = naclEntry(ruleNumber, protocol, ruleAction, egress, cidrBlock);
entry.setPortRangeFrom(from);
entry.setPortRangeTo(to);
acl.getEntries().add(entry);
networkAcls.put(key(region, networkAclId), acl);
}
public synchronized void deleteNetworkAclEntry(String region, String networkAclId, int ruleNumber, boolean egress) {
NetworkAcl acl = getRequiredNetworkAcl(region, networkAclId);
acl.getEntries().removeIf(e -> e.getRuleNumber() == ruleNumber && e.isEgress() == egress);
networkAcls.put(key(region, networkAclId), acl);
}
public synchronized NetworkAclAssociation replaceNetworkAclAssociation(String region, String associationId, String networkAclId) {
NetworkAcl target = getRequiredNetworkAcl(region, networkAclId);
for (NetworkAcl acl : networkAcls.scan(k -> true)) {
if (!region.equals(acl.getRegion())) {
continue;
}
for (NetworkAclAssociation existing : acl.getAssociations()) {
if (existing.getNetworkAclAssociationId().equals(associationId)) {
String subnetId = existing.getSubnetId();
acl.getAssociations().remove(existing);
networkAcls.put(key(region, acl.getNetworkAclId()), acl);
NetworkAclAssociation moved = new NetworkAclAssociation();
moved.setNetworkAclAssociationId("aclassoc-" + randomHex(17));
moved.setNetworkAclId(networkAclId);
moved.setSubnetId(subnetId);
target.getAssociations().add(moved);
networkAcls.put(key(region, networkAclId), target);
return moved;
}
}
}
throw new AwsException("InvalidAssociationID.NotFound",
"The network ACL association ID '" + associationId + "' does not exist", 400);
}
public void deleteNetworkAcl(String region, String networkAclId) {
NetworkAcl acl = getRequiredNetworkAcl(region, networkAclId);
if (acl.isDefault()) {
throw new AwsException("InvalidParameterValue",
"The network ACL '" + networkAclId + "' is the default network ACL and cannot be deleted", 400);
}
if (!acl.getAssociations().isEmpty()) {
throw new AwsException("DependencyViolation",
"The network ACL '" + networkAclId + "' has dependencies and cannot be deleted.", 400);
}
networkAcls.delete(key(region, networkAclId));
}
private String key(String region, String id) {
return region + "::" + id;
}
@@ -680,6 +848,7 @@ public class Ec2Service {
createDefaultSecurityGroup(region, vpcId, "sg-" + randomHex(17));
createMainRouteTable(region, vpc, "rtb-" + randomHex(17), "rtbassoc-" + randomHex(17));
createDefaultNetworkAcl(region, vpcId, "acl-" + randomHex(17));
return vpc;
}
@@ -844,6 +1013,18 @@ public class Ec2Service {
subnet.setRegion(region);
subnet.setSubnetArn(AwsArnUtils.Arn.of("ec2", region, accountId, "subnet/" + subnetId).toString());
subnets.put(key(region, subnetId), subnet);
// Every subnet starts associated with its VPC's default NACL. ReplaceNetworkAclAssociation
// later moves it onto a custom NACL, so this association must exist for that lookup to work.
NetworkAcl defaultAcl = findDefaultNetworkAcl(region, vpcId);
if (defaultAcl != null) {
NetworkAclAssociation assoc = new NetworkAclAssociation();
assoc.setNetworkAclAssociationId("aclassoc-" + randomHex(17));
assoc.setNetworkAclId(defaultAcl.getNetworkAclId());
assoc.setSubnetId(subnetId);
defaultAcl.getAssociations().add(assoc);
networkAcls.put(key(region, defaultAcl.getNetworkAclId()), defaultAcl);
}
return subnet;
}
@@ -1491,7 +1672,9 @@ public class Ec2Service {
VpcEndpoint endpoint = vpcEndpoints.get(storeKey).orElse(null);
if (endpoint != null) { endpoint.setTags(new ArrayList<>(tagList)); vpcEndpoints.put(storeKey, endpoint); return; }
NatGateway natGateway = natGateways.get(storeKey).orElse(null);
if (natGateway != null) { natGateway.setTags(new ArrayList<>(tagList)); natGateways.put(storeKey, natGateway); }
if (natGateway != null) { natGateway.setTags(new ArrayList<>(tagList)); natGateways.put(storeKey, natGateway); return; }
NetworkAcl networkAcl = networkAcls.get(storeKey).orElse(null);
if (networkAcl != null) { networkAcl.setTags(new ArrayList<>(tagList)); networkAcls.put(storeKey, networkAcl); }
}
public List<Map<String, String>> describeTags(String region, Map<String, List<String>> filters) {
@@ -0,0 +1,47 @@
package io.github.hectorvent.floci.services.ec2.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.ArrayList;
import java.util.List;
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class NetworkAcl {
private String networkAclId;
private String vpcId;
private String ownerId;
private String region;
private boolean isDefault;
private List<NetworkAclEntry> entries = new ArrayList<>();
private List<NetworkAclAssociation> associations = new ArrayList<>();
private List<Tag> tags = new ArrayList<>();
public NetworkAcl() {}
public String getNetworkAclId() { return networkAclId; }
public void setNetworkAclId(String networkAclId) { this.networkAclId = networkAclId; }
public String getVpcId() { return vpcId; }
public void setVpcId(String vpcId) { this.vpcId = vpcId; }
public String getOwnerId() { return ownerId; }
public void setOwnerId(String ownerId) { this.ownerId = ownerId; }
public String getRegion() { return region; }
public void setRegion(String region) { this.region = region; }
public boolean isDefault() { return isDefault; }
public void setDefault(boolean aDefault) { isDefault = aDefault; }
public List<NetworkAclEntry> getEntries() { return entries; }
public void setEntries(List<NetworkAclEntry> entries) { this.entries = entries; }
public List<NetworkAclAssociation> getAssociations() { return associations; }
public void setAssociations(List<NetworkAclAssociation> associations) { this.associations = associations; }
public List<Tag> getTags() { return tags; }
public void setTags(List<Tag> tags) { this.tags = tags; }
}
@@ -0,0 +1,24 @@
package io.github.hectorvent.floci.services.ec2.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class NetworkAclAssociation {
private String networkAclAssociationId;
private String networkAclId;
private String subnetId;
public NetworkAclAssociation() {}
public String getNetworkAclAssociationId() { return networkAclAssociationId; }
public void setNetworkAclAssociationId(String networkAclAssociationId) { this.networkAclAssociationId = networkAclAssociationId; }
public String getNetworkAclId() { return networkAclId; }
public void setNetworkAclId(String networkAclId) { this.networkAclId = networkAclId; }
public String getSubnetId() { return subnetId; }
public void setSubnetId(String subnetId) { this.subnetId = subnetId; }
}
@@ -0,0 +1,40 @@
package io.github.hectorvent.floci.services.ec2.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class NetworkAclEntry {
private int ruleNumber;
private String protocol;
private String ruleAction;
private boolean egress;
private String cidrBlock;
private Integer portRangeFrom;
private Integer portRangeTo;
public NetworkAclEntry() {}
public int getRuleNumber() { return ruleNumber; }
public void setRuleNumber(int ruleNumber) { this.ruleNumber = ruleNumber; }
public String getProtocol() { return protocol; }
public void setProtocol(String protocol) { this.protocol = protocol; }
public String getRuleAction() { return ruleAction; }
public void setRuleAction(String ruleAction) { this.ruleAction = ruleAction; }
public boolean isEgress() { return egress; }
public void setEgress(boolean egress) { this.egress = egress; }
public String getCidrBlock() { return cidrBlock; }
public void setCidrBlock(String cidrBlock) { this.cidrBlock = cidrBlock; }
public Integer getPortRangeFrom() { return portRangeFrom; }
public void setPortRangeFrom(Integer portRangeFrom) { this.portRangeFrom = portRangeFrom; }
public Integer getPortRangeTo() { return portRangeTo; }
public void setPortRangeTo(Integer portRangeTo) { this.portRangeTo = portRangeTo; }
}