mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+149
@@ -0,0 +1,149 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.emptyOrNullString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* Security group rules whose source is a managed prefix list, over the EC2 Query protocol:
|
||||
* authorize, both read-back shapes, and the rejection of an unknown list.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class Ec2SecurityGroupPrefixListIntegrationTest {
|
||||
|
||||
private static final String AUTH_HEADER =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
|
||||
|
||||
private static String prefixListId;
|
||||
private static String groupId;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createThePrefixListAndGroup() {
|
||||
prefixListId = given()
|
||||
.formParam("Action", "CreateManagedPrefixList")
|
||||
.formParam("PrefixListName", "sg-source")
|
||||
.formParam("AddressFamily", "IPv4")
|
||||
.formParam("MaxEntries", "5")
|
||||
.formParam("Entry.1.Cidr", "10.0.0.0/8")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("CreateManagedPrefixListResponse.prefixList.prefixListId");
|
||||
|
||||
groupId = given()
|
||||
.formParam("Action", "CreateSecurityGroup")
|
||||
.formParam("GroupName", "prefix-list-consumer")
|
||||
.formParam("GroupDescription", "references a prefix list")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("CreateSecurityGroupResponse.groupId");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void authorizeIngressFromThePrefixList() {
|
||||
given()
|
||||
.formParam("Action", "AuthorizeSecurityGroupIngress")
|
||||
.formParam("GroupId", groupId)
|
||||
.formParam("IpPermissions.1.IpProtocol", "tcp")
|
||||
.formParam("IpPermissions.1.FromPort", "5432")
|
||||
.formParam("IpPermissions.1.ToPort", "5432")
|
||||
.formParam("IpPermissions.1.PrefixListIds.1.PrefixListId", prefixListId)
|
||||
.formParam("IpPermissions.1.PrefixListIds.1.Description", "from-corp")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("AuthorizeSecurityGroupIngressResponse.securityGroupRuleSet.item.prefixListId",
|
||||
equalTo(prefixListId))
|
||||
.body("AuthorizeSecurityGroupIngressResponse.securityGroupRuleSet.item.description",
|
||||
equalTo("from-corp"))
|
||||
.body("AuthorizeSecurityGroupIngressResponse.securityGroupRuleSet.item.securityGroupRuleId",
|
||||
not(emptyOrNullString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void describeSecurityGroupsNestsItUnderThePermission() {
|
||||
given()
|
||||
.formParam("Action", "DescribeSecurityGroups")
|
||||
.formParam("GroupId.1", groupId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item.prefixListIds.item.prefixListId",
|
||||
equalTo(prefixListId))
|
||||
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item.prefixListIds.item.description",
|
||||
equalTo("from-corp"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void describeSecurityGroupRulesCarriesThePrefixListId() {
|
||||
given()
|
||||
.formParam("Action", "DescribeSecurityGroupRules")
|
||||
.formParam("Filter.1.Name", "group-id")
|
||||
.formParam("Filter.1.Value.1", groupId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
// Only the ingress rule carries a prefixListId; the group's default egress rule omits
|
||||
// the element entirely, so this resolves to a single value rather than a list.
|
||||
.body("DescribeSecurityGroupRulesResponse.securityGroupRuleSet.item.prefixListId",
|
||||
equalTo(prefixListId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void authorizeAgainstAnUnknownPrefixListIsRejected() {
|
||||
given()
|
||||
.formParam("Action", "AuthorizeSecurityGroupIngress")
|
||||
.formParam("GroupId", groupId)
|
||||
.formParam("IpPermissions.1.IpProtocol", "tcp")
|
||||
.formParam("IpPermissions.1.FromPort", "9999")
|
||||
.formParam("IpPermissions.1.ToPort", "9999")
|
||||
.formParam("IpPermissions.1.PrefixListIds.1.PrefixListId", "pl-doesnotexist")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidPrefixListID.NotFound"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void revokeRemovesThePermission() {
|
||||
given()
|
||||
.formParam("Action", "RevokeSecurityGroupIngress")
|
||||
.formParam("GroupId", groupId)
|
||||
.formParam("IpPermissions.1.IpProtocol", "tcp")
|
||||
.formParam("IpPermissions.1.FromPort", "5432")
|
||||
.formParam("IpPermissions.1.ToPort", "5432")
|
||||
.formParam("IpPermissions.1.PrefixListIds.1.PrefixListId", prefixListId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeSecurityGroups")
|
||||
.formParam("GroupId.1", groupId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions",
|
||||
emptyOrNullString());
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,8 @@ import io.github.hectorvent.floci.services.ec2.model.Image;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
|
||||
import io.github.hectorvent.floci.services.ec2.model.ManagedPrefixList;
|
||||
import io.github.hectorvent.floci.services.ec2.model.SecurityGroupRule;
|
||||
import io.github.hectorvent.floci.services.ec2.model.PrefixListId;
|
||||
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.PrefixListEntry;
|
||||
@@ -21,6 +23,7 @@ import io.github.hectorvent.floci.services.ec2.model.Reservation;
|
||||
import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Snapshot;
|
||||
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.VpcEndpoint;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Volume;
|
||||
import io.github.hectorvent.floci.services.ec2.model.VolumeAttachment;
|
||||
@@ -35,6 +38,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
@@ -1069,6 +1073,180 @@ class Ec2ServiceTest {
|
||||
.getFirst().getTags().isEmpty());
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Security group rules sourced from a prefix list
|
||||
// =========================================================================
|
||||
|
||||
private static IpPermission tcpPermission(int port) {
|
||||
IpPermission perm = new IpPermission();
|
||||
perm.setIpProtocol("tcp");
|
||||
perm.setFromPort(port);
|
||||
perm.setToPort(port);
|
||||
return perm;
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorizeIngressFromAPrefixListCreatesARuleCarryingIt() {
|
||||
Ec2Service service = prefixListService();
|
||||
ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5,
|
||||
List.of(new PrefixListEntry("10.0.0.0/8", null)), List.of());
|
||||
String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId();
|
||||
|
||||
IpPermission perm = tcpPermission(5432);
|
||||
perm.getPrefixListIds().add(new PrefixListId(list.getPrefixListId(), "from-corp"));
|
||||
List<SecurityGroupRule> rules = service.authorizeSecurityGroupIngress("us-east-1", groupId, List.of(perm));
|
||||
|
||||
assertEquals(1, rules.size());
|
||||
SecurityGroupRule rule = rules.getFirst();
|
||||
assertEquals(list.getPrefixListId(), rule.getPrefixListId());
|
||||
assertEquals("from-corp", rule.getDescription());
|
||||
assertNull(rule.getCidrIpv4(), "a prefix list rule carries no CIDR");
|
||||
assertFalse(rule.isEgress());
|
||||
assertEquals(5432, rule.getFromPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorizeAgainstAnUnknownPrefixListIsRejected() {
|
||||
Ec2Service service = prefixListService();
|
||||
String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId();
|
||||
|
||||
IpPermission perm = tcpPermission(5432);
|
||||
perm.getPrefixListIds().add(new PrefixListId("pl-doesnotexist", null));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.authorizeSecurityGroupIngress("us-east-1", groupId, List.of(perm)));
|
||||
assertEquals("InvalidPrefixListID.NotFound", error.getErrorCode());
|
||||
// The rejected rule must not have been stored. The group still holds its default
|
||||
// allow-all egress rule, so the check is for an ingress rule rather than for none.
|
||||
assertTrue(service.describeSecurityGroupRules("us-east-1", List.of(groupId), List.of()).stream()
|
||||
.noneMatch(r -> !r.isEgress() || r.getPrefixListId() != null));
|
||||
}
|
||||
|
||||
/** AWS emits one rule per source, so a permission naming both expands to two. */
|
||||
@Test
|
||||
void aPermissionNamingBothACidrAndAPrefixListYieldsARuleForEach() {
|
||||
Ec2Service service = prefixListService();
|
||||
ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5,
|
||||
List.of(), List.of());
|
||||
String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId();
|
||||
|
||||
IpPermission perm = tcpPermission(443);
|
||||
perm.getIpRanges().add(new IpRange("10.1.0.0/16", "direct"));
|
||||
perm.getPrefixListIds().add(new PrefixListId(list.getPrefixListId(), "via-list"));
|
||||
List<SecurityGroupRule> rules = service.authorizeSecurityGroupIngress("us-east-1", groupId, List.of(perm));
|
||||
|
||||
assertEquals(2, rules.size());
|
||||
assertEquals(1, rules.stream().filter(r -> "10.1.0.0/16".equals(r.getCidrIpv4())).count());
|
||||
assertEquals(1, rules.stream().filter(r -> list.getPrefixListId().equals(r.getPrefixListId())).count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verified against a live AWS account: a permission naming a valid CIDR alongside an unknown
|
||||
* prefix list persists neither, so the whole call has to resolve before anything is written.
|
||||
*/
|
||||
@Test
|
||||
void anUnknownPrefixListLeavesNoPartialRuleFromTheSamePermission() {
|
||||
Ec2Service service = prefixListService();
|
||||
String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId();
|
||||
|
||||
IpPermission perm = tcpPermission(5432);
|
||||
perm.getIpRanges().add(new IpRange("10.9.0.0/16", "direct"));
|
||||
perm.getPrefixListIds().add(new PrefixListId("pl-doesnotexist", null));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.authorizeSecurityGroupIngress("us-east-1", groupId, List.of(perm)));
|
||||
assertEquals("InvalidPrefixListID.NotFound", error.getErrorCode());
|
||||
assertTrue(service.describeSecurityGroupRules("us-east-1", List.of(groupId), List.of()).stream()
|
||||
.noneMatch(r -> !r.isEgress()), "the CIDR rule must not survive the rejection");
|
||||
}
|
||||
|
||||
/** A later bad permission must not leave an earlier good one applied either. */
|
||||
@Test
|
||||
void anUnknownPrefixListInASecondPermissionLeavesTheFirstUnapplied() {
|
||||
Ec2Service service = prefixListService();
|
||||
String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId();
|
||||
|
||||
IpPermission good = tcpPermission(443);
|
||||
good.getIpRanges().add(new IpRange("10.1.0.0/16", null));
|
||||
IpPermission bad = tcpPermission(5432);
|
||||
bad.getPrefixListIds().add(new PrefixListId("pl-doesnotexist", null));
|
||||
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.authorizeSecurityGroupIngress("us-east-1", groupId, List.of(good, bad)));
|
||||
assertTrue(service.describeSecurityGroupRules("us-east-1", List.of(groupId), List.of()).stream()
|
||||
.noneMatch(r -> !r.isEgress()), "no ingress rule from either permission");
|
||||
}
|
||||
|
||||
/**
|
||||
* Verified against a live AWS account: revoking the prefix list source leaves a CIDR
|
||||
* permission on the same protocol and ports untouched.
|
||||
*/
|
||||
@Test
|
||||
void revokingAPrefixListSourceLeavesACidrOnTheSameTupleAlone() {
|
||||
Ec2Service service = prefixListService();
|
||||
ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5,
|
||||
List.of(), List.of());
|
||||
String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId();
|
||||
|
||||
IpPermission viaCidr = tcpPermission(5432);
|
||||
viaCidr.getIpRanges().add(new IpRange("192.168.0.0/16", null));
|
||||
IpPermission viaList = tcpPermission(5432);
|
||||
viaList.getPrefixListIds().add(new PrefixListId(list.getPrefixListId(), null));
|
||||
service.authorizeSecurityGroupIngress("us-east-1", groupId, List.of(viaCidr, viaList));
|
||||
|
||||
service.revokeSecurityGroupIngress("us-east-1", groupId, List.of(viaList));
|
||||
|
||||
List<IpPermission> left = service.describeSecurityGroups("us-east-1", List.of(groupId), List.of(), Map.of())
|
||||
.getFirst().getIpPermissions();
|
||||
assertEquals(1, left.size(), "only the prefix list permission should have been revoked");
|
||||
assertEquals("192.168.0.0/16", left.getFirst().getIpRanges().getFirst().getCidrIp());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEgressRuleCanAlsoComeFromAPrefixList() {
|
||||
Ec2Service service = prefixListService();
|
||||
ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5,
|
||||
List.of(), List.of());
|
||||
String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId();
|
||||
|
||||
IpPermission perm = tcpPermission(443);
|
||||
perm.getPrefixListIds().add(new PrefixListId(list.getPrefixListId(), null));
|
||||
List<SecurityGroupRule> rules = service.authorizeSecurityGroupEgress("us-east-1", groupId, List.of(perm));
|
||||
|
||||
assertEquals(1, rules.size());
|
||||
assertTrue(rules.getFirst().isEgress());
|
||||
assertEquals(list.getPrefixListId(), rules.getFirst().getPrefixListId());
|
||||
}
|
||||
|
||||
/**
|
||||
* A caller may name the source group by name alone, which authorize resolves to a group id
|
||||
* before storing it. Scoped revocation has to resolve the same way, or a rule survives the
|
||||
* revoke that names it.
|
||||
*/
|
||||
@Test
|
||||
void revokingAGroupSourceNamedByNameOnlyStillMatchesTheStoredReference() {
|
||||
Ec2Service service = prefixListService();
|
||||
String sourceId = service.createSecurityGroup("us-east-1", "app", "app", null).getGroupId();
|
||||
String targetId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId();
|
||||
|
||||
IpPermission authorized = tcpPermission(5432);
|
||||
UserIdGroupPair byName = new UserIdGroupPair();
|
||||
byName.setGroupName("app");
|
||||
authorized.getUserIdGroupPairs().add(byName);
|
||||
List<SecurityGroupRule> rules =
|
||||
service.authorizeSecurityGroupIngress("us-east-1", targetId, List.of(authorized));
|
||||
assertEquals(sourceId, rules.getFirst().getReferencedGroupInfo().getGroupId());
|
||||
|
||||
IpPermission revocation = tcpPermission(5432);
|
||||
UserIdGroupPair alsoByName = new UserIdGroupPair();
|
||||
alsoByName.setGroupName("app");
|
||||
revocation.getUserIdGroupPairs().add(alsoByName);
|
||||
service.revokeSecurityGroupIngress("us-east-1", targetId, List.of(revocation));
|
||||
|
||||
assertTrue(service.describeSecurityGroups("us-east-1", List.of(targetId), List.of(), Map.of())
|
||||
.getFirst().getIpPermissions().isEmpty(), "the revoked group reference must be gone");
|
||||
}
|
||||
|
||||
/**
|
||||
* A rule's tags already reach the store and the rule itself; only DescribeTags mistyped them,
|
||||
* so a resource-type filter never matched.
|
||||
|
||||
Reference in new issue
Block a user