From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 7A3161FF18A for ; Mon, 26 May 2025 16:15:18 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 96002341E5; Mon, 26 May 2025 16:15:31 +0200 (CEST) From: Hannes Laimer To: pbs-devel@lists.proxmox.com Date: Mon, 26 May 2025 16:14:43 +0200 Message-Id: <20250526141445.228717-11-h.laimer@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250526141445.228717-1-h.laimer@proxmox.com> References: <20250526141445.228717-1-h.laimer@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.026 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 Subject: [pbs-devel] [PATCH proxmox-backup v2 10/12] api: admin: pull datastore loading out of check_privs helper X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Backup Server development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pbs-devel-bounces@lists.proxmox.com Sender: "pbs-devel" 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 --- 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( // 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( + store: &Arc>, ns: &BackupNamespace, auth_id: &Authid, full_access_privs: u64, partial_access_privs: u64, - operation: Option, backup_group: &pbs_api_types::BackupGroup, -) -> Result, 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( @@ -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 { 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(¶m)?; let backup_dir: pbs_api_types::BackupDir = Deserialize::deserialize(¶m)?; - 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(¶m)?; let backup_dir_api: pbs_api_types::BackupDir = Deserialize::deserialize(¶m)?; - 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(¶m)?; - 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(¶m)?; let backup_dir: pbs_api_types::BackupDir = Deserialize::deserialize(¶m)?; - 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 { 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