mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): keep security group rule description updates consistent (#8277)
Signed-off-by: Akash Kurait <akurait@amazon.com> Signed-off-by: Jackson Price <jxnprice@amazon.com> Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
a56739ca30
commit
ae62fe04bc
5 files changed
+486
-63
No files matched your search
@@ -401,7 +401,7 @@ private gateway lifecycles are not modelled.
|
||||
| RevokeSecurityGroupEgress | Removes outbound permissions, matched the same way as the inbound call. |
|
||||
| GetSecurityGroupsForVpc | Lists the security groups belonging to one VPC, with the same filters as the describe call. |
|
||||
| DescribeSecurityGroupRules | Lists stored security group rules. |
|
||||
| ModifySecurityGroupRules | Updates supported fields on security group rules. |
|
||||
| ModifySecurityGroupRules | Applies only the nested `SecurityGroupRule.Description` field; other rule fields are ignored. Updates both rule and group views. Every rule must belong to the requested group before any update is applied. |
|
||||
| UpdateSecurityGroupRuleDescriptionsIngress | Updates descriptions on matching inbound security group rules. |
|
||||
| UpdateSecurityGroupRuleDescriptionsEgress | Updates descriptions on matching outbound security group rules. |
|
||||
|
||||
|
||||
@@ -3775,7 +3775,7 @@ public class Ec2QueryHandler {
|
||||
if (ruleId == null) break;
|
||||
Map<String, String> update = new LinkedHashMap<>();
|
||||
update.put("SecurityGroupRuleId", ruleId);
|
||||
String desc = p.getFirst("SecurityGroupRule." + i + ".SecurityGroupRuleRequest.Description");
|
||||
String desc = p.getFirst("SecurityGroupRule." + i + ".SecurityGroupRule.Description");
|
||||
if (desc != null) update.put("Description", desc);
|
||||
updates.add(update);
|
||||
}
|
||||
|
||||
@@ -8444,67 +8444,80 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* prefix list, and the whole permission only once nothing is left in it. Matching on protocol
|
||||
* and ports alone would take out an unrelated rule that happens to share them.
|
||||
*/
|
||||
private boolean removeRecordedPermission(List<IpPermission> perms, SecurityGroupRule rule) {
|
||||
String referencedGroupId = rule.getReferencedGroupInfo() == null
|
||||
? null : rule.getReferencedGroupInfo().getGroupId();
|
||||
for (IpPermission perm : perms) {
|
||||
if (!Objects.equals(perm.getIpProtocol(), rule.getIpProtocol())
|
||||
|| !Objects.equals(perm.getFromPort(), rule.getFromPort())
|
||||
|| !Objects.equals(perm.getToPort(), rule.getToPort())) {
|
||||
private boolean removeRecordedPermission(List<IpPermission> permissions, SecurityGroupRule rule) {
|
||||
RecordedPermission match = findRecordedPermission(permissions, rule);
|
||||
if (match == null) {
|
||||
return false;
|
||||
}
|
||||
match.removeSource();
|
||||
dropIfEmpty(permissions, match.permission());
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Locates a source while the caller holds the group lock through its subsequent mutation. */
|
||||
private RecordedPermission findRecordedPermission(List<IpPermission> permissions, SecurityGroupRule rule) {
|
||||
for (IpPermission permission : permissions) {
|
||||
if (!Objects.equals(permission.getIpProtocol(), rule.getIpProtocol())
|
||||
|| !Objects.equals(permission.getFromPort(), rule.getFromPort())
|
||||
|| !Objects.equals(permission.getToPort(), rule.getToPort())) {
|
||||
continue;
|
||||
}
|
||||
if (rule.getCidrIpv6() != null) {
|
||||
Ipv6Range match6 = perm.getIpv6Ranges().stream()
|
||||
.filter(r -> rule.getCidrIpv6().equals(r.getCidrIpv6()))
|
||||
.findFirst().orElse(null);
|
||||
if (match6 == null) {
|
||||
continue;
|
||||
for (int index = 0; index < permission.getIpv6Ranges().size(); index++) {
|
||||
if (rule.getCidrIpv6().equals(permission.getIpv6Ranges().get(index).getCidrIpv6())) {
|
||||
return new RecordedPermission(permission, PermissionSource.IPV6, index);
|
||||
}
|
||||
}
|
||||
perm.getIpv6Ranges().remove(match6);
|
||||
dropIfEmpty(perms, perm);
|
||||
return true;
|
||||
}
|
||||
if (rule.getReferencedGroupInfo() != null) {
|
||||
// A reference by a name that matched no group was recorded without an id.
|
||||
UserIdGroupPair matchPair = perm.getUserIdGroupPairs().stream()
|
||||
.filter(pair -> Objects.equals(referencedGroupId, pair.getGroupId()))
|
||||
.findFirst().orElse(null);
|
||||
if (matchPair == null) {
|
||||
continue;
|
||||
} else if (rule.getReferencedGroupInfo() != null) {
|
||||
ReferencedSecurityGroup reference = rule.getReferencedGroupInfo();
|
||||
for (int index = 0; index < permission.getUserIdGroupPairs().size(); index++) {
|
||||
UserIdGroupPair pair = permission.getUserIdGroupPairs().get(index);
|
||||
if (Objects.equals(reference.getGroupId(), pair.getGroupId())
|
||||
&& Objects.equals(reference.getUserId(), pair.getUserId())) {
|
||||
return new RecordedPermission(permission, PermissionSource.GROUP, index);
|
||||
}
|
||||
}
|
||||
perm.getUserIdGroupPairs().remove(matchPair);
|
||||
dropIfEmpty(perms, perm);
|
||||
return true;
|
||||
}
|
||||
if (rule.getCidrIpv4() != null) {
|
||||
IpRange match = perm.getIpRanges().stream()
|
||||
.filter(r -> rule.getCidrIpv4().equals(r.getCidrIp()))
|
||||
.findFirst().orElse(null);
|
||||
if (match == null) {
|
||||
continue;
|
||||
} else if (rule.getCidrIpv4() != null) {
|
||||
for (int index = 0; index < permission.getIpRanges().size(); index++) {
|
||||
if (rule.getCidrIpv4().equals(permission.getIpRanges().get(index).getCidrIp())) {
|
||||
return new RecordedPermission(permission, PermissionSource.IPV4, index);
|
||||
}
|
||||
}
|
||||
perm.getIpRanges().remove(match);
|
||||
dropIfEmpty(perms, perm);
|
||||
return true;
|
||||
}
|
||||
if (rule.getPrefixListId() != null) {
|
||||
PrefixListId matchList = perm.getPrefixListIds().stream()
|
||||
.filter(l -> rule.getPrefixListId().equals(l.getPrefixListId()))
|
||||
.findFirst().orElse(null);
|
||||
if (matchList == null) {
|
||||
continue;
|
||||
} else if (rule.getPrefixListId() != null) {
|
||||
for (int index = 0; index < permission.getPrefixListIds().size(); index++) {
|
||||
if (rule.getPrefixListId().equals(permission.getPrefixListIds().get(index).getPrefixListId())) {
|
||||
return new RecordedPermission(permission, PermissionSource.PREFIX_LIST, index);
|
||||
}
|
||||
}
|
||||
perm.getPrefixListIds().remove(matchList);
|
||||
dropIfEmpty(perms, perm);
|
||||
return true;
|
||||
}
|
||||
// A record naming no peer matches only a permission that names none either.
|
||||
if (!hasSources(perm)) {
|
||||
perms.remove(perm);
|
||||
return true;
|
||||
} else if (!hasSources(permission)) {
|
||||
return new RecordedPermission(permission, PermissionSource.EMPTY, -1);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private enum PermissionSource { IPV4, IPV6, GROUP, PREFIX_LIST, EMPTY }
|
||||
|
||||
private record RecordedPermission(IpPermission permission, PermissionSource source, int index) {
|
||||
private void removeSource() {
|
||||
switch (source) {
|
||||
case IPV4 -> permission.getIpRanges().remove(index);
|
||||
case IPV6 -> permission.getIpv6Ranges().remove(index);
|
||||
case GROUP -> permission.getUserIdGroupPairs().remove(index);
|
||||
case PREFIX_LIST -> permission.getPrefixListIds().remove(index);
|
||||
case EMPTY -> { }
|
||||
}
|
||||
}
|
||||
|
||||
private void setDescription(String description) {
|
||||
switch (source) {
|
||||
case IPV4 -> permission.getIpRanges().get(index).setDescription(description);
|
||||
case IPV6 -> permission.getIpv6Ranges().get(index).setDescription(description);
|
||||
case GROUP -> permission.getUserIdGroupPairs().get(index).setDescription(description);
|
||||
case PREFIX_LIST -> permission.getPrefixListIds().get(index).setDescription(description);
|
||||
case EMPTY -> { }
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void dropIfEmpty(List<IpPermission> perms, IpPermission perm) {
|
||||
@@ -8604,20 +8617,51 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
|
||||
public void modifySecurityGroupRules(String region, String groupId, List<Map<String, String>> ruleUpdates) {
|
||||
ensureDefaultResources(region);
|
||||
// Update description on matching rules
|
||||
for (Map<String, String> update : ruleUpdates) {
|
||||
String ruleId = update.get("SecurityGroupRuleId");
|
||||
String desc = update.get("Description");
|
||||
if (ruleId != null) {
|
||||
SecurityGroupRule rule = securityGroupRules.get(key(region, ruleId)).orElse(null);
|
||||
if (rule != null && desc != null) {
|
||||
rule.setDescription(desc);
|
||||
securityGroupRules.put(key(region, ruleId), rule);
|
||||
synchronized (attachmentTopologyLock(region)) {
|
||||
synchronized (lockFor(key(region, groupId))) {
|
||||
SecurityGroup group = getRequiredSecurityGroup(region, groupId);
|
||||
List<RuleDescriptionUpdate> updates = requireRuleDescriptionUpdates(region, groupId, ruleUpdates);
|
||||
for (RuleDescriptionUpdate update : updates) {
|
||||
SecurityGroupRule rule = update.rule();
|
||||
rule.setDescription(update.description());
|
||||
updatePermissionDescription(group, rule);
|
||||
securityGroupRules.put(key(region, rule.getSecurityGroupRuleId()), rule);
|
||||
}
|
||||
if (!updates.isEmpty()) {
|
||||
securityGroups.put(key(region, groupId), group);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<RuleDescriptionUpdate> requireRuleDescriptionUpdates(String region, String groupId,
|
||||
List<Map<String, String>> ruleUpdates) {
|
||||
List<RuleDescriptionUpdate> updates = new ArrayList<>();
|
||||
for (Map<String, String> update : ruleUpdates) {
|
||||
String ruleId = update.get("SecurityGroupRuleId");
|
||||
SecurityGroupRule rule = securityGroupRules.get(key(region, ruleId)).orElse(null);
|
||||
if (rule == null || !groupId.equals(rule.getGroupId())) {
|
||||
throw new AwsException("InvalidSecurityGroupRuleId.NotFound",
|
||||
"The security group rule ID '" + ruleId + "' does not exist", 400);
|
||||
}
|
||||
String description = update.get("Description");
|
||||
if (description != null) {
|
||||
updates.add(new RuleDescriptionUpdate(rule, description));
|
||||
}
|
||||
}
|
||||
return updates;
|
||||
}
|
||||
|
||||
private record RuleDescriptionUpdate(SecurityGroupRule rule, String description) {}
|
||||
|
||||
private void updatePermissionDescription(SecurityGroup group, SecurityGroupRule rule) {
|
||||
List<IpPermission> permissions = rule.isEgress() ? group.getIpPermissionsEgress() : group.getIpPermissions();
|
||||
RecordedPermission match = findRecordedPermission(permissions, rule);
|
||||
if (match != null) {
|
||||
match.setDescription(rule.getDescription());
|
||||
}
|
||||
}
|
||||
|
||||
public void updateSecurityGroupRuleDescriptionsIngress(String region, String groupId, List<IpPermission> permissions) {
|
||||
ensureDefaultResources(region);
|
||||
// no-op for mock
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
package io.floci.aws.services.ec2;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.PartitionCleanup;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.Filter;
|
||||
import software.amazon.awssdk.services.ec2.model.IpPermission;
|
||||
import software.amazon.awssdk.services.ec2.model.IpRange;
|
||||
import software.amazon.awssdk.services.ec2.model.SecurityGroup;
|
||||
import software.amazon.awssdk.services.ec2.model.SecurityGroupRule;
|
||||
import software.amazon.awssdk.services.ec2.model.SecurityGroupRuleRequest;
|
||||
import software.amazon.awssdk.services.ec2.model.SecurityGroupRuleUpdate;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.floci.aws.testing.AwsSdkErrors.assertAwsError;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class Ec2RuleDescriptionIntegrationTest {
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void sdkDescriptionUpdatePreservesTheRuleUntilGroupDeletion(boolean egress) {
|
||||
Ec2Client client = AwsTestClients.create(Ec2Client.builder(), "555566668904", Region.US_WEST_2);
|
||||
cleanup.register(client::close);
|
||||
String vpc = client.createVpc(request -> request.cidrBlock("10.234.0.0/24")).vpc().vpcId();
|
||||
cleanup.register(() -> client.deleteVpc(request -> request.vpcId(vpc)));
|
||||
String name = "rule-description-" + UUID.randomUUID();
|
||||
cleanup.register(() -> client.describeSecurityGroups(request -> request.filters(
|
||||
Filter.builder().name("vpc-id").values(vpc).build(),
|
||||
Filter.builder().name("group-name").values(name).build())).securityGroups()
|
||||
.forEach(group -> client.deleteSecurityGroup(request -> request.groupId(group.groupId()))));
|
||||
String group = client.createSecurityGroup(request -> request.vpcId(vpc).groupName(name)
|
||||
.description("owned description fixture")).groupId();
|
||||
String rule = createRule(client, group, egress).securityGroupRuleId();
|
||||
SecurityGroupRule before = client.describeSecurityGroupRules(request -> request
|
||||
.securityGroupRuleIds(rule)).securityGroupRules().getFirst();
|
||||
IpPermission beforePermission = describedPermission(client, group, egress);
|
||||
|
||||
assertTrue(client.modifySecurityGroupRules(request -> request.groupId(group).securityGroupRules(
|
||||
SecurityGroupRuleUpdate.builder().securityGroupRuleId(rule).securityGroupRule(
|
||||
SecurityGroupRuleRequest.builder().description("after").build()).build())).returnValue());
|
||||
SecurityGroupRule after = client.describeSecurityGroupRules(request -> request
|
||||
.securityGroupRuleIds(rule)).securityGroupRules().getFirst();
|
||||
assertEquals(before.toBuilder().description("after").build(), after);
|
||||
assertEquals(beforePermission.toBuilder().ipRanges(beforePermission.ipRanges().getFirst().toBuilder()
|
||||
.description("after").build()).build(), describedPermission(client, group, egress));
|
||||
|
||||
client.deleteSecurityGroup(request -> request.groupId(group));
|
||||
assertTrue(client.describeSecurityGroupRules(request -> request.filters(
|
||||
Filter.builder().name("group-id").values(group).build())).securityGroupRules().isEmpty());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void aForeignRuleRejectsTheWholeDescriptionUpdate(boolean egress) {
|
||||
Ec2Client client = AwsTestClients.create(Ec2Client.builder(), "555566668904", Region.US_WEST_2);
|
||||
cleanup.register(client::close);
|
||||
String vpc = client.createVpc(request -> request.cidrBlock("10.235.0.0/24")).vpc().vpcId();
|
||||
cleanup.register(() -> client.deleteVpc(request -> request.vpcId(vpc)));
|
||||
String group = createGroup(client, vpc);
|
||||
String otherGroup = createGroup(client, vpc);
|
||||
SecurityGroupRule owned = createRule(client, group, egress);
|
||||
SecurityGroupRule foreign = createRule(client, otherGroup, egress);
|
||||
List<SecurityGroup> before = client.describeSecurityGroups(request -> request.groupIds(group, otherGroup))
|
||||
.securityGroups();
|
||||
|
||||
assertAwsError("InvalidSecurityGroupRuleId.NotFound", 400, () -> client.modifySecurityGroupRules(request ->
|
||||
request.groupId(group).securityGroupRules(descriptionUpdate(owned), descriptionUpdate(foreign))));
|
||||
|
||||
assertEquals(owned, client.describeSecurityGroupRules(request ->
|
||||
request.securityGroupRuleIds(owned.securityGroupRuleId())).securityGroupRules().getFirst());
|
||||
assertEquals(foreign, client.describeSecurityGroupRules(request ->
|
||||
request.securityGroupRuleIds(foreign.securityGroupRuleId())).securityGroupRules().getFirst());
|
||||
assertEquals(before, client.describeSecurityGroups(request -> request.groupIds(group, otherGroup))
|
||||
.securityGroups());
|
||||
}
|
||||
|
||||
private String createGroup(Ec2Client client, String vpc) {
|
||||
String group = client.createSecurityGroup(request -> request.vpcId(vpc)
|
||||
.groupName("rule-owner-" + UUID.randomUUID()).description("owned rule fixture")).groupId();
|
||||
cleanup.register(() -> client.deleteSecurityGroup(request -> request.groupId(group)));
|
||||
return group;
|
||||
}
|
||||
|
||||
private IpPermission describedPermission(Ec2Client client, String group, boolean egress) {
|
||||
SecurityGroup described = client.describeSecurityGroups(request -> request.groupIds(group))
|
||||
.securityGroups().getFirst();
|
||||
List<IpPermission> permissions = egress ? described.ipPermissionsEgress() : described.ipPermissions();
|
||||
return permissions.stream().filter(permission -> "tcp".equals(permission.ipProtocol()))
|
||||
.findFirst().orElseThrow();
|
||||
}
|
||||
|
||||
private SecurityGroupRule createRule(Ec2Client client, String group, boolean egress) {
|
||||
IpPermission permission = IpPermission.builder().ipProtocol("tcp").fromPort(443).toPort(443)
|
||||
.ipRanges(IpRange.builder().cidrIp("10.235.0.0/24").description("before").build()).build();
|
||||
return (egress
|
||||
? client.authorizeSecurityGroupEgress(request -> request.groupId(group)
|
||||
.ipPermissions(permission)).securityGroupRules()
|
||||
: client.authorizeSecurityGroupIngress(request -> request.groupId(group)
|
||||
.ipPermissions(permission)).securityGroupRules()).getFirst();
|
||||
}
|
||||
|
||||
private SecurityGroupRuleUpdate descriptionUpdate(SecurityGroupRule rule) {
|
||||
return SecurityGroupRuleUpdate.builder().securityGroupRuleId(rule.securityGroupRuleId())
|
||||
.securityGroupRule(SecurityGroupRuleRequest.builder().description("after").build()).build();
|
||||
}
|
||||
}
|
||||
+260
@@ -0,0 +1,260 @@
|
||||
package io.floci.aws.services.ec2;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.storage.PersistentStorage;
|
||||
import io.floci.aws.services.ec2.model.IpPermission;
|
||||
import io.floci.aws.services.ec2.model.IpRange;
|
||||
import io.floci.aws.services.ec2.model.Ipv6Range;
|
||||
import io.floci.aws.services.ec2.model.PrefixListId;
|
||||
import io.floci.aws.services.ec2.model.SecurityGroup;
|
||||
import io.floci.aws.services.ec2.model.SecurityGroupRule;
|
||||
import io.floci.aws.services.ec2.model.UserIdGroupPair;
|
||||
import io.floci.aws.testing.Ec2ServiceFixtures;
|
||||
import io.floci.aws.testing.InMemoryStorageFactory;
|
||||
import io.floci.aws.testing.ParkingStore;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class Ec2RuleDescriptionPublicationServiceTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
@TempDir
|
||||
Path directory;
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void capturedDescriptionUpdateCannotRestoreARuleAfterGroupDeletion(boolean egress) throws Exception {
|
||||
Fixture fixture = new Fixture(egress, directory);
|
||||
JsonNode retainedRules = fixture.retainedRules();
|
||||
fixture.rules.parkNextRead();
|
||||
assertOperationWaitsForParkedStore(fixture.rules, fixture::modify,
|
||||
() -> fixture.service.deleteSecurityGroup(REGION, fixture.group));
|
||||
assertEquals(retainedRules, fixture.retainedRules());
|
||||
assertEquals(1, fixture.service.describeSecurityGroups(REGION, List.of(fixture.retainedGroup),
|
||||
List.of(), Map.of()).size());
|
||||
fixture.assertGroupAndRulesAbsent();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void serialDescriptionUpdateIsObservableAndPreservesUnrelatedRules(boolean egress) {
|
||||
Fixture fixture = new Fixture(egress, directory);
|
||||
JsonNode retainedRules = fixture.retainedRules();
|
||||
assertEquals("before", fixture.rule.getDescription());
|
||||
fixture.modify();
|
||||
assertEquals("after", fixture.service.describeSecurityGroupRules(REGION, List.of(fixture.group),
|
||||
List.of(fixture.rule.getSecurityGroupRuleId())).getFirst().getDescription());
|
||||
assertEquals(retainedRules, fixture.retainedRules());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void updateAfterDeletionDoesNotRecreateTheRule(boolean egress) {
|
||||
Fixture fixture = new Fixture(egress, directory);
|
||||
JsonNode retainedRules = fixture.retainedRules();
|
||||
fixture.service.deleteSecurityGroup(REGION, fixture.group);
|
||||
fixture.assertGroupAndRulesAbsent();
|
||||
AwsException error = assertThrows(AwsException.class, fixture::modify);
|
||||
assertEquals("InvalidGroup.NotFound", error.getErrorCode());
|
||||
fixture.assertGroupAndRulesAbsent();
|
||||
assertEquals(retainedRules, fixture.retainedRules());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void capturedDescriptionUpdateCannotRestoreARevokedRule(boolean egress) throws Exception {
|
||||
Fixture fixture = new Fixture(egress, directory);
|
||||
fixture.rules.parkNextRead();
|
||||
assertOperationWaitsForParkedStore(fixture.rules, fixture::modify, () ->
|
||||
fixture.service.revokeSecurityGroupRulesByIds(REGION, fixture.group,
|
||||
List.of(fixture.rule.getSecurityGroupRuleId()), egress));
|
||||
assertTrue(fixture.service.describeSecurityGroupRules(REGION, List.of(fixture.group),
|
||||
List.of(fixture.rule.getSecurityGroupRuleId())).isEmpty());
|
||||
assertTrue((egress ? fixture.groupValue().getIpPermissionsEgress() : fixture.groupValue().getIpPermissions())
|
||||
.stream().noneMatch(permission -> "tcp".equals(permission.getIpProtocol())));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"false, ipRanges", "true, ipRanges",
|
||||
"false, ipv6Ranges", "true, ipv6Ranges",
|
||||
"false, prefixListIds", "true, prefixListIds",
|
||||
"false, userIdGroupPairs", "true, userIdGroupPairs"
|
||||
})
|
||||
void selectedSourceDescriptionAndRemovalPreserveSiblingsAfterReload(boolean egress, String source) {
|
||||
Fixture fixture = new Fixture(egress, directory);
|
||||
IpPermission permission = fixture.mixedSourcePermission();
|
||||
List<SecurityGroupRule> added = egress
|
||||
? fixture.service.authorizeSecurityGroupEgress(REGION, fixture.group, List.of(permission))
|
||||
: fixture.service.authorizeSecurityGroupIngress(REGION, fixture.group, List.of(permission));
|
||||
SecurityGroupRule selected = added.stream().filter(rule -> switch (source) {
|
||||
case "ipRanges" -> rule.getCidrIpv4() != null;
|
||||
case "ipv6Ranges" -> rule.getCidrIpv6() != null;
|
||||
case "prefixListIds" -> rule.getPrefixListId() != null;
|
||||
case "userIdGroupPairs" -> rule.getReferencedGroupInfo() != null;
|
||||
default -> throw new IllegalArgumentException(source);
|
||||
}).findFirst().orElseThrow();
|
||||
JsonNode expectedGroup = MAPPER.valueToTree(fixture.groupValue());
|
||||
for (JsonNode expectedPermission : expectedGroup.get(egress ? "ipPermissionsEgress" : "ipPermissions")) {
|
||||
if (expectedPermission.path("fromPort").asInt() == 8443) {
|
||||
((ObjectNode) expectedPermission.get(source).get(0)).put("description", "after");
|
||||
}
|
||||
}
|
||||
List<String> retainedIds = added.stream().filter(rule -> rule != selected)
|
||||
.map(SecurityGroupRule::getSecurityGroupRuleId).toList();
|
||||
JsonNode retainedRules = MAPPER.valueToTree(fixture.service.describeSecurityGroupRules(
|
||||
REGION, List.of(fixture.group), retainedIds));
|
||||
|
||||
fixture.service.modifySecurityGroupRules(REGION, fixture.group, List.of(
|
||||
Map.of("SecurityGroupRuleId", selected.getSecurityGroupRuleId(), "Description", "after")));
|
||||
|
||||
assertEquals("after", fixture.service.describeSecurityGroupRules(REGION, List.of(fixture.group),
|
||||
List.of(selected.getSecurityGroupRuleId())).getFirst().getDescription());
|
||||
assertEquals(retainedRules, MAPPER.valueToTree(fixture.service.describeSecurityGroupRules(
|
||||
REGION, List.of(fixture.group), retainedIds)));
|
||||
assertEquals(expectedGroup, MAPPER.valueToTree(fixture.groupValue()));
|
||||
fixture.groups.load();
|
||||
assertEquals(expectedGroup, MAPPER.valueToTree(fixture.groupValue()));
|
||||
|
||||
fixture.service.revokeSecurityGroupRulesByIds(REGION, fixture.group,
|
||||
List.of(selected.getSecurityGroupRuleId()), egress);
|
||||
for (JsonNode expectedPermission : expectedGroup.get(egress ? "ipPermissionsEgress" : "ipPermissions")) {
|
||||
if (expectedPermission.path("fromPort").asInt() == 8443) {
|
||||
((ArrayNode) expectedPermission.get(source)).remove(0);
|
||||
}
|
||||
}
|
||||
assertEquals(retainedRules, MAPPER.valueToTree(fixture.service.describeSecurityGroupRules(
|
||||
REGION, List.of(fixture.group), retainedIds)));
|
||||
assertTrue(fixture.service.describeSecurityGroupRules(REGION, List.of(fixture.group),
|
||||
List.of(selected.getSecurityGroupRuleId())).isEmpty());
|
||||
assertEquals(expectedGroup, MAPPER.valueToTree(fixture.groupValue()));
|
||||
fixture.groups.load();
|
||||
assertEquals(expectedGroup, MAPPER.valueToTree(fixture.groupValue()));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void groupReferenceUpdateAndRemovalUseTheRecordedOwner(boolean egress) {
|
||||
Fixture fixture = new Fixture(egress, directory);
|
||||
UserIdGroupPair retained = new UserIdGroupPair();
|
||||
retained.setGroupId(fixture.retainedGroup);
|
||||
retained.setUserId("111122223333");
|
||||
retained.setDescription("retained");
|
||||
UserIdGroupPair selected = new UserIdGroupPair();
|
||||
selected.setGroupId(fixture.retainedGroup);
|
||||
selected.setUserId("444455556666");
|
||||
selected.setDescription("before");
|
||||
IpPermission permission = new IpPermission();
|
||||
permission.setIpProtocol("tcp");
|
||||
permission.setFromPort(8443);
|
||||
permission.setToPort(8443);
|
||||
permission.setUserIdGroupPairs(List.of(retained, selected));
|
||||
List<SecurityGroupRule> added = egress
|
||||
? fixture.service.authorizeSecurityGroupEgress(REGION, fixture.group, List.of(permission))
|
||||
: fixture.service.authorizeSecurityGroupIngress(REGION, fixture.group, List.of(permission));
|
||||
String selectedRule = added.stream().filter(rule -> selected.getUserId().equals(
|
||||
rule.getReferencedGroupInfo().getUserId())).findFirst().orElseThrow().getSecurityGroupRuleId();
|
||||
|
||||
fixture.service.modifySecurityGroupRules(REGION, fixture.group, List.of(
|
||||
Map.of("SecurityGroupRuleId", selectedRule, "Description", "after")));
|
||||
fixture.groups.load();
|
||||
List<IpPermission> permissions = egress ? fixture.groupValue().getIpPermissionsEgress()
|
||||
: fixture.groupValue().getIpPermissions();
|
||||
IpPermission updated = permissions.stream().filter(value -> Integer.valueOf(8443).equals(value.getFromPort()))
|
||||
.findFirst().orElseThrow();
|
||||
assertEquals("retained", updated.getUserIdGroupPairs().getFirst().getDescription());
|
||||
assertEquals("after", updated.getUserIdGroupPairs().getLast().getDescription());
|
||||
|
||||
fixture.service.revokeSecurityGroupRulesByIds(REGION, fixture.group, List.of(selectedRule), egress);
|
||||
fixture.groups.load();
|
||||
List<IpPermission> remaining = egress ? fixture.groupValue().getIpPermissionsEgress()
|
||||
: fixture.groupValue().getIpPermissions();
|
||||
IpPermission retainedPermission = remaining.stream()
|
||||
.filter(value -> Integer.valueOf(8443).equals(value.getFromPort())).findFirst().orElseThrow();
|
||||
assertEquals(MAPPER.valueToTree(List.of(retained)), MAPPER.valueToTree(retainedPermission.getUserIdGroupPairs()));
|
||||
assertTrue(fixture.service.describeSecurityGroupRules(REGION, List.of(fixture.group),
|
||||
List.of(selectedRule)).isEmpty());
|
||||
}
|
||||
|
||||
private static final class Fixture {
|
||||
private final ParkingStore<SecurityGroupRule> rules = new ParkingStore<>();
|
||||
private final PersistentStorage<String, SecurityGroup> groups;
|
||||
private final Ec2Service service;
|
||||
private final String group;
|
||||
private final String retainedGroup;
|
||||
private final SecurityGroupRule rule;
|
||||
|
||||
private Fixture(boolean egress, Path directory) {
|
||||
groups = new PersistentStorage<>(directory.resolve("groups.json"), new TypeReference<>() {});
|
||||
InMemoryStorageFactory stores = new InMemoryStorageFactory("000000000000")
|
||||
.withStore("ec2-security-group-rules.json", rules)
|
||||
.withStore("ec2-security-groups.json", groups);
|
||||
service = Ec2ServiceFixtures.mockModeService(stores);
|
||||
String vpc = service.createVpc(REGION, "10.66.0.0/16", false).getVpcId();
|
||||
group = service.createSecurityGroup(REGION, "owned", "owned", vpc).getGroupId();
|
||||
retainedGroup = service.createSecurityGroup(REGION, "retained", "retained", vpc).getGroupId();
|
||||
IpPermission permission = new IpPermission();
|
||||
permission.setIpProtocol("tcp");
|
||||
permission.setFromPort(443);
|
||||
permission.setToPort(443);
|
||||
permission.setIpRanges(new ArrayList<>(List.of(new IpRange("10.66.0.0/16", "before"))));
|
||||
List<SecurityGroupRule> added = egress
|
||||
? service.authorizeSecurityGroupEgress(REGION, group, List.of(permission))
|
||||
: service.authorizeSecurityGroupIngress(REGION, group, List.of(permission));
|
||||
rule = added.getFirst();
|
||||
}
|
||||
|
||||
private IpPermission mixedSourcePermission() {
|
||||
String prefixList = service.createManagedPrefixList(REGION, "description-source", "IPv4",
|
||||
1, List.of(), List.of()).getPrefixListId();
|
||||
UserIdGroupPair pair = new UserIdGroupPair();
|
||||
pair.setGroupId(retainedGroup);
|
||||
pair.setDescription("before");
|
||||
IpPermission permission = new IpPermission();
|
||||
permission.setIpProtocol("tcp");
|
||||
permission.setFromPort(8443);
|
||||
permission.setToPort(8443);
|
||||
permission.setIpRanges(List.of(new IpRange("10.66.1.0/24", "before")));
|
||||
permission.setIpv6Ranges(List.of(new Ipv6Range("2001:db8::/64", "before")));
|
||||
permission.setPrefixListIds(List.of(new PrefixListId(prefixList, "before")));
|
||||
permission.setUserIdGroupPairs(List.of(pair));
|
||||
return permission;
|
||||
}
|
||||
|
||||
private SecurityGroup groupValue() {
|
||||
return service.describeSecurityGroups(REGION, List.of(group), List.of(), Map.of()).getFirst();
|
||||
}
|
||||
|
||||
private void modify() {
|
||||
service.modifySecurityGroupRules(REGION, group, List.of(
|
||||
Map.of("SecurityGroupRuleId", rule.getSecurityGroupRuleId(), "Description", "after")));
|
||||
}
|
||||
|
||||
private JsonNode retainedRules() {
|
||||
return MAPPER.valueToTree(service.describeSecurityGroupRules(REGION, List.of(retainedGroup), List.of()));
|
||||
}
|
||||
|
||||
private void assertGroupAndRulesAbsent() {
|
||||
assertTrue(service.describeSecurityGroups(REGION, List.of(), List.of(), Map.of()).stream()
|
||||
.noneMatch(candidate -> candidate.getGroupId().equals(group)), "Deleted group stays absent");
|
||||
assertTrue(service.describeSecurityGroupRules(REGION, List.of(group), List.of()).isEmpty(),
|
||||
"Description update must not restore a rule whose group was deleted");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user