mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 18:48:00 -05:00
fix(ec2): revoke security group rules by SecurityGroupRuleId (#6192)
* fix(ec2): revoke security group rules by SecurityGroupRuleId Both revoke operations read SecurityGroupRuleId.N and delete each named rule, which also drops it from the group's IpPermissions. A rule id that names no rule of the group in that direction fails the call with InvalidSecurityGroupRuleId.NotFound and leaves every rule in place. * fix(ec2): revoke prefix list and unresolved group rules by id Removing a rule's peer matches its prefix list id, and a group reference recorded without an id matches the pair that also has none. A permission is dropped only once every source, prefix lists included, is gone. Revoke by id checks and removes its rules under the group lock, and the tests share one CIDR authorize helper. * test(ec2): build every rule source through one authorize helper The helper takes the action, prefix lists, CIDR and description, and declares its request as RequestSpecification.
This commit is contained in:
1 parent
f50a698ab7
commit
5ed0eca7a9
3 files changed
+332
-36
No files matched your search
+243
-8
@@ -1,13 +1,18 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
@@ -25,6 +30,8 @@ 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";
|
||||
private static final String INGRESS = "AuthorizeSecurityGroupIngress";
|
||||
private static final String EGRESS = "AuthorizeSecurityGroupEgress";
|
||||
|
||||
@Test
|
||||
void groupReferenceSurvivesAuthorizeAndBothDescribes() {
|
||||
@@ -57,7 +64,7 @@ class Ec2SecurityGroupRuleSourcesIntegrationTest {
|
||||
"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");
|
||||
authorize(INGRESS, targetId, "8443", List.of(), "10.0.0.0/8", "control");
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeSecurityGroups")
|
||||
@@ -289,6 +296,182 @@ class Ec2SecurityGroupRuleSourcesIntegrationTest {
|
||||
+ ".item.description", equalTo("v6-range"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void revokeIngressBySecurityGroupRuleIdRemovesTheRule() {
|
||||
String vpcId = createVpc();
|
||||
String groupId = createSecurityGroup(uniqueName("revoke-by-id"), vpcId);
|
||||
Response authorize = authorize(INGRESS, groupId, "5432", List.of(), "10.0.0.0/16", null);
|
||||
String ruleId = ruleIdFor(INGRESS, authorize, "cidrIpv4", "10.0.0.0/16");
|
||||
|
||||
revokeByRuleId("RevokeSecurityGroupIngress", groupId, ruleId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("RevokeSecurityGroupIngressResponse.return", equalTo("true"));
|
||||
|
||||
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)
|
||||
.body(not(containsString(ruleId)));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeSecurityGroups")
|
||||
.formParam("GroupId.1", groupId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(not(containsString("<fromPort>5432</fromPort>")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void revokeEgressBySecurityGroupRuleIdRemovesTheRule() {
|
||||
String vpcId = createVpc();
|
||||
String groupId = createSecurityGroup(uniqueName("revoke-egress-by-id"), vpcId);
|
||||
Response authorize = authorize(EGRESS, groupId, "8443", List.of(), "10.5.0.0/16", null);
|
||||
String ruleId = ruleIdFor(EGRESS, authorize, "cidrIpv4", "10.5.0.0/16");
|
||||
|
||||
revokeByRuleId("RevokeSecurityGroupEgress", groupId, ruleId)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeSecurityGroups")
|
||||
.formParam("GroupId.1", groupId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(not(containsString("10.5.0.0/16")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void revokeBySecurityGroupRuleIdOfAnotherGroupFailsAndKeepsTheRule() {
|
||||
String vpcId = createVpc();
|
||||
String targetId = createSecurityGroup(uniqueName("revoke-target"), vpcId);
|
||||
String otherId = createSecurityGroup(uniqueName("revoke-other"), vpcId);
|
||||
Response authorize = authorize(INGRESS, otherId, "9999", List.of(), "10.0.0.0/16", null);
|
||||
String otherRuleId = ruleIdFor(INGRESS, authorize, "cidrIpv4", "10.0.0.0/16");
|
||||
|
||||
revokeByRuleId("RevokeSecurityGroupIngress", targetId, otherRuleId)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidSecurityGroupRuleId.NotFound</Code>"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeSecurityGroupRules")
|
||||
.formParam("SecurityGroupRuleId.1", otherRuleId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString(otherRuleId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void revokingOnePrefixListRuleKeepsAnotherAuthorizedSeparately() {
|
||||
String groupId = createSecurityGroup(uniqueName("revoke-pl-separate"), createVpc());
|
||||
String keptList = createPrefixList();
|
||||
String revokedList = createPrefixList();
|
||||
authorize(INGRESS, groupId, "443", List.of(keptList), null, null);
|
||||
Response authorize = authorize(INGRESS, groupId, "443", List.of(revokedList), null, null);
|
||||
String ruleId = ruleIdFor(INGRESS, authorize, "prefixListId", revokedList);
|
||||
|
||||
revokeByRuleId("RevokeSecurityGroupIngress", groupId, ruleId).then().statusCode(200);
|
||||
|
||||
String group = describeGroup(groupId);
|
||||
assertTrue(group.contains(keptList), group);
|
||||
assertFalse(group.contains(revokedList), group);
|
||||
}
|
||||
|
||||
@Test
|
||||
void revokingOnePrefixListRuleKeepsItsNeighbourInThePermission() {
|
||||
String groupId = createSecurityGroup(uniqueName("revoke-pl-shared"), createVpc());
|
||||
String keptList = createPrefixList();
|
||||
String revokedList = createPrefixList();
|
||||
Response authorize = authorize(INGRESS, groupId, "443", List.of(keptList, revokedList), null, null);
|
||||
String ruleId = ruleIdFor(INGRESS, authorize, "prefixListId", revokedList);
|
||||
|
||||
revokeByRuleId("RevokeSecurityGroupIngress", groupId, ruleId).then().statusCode(200);
|
||||
|
||||
String group = describeGroup(groupId);
|
||||
assertTrue(group.contains(keptList), group);
|
||||
assertFalse(group.contains(revokedList), group);
|
||||
}
|
||||
|
||||
@Test
|
||||
void revokingAPrefixListRuleBesideACidrKeepsTheCidr() {
|
||||
String groupId = createSecurityGroup(uniqueName("revoke-pl-cidr"), createVpc());
|
||||
String prefixList = createPrefixList();
|
||||
Response authorize = authorize(INGRESS, groupId, "443", List.of(prefixList), "10.9.0.0/16", null);
|
||||
String ruleId = ruleIdFor(INGRESS, authorize, "prefixListId", prefixList);
|
||||
|
||||
revokeByRuleId("RevokeSecurityGroupIngress", groupId, ruleId).then().statusCode(200);
|
||||
|
||||
String group = describeGroup(groupId);
|
||||
assertFalse(group.contains(prefixList), group);
|
||||
assertTrue(group.contains("10.9.0.0/16"), group);
|
||||
}
|
||||
|
||||
@Test
|
||||
void revokingACidrRuleBesideAPrefixListKeepsTheList() {
|
||||
String groupId = createSecurityGroup(uniqueName("revoke-cidr-pl"), createVpc());
|
||||
String prefixList = createPrefixList();
|
||||
Response authorize = authorize(INGRESS, groupId, "443", List.of(prefixList), "10.9.0.0/16", null);
|
||||
String cidrRuleId = ruleIdFor(INGRESS, authorize, "cidrIpv4", "10.9.0.0/16");
|
||||
String listRuleId = ruleIdFor(INGRESS, authorize, "prefixListId", prefixList);
|
||||
|
||||
revokeByRuleId("RevokeSecurityGroupIngress", groupId, cidrRuleId).then().statusCode(200);
|
||||
|
||||
String group = describeGroup(groupId);
|
||||
assertTrue(group.contains(prefixList), group);
|
||||
assertFalse(group.contains("10.9.0.0/16"), group);
|
||||
assertTrue(describeRules(groupId).contains(listRuleId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void revokingARuleForAnUnknownGroupNameRemovesThePair() {
|
||||
String groupId = createSecurityGroup(uniqueName("revoke-unknown-name"), createVpc());
|
||||
String missingName = uniqueName("no-such-group");
|
||||
String ruleId = given()
|
||||
.formParam("Action", "AuthorizeSecurityGroupIngress")
|
||||
.formParam("GroupId", groupId)
|
||||
.formParam("IpPermissions.1.IpProtocol", "tcp")
|
||||
.formParam("IpPermissions.1.FromPort", "9100")
|
||||
.formParam("IpPermissions.1.ToPort", "9100")
|
||||
.formParam("IpPermissions.1.Groups.1.GroupName", missingName)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("AuthorizeSecurityGroupIngressResponse.securityGroupRuleSet.item.securityGroupRuleId");
|
||||
|
||||
revokeByRuleId("RevokeSecurityGroupIngress", groupId, ruleId).then().statusCode(200);
|
||||
|
||||
String group = describeGroup(groupId);
|
||||
assertFalse(group.contains(missingName), group);
|
||||
assertFalse(group.contains("<fromPort>9100</fromPort>"), group);
|
||||
}
|
||||
|
||||
private Response revokeByRuleId(String action, String groupId, String ruleId) {
|
||||
return given()
|
||||
.formParam("Action", action)
|
||||
.formParam("GroupId", groupId)
|
||||
.formParam("SecurityGroupRuleId.1", ruleId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/");
|
||||
}
|
||||
|
||||
private String referencedGroupInfoXml(String groupId) {
|
||||
return "<referencedGroupInfo><groupId>" + groupId + "</groupId>"
|
||||
+ "<userId>" + ACCOUNT_ID + "</userId></referencedGroupInfo>";
|
||||
@@ -325,19 +508,71 @@ class Ec2SecurityGroupRuleSourcesIntegrationTest {
|
||||
.extract().path("CreateSecurityGroupResponse.groupId");
|
||||
}
|
||||
|
||||
private void authorizeCidrIngress(String groupId, String port, String cidr, String description) {
|
||||
given()
|
||||
.formParam("Action", "AuthorizeSecurityGroupIngress")
|
||||
/** Authorizes one tcp permission on {@code port} naming every source given. */
|
||||
private Response authorize(String action, String groupId, String port, List<String> prefixLists,
|
||||
String cidr, String description) {
|
||||
RequestSpecification request = given()
|
||||
.formParam("Action", action)
|
||||
.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)
|
||||
.formParam("IpPermissions.1.ToPort", port);
|
||||
for (int i = 0; i < prefixLists.size(); i++) {
|
||||
request.formParam("IpPermissions.1.PrefixListIds." + (i + 1) + ".PrefixListId", prefixLists.get(i));
|
||||
}
|
||||
if (cidr != null) {
|
||||
request.formParam("IpPermissions.1.IpRanges.1.CidrIp", cidr);
|
||||
}
|
||||
if (description != null) {
|
||||
request.formParam("IpPermissions.1.IpRanges.1.Description", description);
|
||||
}
|
||||
Response response = request.header("Authorization", AUTH_HEADER).when().post("/");
|
||||
response.then().statusCode(200);
|
||||
return response;
|
||||
}
|
||||
|
||||
private String ruleIdFor(String action, Response authorize, String field, String value) {
|
||||
return authorize.path(action + "Response.securityGroupRuleSet.item.find { it."
|
||||
+ field + " == '" + value + "' }.securityGroupRuleId");
|
||||
}
|
||||
|
||||
private String createPrefixList() {
|
||||
return given()
|
||||
.formParam("Action", "CreateManagedPrefixList")
|
||||
.formParam("PrefixListName", uniqueName("revoke-pl"))
|
||||
.formParam("AddressFamily", "IPv4")
|
||||
.formParam("MaxEntries", "5")
|
||||
.formParam("Entry.1.Cidr", "10.0.0.0/8")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
.statusCode(200)
|
||||
.extract().path("CreateManagedPrefixListResponse.prefixList.prefixListId");
|
||||
}
|
||||
|
||||
private String describeGroup(String groupId) {
|
||||
return given()
|
||||
.formParam("Action", "DescribeSecurityGroups")
|
||||
.formParam("GroupId.1", groupId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().body().asString();
|
||||
}
|
||||
|
||||
private String describeRules(String groupId) {
|
||||
return 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)
|
||||
.extract().body().asString();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user