From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <l.wagner@proxmox.com>
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 4FC8FC00CF
 for <pve-devel@lists.proxmox.com>; Wed, 10 Jan 2024 09:30:04 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 3145A30854
 for <pve-devel@lists.proxmox.com>; Wed, 10 Jan 2024 09:29:34 +0100 (CET)
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 <pve-devel@lists.proxmox.com>; Wed, 10 Jan 2024 09:29:33 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id E9FDE4903F
 for <pve-devel@lists.proxmox.com>; Wed, 10 Jan 2024 09:29:32 +0100 (CET)
Message-ID: <890cf69c-b09b-41d8-b664-976cdf109723@proxmox.com>
Date: Wed, 10 Jan 2024 09:29:32 +0100
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Content-Language: de-AT, en-US
To: Wolfgang Bumiller <w.bumiller@proxmox.com>
Cc: pve-devel@lists.proxmox.com
References: <20231214124208.219312-1-l.wagner@proxmox.com>
 <phm2zgtup2jl7w6ruwycxw4osjeexukhgm6xif2htca2h4stdr@ywc7ipfqhped>
From: Lukas Wagner <l.wagner@proxmox.com>
In-Reply-To: <phm2zgtup2jl7w6ruwycxw4osjeexukhgm6xif2htca2h4stdr@ywc7ipfqhped>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.004 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 -
Subject: Re: [pve-devel] [PATCH proxmox 1/2] notify: api: allow resetting
 built-in targets when they are referenced
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Wed, 10 Jan 2024 08:30:04 -0000



On 1/9/24 14:18, Wolfgang Bumiller wrote:
> On Thu, Dec 14, 2023 at 01:42:07PM +0100, Lukas Wagner wrote:
>> by a matcher.
> 
> ^ this line should be part of the subject
> 
>>
>> In the 'delete'-handler targets, we check if a
>> target is still referenced by a matcher - if it is, we return an
>> error. For built-in targets, this is actually not necessary, since
>> 'deleting' a built-in only resets it to its default settings - it will
>> continue to exist after that.
>> The user could easily trigger this if 'mail-to-root', which is
>> referenced by 'default-matcher' is modified and then reset to its
>> defaults: An error is shown, the built-in target is not reset.
>>
>> This commit disables this check if it is a built-in target.
>>
>> Renamed the helper 'ensure_unused' to 'ensure_safe_to_delete' in the
>> process.
>>
>> Also fixed the tests in api::test - they were never executed due to a
>> faulty #[cfg] directive.
>>
>> Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
>> ---
>>   proxmox-notify/src/api/gotify.rs   |  2 +-
>>   proxmox-notify/src/api/mod.rs      | 74 ++++++++++++++++++++++--------
>>   proxmox-notify/src/api/sendmail.rs |  2 +-
>>   proxmox-notify/src/api/smtp.rs     |  2 +-
>>   4 files changed, 59 insertions(+), 21 deletions(-)
>>
>> diff --git a/proxmox-notify/src/api/gotify.rs b/proxmox-notify/src/api/gotify.rs
>> index 98ff255..a93a024 100644
>> --- a/proxmox-notify/src/api/gotify.rs
>> +++ b/proxmox-notify/src/api/gotify.rs
>> @@ -136,7 +136,7 @@ pub fn update_endpoint(
>>   pub fn delete_gotify_endpoint(config: &mut Config, name: &str) -> Result<(), HttpError> {
>>       // Check if the endpoint exists
>>       let _ = get_endpoint(config, name)?;
>> -    super::ensure_unused(config, name)?;
>> +    super::ensure_safe_to_delete(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 762d448..919dcb9 100644
>> --- a/proxmox-notify/src/api/mod.rs
>> +++ b/proxmox-notify/src/api/mod.rs
>> @@ -3,7 +3,7 @@ use std::collections::HashSet;
>>   
>>   use proxmox_http_error::HttpError;
>>   
>> -use crate::Config;
>> +use crate::{Config, Origin};
>>   
>>   pub mod common;
>>   #[cfg(feature = "gotify")]
>> @@ -111,7 +111,26 @@ fn get_referrers(config: &Config, entity: &str) -> Result<HashSet<String>, HttpE
>>       Ok(referrers)
>>   }
>>   
>> -fn ensure_unused(config: &Config, entity: &str) -> Result<(), HttpError> {
>> +fn ensure_safe_to_delete(config: &Config, entity: &str) -> Result<(), HttpError> {
>> +    if let Some(entity_config) = config.config.sections.get(entity) {
>> +        let origin: Option<Origin> = entity_config
>> +            .1
>> +            .as_object()
>> +            .and_then(|obj| obj.get("origin"))
>> +            .cloned()
>> +            .and_then(|origin_value| serde_json::from_value(origin_value).ok());
> 
> ^ .cloned() should not be necessary, Value and &Value implement
> `Deserialize_r_`, so you can use:
> 
>      .and_then(|origin_value| Origin::deserialize(origin_value).ok());
> 
> Additionally, Value returns a valid `Null` value when trying to index a
> non-object as well as when the key does not exist in the object, so it
> should be possible to do replace all of the above and the line below
> with:
> 
>      if let Some(origin) = Origin::deserialize(&entity_config.1["origin"]) {
> 

I'll try that, thanks!

>> +        if let Some(origin) = origin {
>> +            // Built-ins are never actually removed, only reset to their default
>> +            // It is thus safe to do the reset if another entity depends
>> +            // on it
>> +            if origin == Origin::Builtin || origin == Origin::ModifiedBuiltin {
>> +                return Ok(());
>> +            }
>> +        }
>> +    } else {
>> +        http_bail!(NOT_FOUND, "entity '{entity}' does not exist");
>> +    }
>> +
>>       let referrers = get_referrers(config, entity)?;
>>   
>>       if !referrers.is_empty() {
>> @@ -191,31 +210,31 @@ mod test_helpers {

-- 
- Lukas