fix(ec2): keep the source on security group rules (#2266)

* fix(ec2): keep the source on security group rules

AuthorizeSecurityGroupIngress and -Egress read only
IpPermissions.N.IpRanges.M.CidrIp, so a rule whose source is another
security group was accepted and then silently stripped: it came back with
no source at all on the authorize response, on DescribeSecurityGroups and
on DescribeSecurityGroupRules. "Allow traffic from security group X" is how
an ALB is wired to its backend tasks, so that whole class of wiring could
be created against Floci but never asserted afterwards.

Parse the source types the permission actually carries: group references,
which the EC2 Query protocol serializes under the wire name Groups rather
than the SDK-facing UserIdGroupPairs, and IPv6 ranges. A reference made by
group name resolves to its group id within the target group's VPC, since
names are unique per VPC and ReferencedSecurityGroup has no name field.

Flatten one SecurityGroupRule per source entry, each keeping its own
description, matching the AWS invariant that a rule holds exactly one
source. A permission with no source at all still yields a single sourceless
rule, as before. The reference is returned as referencedGroupInfo on
DescribeSecurityGroupRules and as UserIdGroupPairs on
DescribeSecurityGroups.

An IPv6-only TCP ingress rule now publishes its host port, which it could
not do while the ranges were dropped at the parse; Ec2PortForwardManager
already treats IPv6 ranges as a CIDR source, and group references stay
unpublished.

Revoke still matches on protocol and port range alone and ignores sources,
unchanged by this.

Closes #2190

* style(ec2): brace the conditionals in the new source parsing

AGENTS.md requires braces on every conditional. The new IPv6 and group
source loops and the group-reference resolution used the unbraced form
the surrounding parse loop happens to use.
This commit is contained in:
TiM authored and GitHub committed 2026-08-14 11:41:36 -04:00
1 parent ecad6acf58
commit a6f5f12295
11 files changed
+653 -33

No files matched your search

@@ -322,6 +322,15 @@ public class Ec2QueryHandler {
throw new AwsException("InvalidParameterValue", name + " is not a valid boolean.", 400);
}
/**
* Reads the {@code IpPermissions.N} list of an authorize/revoke request.
*
* <p>Each permission carries up to four kinds of source, and every one of them has to be read
* here or it is silently dropped before the service layer ever sees it (#2190). Note the EC2
* Query protocol serializes the {@code UserIdGroupPairs} member under the wire name
* {@code Groups}, which is why a group reference arrives as
* {@code IpPermissions.1.Groups.1.GroupId} rather than under the SDK-facing field name.
*/
private List<IpPermission> parseIpPermissions(MultivaluedMap<String, String> p, String prefix) {
List<IpPermission> perms = new ArrayList<>();
for (int i = 1; ; i++) {
@@ -340,6 +349,32 @@ public class Ec2QueryHandler {
String desc = p.getFirst(prefix + "." + i + ".IpRanges." + j + ".Description");
perm.getIpRanges().add(new IpRange(cidr, desc));
}
for (int j = 1; ; j++) {
String cidr = p.getFirst(prefix + "." + i + ".Ipv6Ranges." + j + ".CidrIpv6");
if (cidr == null) {
break;
}
String desc = p.getFirst(prefix + "." + i + ".Ipv6Ranges." + j + ".Description");
perm.getIpv6Ranges().add(new Ipv6Range(cidr, desc));
}
for (int j = 1; ; j++) {
String base = prefix + "." + i + ".Groups." + j;
String groupId = p.getFirst(base + ".GroupId");
String groupName = p.getFirst(base + ".GroupName");
String userId = p.getFirst(base + ".UserId");
String desc = p.getFirst(base + ".Description");
// A default-VPC caller may reference a group by name alone, so the loop cannot end
// on a missing GroupId.
if (groupId == null && groupName == null && userId == null && desc == null) {
break;
}
UserIdGroupPair pair = new UserIdGroupPair();
pair.setGroupId(groupId);
pair.setGroupName(groupName);
pair.setUserId(userId);
pair.setDescription(desc);
perm.getUserIdGroupPairs().add(pair);
}
perms.add(perm);
}
return perms;
@@ -2406,8 +2441,20 @@ 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("description", rule.getDescription())
.elem("cidrIpv6", rule.getCidrIpv6());
// 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();
if (ref != null) {
xml.start("referencedGroupInfo")
.elem("groupId", ref.getGroupId())
.elem("peeringStatus", ref.getPeeringStatus())
.elem("userId", ref.getUserId())
.elem("vpcId", ref.getVpcId())
.elem("vpcPeeringConnectionId", ref.getVpcPeeringConnectionId())
.end("referencedGroupInfo");
}
xml.elem("description", rule.getDescription())
.raw(tagSetXml(rule.getTags()));
return xml.build();
}
@@ -2881,7 +2928,10 @@ public class Ec2QueryHandler {
xml.end("ipRanges")
.start("ipv6Ranges");
for (Ipv6Range r : perm.getIpv6Ranges()) {
xml.start("item").elem("cidrIpv6", r.getCidrIpv6()).end("item");
xml.start("item")
.elem("cidrIpv6", r.getCidrIpv6())
.elem("description", r.getDescription())
.end("item");
}
xml.end("ipv6Ranges")
.start("groups");
@@ -2890,6 +2940,7 @@ public class Ec2QueryHandler {
.elem("userId", g.getUserId())
.elem("groupId", g.getGroupId())
.elem("groupName", g.getGroupName())
.elem("description", g.getDescription())
.end("item");
}
xml.end("groups").end("item");
@@ -48,6 +48,7 @@ import io.github.hectorvent.floci.services.ec2.model.InternetGateway;
import io.github.hectorvent.floci.services.ec2.model.InternetGatewayAttachment;
import io.github.hectorvent.floci.services.ec2.model.IpPermission;
import io.github.hectorvent.floci.services.ec2.model.IpRange;
import io.github.hectorvent.floci.services.ec2.model.Ipv6Range;
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;
@@ -59,6 +60,7 @@ 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.PrefixListEntry;
import io.github.hectorvent.floci.services.ec2.model.Placement;
import io.github.hectorvent.floci.services.ec2.model.ReferencedSecurityGroup;
import io.github.hectorvent.floci.services.ec2.model.Reservation;
import io.github.hectorvent.floci.services.ec2.model.Route;
import io.github.hectorvent.floci.services.ec2.model.RouteTable;
@@ -68,6 +70,7 @@ import io.github.hectorvent.floci.services.ec2.model.SecurityGroupRule;
import io.github.hectorvent.floci.services.ec2.model.Snapshot;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.ec2.model.UserIdGroupPair;
import io.github.hectorvent.floci.services.ec2.model.Volume;
import io.github.hectorvent.floci.services.ec2.model.VolumeAttachment;
import io.github.hectorvent.floci.services.ec2.model.Vpc;
@@ -1677,6 +1680,7 @@ public class Ec2Service implements ContainerTeardown {
SecurityGroup sg = getRequiredSecurityGroup(region, groupId);
List<IpPermission> next = new ArrayList<>(sg.getIpPermissions());
for (IpPermission perm : permissions) {
resolveGroupReferences(region, sg.getVpcId(), perm);
next.add(perm);
rules.addAll(createRules(region, groupId, perm, false));
}
@@ -1694,6 +1698,7 @@ public class Ec2Service implements ContainerTeardown {
SecurityGroup sg = getRequiredSecurityGroup(region, groupId);
List<IpPermission> next = new ArrayList<>(sg.getIpPermissionsEgress());
for (IpPermission perm : permissions) {
resolveGroupReferences(region, sg.getVpcId(), perm);
next.add(perm);
rules.addAll(createRules(region, groupId, perm, true));
}
@@ -1703,39 +1708,91 @@ public class Ec2Service implements ContainerTeardown {
return rules;
}
/**
* 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.
*/
private List<SecurityGroupRule> createRules(String region, String groupId, IpPermission perm, boolean egress) {
List<SecurityGroupRule> rules = new ArrayList<>();
List<IpRange> ranges = perm.getIpRanges();
if (ranges == null || ranges.isEmpty()) {
SecurityGroupRule rule = new SecurityGroupRule();
rule.setSecurityGroupRuleId("sgr-" + randomHex(17));
rule.setGroupId(groupId);
rule.setGroupOwnerId(accountId);
rule.setEgress(egress);
rule.setIpProtocol(perm.getIpProtocol());
rule.setFromPort(perm.getFromPort());
rule.setToPort(perm.getToPort());
securityGroupRules.put(key(region, rule.getSecurityGroupRuleId()), rule);
rules.add(rule);
} else {
for (IpRange range : ranges) {
SecurityGroupRule rule = new SecurityGroupRule();
rule.setSecurityGroupRuleId("sgr-" + randomHex(17));
rule.setGroupId(groupId);
rule.setGroupOwnerId(accountId);
rule.setEgress(egress);
rule.setIpProtocol(perm.getIpProtocol());
rule.setFromPort(perm.getFromPort());
rule.setToPort(perm.getToPort());
if (perm.getIpRanges() != null) {
for (IpRange range : perm.getIpRanges()) {
SecurityGroupRule rule = newRule(groupId, perm, egress);
rule.setCidrIpv4(range.getCidrIp());
rule.setDescription(range.getDescription());
securityGroupRules.put(key(region, rule.getSecurityGroupRuleId()), rule);
rules.add(rule);
}
}
if (perm.getIpv6Ranges() != null) {
for (Ipv6Range range : perm.getIpv6Ranges()) {
SecurityGroupRule rule = newRule(groupId, perm, egress);
rule.setCidrIpv6(range.getCidrIpv6());
rule.setDescription(range.getDescription());
rules.add(rule);
}
}
if (perm.getUserIdGroupPairs() != null) {
for (UserIdGroupPair pair : perm.getUserIdGroupPairs()) {
SecurityGroupRule rule = newRule(groupId, perm, egress);
ReferencedSecurityGroup ref = new ReferencedSecurityGroup();
ref.setGroupId(pair.getGroupId());
ref.setUserId(pair.getUserId());
rule.setReferencedGroupInfo(ref);
rule.setDescription(pair.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()) {
rules.add(newRule(groupId, perm, egress));
}
for (SecurityGroupRule rule : rules) {
securityGroupRules.put(key(region, rule.getSecurityGroupRuleId()), rule);
}
return rules;
}
private SecurityGroupRule newRule(String groupId, IpPermission perm, boolean egress) {
SecurityGroupRule rule = new SecurityGroupRule();
rule.setSecurityGroupRuleId("sgr-" + randomHex(17));
rule.setGroupId(groupId);
rule.setGroupOwnerId(accountId);
rule.setEgress(egress);
rule.setIpProtocol(perm.getIpProtocol());
rule.setFromPort(perm.getFromPort());
rule.setToPort(perm.getToPort());
return rule;
}
/**
* Fills in the source details AWS returns but a caller may leave out: an absent {@code UserId}
* is this account, and a reference made by group name is resolved to its group id so the
* flattened rule can carry a {@code referencedGroupInfo} (the AWS shape has no group name).
*
* <p>Group names are unique per VPC rather than per region, so resolution is confined to the
* VPC of the group being authorized. A name matching nothing there stays unresolved: Floci does
* not check that a referenced group exists, for ids either.
*/
private void resolveGroupReferences(String region, String vpcId, IpPermission perm) {
if (perm.getUserIdGroupPairs() == null) {
return;
}
for (UserIdGroupPair pair : perm.getUserIdGroupPairs()) {
if (pair.getUserId() == null) {
pair.setUserId(accountId);
}
if (pair.getGroupId() == null && pair.getGroupName() != null) {
securityGroups.scan(k -> true).stream()
.filter(sg -> sg.getRegion().equals(region)
&& Objects.equals(vpcId, sg.getVpcId())
&& pair.getGroupName().equals(sg.getGroupName()))
.findFirst()
.ifPresent(sg -> pair.setGroupId(sg.getGroupId()));
}
}
}
public void revokeSecurityGroupIngress(String region, String groupId, List<IpPermission> permissions) {
ensureDefaultResources(region);
synchronized (lockFor(key(region, groupId))) {
@@ -16,6 +16,11 @@ public class Ipv6Range {
this.cidrIpv6 = cidrIpv6;
}
public Ipv6Range(String cidrIpv6, String description) {
this.cidrIpv6 = cidrIpv6;
this.description = description;
}
public String getCidrIpv6() { return cidrIpv6; }
public void setCidrIpv6(String cidrIpv6) { this.cidrIpv6 = cidrIpv6; }
@@ -0,0 +1,42 @@
package io.github.hectorvent.floci.services.ec2.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
/**
* The security group a {@link SecurityGroupRule} allows traffic from or to, as returned in the
* {@code referencedGroupInfo} member of DescribeSecurityGroupRules.
*
* <p>A rule carries exactly one source, so this is the group-reference counterpart of
* {@code cidrIpv4} / {@code cidrIpv6}. Note the shape has no group name: a reference authorized by
* name is resolved to its group id before the rule is stored.
*/
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class ReferencedSecurityGroup {
private String groupId;
private String userId;
private String vpcId;
private String vpcPeeringConnectionId;
private String peeringStatus;
public ReferencedSecurityGroup() {}
public String getGroupId() { return groupId; }
public void setGroupId(String groupId) { this.groupId = groupId; }
public String getUserId() { return userId; }
public void setUserId(String userId) { this.userId = userId; }
public String getVpcId() { return vpcId; }
public void setVpcId(String vpcId) { this.vpcId = vpcId; }
public String getVpcPeeringConnectionId() { return vpcPeeringConnectionId; }
public void setVpcPeeringConnectionId(String vpcPeeringConnectionId) {
this.vpcPeeringConnectionId = vpcPeeringConnectionId;
}
public String getPeeringStatus() { return peeringStatus; }
public void setPeeringStatus(String peeringStatus) { this.peeringStatus = peeringStatus; }
}
@@ -19,6 +19,7 @@ public class SecurityGroupRule {
private Integer toPort;
private String cidrIpv4;
private String cidrIpv6;
private ReferencedSecurityGroup referencedGroupInfo;
private String description;
private List<Tag> tags = new ArrayList<>();
@@ -51,6 +52,11 @@ public class SecurityGroupRule {
public String getCidrIpv6() { return cidrIpv6; }
public void setCidrIpv6(String cidrIpv6) { this.cidrIpv6 = cidrIpv6; }
public ReferencedSecurityGroup getReferencedGroupInfo() { return referencedGroupInfo; }
public void setReferencedGroupInfo(ReferencedSecurityGroup referencedGroupInfo) {
this.referencedGroupInfo = referencedGroupInfo;
}
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
@@ -10,6 +10,7 @@ public class UserIdGroupPair {
private String groupId;
private String userId;
private String groupName;
private String description;
public UserIdGroupPair() {}
@@ -21,4 +22,7 @@ public class UserIdGroupPair {
public String getGroupName() { return groupName; }
public void setGroupName(String groupName) { this.groupName = groupName; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
}