docbox_core/secrets/
memory.rs

1use tokio::sync::Mutex;
2
3use super::{Secret, SecretManager};
4use std::collections::HashMap;
5
6/// In memory secret manager
7#[derive(Default)]
8pub struct MemorySecretManager {
9    data: Mutex<HashMap<String, Secret>>,
10    default: Option<Secret>,
11}
12
13impl MemorySecretManager {
14    pub fn new(data: HashMap<String, Secret>, default: Option<Secret>) -> Self {
15        Self {
16            data: Mutex::new(data),
17            default,
18        }
19    }
20}
21
22impl SecretManager for MemorySecretManager {
23    async fn get_secret(&self, name: &str) -> anyhow::Result<Option<super::Secret>> {
24        if let Some(value) = self.data.lock().await.get(name) {
25            return Ok(Some(value.clone()));
26        }
27
28        if let Some(value) = self.default.as_ref() {
29            return Ok(Some(value.clone()));
30        }
31
32        Ok(None)
33    }
34
35    async fn create_secret(&self, name: &str, value: &str) -> anyhow::Result<()> {
36        self.data
37            .lock()
38            .await
39            .insert(name.to_string(), Secret::String(value.to_string()));
40        Ok(())
41    }
42}