[pbs-devel] [PATCH proxmox-backup 10/17] manager: add sync command for LDAP realms

Lukas Wagner l.wagner at proxmox.com
Tue Jan 3 15:23:01 CET 2023


Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 src/bin/proxmox_backup_manager/ldap.rs | 83 +++++++++++++++++++++++++-
 1 file changed, 81 insertions(+), 2 deletions(-)

diff --git a/src/bin/proxmox_backup_manager/ldap.rs b/src/bin/proxmox_backup_manager/ldap.rs
index 4020caee..407c675d 100644
--- a/src/bin/proxmox_backup_manager/ldap.rs
+++ b/src/bin/proxmox_backup_manager/ldap.rs
@@ -1,10 +1,15 @@
 use anyhow::Error;
+use futures::FutureExt;
 use serde_json::Value;
+use tokio::signal::unix::{signal, SignalKind};
 
-use proxmox_router::{cli::*, ApiHandler, RpcEnvironment};
+use proxmox_router::{cli::*, ApiHandler, Permission, RpcEnvironment};
 use proxmox_schema::api;
 
-use pbs_api_types::REALM_ID_SCHEMA;
+use pbs_api_types::{
+    Realm, PRIV_PERMISSIONS_MODIFY, PROXMOX_UPID_REGEX, REALM_ID_SCHEMA, REMOVE_VANISHED_SCHEMA,
+    UPID,
+};
 
 use proxmox_backup::api2;
 
@@ -67,6 +72,74 @@ fn show_ldap_realm(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Valu
     Ok(Value::Null)
 }
 
+#[api(
+    protected: true,
+    input: {
+        properties: {
+            realm: {
+                type: Realm,
+            },
+            "dry-run": {
+                type: bool,
+                description: "If set, do not create/delete anything",
+                default: false,
+                optional: true,
+            },
+            "remove-vanished": {
+                optional: true,
+                schema: REMOVE_VANISHED_SCHEMA,
+            },
+            "enable-new": {
+                description: "Enable newly synced users immediately",
+                optional: true,
+                type: bool,
+            }
+         },
+    },
+    access: {
+        permission: &Permission::Privilege(&["access", "users"], PRIV_PERMISSIONS_MODIFY, false),
+    },
+)]
+/// List configured LDAP realms
+async fn sync_ldap_realm(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
+    let info = &api2::access::domain::API_METHOD_SYNC_REALM;
+    let data = match info.handler {
+        ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
+        _ => unreachable!(),
+    };
+
+    if let Some(upid) = data.as_str() {
+        if PROXMOX_UPID_REGEX.is_match(upid) {
+            handle_worker(upid).await?;
+        }
+    }
+
+    Ok(Value::Null)
+}
+
+// TODO: This was copied from proxmox_backup_debug/api.rs - is there a good place to
+// put this so we can use the same impl for both?
+async fn handle_worker(upid_str: &str) -> Result<(), Error> {
+    let upid: UPID = upid_str.parse()?;
+    let mut signal_stream = signal(SignalKind::interrupt())?;
+    let abort_future = async move {
+        while signal_stream.recv().await.is_some() {
+            println!("got shutdown request (SIGINT)");
+            proxmox_rest_server::abort_local_worker(upid.clone());
+        }
+        Ok::<_, Error>(())
+    };
+
+    let result_future = proxmox_rest_server::wait_for_local_worker(upid_str);
+
+    futures::select! {
+        result = result_future.fuse() => result?,
+        abort = abort_future.fuse() => abort?,
+    };
+
+    Ok(())
+}
+
 pub fn ldap_commands() -> CommandLineInterface {
     let cmd_def = CliCommandMap::new()
         .insert("list", CliCommand::new(&API_METHOD_LIST_LDAP_REALMS))
@@ -93,6 +166,12 @@ pub fn ldap_commands() -> CommandLineInterface {
             CliCommand::new(&api2::config::access::ldap::API_METHOD_DELETE_LDAP_REALM)
                 .arg_param(&["realm"])
                 .completion_cb("realm", pbs_config::domains::complete_ldap_realm_name),
+        )
+        .insert(
+            "sync",
+            CliCommand::new(&API_METHOD_SYNC_LDAP_REALM)
+                .arg_param(&["realm"])
+                .completion_cb("realm", pbs_config::domains::complete_ldap_realm_name),
         );
 
     cmd_def.into()
-- 
2.30.2






More information about the pbs-devel mailing list