mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ec2): allow a security group rule to take a prefix list as its source (#2271)
* feat(ec2): allow a security group rule to take a prefix list as its source AuthorizeSecurityGroupIngress and AuthorizeSecurityGroupEgress now read IpPermissions.N.PrefixListIds.M, so a rule can name a managed prefix list instead of a CIDR. The rule carries prefixListId in place of cidrIpv4 and DescribeSecurityGroups nests the reference under the permission, matching the shapes AWS returns. AWS emits one rule per source, so createRules expands a permission across its CIDRs and its prefix lists rather than treating the two as alternatives; a permission naming neither still yields the single bare rule it did before. An unknown list is rejected with InvalidPrefixListID.NotFound before the rule is stored, so a typo cannot leave a rule pointing at nothing. Closes #2269 * fix(ec2): resolve every prefix list before writing any security group rule createRules stored the CIDR rules of a permission before validating its prefix lists, so a permission naming a valid CIDR alongside an unknown list returned InvalidPrefixListID.NotFound while leaving the CIDR rule behind. AWS persists neither, checked against a live account, and rejects the call as a whole rather than per permission. Resolve every reference a request names before the first write, across all of its permissions, so a later bad permission cannot leave an earlier good one applied. Also brace the new parsing conditional, as AGENTS.md requires. * style(ec2): brace the conditionals in parseIpPermissions AGENTS.md is unconditional about braces, and the method held five unbraced conditionals around the line this branch added. Bracing the whole method rather than only the new line keeps it internally consistent. * fix(ec2): scope security group revocation to the sources it names Revocation compared only protocol and ports, so revoking one source removed every permission sharing that tuple: revoking a prefix list also dropped a CIDR rule on the same port. AWS scopes it to the source, verified against a live account, and leaves the others in place. Subtract the named sources instead, dropping a permission only once it had sources and has lost them all. A request naming no source at all still removes the whole matching permission, which is how a bare protocol and port revoke behaves. The permission a revoke names can be the very object authorize stored on the group, so the source values are snapshotted before either list is mutated. * fix(ec2): validate prefix lists once per request rather than per permission Resolving the prefix lists again inside createRules reopened the window the request-wide check had closed: a list can be deleted between the two, since the group lock and the prefix list lock are different monitors, so a later permission could fail after earlier ones had already stored their rules. Authorize resolves every reference before the first write, so the repeat is removed. createRules keeps no validation of its own; its other callers pass the CIDR-only default egress permission. * fix(ec2): resolve group references when revoking too Scoped revocation compares the sources a request names against the ones stored on the permission. Authorize resolves a reference made by group name to its group id before storing it, so a revoke naming the same source by name alone carried no id and matched nothing: the rule survived the revoke that named it. Resolve on the revoke path as well, mirroring authorize. This only surfaces once both source-scoped revocation and group references as a source exist, so the gap is between this branch and #2266 rather than in either alone. --------- Co-authored-by: Michal Schreiber <michal.schreiber@benefitsystems.pl>
This commit is contained in:
1 parent
0818e5e2d6
commit
dfcd492198
9 files changed
+534
-18
No files matched your search
@@ -336,17 +336,27 @@ public class Ec2QueryHandler {
|
||||
List<IpPermission> perms = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
String proto = p.getFirst(prefix + "." + i + ".IpProtocol");
|
||||
if (proto == null) break;
|
||||
if (proto == null) {
|
||||
break;
|
||||
}
|
||||
IpPermission perm = new IpPermission();
|
||||
perm.setIpProtocol(proto);
|
||||
String fromPort = p.getFirst(prefix + "." + i + ".FromPort");
|
||||
String toPort = p.getFirst(prefix + "." + i + ".ToPort");
|
||||
if (fromPort != null) perm.setFromPort(Integer.parseInt(fromPort));
|
||||
if (toPort != null) perm.setToPort(Integer.parseInt(toPort));
|
||||
if (fromPort != null) {
|
||||
perm.setFromPort(Integer.parseInt(fromPort));
|
||||
}
|
||||
if (toPort != null) {
|
||||
perm.setToPort(Integer.parseInt(toPort));
|
||||
}
|
||||
for (int j = 1; ; j++) {
|
||||
String cidr = p.getFirst(prefix + "." + i + ".IpRanges." + j + ".CidrIp");
|
||||
if (cidr == null) cidr = p.getFirst(prefix + "." + i + ".IpRanges." + j);
|
||||
if (cidr == null) break;
|
||||
if (cidr == null) {
|
||||
cidr = p.getFirst(prefix + "." + i + ".IpRanges." + j);
|
||||
}
|
||||
if (cidr == null) {
|
||||
break;
|
||||
}
|
||||
String desc = p.getFirst(prefix + "." + i + ".IpRanges." + j + ".Description");
|
||||
perm.getIpRanges().add(new IpRange(cidr, desc));
|
||||
}
|
||||
@@ -376,6 +386,14 @@ public class Ec2QueryHandler {
|
||||
pair.setDescription(desc);
|
||||
perm.getUserIdGroupPairs().add(pair);
|
||||
}
|
||||
for (int j = 1; ; j++) {
|
||||
String prefixListId = p.getFirst(prefix + "." + i + ".PrefixListIds." + j + ".PrefixListId");
|
||||
if (prefixListId == null) {
|
||||
break;
|
||||
}
|
||||
String desc = p.getFirst(prefix + "." + i + ".PrefixListIds." + j + ".Description");
|
||||
perm.getPrefixListIds().add(new PrefixListId(prefixListId, desc));
|
||||
}
|
||||
perms.add(perm);
|
||||
}
|
||||
return perms;
|
||||
@@ -2506,7 +2524,8 @@ public class Ec2QueryHandler {
|
||||
if (rule.getFromPort() != null) xml.elem("fromPort", String.valueOf(rule.getFromPort()));
|
||||
if (rule.getToPort() != null) xml.elem("toPort", String.valueOf(rule.getToPort()));
|
||||
xml.elem("cidrIpv4", rule.getCidrIpv4())
|
||||
.elem("cidrIpv6", rule.getCidrIpv6());
|
||||
.elem("cidrIpv6", rule.getCidrIpv6())
|
||||
.elem("prefixListId", rule.getPrefixListId());
|
||||
// Guarded: unlike elem(), start()/end() emit even when every child is null, which would put
|
||||
// an empty <referencedGroupInfo/> on every CIDR rule.
|
||||
ReferencedSecurityGroup ref = rule.getReferencedGroupInfo();
|
||||
@@ -3008,7 +3027,14 @@ public class Ec2QueryHandler {
|
||||
.elem("description", g.getDescription())
|
||||
.end("item");
|
||||
}
|
||||
xml.end("groups").end("item");
|
||||
xml.end("groups").start("prefixListIds");
|
||||
for (PrefixListId prefixList : perm.getPrefixListIds()) {
|
||||
xml.start("item")
|
||||
.elem("prefixListId", prefixList.getPrefixListId())
|
||||
.elem("description", prefixList.getDescription())
|
||||
.end("item");
|
||||
}
|
||||
xml.end("prefixListIds").end("item");
|
||||
}
|
||||
xml.end(wrapperTag);
|
||||
return xml.build();
|
||||
|
||||
@@ -58,6 +58,7 @@ import io.github.hectorvent.floci.services.ec2.model.NetworkAcl;
|
||||
import io.github.hectorvent.floci.services.ec2.model.NetworkAclAssociation;
|
||||
import io.github.hectorvent.floci.services.ec2.model.NetworkAclEntry;
|
||||
import io.github.hectorvent.floci.services.ec2.model.PrefixList;
|
||||
import io.github.hectorvent.floci.services.ec2.model.PrefixListId;
|
||||
import io.github.hectorvent.floci.services.ec2.model.PrefixListEntry;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Placement;
|
||||
import io.github.hectorvent.floci.services.ec2.model.ReferencedSecurityGroup;
|
||||
@@ -1682,6 +1683,7 @@ public class Ec2Service implements ContainerTeardown {
|
||||
List<SecurityGroupRule> rules = new ArrayList<>();
|
||||
synchronized (lockFor(key(region, groupId))) {
|
||||
SecurityGroup sg = getRequiredSecurityGroup(region, groupId);
|
||||
requireKnownPrefixLists(region, permissions);
|
||||
List<IpPermission> next = new ArrayList<>(sg.getIpPermissions());
|
||||
for (IpPermission perm : permissions) {
|
||||
resolveGroupReferences(region, sg.getVpcId(), perm);
|
||||
@@ -1700,6 +1702,7 @@ public class Ec2Service implements ContainerTeardown {
|
||||
List<SecurityGroupRule> rules = new ArrayList<>();
|
||||
synchronized (lockFor(key(region, groupId))) {
|
||||
SecurityGroup sg = getRequiredSecurityGroup(region, groupId);
|
||||
requireKnownPrefixLists(region, permissions);
|
||||
List<IpPermission> next = new ArrayList<>(sg.getIpPermissionsEgress());
|
||||
for (IpPermission perm : permissions) {
|
||||
resolveGroupReferences(region, sg.getVpcId(), perm);
|
||||
@@ -1716,6 +1719,12 @@ public class Ec2Service implements ContainerTeardown {
|
||||
* Flattens one permission into the {@link SecurityGroupRule} entries DescribeSecurityGroupRules
|
||||
* serves. AWS gives every rule exactly one source, so a permission carrying several sources fans
|
||||
* out into one rule each.
|
||||
*
|
||||
* <p>Deliberately does not validate the prefix lists it reads: authorize resolves every list a
|
||||
* request names before the first write. Re-checking per permission would reopen the
|
||||
* partial-write window, since a list can be deleted between the two checks — the group lock and
|
||||
* the prefix list lock are different monitors. The other callers pass a CIDR-only default egress
|
||||
* permission.
|
||||
*/
|
||||
private List<SecurityGroupRule> createRules(String region, String groupId, IpPermission perm, boolean egress) {
|
||||
List<SecurityGroupRule> rules = new ArrayList<>();
|
||||
@@ -1746,6 +1755,14 @@ public class Ec2Service implements ContainerTeardown {
|
||||
rules.add(rule);
|
||||
}
|
||||
}
|
||||
if (perm.getPrefixListIds() != null) {
|
||||
for (PrefixListId prefixList : perm.getPrefixListIds()) {
|
||||
SecurityGroupRule rule = newRule(groupId, perm, egress);
|
||||
rule.setPrefixListId(prefixList.getPrefixListId());
|
||||
rule.setDescription(prefixList.getDescription());
|
||||
rules.add(rule);
|
||||
}
|
||||
}
|
||||
// Real AWS rejects a permission with no source at all; Floci keeps accepting it, so it still
|
||||
// needs a rule to describe.
|
||||
if (rules.isEmpty()) {
|
||||
@@ -1757,6 +1774,21 @@ public class Ec2Service implements ContainerTeardown {
|
||||
return rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves every prefix list a request names before anything is written. AWS rejects the whole
|
||||
* call, so a permission carrying a valid CIDR alongside an unknown list must persist neither.
|
||||
*/
|
||||
private void requireKnownPrefixLists(String region, List<IpPermission> permissions) {
|
||||
for (IpPermission perm : permissions) {
|
||||
if (perm.getPrefixListIds() == null) {
|
||||
continue;
|
||||
}
|
||||
for (PrefixListId prefixList : perm.getPrefixListIds()) {
|
||||
getRequiredManagedPrefixList(region, prefixList.getPrefixListId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private SecurityGroupRule newRule(String groupId, IpPermission perm, boolean egress) {
|
||||
SecurityGroupRule rule = new SecurityGroupRule();
|
||||
rule.setSecurityGroupRuleId("sgr-" + randomHex(17));
|
||||
@@ -1801,8 +1833,12 @@ public class Ec2Service implements ContainerTeardown {
|
||||
ensureDefaultResources(region);
|
||||
synchronized (lockFor(key(region, groupId))) {
|
||||
SecurityGroup sg = getRequiredSecurityGroup(region, groupId);
|
||||
List<IpPermission> next = new ArrayList<>(sg.getIpPermissions());
|
||||
next.removeIf(p -> matchesAnyPermission(p, permissions));
|
||||
// Authorize stores a group reference by id, so a revoke naming it by name alone has to
|
||||
// resolve the same way before the sources can be compared.
|
||||
for (IpPermission perm : permissions) {
|
||||
resolveGroupReferences(region, sg.getVpcId(), perm);
|
||||
}
|
||||
List<IpPermission> next = revokeSources(new ArrayList<>(sg.getIpPermissions()), permissions);
|
||||
sg.setIpPermissions(next);
|
||||
securityGroups.put(key(region, groupId), sg);
|
||||
}
|
||||
@@ -1813,8 +1849,10 @@ public class Ec2Service implements ContainerTeardown {
|
||||
ensureDefaultResources(region);
|
||||
synchronized (lockFor(key(region, groupId))) {
|
||||
SecurityGroup sg = getRequiredSecurityGroup(region, groupId);
|
||||
List<IpPermission> next = new ArrayList<>(sg.getIpPermissionsEgress());
|
||||
next.removeIf(p -> matchesAnyPermission(p, permissions));
|
||||
for (IpPermission perm : permissions) {
|
||||
resolveGroupReferences(region, sg.getVpcId(), perm);
|
||||
}
|
||||
List<IpPermission> next = revokeSources(new ArrayList<>(sg.getIpPermissionsEgress()), permissions);
|
||||
sg.setIpPermissionsEgress(next);
|
||||
securityGroups.put(key(region, groupId), sg);
|
||||
}
|
||||
@@ -1828,15 +1866,58 @@ public class Ec2Service implements ContainerTeardown {
|
||||
return sg;
|
||||
}
|
||||
|
||||
private boolean matchesAnyPermission(IpPermission existing, List<IpPermission> toRemove) {
|
||||
for (IpPermission perm : toRemove) {
|
||||
if (Objects.equals(existing.getIpProtocol(), perm.getIpProtocol())
|
||||
&& Objects.equals(existing.getFromPort(), perm.getFromPort())
|
||||
&& Objects.equals(existing.getToPort(), perm.getToPort())) {
|
||||
return true;
|
||||
/**
|
||||
* Revocation is scoped to the sources it names, as on AWS: revoking one source leaves other
|
||||
* permissions sharing the same protocol and ports in place, and a permission that names
|
||||
* several sources loses only those revoked. A request naming no source at all still removes
|
||||
* the whole matching permission, which is how a bare protocol/port revoke behaves.
|
||||
*
|
||||
* <p>Returns the permissions that remain.
|
||||
*/
|
||||
private List<IpPermission> revokeSources(List<IpPermission> existing, List<IpPermission> toRemove) {
|
||||
List<IpPermission> remaining = new ArrayList<>();
|
||||
for (IpPermission perm : existing) {
|
||||
boolean dropWholePermission = false;
|
||||
boolean hadSources = hasSources(perm);
|
||||
for (IpPermission removal : toRemove) {
|
||||
if (!sameProtocolAndPorts(perm, removal)) {
|
||||
continue;
|
||||
}
|
||||
if (!hasSources(removal)) {
|
||||
dropWholePermission = true;
|
||||
break;
|
||||
}
|
||||
// authorize stores the caller's IpPermission object, so a revoke can name the very
|
||||
// instance held on the group. Snapshot the values before mutating either list.
|
||||
List<String> cidrs = removal.getIpRanges().stream().map(IpRange::getCidrIp).toList();
|
||||
List<String> cidrsV6 = removal.getIpv6Ranges().stream().map(Ipv6Range::getCidrIpv6).toList();
|
||||
List<String> lists = removal.getPrefixListIds().stream()
|
||||
.map(PrefixListId::getPrefixListId).toList();
|
||||
List<String> groups = removal.getUserIdGroupPairs().stream()
|
||||
.map(UserIdGroupPair::getGroupId).toList();
|
||||
perm.getIpRanges().removeIf(e -> cidrs.contains(e.getCidrIp()));
|
||||
perm.getIpv6Ranges().removeIf(e -> cidrsV6.contains(e.getCidrIpv6()));
|
||||
perm.getPrefixListIds().removeIf(e -> lists.contains(e.getPrefixListId()));
|
||||
perm.getUserIdGroupPairs().removeIf(e -> groups.contains(e.getGroupId()));
|
||||
}
|
||||
// A permission that had sources and has lost them all is gone; one that never had any
|
||||
// survives unless a sourceless revoke named it.
|
||||
if (!dropWholePermission && (!hadSources || hasSources(perm))) {
|
||||
remaining.add(perm);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return remaining;
|
||||
}
|
||||
|
||||
private boolean sameProtocolAndPorts(IpPermission a, IpPermission b) {
|
||||
return Objects.equals(a.getIpProtocol(), b.getIpProtocol())
|
||||
&& Objects.equals(a.getFromPort(), b.getFromPort())
|
||||
&& Objects.equals(a.getToPort(), b.getToPort());
|
||||
}
|
||||
|
||||
private boolean hasSources(IpPermission perm) {
|
||||
return !perm.getIpRanges().isEmpty() || !perm.getIpv6Ranges().isEmpty()
|
||||
|| !perm.getPrefixListIds().isEmpty() || !perm.getUserIdGroupPairs().isEmpty();
|
||||
}
|
||||
|
||||
public List<SecurityGroupRule> describeSecurityGroupRules(String region, List<String> groupIds, List<String> ruleIds) {
|
||||
|
||||
@@ -16,6 +16,7 @@ public class IpPermission {
|
||||
private List<IpRange> ipRanges = new ArrayList<>();
|
||||
private List<Ipv6Range> ipv6Ranges = new ArrayList<>();
|
||||
private List<UserIdGroupPair> userIdGroupPairs = new ArrayList<>();
|
||||
private List<PrefixListId> prefixListIds = new ArrayList<>();
|
||||
|
||||
public IpPermission() {}
|
||||
|
||||
@@ -35,5 +36,8 @@ public class IpPermission {
|
||||
public void setIpv6Ranges(List<Ipv6Range> ipv6Ranges) { this.ipv6Ranges = ipv6Ranges; }
|
||||
|
||||
public List<UserIdGroupPair> getUserIdGroupPairs() { return userIdGroupPairs; }
|
||||
|
||||
public List<PrefixListId> getPrefixListIds() { return prefixListIds; }
|
||||
public void setPrefixListIds(List<PrefixListId> prefixListIds) { this.prefixListIds = prefixListIds; }
|
||||
public void setUserIdGroupPairs(List<UserIdGroupPair> userIdGroupPairs) { this.userIdGroupPairs = userIdGroupPairs; }
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package io.github.hectorvent.floci.services.ec2.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
/** A prefix list reference inside an {@link IpPermission}, as AWS returns it under prefixListIds. */
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class PrefixListId {
|
||||
|
||||
private String prefixListId;
|
||||
private String description;
|
||||
|
||||
public PrefixListId() {}
|
||||
|
||||
public PrefixListId(String prefixListId, String description) {
|
||||
this.prefixListId = prefixListId;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getPrefixListId() { return prefixListId; }
|
||||
public void setPrefixListId(String prefixListId) { this.prefixListId = prefixListId; }
|
||||
|
||||
public String getDescription() { return description; }
|
||||
public void setDescription(String description) { this.description = description; }
|
||||
}
|
||||
@@ -19,6 +19,8 @@ public class SecurityGroupRule {
|
||||
private Integer toPort;
|
||||
private String cidrIpv4;
|
||||
private String cidrIpv6;
|
||||
/** Set instead of a CIDR when the rule's source is a managed prefix list. */
|
||||
private String prefixListId;
|
||||
private ReferencedSecurityGroup referencedGroupInfo;
|
||||
private String description;
|
||||
private List<Tag> tags = new ArrayList<>();
|
||||
@@ -52,6 +54,9 @@ public class SecurityGroupRule {
|
||||
public String getCidrIpv6() { return cidrIpv6; }
|
||||
public void setCidrIpv6(String cidrIpv6) { this.cidrIpv6 = cidrIpv6; }
|
||||
|
||||
public String getPrefixListId() { return prefixListId; }
|
||||
public void setPrefixListId(String prefixListId) { this.prefixListId = prefixListId; }
|
||||
|
||||
public ReferencedSecurityGroup getReferencedGroupInfo() { return referencedGroupInfo; }
|
||||
public void setReferencedGroupInfo(ReferencedSecurityGroup referencedGroupInfo) {
|
||||
this.referencedGroupInfo = referencedGroupInfo;
|
||||
|
||||
Reference in new issue
Block a user