From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 4DE24E4F5 for ; Tue, 18 Jul 2023 15:20:11 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 366071B811 for ; Tue, 18 Jul 2023 15:20:11 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Tue, 18 Jul 2023 15:20:10 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 66ECA42CED for ; Tue, 18 Jul 2023 15:20:10 +0200 (CEST) Date: Tue, 18 Jul 2023 15:20:09 +0200 From: Wolfgang Bumiller To: Lukas Wagner Cc: pve-devel@lists.proxmox.com Message-ID: References: <20230717150051.710464-1-l.wagner@proxmox.com> <20230717150051.710464-21-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230717150051.710464-21-l.wagner@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL 0.119 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 T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [gotify.rs, filter.rs, sendmail.rs, mod.rs] Subject: Re: [pve-devel] [PATCH v3 proxmox 20/66] notify: on deletion, check if a filter/endp. is still used by anything X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Jul 2023 13:20:11 -0000 On Mon, Jul 17, 2023 at 05:00:05PM +0200, Lukas Wagner wrote: > Signed-off-by: Lukas Wagner > --- > proxmox-notify/src/api/filter.rs | 1 + > proxmox-notify/src/api/gotify.rs | 1 + > proxmox-notify/src/api/mod.rs | 113 ++++++++++++++++++++++++++--- > proxmox-notify/src/api/sendmail.rs | 1 + > 4 files changed, 106 insertions(+), 10 deletions(-) > > diff --git a/proxmox-notify/src/api/filter.rs b/proxmox-notify/src/api/filter.rs > index 3fcff6b9..824f802d 100644 > --- a/proxmox-notify/src/api/filter.rs > +++ b/proxmox-notify/src/api/filter.rs > @@ -115,6 +115,7 @@ pub fn update_filter( > pub fn delete_filter(config: &mut Config, name: &str) -> Result<(), ApiError> { > // Check if the filter exists > let _ = get_filter(config, name)?; > + super::ensure_unused(config, name)?; > > config.config.sections.remove(name); > > diff --git a/proxmox-notify/src/api/gotify.rs b/proxmox-notify/src/api/gotify.rs > index d6f33064..5c4db4be 100644 > --- a/proxmox-notify/src/api/gotify.rs > +++ b/proxmox-notify/src/api/gotify.rs > @@ -145,6 +145,7 @@ pub fn update_endpoint( > pub fn delete_gotify_endpoint(config: &mut Config, name: &str) -> Result<(), ApiError> { > // Check if the endpoint exists > let _ = get_endpoint(config, name)?; > + super::ensure_unused(config, name)?; > > remove_private_config_entry(config, name)?; > config.config.sections.remove(name); > diff --git a/proxmox-notify/src/api/mod.rs b/proxmox-notify/src/api/mod.rs > index d8a44bf2..81c182c7 100644 > --- a/proxmox-notify/src/api/mod.rs > +++ b/proxmox-notify/src/api/mod.rs > @@ -102,6 +102,59 @@ fn endpoint_exists(config: &Config, name: &str) -> bool { > exists > } > > +fn get_referrers(config: &Config, entity: &str) -> Result, ApiError> { > + let mut referrers = HashSet::new(); > + > + for group in group::get_groups(config)? { > + for endpoint in group.endpoint { > + if endpoint == entity { > + referrers.insert(group.name.clone()); We could `break` here, since `group` comes from the outer loop. Or, in fact, replace the inner for loop with: if group.endpoint.iter().any(|endpoint| endpoint == entity) { referrers.insert(group.name.clone()); } > + } > + } > + > + if let Some(filter) = group.filter { > + if filter == entity { > + referrers.insert(group.name); > + } > + } > + } > + > + #[cfg(feature = "sendmail")] > + for endpoint in sendmail::get_endpoints(config)? { > + if let Some(filter) = endpoint.filter { > + if filter == entity { > + referrers.insert(endpoint.name); > + } > + } > + } > + > + #[cfg(feature = "gotify")] > + for endpoint in gotify::get_endpoints(config)? { > + if let Some(filter) = endpoint.filter { > + if filter == entity { > + referrers.insert(endpoint.name); > + } > + } > + } > + > + Ok(referrers) > +} > + > +fn ensure_unused(config: &Config, entity: &str) -> Result<(), ApiError> { > + let referrers = get_referrers(config, entity)?; > + > + if !referrers.is_empty() { > + let used_by = referrers.into_iter().collect::>().join(", "); *sigh*... iterators vs join... Some day this can hopefully be refererrs .into_iter() .map(Cow::Owned) .intersperse(Cow::Borrowed(", ")) .collect::(); Yeah okay fine, the ergonomics with `String` vs the `&'static str` aren't great either... exactly as long as the for loop variant, but w/e. Could also just be referrers.iter().map(String::as_str).intersperse(", ").collect::(); ... Oh well ... > + > + return Err(ApiError::bad_request( > + format!("cannot delete '{entity}', referenced by: {used_by}"), > + None, > + )); > + } > + > + Ok(()) > +} > + > fn get_referenced_entities(config: &Config, entity: &str) -> HashSet { > let mut to_expand = HashSet::new(); > let mut expanded = HashSet::new(); (...)