[pbs-devel] [PATCH proxmox-backup v2 10/12] api: admin: pull datastore loading out of check_privs helper

Hannes Laimer h.laimer at proxmox.com
Mon May 26 16:14:43 CEST 2025


We have to use different lookup functions depending on what we
plan to do with the reference, deciding what function to use
based on the passed operation type was problematic as
the different functions return differently typed datastore
references which is a problem for the return type of the helper
function.

Like this the loading and permission checking is separated, which
can definitely make debugging easier and may itself be a reason for
separating it in the first place, though that was not why it was done
here.

Signed-off-by: Hannes Laimer <h.laimer at proxmox.com>
---
 src/api2/admin/datastore.rs | 116 ++++++++++++++++++------------------
 1 file changed, 59 insertions(+), 57 deletions(-)

diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index e3f93cdd..218d7e73 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -41,7 +41,7 @@ use pbs_api_types::{
     BackupContent, BackupGroupDeleteStats, BackupNamespace, BackupType, Counts, CryptMode,
     DataStoreConfig, DataStoreListItem, DataStoreMountStatus, DataStoreStatus,
     GarbageCollectionJobStatus, GroupListItem, JobScheduleStatus, KeepOptions, MaintenanceMode,
-    MaintenanceType, Operation, PruneJobOptions, SnapshotListItem, SnapshotVerifyState,
+    MaintenanceType, PruneJobOptions, SnapshotListItem, SnapshotVerifyState,
     BACKUP_ARCHIVE_NAME_SCHEMA, BACKUP_ID_SCHEMA, BACKUP_NAMESPACE_SCHEMA, BACKUP_TIME_SCHEMA,
     BACKUP_TYPE_SCHEMA, CATALOG_NAME, CLIENT_LOG_BLOB_NAME, DATASTORE_SCHEMA,
     IGNORE_VERIFIED_BACKUPS_SCHEMA, MANIFEST_BLOB_NAME, MAX_NAMESPACE_DEPTH, NS_MAX_DEPTH_SCHEMA,
@@ -92,27 +92,29 @@ fn get_group_note_path<T>(
 
 // helper to unify common sequence of checks:
 // 1. check privs on NS (full or limited access)
-// 2. load datastore
-// 3. if needed (only limited access), check owner of group
-fn check_privs_and_load_store(
-    store: &str,
+// 2. if needed (only limited access), check owner of group
+fn check_privs<T: CanRead>(
+    store: &Arc<DataStore<T>>,
     ns: &BackupNamespace,
     auth_id: &Authid,
     full_access_privs: u64,
     partial_access_privs: u64,
-    operation: Option<Operation>,
     backup_group: &pbs_api_types::BackupGroup,
-) -> Result<Arc<DataStore>, Error> {
-    let limited = check_ns_privs_full(store, ns, auth_id, full_access_privs, partial_access_privs)?;
-
-    let datastore = DataStore::lookup_datastore(store, operation)?;
+) -> Result<(), Error> {
+    let limited = check_ns_privs_full(
+        store.name(),
+        ns,
+        auth_id,
+        full_access_privs,
+        partial_access_privs,
+    )?;
 
     if limited {
-        let owner = datastore.get_owner(ns, backup_group)?;
+        let owner = store.get_owner(ns, backup_group)?;
         check_backup_owner(&owner, auth_id)?;
     }
 
-    Ok(datastore)
+    Ok(())
 }
 
 fn read_backup_index<T: CanRead>(
@@ -303,13 +305,13 @@ pub async fn delete_group(
     tokio::task::spawn_blocking(move || {
         let ns = ns.unwrap_or_default();
 
-        let datastore = check_privs_and_load_store(
-            &store,
+        let datastore = DataStore::lookup_datastore_write(&store)?;
+        check_privs(
+            &datastore,
             &ns,
             &auth_id,
             PRIV_DATASTORE_MODIFY,
             PRIV_DATASTORE_PRUNE,
-            Some(Operation::Write),
             &group,
         )?;
 
@@ -370,13 +372,13 @@ pub async fn list_snapshot_files(
     tokio::task::spawn_blocking(move || {
         let ns = ns.unwrap_or_default();
 
-        let datastore = check_privs_and_load_store(
-            &store,
+        let datastore = DataStore::lookup_datastore_read(&store)?;
+        check_privs(
+            &datastore,
             &ns,
             &auth_id,
             PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_READ,
             PRIV_DATASTORE_BACKUP,
-            Some(Operation::Read),
             &backup_dir.group,
         )?;
 
@@ -424,13 +426,13 @@ pub async fn delete_snapshot(
     tokio::task::spawn_blocking(move || {
         let ns = ns.unwrap_or_default();
 
-        let datastore = check_privs_and_load_store(
-            &store,
+        let datastore = DataStore::lookup_datastore_write(&store)?;
+        check_privs(
+            &datastore,
             &ns,
             &auth_id,
             PRIV_DATASTORE_MODIFY,
             PRIV_DATASTORE_PRUNE,
-            Some(Operation::Write),
             &backup_dir.group,
         )?;
 
@@ -1001,13 +1003,13 @@ pub fn prune(
 ) -> Result<Value, Error> {
     let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
     let ns = ns.unwrap_or_default();
-    let datastore = check_privs_and_load_store(
-        &store,
+    let datastore = DataStore::lookup_datastore_write(&store)?;
+    check_privs(
+        &datastore,
         &ns,
         &auth_id,
         PRIV_DATASTORE_MODIFY,
         PRIV_DATASTORE_PRUNE,
-        Some(Operation::Write),
         &group,
     )?;
 
@@ -1405,13 +1407,13 @@ pub fn download_file(
         let backup_ns = optional_ns_param(&param)?;
 
         let backup_dir: pbs_api_types::BackupDir = Deserialize::deserialize(&param)?;
-        let datastore = check_privs_and_load_store(
-            store,
+        let datastore = DataStore::lookup_datastore_read(store)?;
+        check_privs(
+            &datastore,
             &backup_ns,
             &auth_id,
             PRIV_DATASTORE_READ,
             PRIV_DATASTORE_BACKUP,
-            Some(Operation::Read),
             &backup_dir.group,
         )?;
 
@@ -1490,13 +1492,13 @@ pub fn download_file_decoded(
         let backup_ns = optional_ns_param(&param)?;
 
         let backup_dir_api: pbs_api_types::BackupDir = Deserialize::deserialize(&param)?;
-        let datastore = check_privs_and_load_store(
-            store,
+        let datastore = DataStore::lookup_datastore_read(store)?;
+        check_privs(
+            &datastore,
             &backup_ns,
             &auth_id,
             PRIV_DATASTORE_READ,
             PRIV_DATASTORE_BACKUP,
-            Some(Operation::Read),
             &backup_dir_api.group,
         )?;
 
@@ -1617,13 +1619,13 @@ pub fn upload_backup_log(
 
         let backup_dir_api: pbs_api_types::BackupDir = Deserialize::deserialize(&param)?;
 
-        let datastore = check_privs_and_load_store(
-            store,
+        let datastore = DataStore::lookup_datastore_write(store)?;
+        check_privs(
+            &datastore,
             &backup_ns,
             &auth_id,
             0,
             PRIV_DATASTORE_BACKUP,
-            Some(Operation::Write),
             &backup_dir_api.group,
         )?;
         let backup_dir = datastore.backup_dir(backup_ns.clone(), backup_dir_api.clone())?;
@@ -1713,13 +1715,13 @@ pub async fn catalog(
 
     let ns = ns.unwrap_or_default();
 
-    let datastore = check_privs_and_load_store(
-        &store,
+    let datastore = DataStore::lookup_datastore_read(&store)?;
+    check_privs(
+        &datastore,
         &ns,
         &auth_id,
         PRIV_DATASTORE_READ,
         PRIV_DATASTORE_BACKUP,
-        Some(Operation::Read),
         &backup_dir.group,
     )?;
 
@@ -1833,13 +1835,13 @@ pub fn pxar_file_download(
         let ns = optional_ns_param(&param)?;
 
         let backup_dir: pbs_api_types::BackupDir = Deserialize::deserialize(&param)?;
-        let datastore = check_privs_and_load_store(
-            store,
+        let datastore = DataStore::lookup_datastore_read(store)?;
+        check_privs(
+            &datastore,
             &ns,
             &auth_id,
             PRIV_DATASTORE_READ,
             PRIV_DATASTORE_BACKUP,
-            Some(Operation::Read),
             &backup_dir.group,
         )?;
 
@@ -2044,13 +2046,13 @@ pub fn get_group_notes(
     let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
     let ns = ns.unwrap_or_default();
 
-    let datastore = check_privs_and_load_store(
-        &store,
+    let datastore = DataStore::lookup_datastore_read(&store)?;
+    check_privs(
+        &datastore,
         &ns,
         &auth_id,
         PRIV_DATASTORE_AUDIT,
         PRIV_DATASTORE_BACKUP,
-        Some(Operation::Read),
         &backup_group,
     )?;
 
@@ -2092,13 +2094,13 @@ pub fn set_group_notes(
     let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
     let ns = ns.unwrap_or_default();
 
-    let datastore = check_privs_and_load_store(
-        &store,
+    let datastore = DataStore::lookup_datastore_write(&store)?;
+    check_privs(
+        &datastore,
         &ns,
         &auth_id,
         PRIV_DATASTORE_MODIFY,
         PRIV_DATASTORE_BACKUP,
-        Some(Operation::Write),
         &backup_group,
     )?;
 
@@ -2138,13 +2140,13 @@ pub fn get_notes(
     let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
     let ns = ns.unwrap_or_default();
 
-    let datastore = check_privs_and_load_store(
-        &store,
+    let datastore = DataStore::lookup_datastore_read(&store)?;
+    check_privs(
+        &datastore,
         &ns,
         &auth_id,
         PRIV_DATASTORE_AUDIT,
         PRIV_DATASTORE_BACKUP,
-        Some(Operation::Read),
         &backup_dir.group,
     )?;
 
@@ -2191,13 +2193,13 @@ pub fn set_notes(
     let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
     let ns = ns.unwrap_or_default();
 
-    let datastore = check_privs_and_load_store(
-        &store,
+    let datastore = DataStore::lookup_datastore_write(&store)?;
+    check_privs(
+        &datastore,
         &ns,
         &auth_id,
         PRIV_DATASTORE_MODIFY,
         PRIV_DATASTORE_BACKUP,
-        Some(Operation::Write),
         &backup_dir.group,
     )?;
 
@@ -2241,13 +2243,13 @@ pub fn get_protection(
 ) -> Result<bool, Error> {
     let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
     let ns = ns.unwrap_or_default();
-    let datastore = check_privs_and_load_store(
-        &store,
+    let datastore = DataStore::lookup_datastore_read(&store)?;
+    check_privs(
+        &datastore,
         &ns,
         &auth_id,
         PRIV_DATASTORE_AUDIT,
         PRIV_DATASTORE_BACKUP,
-        Some(Operation::Read),
         &backup_dir.group,
     )?;
 
@@ -2291,13 +2293,13 @@ pub async fn set_protection(
 
     tokio::task::spawn_blocking(move || {
         let ns = ns.unwrap_or_default();
-        let datastore = check_privs_and_load_store(
-            &store,
+        let datastore = DataStore::lookup_datastore_write(&store)?;
+        check_privs(
+            &datastore,
             &ns,
             &auth_id,
             PRIV_DATASTORE_MODIFY,
             PRIV_DATASTORE_BACKUP,
-            Some(Operation::Write),
             &backup_dir.group,
         )?;
 
-- 
2.39.5





More information about the pbs-devel mailing list