Skip to content

Commit c7f282b

Browse files
committed
fix(vpcpeering): Do not insert peering with duplicate name
When we try to insert a peering with a name that already exists for another peering in a VpcPeeringTable, this results in an error, but it also results in the peering being inserted anyway, as we only detect the name collision when inserting (and we do not implement rollback). As a consequence, we keep an incorrect peering entry in the table. This can cause unexpected collisions on prefixes when trying to add subsequent, legitimate peerings to the table, for example. Instead of inserting unconditionally, let's check first for the existence of the peering name in the table. Fixes: e45c0ef (feat(mgmt): define external model) Signed-off-by: Quentin Monnet <[email protected]>
1 parent 0bb337b commit c7f282b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

mgmt/src/models/external/overlay/vpcpeering.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,16 @@ impl VpcPeeringTable {
234234
/// Add a [`VpcPeering`] to a [`VpcPeeringTable`]
235235
pub fn add(&mut self, peering: VpcPeering) -> ConfigResult {
236236
peering.validate()?;
237-
if let Some(peering) = self.0.insert(peering.name.to_owned(), peering) {
238-
Err(ConfigError::DuplicateVpcPeeringId(peering.name.clone()))
237+
238+
// First look for an existing entry, to avoid inserting a duplicate peering
239+
if self.0.contains_key(&peering.name) {
240+
return Err(ConfigError::DuplicateVpcPeeringId(peering.name.clone()));
241+
}
242+
243+
if self.0.insert(peering.name.to_owned(), peering).is_some() {
244+
// We should have prevented this case by checking for duplicates just above.
245+
// This should never happen, unless we have another thread modifying the table.
246+
unreachable!()
239247
} else {
240248
Ok(())
241249
}

0 commit comments

Comments
 (0)