From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 40C011FF13B for ; Wed, 03 Jun 2026 16:56:22 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 18AC41046F; Wed, 3 Jun 2026 16:56:17 +0200 (CEST) From: David Riley To: pve-devel@lists.proxmox.com Subject: [PATCH pve-network v3 5/5] fix #7520: config: prune orphaned ACLs and relocate moved VNets Date: Wed, 3 Jun 2026 16:55:23 +0200 Message-ID: <20260603145523.120075-6-d.riley@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260603145523.120075-1-d.riley@proxmox.com> References: <20260603145523.120075-1-d.riley@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1780498534534 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.211 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: U7EA77RSSCCWMZABBFILPFSG2ISU3GRZ X-Message-ID-Hash: U7EA77RSSCCWMZABBFILPFSG2ISU3GRZ X-MailFrom: d.riley@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Riley X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Compare the running configuration with the newly compiled state during config commit to identify and prune orphaned SDN ACL entries. This ensures state consistency for manual applies via the UI/API as well as during automatic configuration reloads on system boot. Track configuration changes across: * Zones * VNets * Fabrics * Controllers * Route maps * Prefix lists Trigger a validation check when a VNet is moved between zones to mitigate potential ACL path conflicts. Abort the apply operation if a conflict is detected to prevent configuration overwrites. If the path is clear, relocate the ACLs to the updated zone path. Suggested-by: Stefan Hanreich Signed-off-by: David Riley --- src/PVE/Network/SDN.pm | 123 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/src/PVE/Network/SDN.pm b/src/PVE/Network/SDN.pm index 33a3cf3..6a49621 100644 --- a/src/PVE/Network/SDN.pm +++ b/src/PVE/Network/SDN.pm @@ -12,6 +12,7 @@ use UUID; use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file); use PVE::INotify; +use PVE::AccessControl; use PVE::RESTEnvironment qw(log_warn); use PVE::RPCEnvironment; use PVE::Tools qw(file_get_contents file_set_contents extract_param dir_glob_regex run_command); @@ -238,11 +239,133 @@ sub compile_running_cfg { } sub commit_config { + my $old_cfg = cfs_read_file($RUNNING_CFG_FILENAME); my $cfg = compile_running_cfg(); + cleanup($old_cfg, $cfg); + cfs_write_file($RUNNING_CFG_FILENAME, $cfg); } +sub cleanup { + my ($old_cfg, $cfg) = @_; + return if !$old_cfg || !$cfg; + + my $route_map_paths = diff_route_maps($old_cfg, $cfg); + my $generic_paths = diff_generic_resources($old_cfg, $cfg); + my ($vnet_delete_paths, $vnet_move_paths) = diff_vnets($old_cfg, $cfg); + + if (@$vnet_move_paths) { + PVE::AccessControl::migrate_sdn_resource_access($vnet_move_paths); + } + + my @paths_to_delete = (@$vnet_delete_paths, @$route_map_paths, @$generic_paths); + if (@paths_to_delete) { + PVE::AccessControl::remove_sdn_resource_access(\@paths_to_delete); + } + +} + +sub diff_generic_resources { + my ($old_cfg, $cfg) = @_; + + my @paths_to_delete; + + my @types = qw(zones controllers fabrics prefix-lists); + foreach my $type (@types) { + next if !$old_cfg->{$type} || !$old_cfg->{$type}->{ids}; + + my $old_ids = $old_cfg->{$type}->{ids}; + my $new_ids = {}; + if (my $type_block = $cfg->{$type}) { + $new_ids = $type_block->{ids} // {}; + } + + foreach my $id (keys %$old_ids) { + next if $new_ids->{$id}; + push @paths_to_delete, [$type, $id]; + } + } + + return \@paths_to_delete; +} + +sub diff_route_maps { + my ($old_cfg, $cfg) = @_; + + my @paths_to_delete; + + if ($old_cfg->{'route-maps'} && $old_cfg->{'route-maps'}->{ids}) { + my $old_route_maps = $old_cfg->{'route-maps'}->{ids}; + my $route_map_suffix = qr/_\d+$/; + + my %active_route_maps; + if ($cfg->{'route-maps'} && $cfg->{'route-maps'}->{ids}) { + foreach my $id (keys %{ $cfg->{'route-maps'}->{ids} }) { + (my $base_name = $id) =~ s/$route_map_suffix//; + $active_route_maps{$base_name} = 1; + } + } + + my %queued_route_maps; + foreach my $id (keys %$old_route_maps) { + next if $cfg->{'route-maps'}->{ids}->{$id}; + + (my $base_name = $id) =~ s/$route_map_suffix//; + next if $active_route_maps{$base_name}; + next if $queued_route_maps{$base_name}; + + push @paths_to_delete, ['route-maps', $base_name]; + $queued_route_maps{$base_name} = 1; + } + } + + return \@paths_to_delete; +} + +sub diff_vnets { + my ($old_cfg, $cfg) = @_; + + my @paths_to_delete; + my @paths_to_move; + + if ($old_cfg->{vnets} && $old_cfg->{vnets}->{ids}) { + my $old_vnets = $old_cfg->{vnets}->{ids}; + my $new_vnets = {}; + + if (my $vnet_cfg = $cfg->{vnets}) { + $new_vnets = $vnet_cfg->{ids} // {}; + } + + foreach my $vnetid (keys %$old_vnets) { + my $old_zone = $old_vnets->{$vnetid}->{zone}; + my $new_zone; + $new_zone = $new_vnets->{$vnetid}->{zone} if $new_vnets->{$vnetid}; + + if (!$new_vnets->{$vnetid}) { + if ($old_zone) { + push @paths_to_delete, ['zones', $old_zone, $vnetid]; + } else { + log_warn( + "SDN Cleanup: Could not find zone for deleted VNet $vnetid, skipping ACL" + . " cleanup"); + } + next; + } + + if ($old_zone && $new_zone && $old_zone ne $new_zone) { + push @paths_to_move, + { + src_path => ['zones', $old_zone, $vnetid], + dest_path => ['zones', $new_zone, $vnetid], + }; + } + } + } + + return (\@paths_to_delete, \@paths_to_move); +} + sub has_pending_changes { my $running_cfg = PVE::Network::SDN::running_config(); -- 2.47.3