acme-api: use inner mutability for ACME_ACME_CONFIG

in edition 2024 references to mutable static will be disallowed [1]. the
recommended way around this is to use types with inner mutability. so
use a `OnceLock` for `ACME_ACME_CONFIG` as the directoryfor ACME
configuration purposes shouldn't change throughout the run time of an
application.

[1]:
https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html

Signed-off-by: Shannon Sterz <s.sterz@proxmox.com>
This commit is contained in:
Shannon Sterz 2024-12-03 14:53:24 +01:00 committed by Dietmar Maurer
parent b256ee391c
commit 115adf42bd

View File

@ -1,26 +1,28 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use anyhow::Error; use anyhow::Error;
use proxmox_product_config::create_secret_dir; use proxmox_product_config::create_secret_dir;
#[derive(Debug)]
struct AcmeApiConfig { struct AcmeApiConfig {
acme_config_dir: PathBuf, acme_config_dir: PathBuf,
acme_account_dir: PathBuf, acme_account_dir: PathBuf,
} }
static mut ACME_ACME_CONFIG: Option<AcmeApiConfig> = None; static ACME_ACME_CONFIG: OnceLock<AcmeApiConfig> = OnceLock::new();
/// Initialize the global product configuration. /// Initialize the global product configuration.
pub fn init<P: AsRef<Path>>(acme_config_dir: P, create_subdirs: bool) -> Result<(), Error> { pub fn init<P: AsRef<Path>>(acme_config_dir: P, create_subdirs: bool) -> Result<(), Error> {
let acme_config_dir = acme_config_dir.as_ref().to_owned(); let acme_config_dir = acme_config_dir.as_ref().to_owned();
unsafe { ACME_ACME_CONFIG
ACME_ACME_CONFIG = Some(AcmeApiConfig { .set(AcmeApiConfig {
acme_account_dir: acme_config_dir.join("accounts"), acme_account_dir: acme_config_dir.join("accounts"),
acme_config_dir, acme_config_dir,
}); })
} .expect("cannot set acme configuration twice");
if create_subdirs { if create_subdirs {
create_secret_dir(self::acme_config_dir(), false)?; create_secret_dir(self::acme_config_dir(), false)?;
@ -31,11 +33,9 @@ pub fn init<P: AsRef<Path>>(acme_config_dir: P, create_subdirs: bool) -> Result<
} }
fn acme_api_config() -> &'static AcmeApiConfig { fn acme_api_config() -> &'static AcmeApiConfig {
unsafe {
ACME_ACME_CONFIG ACME_ACME_CONFIG
.as_ref() .get()
.expect("ProxmoxProductConfig is not initialized!") .expect("ProxmoxProductConfig is not initialized!")
}
} }
fn acme_config_dir() -> &'static Path { fn acme_config_dir() -> &'static Path {