mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(cloudformation): validate prefix list entries before update diffing (#6725)
This commit is contained in:
1 parent
a4ef9eaa6a
commit
d58ce5c31c
4 files changed
+72
-6
No files matched your search
+5
-3
@@ -702,6 +702,9 @@ public class Ec2NetworkCfnProvisioner implements CfnResourceProvisioner {
|
||||
throw new AwsException("ValidationError", PREFIX_LIST + " cannot change the AddressFamily of prefix list "
|
||||
+ existing.getPrefixListId() + " from " + existing.getAddressFamily() + " to " + family, 400);
|
||||
}
|
||||
if (entries != null) {
|
||||
entries.forEach(entry -> ec2Service.validatePrefixListEntry(entry, family));
|
||||
}
|
||||
List<PrefixListEntry> desired = entries != null ? entries
|
||||
: ctx.previouslyDeclared("Entries") ? List.of() : existing.currentEntries();
|
||||
EntryChanges changes = EntryChanges.between(existing.currentEntries(), desired);
|
||||
@@ -800,9 +803,8 @@ public class Ec2NetworkCfnProvisioner implements CfnResourceProvisioner {
|
||||
/**
|
||||
* What takes a prefix list from its current entries to the desired ones: the entries to add and the
|
||||
* CIDRs to remove, with an entry whose block is both removed and added back (a changed description)
|
||||
* kept apart as a CIDR to {@code redescribe} and an entry to {@code readd}. An entry matches when its
|
||||
* CIDR is the same block and its description is the same, so a CIDR written with host bits does not
|
||||
* read as a change.
|
||||
* kept apart as a CIDR to {@code redescribe} and an entry to {@code readd}. Validated entries match
|
||||
* when their CIDRs identify the same block and their descriptions are the same.
|
||||
*/
|
||||
private record EntryChanges(List<PrefixListEntry> add, List<String> remove, List<String> redescribe,
|
||||
List<PrefixListEntry> readd) {
|
||||
|
||||
@@ -2076,7 +2076,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
}
|
||||
|
||||
private void validatePrefixListEntry(PrefixListEntry entry, String addressFamily) {
|
||||
/** Validates an entry before a consumer computes or applies a prefix-list update. */
|
||||
public void validatePrefixListEntry(PrefixListEntry entry, String addressFamily) {
|
||||
if (entry.getCidr() == null || entry.getCidr().isBlank()) {
|
||||
throw new AwsException("MissingParameter", "Every prefix list entry must specify a Cidr.", 400);
|
||||
}
|
||||
|
||||
+19
-2
@@ -1200,16 +1200,33 @@ class Ec2NetworkCfnProvisionerTest {
|
||||
describesPrefixList(prefixList(1, 2, OFFICE));
|
||||
StackResource r = prior("AWS::EC2::PrefixList", "Firewall", PREFIX_LIST_ID, Map.of("PrefixListId", PREFIX_LIST_ID));
|
||||
|
||||
provisioner.provision(r, prefixListProps(2, "192.0.2.19/24", "office"), ctx(PREFIX_LIST_ID));
|
||||
provisioner.provision(r, prefixListProps(2, "192.0.2.0/24", "office"), ctx(PREFIX_LIST_ID));
|
||||
|
||||
assertEquals(PREFIX_LIST_ID, r.getPhysicalId());
|
||||
assertEquals("1", r.getAttributes().get("Version"));
|
||||
assertFalse(r.getAttributes().containsKey(Ec2NetworkCfnProvisioner.IN_PLACE_PRIOR_ATTR),
|
||||
"a CIDR written with host bits is the same block, not a change");
|
||||
"an unchanged canonical entry needs no update");
|
||||
verify(ec2, never()).modifyManagedPrefixList(any(), any(), any(), any(), any(), any(), any());
|
||||
verify(ec2, never()).createManagedPrefixList(any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidUpdateEntriesAreRejectedBeforeDiffingOrTags() {
|
||||
describesPrefixList(prefixList(1, 2, OFFICE));
|
||||
StackResource r = prior("AWS::EC2::PrefixList", "Firewall", PREFIX_LIST_ID, Map.of("PrefixListId", PREFIX_LIST_ID));
|
||||
doThrow(new AwsException("InvalidParameterValue", "invalid CIDR", 400))
|
||||
.when(ec2).validatePrefixListEntry(any(), eq("IPv4"));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> provisioner.provision(r,
|
||||
withTags(prefixListProps(2, "192.0.2.19/24", "office"), "team", "changed"), ctx(PREFIX_LIST_ID)));
|
||||
|
||||
assertEquals("InvalidParameterValue", error.getErrorCode());
|
||||
assertFalse(r.getAttributes().containsKey(Ec2NetworkCfnProvisioner.IN_PLACE_PRIOR_ATTR));
|
||||
verify(ec2, never()).modifyManagedPrefixList(any(), any(), any(), any(), any(), any(), any());
|
||||
verify(ec2, never()).createTags(any(), any(), any());
|
||||
verify(ec2, never()).deleteTags(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aResizeAloneChangesOnlyMaxEntriesAndARollbackPutsTheCapacityBack() {
|
||||
ManagedPrefixList resized = prefixList(1, 5, OFFICE);
|
||||
|
||||
+46
@@ -92,6 +92,52 @@ class Ec2PrefixListCidrIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void cloudFormationRejectsSameBlockHostBitUpdateBeforeMutation() {
|
||||
verifyUpdate("cfn-same-block-host-bits", "192.0.2.19/24", "UPDATE_ROLLBACK_COMPLETE", "192.0.2.0/24", 1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void cloudFormationAppliesCanonicalBlockUpdate() {
|
||||
verifyUpdate("cfn-canonical-block-update", "198.51.100.0/24", "UPDATE_COMPLETE", "198.51.100.0/24", 2L);
|
||||
}
|
||||
|
||||
private static void verifyUpdate(String name, String desired, String status, String expected, long version) {
|
||||
try (Ec2Client client = client()) {
|
||||
try {
|
||||
stackRequest("CreateStack", name, "192.0.2.0/24");
|
||||
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(name, CFN_AUTH).status());
|
||||
String id = ids(client, name).getFirst();
|
||||
stackRequest("UpdateStack", name, desired);
|
||||
assertEquals(status, CfnStackWaits.awaitTerminal(name, CFN_AUTH).status());
|
||||
if (status.equals("UPDATE_ROLLBACK_COMPLETE")) {
|
||||
assertTrue(CfnStackWaits.failedResourceReason(name, CFN_AUTH)
|
||||
.contains("The specified CIDR (192.0.2.19/24) is not valid."));
|
||||
}
|
||||
assertEquals(List.of(id), ids(client, name));
|
||||
assertEquals(expected, client.getManagedPrefixListEntries(r -> r.prefixListId(id))
|
||||
.entries().getFirst().cidr());
|
||||
assertEquals(version, client.describeManagedPrefixLists(r -> r.prefixListIds(id))
|
||||
.prefixLists().getFirst().version());
|
||||
} finally {
|
||||
given().header("Authorization", CFN_AUTH).formParam("Action", "DeleteStack")
|
||||
.formParam("StackName", name).post("/").then().statusCode(200);
|
||||
CfnStackWaits.awaitStackDeleted(name);
|
||||
}
|
||||
assertTrue(ids(client, name).isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
private static void stackRequest(String action, String name, String cidr) {
|
||||
given().header("Authorization", CFN_AUTH).formParam("Action", action).formParam("StackName", name)
|
||||
.formParam("TemplateBody", """
|
||||
{"Resources":{"List":{"Type":"AWS::EC2::PrefixList","Properties":{
|
||||
"PrefixListName":"%s","AddressFamily":"IPv4","MaxEntries":2,
|
||||
"Entries":[{"Cidr":"%s"}]}}}}
|
||||
""".formatted(name, cidr))
|
||||
.post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static List<String> ids(Ec2Client client, String name) {
|
||||
return client.describeManagedPrefixLists(r -> r.filters(Filter.builder()
|
||||
.name("prefix-list-name").values(name).build())).prefixLists().stream()
|
||||
|
||||
Reference in new issue
Block a user