Skip to main content

agent_vault/core/
metadata.rs

1use std::path::Path;
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use crate::error::VaultError;
7
8#[derive(Debug, Serialize, Deserialize)]
9pub struct SecretMetadata {
10    pub name: String,
11    pub group: String,
12    pub created: DateTime<Utc>,
13    pub rotated: DateTime<Utc>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub expires: Option<DateTime<Utc>>,
16    pub authorized_agents: Vec<String>,
17}
18
19impl SecretMetadata {
20    pub fn new(name: &str, group: &str, authorized_agents: Vec<String>) -> Self {
21        let now = Utc::now();
22        Self {
23            name: name.to_string(),
24            group: group.to_string(),
25            created: now,
26            rotated: now,
27            expires: None,
28            authorized_agents,
29        }
30    }
31
32    pub fn load(path: &Path) -> Result<Self, VaultError> {
33        let contents = std::fs::read_to_string(path)?;
34        let meta: SecretMetadata = serde_yaml::from_str(&contents)?;
35        Ok(meta)
36    }
37
38    pub fn save(&self, path: &Path) -> Result<(), VaultError> {
39        let yaml = serde_yaml::to_string(self)?;
40        std::fs::write(path, yaml)?;
41        Ok(())
42    }
43}