[pbs-devel] [PATCH v7 proxmox-backup 02/31] sync: extend sync source's list namespaces method by filter callback
Christian Ebner
c.ebner at proxmox.com
Mon Nov 11 16:43:24 CET 2024
Allow to filter namespaces by given callback function. This will be
used to pre-filter the list of namespaces to push to a remote target
for sync jobs in push direction, based on the privs of the sync jobs
local user on the source datastore.
Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
changes since version 6:
- not present in previous version
src/server/pull.rs | 11 ++++++++++-
src/server/sync.rs | 29 +++++++++++++++++++++++++++--
2 files changed, 37 insertions(+), 3 deletions(-)
diff --git a/src/server/pull.rs b/src/server/pull.rs
index c12ecec82..d059c3ff6 100644
--- a/src/server/pull.rs
+++ b/src/server/pull.rs
@@ -737,7 +737,16 @@ pub(crate) async fn pull_store(mut params: PullParameters) -> Result<SyncStats,
let mut namespaces = if params.source.get_ns().is_root() && old_max_depth == Some(0) {
vec![params.source.get_ns()] // backwards compat - don't query remote namespaces!
} else {
- params.source.list_namespaces(&mut params.max_depth).await?
+ let user_info = CachedUserInfo::new()?;
+ params
+ .source
+ .list_namespaces(
+ &mut params.max_depth,
+ ¶ms.owner,
+ &user_info,
+ Box::new(|_| true),
+ )
+ .await?
};
check_namespace_depth_limit(¶ms.source.get_ns(), ¶ms.target.ns, &namespaces)?;
diff --git a/src/server/sync.rs b/src/server/sync.rs
index bd68dda46..19b244f5a 100644
--- a/src/server/sync.rs
+++ b/src/server/sync.rs
@@ -18,6 +18,7 @@ use pbs_api_types::{
MAX_NAMESPACE_DEPTH, PRIV_DATASTORE_BACKUP, PRIV_DATASTORE_READ,
};
use pbs_client::{BackupReader, BackupRepository, HttpClient, RemoteChunkReader};
+use pbs_config::CachedUserInfo;
use pbs_datastore::data_blob::DataBlob;
use pbs_datastore::manifest::CLIENT_LOG_BLOB_NAME;
use pbs_datastore::read_chunk::AsyncReadChunk;
@@ -208,6 +209,9 @@ impl SyncSourceReader for LocalSourceReader {
}
}
+pub type NamespaceFilter =
+ Box<dyn FnMut((&BackupNamespace, &str, &Authid, &CachedUserInfo)) -> bool + Send>;
+
#[async_trait::async_trait]
/// `SyncSource` is a trait that provides an interface for synchronizing data/information from a
/// source.
@@ -218,6 +222,9 @@ pub(crate) trait SyncSource: Send + Sync {
async fn list_namespaces(
&self,
max_depth: &mut Option<usize>,
+ auth_id: &Authid,
+ user_info: &CachedUserInfo,
+ filter_callback: NamespaceFilter,
) -> Result<Vec<BackupNamespace>, Error>;
/// Lists groups within a specific namespace from the source.
@@ -260,6 +267,9 @@ impl SyncSource for RemoteSource {
async fn list_namespaces(
&self,
max_depth: &mut Option<usize>,
+ auth_id: &Authid,
+ user_info: &CachedUserInfo,
+ mut filter_callback: NamespaceFilter,
) -> Result<Vec<BackupNamespace>, Error> {
if self.ns.is_root() && max_depth.map_or(false, |depth| depth == 0) {
return Ok(vec![self.ns.clone()]);
@@ -307,6 +317,11 @@ impl SyncSource for RemoteSource {
.map(|list_item| list_item.ns)
.collect();
+ let list = list
+ .into_iter()
+ .filter(|namespace| filter_callback((namespace, self.get_store(), auth_id, user_info)))
+ .collect();
+
Ok(list)
}
@@ -400,13 +415,23 @@ impl SyncSource for LocalSource {
async fn list_namespaces(
&self,
max_depth: &mut Option<usize>,
+ auth_id: &Authid,
+ user_info: &CachedUserInfo,
+ mut filter_callback: NamespaceFilter,
) -> Result<Vec<BackupNamespace>, Error> {
- ListNamespacesRecursive::new_max_depth(
+ let list: Result<Vec<BackupNamespace>, Error> = ListNamespacesRecursive::new_max_depth(
self.store.clone(),
self.ns.clone(),
max_depth.unwrap_or(MAX_NAMESPACE_DEPTH),
)?
- .collect()
+ .collect();
+
+ let list = list?
+ .into_iter()
+ .filter(|namespace| filter_callback((namespace, self.get_store(), auth_id, user_info)))
+ .collect();
+
+ Ok(list)
}
async fn list_groups(
--
2.39.5
More information about the pbs-devel
mailing list