trz_gateway_common/security_configuration/
common.rs

1use openssl::error::ErrorStack;
2use openssl::x509::X509;
3
4pub fn parse_pem_certificates(pems: &str) -> impl Iterator<Item = Result<X509, ErrorStack>> + '_ {
5    pems.split_inclusive("-----END CERTIFICATE-----")
6        .map(|pem| pem.trim())
7        .filter(|pem| !pem.is_empty())
8        .map(|pem| X509::from_pem(pem.as_bytes()))
9}
10
11pub(super) fn get_or_init<T: Clone, E>(
12    mutex: &std::sync::Mutex<Option<T>>,
13    make: impl FnOnce() -> Result<T, E>,
14) -> Result<T, E> {
15    let mut lock = mutex.lock().unwrap();
16    match &mut *lock {
17        Some(value) => Ok(value.clone()),
18        None => {
19            let value = make()?;
20            *lock = Some(value.clone());
21            return Ok(value);
22        }
23    }
24}