If not set, infer the auth_method SmtpConfig field from the presence
of a password in the config. This makes sure the new API stays
backwards-compatible with old scripts and updates old configurations
when they are edited.

Signed-off-by: Arthur Bied-Charreton <[email protected]>
---
 proxmox-notify/src/api/smtp.rs | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/proxmox-notify/src/api/smtp.rs b/proxmox-notify/src/api/smtp.rs
index 80f3ede6..86fb0091 100644
--- a/proxmox-notify/src/api/smtp.rs
+++ b/proxmox-notify/src/api/smtp.rs
@@ -57,6 +57,22 @@ fn get_private_config(config: &Config, name: &str) -> 
Result<SmtpPrivateConfig,
         })
 }
 
+/// Before the `auth_method` field was introduced into [`SmtpConfig`], the 
authentication method
+/// was inferred from the presence of a password in the config.
+///
+/// Infer it in the same way, returning a new [`SmtpConfig`] object that can 
then be persisted
+/// to migrate old configs.
+fn infer_auth_method(config: SmtpConfig, private_config: SmtpPrivateConfig) -> 
SmtpConfig {
+    if config.auth_method.is_none() && private_config.password.is_some() {
+        SmtpConfig {
+            auth_method: Some(SmtpAuthMethod::Plain),
+            ..config
+        }
+    } else {
+        config
+    }
+}
+
 /// Add a new smtp endpoint.
 ///
 /// The caller is responsible for any needed permission checks.
@@ -92,11 +108,13 @@ pub fn add_endpoint(
 
     super::set_private_config_entry(
         config,
-        private_endpoint_config,
+        &private_endpoint_config,
         SMTP_TYPENAME,
         &endpoint_config.name,
     )?;
 
+    let endpoint_config = infer_auth_method(endpoint_config, 
private_endpoint_config);
+
     config
         .config
         .set_data(&endpoint_config.name, SMTP_TYPENAME, &endpoint_config)
@@ -212,6 +230,8 @@ pub fn update_endpoint(
         }
     })?;
 
+    let endpoint = infer_auth_method(endpoint, get_private_config(config, 
name)?);
+
     config
         .config
         .set_data(name, SMTP_TYPENAME, &endpoint)
-- 
2.47.3



Reply via email to