public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: David Riley <d.riley@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: David Riley <d.riley@proxmox.com>
Subject: [PATCH pve-network v2 2/2] fix #7520: sdn: clean up dangling ACLs on resource deletion
Date: Thu, 21 May 2026 16:40:59 +0200	[thread overview]
Message-ID: <20260521144059.107229-3-d.riley@proxmox.com> (raw)
In-Reply-To: <20260521144059.107229-1-d.riley@proxmox.com>

Add pruning mechanism to clean up orphaned SDN ACL entries by
comparing the current `running-config` with the newly compiled state
during `commit_config`.

This ensures state consistency for manual applies via the UI/API
as well as during the automatic configuration reload on system boot.
Covers resources tracked in the `running-config`:
* Zones
* VNets
* Fabrics
* Controllers
* Route Maps
* Prefix Lists

Suggested-by: Stefan Hanreich <s.hanreich@proxmox.com>
Signed-off-by: David Riley <d.riley@proxmox.com>
---
 src/PVE/Network/SDN.pm | 76 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)

diff --git a/src/PVE/Network/SDN.pm b/src/PVE/Network/SDN.pm
index 33a3cf3..7bb2fca 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,86 @@ sub compile_running_cfg {
 }
 
 sub commit_config {
+    my $old_cfg = cfs_read_file($RUNNING_CFG_FILENAME);
     my $cfg = compile_running_cfg();
 
+    cleanup_sdn_acls($old_cfg, $cfg);
+
     cfs_write_file($RUNNING_CFG_FILENAME, $cfg);
 }
 
+sub cleanup_sdn_acls {
+    my ($old_cfg, $new_cfg) = @_;
+    return if !$old_cfg || !$new_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 = $new_cfg->{$type}) {
+            $new_ids = $type_block->{ids} // {};
+        }
+
+        foreach my $id (keys %$old_ids) {
+            next if $new_ids->{$id};
+            push @paths_to_delete, [$type, $id];
+        }
+    }
+
+    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 ($new_cfg->{'route-maps'} && $new_cfg->{'route-maps'}->{ids}) {
+            foreach my $id (keys %{ $new_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 $new_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;
+        }
+    }
+
+    if ($old_cfg->{vnets} && $old_cfg->{vnets}->{ids}) {
+        my $old_vnets = $old_cfg->{vnets}->{ids};
+        my $new_vnets = {};
+
+        if (my $type_block = $new_cfg->{vnets}) {
+            $new_vnets = $type_block->{ids} // {};
+        }
+
+        foreach my $vnetid (keys %$old_vnets) {
+            next if $new_vnets->{$vnetid};
+
+            if (my $zoneid = $old_vnets->{$vnetid}->{zone}) {
+                push @paths_to_delete, ['zones', $zoneid, $vnetid];
+                next;
+            }
+
+            log_warn("SDN Cleanup: Could not find zone for VNet $vnetid, skipping ACL cleanup");
+        }
+    }
+
+    if (@paths_to_delete) {
+        PVE::AccessControl::remove_sdn_resource_access(\@paths_to_delete);
+    }
+}
+
 sub has_pending_changes {
     my $running_cfg = PVE::Network::SDN::running_config();
 
-- 
2.47.3





      parent reply	other threads:[~2026-05-21 14:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-21 14:40 [PATCH access-control/network v2 0/2] fix #7520: sdn: clean up dangling ACLs on resource deletion David Riley
2026-05-21 14:40 ` [PATCH pve-access-control v2 1/2] fix #7520: access-control: add SDN ACL cleanup helper David Riley
2026-05-21 14:40 ` David Riley [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260521144059.107229-3-d.riley@proxmox.com \
    --to=d.riley@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal