public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Hannes Laimer <h.laimer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup v2 10/12] api: admin: pull datastore loading out of check_privs helper
Date: Mon, 26 May 2025 16:14:43 +0200	[thread overview]
Message-ID: <20250526141445.228717-11-h.laimer@proxmox.com> (raw)
In-Reply-To: <20250526141445.228717-1-h.laimer@proxmox.com>

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@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



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


  parent reply	other threads:[~2025-05-26 14:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-26 14:14 [pbs-devel] [PATCH proxmox-backup v2 00/12] introduce typestate for datastore/chunkstore Hannes Laimer
2025-05-26 14:14 ` [pbs-devel] [PATCH proxmox-backup v2 01/12] chunkstore: add CanRead and CanWrite trait Hannes Laimer
2025-05-26 14:14 ` [pbs-devel] [PATCH proxmox-backup v2 02/12] chunkstore: separate functions into impl block Hannes Laimer
2025-05-26 14:14 ` [pbs-devel] [PATCH proxmox-backup v2 03/12] datastore: add generics and new lookup functions Hannes Laimer
2025-05-26 14:14 ` [pbs-devel] [PATCH proxmox-backup v2 04/12] datastore: separate functions into impl block Hannes Laimer
2025-05-26 14:14 ` [pbs-devel] [PATCH proxmox-backup v2 05/12] backup_info: add generics and separate functions into impl blocks Hannes Laimer
2025-05-26 14:14 ` [pbs-devel] [PATCH proxmox-backup v2 06/12] pbs-datastore: " Hannes Laimer
2025-05-26 14:14 ` [pbs-devel] [PATCH proxmox-backup v2 07/12] api: backup: env: add generics and separate functions into impl block Hannes Laimer
2025-05-26 14:14 ` [pbs-devel] [PATCH proxmox-backup v2 08/12] api/backup/bin/server/tape: add missing generics Hannes Laimer
2025-05-26 14:14 ` [pbs-devel] [PATCH proxmox-backup v2 09/12] examples/tests: " Hannes Laimer
2025-05-26 14:14 ` Hannes Laimer [this message]
2025-05-26 14:14 ` [pbs-devel] [PATCH proxmox-backup v2 11/12] datastore: move `fn gc_running` out of DataStoreImpl Hannes Laimer
2025-05-26 14:14 ` [pbs-devel] [PATCH proxmox-backup v2 12/12] api/server: replace datastore_lookup with new, state-typed datastore returning functions Hannes Laimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250526141445.228717-11-h.laimer@proxmox.com \
    --to=h.laimer@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal