[pbs-devel] [PATCH proxmox 1/5] access: add the proxmox-access crate to reuse acl trees

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Jun 11 14:53:53 CEST 2024


On Mon, Jun 10, 2024 at 05:42:10PM GMT, Shannon Sterz wrote:
> diff --git a/proxmox-access/src/init.rs b/proxmox-access/src/init.rs
> new file mode 100644
> index 00000000..71f2f8fc
> --- /dev/null
> +++ b/proxmox-access/src/init.rs
> @@ -0,0 +1,73 @@
> +use anyhow::{format_err, Error};
> +use std::{
> +    collections::HashMap,
> +    path::{Path, PathBuf},
> +    sync::OnceLock,
> +};
> +
> +static ACM_CONF: OnceLock<&'static dyn AcmConfig> = OnceLock::new();
> +static ACM_CONF_DIR: OnceLock<PathBuf> = OnceLock::new();
> +
> +/// This trait specifies the functions a product needs to implement to get ACL tree based access
> +/// control management from this plugin.
> +pub trait AcmConfig: Send + Sync {

This is a terrible name ;-)
Given the methods defined here, we could just call it `RoleSetup`?

> +    /// Returns a mapping of all recognized roles and their corresponding `u64` value.
> +    fn roles(&self) -> &HashMap<&str, u64>;
> +
> +    /// Optionally returns a role that has no access to any resource.
> +    ///
> +    /// Default: Returns `None`.
> +    fn role_no_access(&self) -> Option<&str> {
> +        None
> +    }
> +
> +    /// Optionally returns a role that is allowed to access all resources.
> +    ///
> +    /// Default: Returns `None`.
> +    fn role_admin(&self) -> Option<&str> {
> +        None
> +    }
> +}
> +
> +pub fn init<P: AsRef<Path>>(
> +    acm_config: &'static dyn AcmConfig,
> +    config_dir: P,
> +) -> Result<(), Error> {
> +    init_acm_config(acm_config)?;
> +    init_acm_config_dir(config_dir)
> +}
> +
> +pub fn init_acm_config_dir<P: AsRef<Path>>(config_dir: P) -> Result<(), Error> {

^ pub(crate) ?

> +    ACM_CONF_DIR
> +        .set(config_dir.as_ref().to_owned())
> +        .map_err(|_e| format_err!("cannot initialize acl tree config twice!"))
> +}
> +
> +pub(crate) fn init_acm_config(config: &'static dyn AcmConfig) -> Result<(), Error> {
> +    ACM_CONF
> +        .set(config)
> +        .map_err(|_e| format_err!("cannot initialize acl tree config twice!"))
> +}
> +
> +
> +pub(crate) fn acm_conf() -> &'static dyn AcmConfig {
> +    *ACM_CONF
> +        .get()
> +        .expect("please initialize the acm config before using it!")
> +}
> +
> +
> +fn conf_dir() -> &'static PathBuf {
> +    ACM_CONF_DIR
> +        .get()
> +        .expect("please initialize acm config dir before using it!")
> +}
> +
> +pub(crate) fn acl_config() -> PathBuf {
> +    conf_dir().with_file_name("acl.cfg")
> +}
> +
> +pub(crate) fn acl_config_lock() -> PathBuf {
> +    conf_dir().with_file_name(".acl.lck")
> +}
> +
> diff --git a/proxmox-access/src/lib.rs b/proxmox-access/src/lib.rs
> new file mode 100644
> index 00000000..8ad2c83d
> --- /dev/null
> +++ b/proxmox-access/src/lib.rs
> @@ -0,0 +1,2 @@
> +pub mod acl;
> +pub mod init;
> -- 
> 2.39.2




More information about the pbs-devel mailing list