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

@@ -883,10 +883,11 @@ class Ec2IntegrationTest {
.then()
.statusCode(200)
.contentType("application/xml")
// Must contain at least the default egress-all rule plus the authorized
// ingress (ssh/22) and egress (tcp/443) rules
// Exactly the default egress-all rule plus the authorized ingress (ssh/22) and egress
// (tcp/443) rules. Exact, not "at least": each permission carries a single source, so a
// fan-out that double-emitted would still satisfy a lower bound.
.body("DescribeSecurityGroupRulesResponse.securityGroupRuleSet.item.size()",
greaterThanOrEqualTo(3))
equalTo(3))
.body("DescribeSecurityGroupRulesResponse.securityGroupRuleSet.item.groupId",
everyItem(equalTo(securityGroupId)));
}
@@ -0,0 +1,344 @@
package io.github.hectorvent.floci.services.ec2;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import io.quarkus.test.junit.QuarkusTest;
/**
* Issue #2190: a security group rule whose source is another security group must keep that source.
*
* <p>Transcribes the reported repro: authorize tcp/443 from a source group, plus a tcp/8443 CIDR
* rule as the control, then read both back. The source has to survive on three surfaces, since the
* report shows it lost on all three: the rule echoed by AuthorizeSecurityGroupIngress,
* DescribeSecurityGroups ({@code UserIdGroupPairs}) and DescribeSecurityGroupRules
* ({@code ReferencedGroupInfo}).
*/
@QuarkusTest
class Ec2SecurityGroupRuleSourcesIntegrationTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
private static final String ACCOUNT_ID = "000000000000";
@Test
void groupReferenceSurvivesAuthorizeAndBothDescribes() {
String vpcId = createVpc();
String sourceId = createSecurityGroup(uniqueName("source-ingress"), vpcId);
String targetId = createSecurityGroup(uniqueName("target-ingress"), vpcId);
// The rule under test: allow tcp/443 from another security group.
String authorizeBody = given()
.formParam("Action", "AuthorizeSecurityGroupIngress")
.formParam("GroupId", targetId)
.formParam("IpPermissions.1.IpProtocol", "tcp")
.formParam("IpPermissions.1.FromPort", "443")
.formParam("IpPermissions.1.ToPort", "443")
.formParam("IpPermissions.1.Groups.1.GroupId", sourceId)
.formParam("IpPermissions.1.Groups.1.Description", "from-source-sg")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.extract().body().asString();
// The echoed rule is the first symptom in the report, so assert it before the describes.
// The exact fragment also pins the shape: a bare object with scalar children (no <item>
// wrapper), with description as a sibling rather than a child.
assertTrue(
authorizeBody.contains(referencedGroupInfoXml(sourceId)
+ "<description>from-source-sg</description>"),
"AuthorizeSecurityGroupIngress must echo the source group, got: " + authorizeBody);
// Control: the same group with a CIDR rule, which already worked.
authorizeCidrIngress(targetId, "8443", "10.0.0.0/8", "control");
given()
.formParam("Action", "DescribeSecurityGroups")
.formParam("GroupId.1", targetId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item[0].fromPort",
equalTo("443"))
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item[0].groups.item.groupId",
equalTo(sourceId))
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item[0].groups.item.userId",
equalTo(ACCOUNT_ID))
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item[0].groups.item.description",
equalTo("from-source-sg"))
// The CIDR control is unchanged by the fan-out change.
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item[1].ipRanges.item.cidrIp",
equalTo("10.0.0.0/8"))
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item[1].ipRanges.item.description",
equalTo("control"));
given()
.formParam("Action", "DescribeSecurityGroupRules")
.formParam("Filter.1.Name", "group-id")
.formParam("Filter.1.Value.1", targetId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString(referencedGroupInfoXml(sourceId)
+ "<description>from-source-sg</description>"))
.body(containsString("<cidrIpv4>10.0.0.0/8</cidrIpv4>"))
// The group-reference rule carries a group, not a CIDR, and vice versa.
.body(not(containsString("<cidrIpv4>10.0.0.0/8</cidrIpv4><referencedGroupInfo>")));
}
@Test
void groupReferenceSurvivesOnEgress() {
String vpcId = createVpc();
String sourceId = createSecurityGroup(uniqueName("source-egress"), vpcId);
String targetId = createSecurityGroup(uniqueName("target-egress"), vpcId);
given()
.formParam("Action", "AuthorizeSecurityGroupEgress")
.formParam("GroupId", targetId)
.formParam("IpPermissions.1.IpProtocol", "tcp")
.formParam("IpPermissions.1.FromPort", "5432")
.formParam("IpPermissions.1.ToPort", "5432")
.formParam("IpPermissions.1.Groups.1.GroupId", sourceId)
.formParam("IpPermissions.1.Groups.1.Description", "to-database-sg")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<isEgress>true</isEgress>"))
.body(containsString(referencedGroupInfoXml(sourceId)));
given()
.formParam("Action", "DescribeSecurityGroups")
.formParam("GroupId.1", targetId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
// [0] is the default allow-all egress created with the group.
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissionsEgress.item[1]"
+ ".groups.item.groupId", equalTo(sourceId))
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissionsEgress.item[1]"
+ ".groups.item.description", equalTo("to-database-sg"));
given()
.formParam("Action", "DescribeSecurityGroupRules")
.formParam("Filter.1.Name", "group-id")
.formParam("Filter.1.Value.1", targetId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString(referencedGroupInfoXml(sourceId)
+ "<description>to-database-sg</description>"));
}
@Test
void groupReferenceByNameResolvesToGroupId() {
String vpcId = createVpc();
String sourceName = uniqueName("source-by-name");
String sourceId = createSecurityGroup(sourceName, vpcId);
String targetId = createSecurityGroup(uniqueName("target-by-name"), vpcId);
// A default-VPC caller may name the source group instead of identifying it, and
// ReferencedGroupInfo has no group name, so the id has to be resolved on the way in.
given()
.formParam("Action", "AuthorizeSecurityGroupIngress")
.formParam("GroupId", targetId)
.formParam("IpPermissions.1.IpProtocol", "tcp")
.formParam("IpPermissions.1.FromPort", "80")
.formParam("IpPermissions.1.ToPort", "80")
.formParam("IpPermissions.1.Groups.1.GroupName", sourceName)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString(referencedGroupInfoXml(sourceId)));
given()
.formParam("Action", "DescribeSecurityGroups")
.formParam("GroupId.1", targetId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item.groups.item.groupId",
equalTo(sourceId))
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item.groups.item.groupName",
equalTo(sourceName));
}
@Test
void groupNameResolvesWithinTheTargetVpcOnly() {
String otherVpcId = createVpc();
String targetVpcId = createVpc();
// Group names are unique per VPC, not per region, so the same name can exist in both. Only
// the one sharing the target group's VPC may be resolved.
String sharedName = uniqueName("shared-name");
String decoyId = createSecurityGroup(sharedName, otherVpcId);
String wantedId = createSecurityGroup(sharedName, targetVpcId);
String targetId = createSecurityGroup(uniqueName("target-scoped"), targetVpcId);
given()
.formParam("Action", "AuthorizeSecurityGroupIngress")
.formParam("GroupId", targetId)
.formParam("IpPermissions.1.IpProtocol", "tcp")
.formParam("IpPermissions.1.FromPort", "8080")
.formParam("IpPermissions.1.ToPort", "8080")
.formParam("IpPermissions.1.Groups.1.GroupName", sharedName)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString(referencedGroupInfoXml(wantedId)))
.body(not(containsString(decoyId)));
}
@Test
void unknownGroupNameLeavesTheReferenceWithoutAnId() {
String vpcId = createVpc();
String targetId = createSecurityGroup(uniqueName("target-unknown-name"), vpcId);
// Floci does not validate the source group's existence on authorize, so a name that matches
// nothing must still round-trip as a name rather than fabricating an id or failing.
given()
.formParam("Action", "AuthorizeSecurityGroupIngress")
.formParam("GroupId", targetId)
.formParam("IpPermissions.1.IpProtocol", "tcp")
.formParam("IpPermissions.1.FromPort", "9000")
.formParam("IpPermissions.1.ToPort", "9000")
.formParam("IpPermissions.1.Groups.1.GroupName", uniqueName("no-such-group"))
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<referencedGroupInfo><userId>" + ACCOUNT_ID + "</userId>"
+ "</referencedGroupInfo>"));
}
@Test
void mixedSourcesFanOutToOneRulePerSource() {
String vpcId = createVpc();
String sourceId = createSecurityGroup(uniqueName("source-mixed"), vpcId);
String targetId = createSecurityGroup(uniqueName("target-mixed"), vpcId);
// One permission, four sources. AWS gives every rule exactly one source, so this must
// produce four rules and no sourceless one.
given()
.formParam("Action", "AuthorizeSecurityGroupIngress")
.formParam("GroupId", targetId)
.formParam("IpPermissions.1.IpProtocol", "tcp")
.formParam("IpPermissions.1.FromPort", "3306")
.formParam("IpPermissions.1.ToPort", "3306")
.formParam("IpPermissions.1.IpRanges.1.CidrIp", "10.1.0.0/16")
.formParam("IpPermissions.1.IpRanges.2.CidrIp", "10.2.0.0/16")
.formParam("IpPermissions.1.Ipv6Ranges.1.CidrIpv6", "2001:db8::/32")
.formParam("IpPermissions.1.Ipv6Ranges.1.Description", "v6-range")
.formParam("IpPermissions.1.Groups.1.GroupId", sourceId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("AuthorizeSecurityGroupIngressResponse.securityGroupRuleSet.item.size()", equalTo(4));
given()
.formParam("Action", "DescribeSecurityGroupRules")
.formParam("Filter.1.Name", "group-id")
.formParam("Filter.1.Value.1", targetId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
// The four fanned-out rules plus the group's own default egress rule.
.body("DescribeSecurityGroupRulesResponse.securityGroupRuleSet.item.size()", equalTo(5))
.body("DescribeSecurityGroupRulesResponse.securityGroupRuleSet.item.cidrIpv4",
hasItems("10.1.0.0/16", "10.2.0.0/16"))
.body(containsString("<cidrIpv6>2001:db8::/32</cidrIpv6><description>v6-range</description>"))
.body(containsString(referencedGroupInfoXml(sourceId)));
given()
.formParam("Action", "DescribeSecurityGroups")
.formParam("GroupId.1", targetId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item.ipv6Ranges"
+ ".item.cidrIpv6", equalTo("2001:db8::/32"))
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item.ipv6Ranges"
+ ".item.description", equalTo("v6-range"));
}
private String referencedGroupInfoXml(String groupId) {
return "<referencedGroupInfo><groupId>" + groupId + "</groupId>"
+ "<userId>" + ACCOUNT_ID + "</userId></referencedGroupInfo>";
}
private String createVpc() {
return given()
.formParam("Action", "CreateVpc")
.formParam("CidrBlock", "10.0.0.0/16")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.extract().path("CreateVpcResponse.vpc.vpcId");
}
/** The suite shares one emulator and group names are unique per VPC, so never reuse one. */
private String uniqueName(String prefix) {
return prefix + "-" + System.nanoTime();
}
private String createSecurityGroup(String groupName, String vpcId) {
return given()
.formParam("Action", "CreateSecurityGroup")
.formParam("GroupName", groupName)
.formParam("GroupDescription", groupName)
.formParam("VpcId", vpcId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.extract().path("CreateSecurityGroupResponse.groupId");
}
private void authorizeCidrIngress(String groupId, String port, String cidr, String description) {
given()
.formParam("Action", "AuthorizeSecurityGroupIngress")
.formParam("GroupId", groupId)
.formParam("IpPermissions.1.IpProtocol", "tcp")
.formParam("IpPermissions.1.FromPort", port)
.formParam("IpPermissions.1.ToPort", port)
.formParam("IpPermissions.1.IpRanges.1.CidrIp", cidr)
.formParam("IpPermissions.1.IpRanges.1.Description", description)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200);
}
}
@@ -11,6 +11,8 @@ import io.github.hectorvent.floci.services.ec2.model.ManagedPrefixList;
import io.github.hectorvent.floci.services.ec2.model.PrefixListEntry;
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.IpPermission;
import io.github.hectorvent.floci.services.ec2.model.UserIdGroupPair;
import io.github.hectorvent.floci.services.ec2.model.Image;
import io.github.hectorvent.floci.services.ec2.model.NetworkAcl;
import io.github.hectorvent.floci.services.ec2.model.KeyPair;
@@ -117,6 +119,48 @@ class Ec2ServicePersistenceTest {
assertEquals("corporate", firstVersion.getFirst().getDescription());
}
@Test
void securityGroupRuleSourcesSurviveRestart(@TempDir Path dir) {
Ec2Service first = newService(dir);
Vpc vpc = first.createVpc(REGION, "10.0.0.0/16", false);
SecurityGroup source = first.createSecurityGroup(REGION, "source-sg", "traffic source", vpc.getVpcId());
SecurityGroup target = first.createSecurityGroup(REGION, "target-sg", "traffic target", vpc.getVpcId());
IpPermission perm = new IpPermission();
perm.setIpProtocol("tcp");
perm.setFromPort(443);
perm.setToPort(443);
UserIdGroupPair pair = new UserIdGroupPair();
pair.setGroupId(source.getGroupId());
pair.setDescription("from-source-sg");
perm.getUserIdGroupPairs().add(pair);
first.authorizeSecurityGroupIngress(REGION, target.getGroupId(), List.of(perm));
// A fresh service over the same persistent files = a restart with the same data dir. Note
// PersistentStorage.load() quarantines a broken deserialization into an EMPTY store, so the
// assertions below have to touch restored content or they would pass through that failure.
Ec2Service restarted = newService(dir);
SecurityGroup restoredGroup =
restarted.describeSecurityGroups(REGION, List.of(target.getGroupId()), List.of(), Map.of()).getFirst();
List<UserIdGroupPair> restoredPairs = restoredGroup.getIpPermissions().getFirst().getUserIdGroupPairs();
assertEquals(1, restoredPairs.size(), "group reference must survive restart");
assertEquals(source.getGroupId(), restoredPairs.getFirst().getGroupId());
assertEquals("000000000000", restoredPairs.getFirst().getUserId());
assertEquals("from-source-sg", restoredPairs.getFirst().getDescription());
// referencedGroupInfo is a nested object on the flattened rule, so a restart is the first
// place a broken serialization round trip would show up.
List<SecurityGroupRule> ingress =
restarted.describeSecurityGroupRules(REGION, List.of(target.getGroupId()), List.of()).stream()
.filter(r -> !r.isEgress())
.toList();
assertEquals(1, ingress.size(), "one rule per source, and only the ingress rule here");
assertEquals(source.getGroupId(), ingress.getFirst().getReferencedGroupInfo().getGroupId());
assertEquals("000000000000", ingress.getFirst().getReferencedGroupInfo().getUserId());
assertEquals("from-source-sg", ingress.getFirst().getDescription());
}
private BlockDeviceMapping blockDeviceMapping(String snapshotId, int volumeSize) {
EbsBlockDevice ebs = new EbsBlockDevice();
ebs.setSnapshotId(snapshotId);
@@ -129,7 +173,12 @@ class Ec2ServicePersistenceTest {
private Ec2Service newService(Path dir) {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.Ec2ServiceConfig ec2 = mock(EmulatorConfig.Ec2ServiceConfig.class);
when(config.defaultAccountId()).thenReturn("000000000000");
when(config.services()).thenReturn(services);
when(services.ec2()).thenReturn(ec2);
when(ec2.mock()).thenReturn(true);
Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog();
return new Ec2Service(config, null, mock(Ec2PortForwardManager.class),
new AmiImageResolver(imageCatalog), imageCatalog,