diff --git a/docs/services/ec2.md b/docs/services/ec2.md index a42acaf15b..1edf97ffdd 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -102,6 +102,7 @@ Floci seeds the following resources on first use in each region so Terraform, th | Default Security Group | `sg-default` | `groupName=default`, all-traffic egress | | Default Internet Gateway | `igw-default` | Attached to default VPC | | Main Route Table | `rtb-default` | Associated with default VPC | +| Default Network ACL | `acl-default` | Allow-all, associated with the default subnets | ## Supported Actions @@ -132,6 +133,9 @@ Floci seeds the following resources on first use in each region so Terraform, th ### Route Tables `CreateRouteTable` · `DescribeRouteTables` · `DeleteRouteTable` · `AssociateRouteTable` · `DisassociateRouteTable` · `CreateRoute` · `DeleteRoute` +### Network ACLs +`CreateNetworkAcl` · `DescribeNetworkAcls` · `DeleteNetworkAcl` · `CreateNetworkAclEntry` · `ReplaceNetworkAclEntry` · `DeleteNetworkAclEntry` · `ReplaceNetworkAclAssociation` + ### NAT Gateways `CreateNatGateway` · `DescribeNatGateways` · `DeleteNatGateway` diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java index 8ae89770ae..8649addcda 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java @@ -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", 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 58fb77f87e..733db0ac0e 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 @@ -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 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 p, String region) { + List ids = getList(p, "NetworkAclId"); + Map> filters = getFilters(p); + List 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 p, String region) { + service.deleteNetworkAcl(region, p.getFirst("NetworkAclId")); + return booleanResponse("DeleteNetworkAcl"); + } + + private Response handleNetworkAclEntry(MultivaluedMap 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 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 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 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()) 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 4ac5c956b8..313cd73424 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 @@ -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 vpcEndpoints; private final StorageBackend natGateways; private final StorageBackend spotInstanceRequests; + private final StorageBackend networkAcls; // resourceId → List private final StorageBackend> tags; private final Set seededRegions = ConcurrentHashMap.newKeySet(); @@ -122,6 +126,7 @@ public class Ec2Service { storageFactory.create("ec2", "ec2-vpc-endpoints.json", new TypeReference>() {}), storageFactory.create("ec2", "ec2-nat-gateways.json", new TypeReference>() {}), storageFactory.create("ec2", "ec2-spot-instance-requests.json", new TypeReference>() {}), + storageFactory.create("ec2", "ec2-network-acls.json", new TypeReference>() {}), storageFactory.create("ec2", "ec2-tags.json", new TypeReference>>() {})); } @@ -142,6 +147,7 @@ public class Ec2Service { StorageBackend vpcEndpoints, StorageBackend natGateways, StorageBackend spotInstanceRequests, + StorageBackend networkAcls, StorageBackend> 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 describeNetworkAcls(String region, List ids, Map> 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> filters) { + for (Map.Entry> f : filters.entrySet()) { + List 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> describeTags(String region, Map> filters) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAcl.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAcl.java new file mode 100644 index 0000000000..073d67f6e3 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAcl.java @@ -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 entries = new ArrayList<>(); + private List associations = new ArrayList<>(); + private List 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 getEntries() { return entries; } + public void setEntries(List entries) { this.entries = entries; } + + public List getAssociations() { return associations; } + public void setAssociations(List associations) { this.associations = associations; } + + public List getTags() { return tags; } + public void setTags(List tags) { this.tags = tags; } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAclAssociation.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAclAssociation.java new file mode 100644 index 0000000000..b742948828 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAclAssociation.java @@ -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; } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAclEntry.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAclEntry.java new file mode 100644 index 0000000000..e9d79afa90 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/NetworkAclEntry.java @@ -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; } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java index 5d0f9aa6b5..d8ea33faaa 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java @@ -2390,4 +2390,194 @@ class Ec2IntegrationTest { .statusCode(200) .body("DescribeSpotInstanceRequestsResponse.spotInstanceRequestSet.item[0].state", equalTo("cancelled")); } + + private String newVpc(String cidr) { + return given() + .formParam("Action", "CreateVpc") + .formParam("CidrBlock", cidr) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("CreateVpcResponse.vpc.vpcId"); + } + + @Test + @Order(310) + void defaultNetworkAclCreatedWithVpc() { + String vpc = newVpc("10.30.0.0/16"); + given() + .formParam("Action", "DescribeNetworkAcls") + .formParam("Filter.1.Name", "vpc-id") + .formParam("Filter.1.Value.1", vpc) + .formParam("Filter.2.Name", "default") + .formParam("Filter.2.Value.1", "true") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then() + .statusCode(200) + .body("DescribeNetworkAclsResponse.networkAclSet.item.networkAclId", startsWith("acl-")) + .body("DescribeNetworkAclsResponse.networkAclSet.item.vpcId", equalTo(vpc)); + } + + @Test + @Order(311) + void networkAclCreateEntryAndDelete() { + String vpc = newVpc("10.31.0.0/16"); + String aclId = given() + .formParam("Action", "CreateNetworkAcl") + .formParam("VpcId", vpc) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then() + .statusCode(200) + .body("CreateNetworkAclResponse.networkAcl.networkAclId", startsWith("acl-")) + .body("CreateNetworkAclResponse.networkAcl.vpcId", equalTo(vpc)) + .extract().path("CreateNetworkAclResponse.networkAcl.networkAclId"); + + given() + .formParam("Action", "CreateNetworkAclEntry") + .formParam("NetworkAclId", aclId) + .formParam("RuleNumber", "100") + .formParam("Protocol", "6") + .formParam("RuleAction", "allow") + .formParam("Egress", "false") + .formParam("CidrBlock", "0.0.0.0/0") + .formParam("PortRange.From", "443") + .formParam("PortRange.To", "443") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .body("CreateNetworkAclEntryResponse.return", equalTo("true")); + + given() + .formParam("Action", "DescribeNetworkAcls") + .formParam("NetworkAclId.1", aclId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then() + .statusCode(200) + .body("DescribeNetworkAclsResponse.networkAclSet.item.networkAclId", equalTo(aclId)); + + given() + .formParam("Action", "DeleteNetworkAcl") + .formParam("NetworkAclId", aclId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .body("DeleteNetworkAclResponse.return", equalTo("true")); + } + + @Test + @Order(312) + void createNetworkAclEntryRejectsDuplicateButReplaceOverwrites() { + String vpc = newVpc("10.32.0.0/16"); + String aclId = given() + .formParam("Action", "CreateNetworkAcl") + .formParam("VpcId", vpc) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("CreateNetworkAclResponse.networkAcl.networkAclId"); + + given() + .formParam("Action", "CreateNetworkAclEntry") + .formParam("NetworkAclId", aclId) + .formParam("RuleNumber", "100") + .formParam("Protocol", "6") + .formParam("RuleAction", "allow") + .formParam("Egress", "false") + .formParam("CidrBlock", "0.0.0.0/0") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .body("CreateNetworkAclEntryResponse.return", equalTo("true")); + + // Re-creating the same rule number/direction must fail — only Replace may overwrite. + given() + .formParam("Action", "CreateNetworkAclEntry") + .formParam("NetworkAclId", aclId) + .formParam("RuleNumber", "100") + .formParam("Protocol", "6") + .formParam("RuleAction", "deny") + .formParam("Egress", "false") + .formParam("CidrBlock", "0.0.0.0/0") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(400) + .body("Response.Errors.Error.Code", equalTo("NetworkAclEntryAlreadyExists")); + + // Replace on the same rule number succeeds and overwrites the existing entry. + given() + .formParam("Action", "ReplaceNetworkAclEntry") + .formParam("NetworkAclId", aclId) + .formParam("RuleNumber", "100") + .formParam("Protocol", "6") + .formParam("RuleAction", "deny") + .formParam("Egress", "false") + .formParam("CidrBlock", "0.0.0.0/0") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .body("ReplaceNetworkAclEntryResponse.return", equalTo("true")); + + // Confirm the rule was actually overwritten (allow -> deny), not just that the call succeeded. + given() + .formParam("Action", "DescribeNetworkAcls") + .formParam("NetworkAclId.1", aclId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .body("DescribeNetworkAclsResponse.networkAclSet.item.entrySet.item.find { it.ruleNumber == '100' }.ruleAction", + equalTo("deny")); + } + + @Test + @Order(313) + void deleteNetworkAclWithAssociationFails() { + String vpc = newVpc("10.33.0.0/16"); + given() + .formParam("Action", "CreateSubnet") + .formParam("VpcId", vpc) + .formParam("CidrBlock", "10.33.1.0/24") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200); + + // The subnet starts on the VPC's default NACL — grab that association ID. + String associationId = given() + .formParam("Action", "DescribeNetworkAcls") + .formParam("Filter.1.Name", "vpc-id") + .formParam("Filter.1.Value.1", vpc) + .formParam("Filter.2.Name", "default") + .formParam("Filter.2.Value.1", "true") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("DescribeNetworkAclsResponse.networkAclSet.item.associationSet.item.networkAclAssociationId"); + + String aclId = given() + .formParam("Action", "CreateNetworkAcl") + .formParam("VpcId", vpc) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().path("CreateNetworkAclResponse.networkAcl.networkAclId"); + + // Move the subnet onto the custom NACL so it now has a live association. + given() + .formParam("Action", "ReplaceNetworkAclAssociation") + .formParam("AssociationId", associationId) + .formParam("NetworkAclId", aclId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200); + + given() + .formParam("Action", "DeleteNetworkAcl") + .formParam("NetworkAclId", aclId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(400) + .body("Response.Errors.Error.Code", equalTo("DependencyViolation")); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java index 2e600aed06..652323bfd8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java @@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.services.ec2.model.Address; import io.github.hectorvent.floci.services.ec2.model.Instance; import io.github.hectorvent.floci.services.ec2.model.InternetGateway; +import io.github.hectorvent.floci.services.ec2.model.NetworkAcl; 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.NatGateway; @@ -82,6 +83,7 @@ class Ec2ServicePersistenceTest { load(dir, "ec2-vpc-endpoints.json", new TypeReference>() {}), load(dir, "ec2-nat-gateways.json", new TypeReference>() {}), load(dir, "ec2-spot-instance-requests.json", new TypeReference>() {}), + load(dir, "ec2-network-acls.json", new TypeReference>() {}), load(dir, "ec2-tags.json", new TypeReference>>() {})); }