secret_vault/
common_types.rs

1use chrono::prelude::*;
2use rsb_derive::*;
3use rvstruct::*;
4use secret_vault_value::SecretValue;
5
6#[derive(Debug, Clone, Eq, PartialEq, Hash, ValueStruct)]
7pub struct SecretName(String);
8
9#[derive(Debug, Clone, Eq, PartialEq, Hash, ValueStruct)]
10pub struct SecretNamespace(String);
11
12#[derive(Debug, Clone, Eq, PartialEq, Hash, ValueStruct)]
13pub struct SecretVersion(String);
14
15#[derive(Debug, Clone, Eq, PartialEq, Hash)]
16pub struct SecretVaultRef {
17    pub key: SecretVaultKey,
18
19    pub required: bool,
20    pub auto_refresh: bool,
21    pub allow_in_snapshots: bool,
22    pub predefined_labels: Vec<SecretMetadataLabel>,
23}
24
25impl SecretVaultRef {
26    pub fn new(secret_name: SecretName) -> Self {
27        Self {
28            key: SecretVaultKey::new(secret_name),
29            required: true,
30            auto_refresh: false,
31            allow_in_snapshots: false,
32            predefined_labels: Vec::new(),
33        }
34    }
35
36    pub fn with_secret_version(self, value: SecretVersion) -> Self {
37        Self {
38            key: self.key.with_secret_version(value),
39            ..self
40        }
41    }
42
43    pub fn opt_secret_version(self, value: Option<SecretVersion>) -> Self {
44        Self {
45            key: self.key.opt_secret_version(value),
46            ..self
47        }
48    }
49
50    pub fn with_namespace(self, value: SecretNamespace) -> Self {
51        Self {
52            key: self.key.with_namespace(value),
53            ..self
54        }
55    }
56
57    pub fn opt_namespace(self, value: Option<SecretNamespace>) -> Self {
58        Self {
59            key: self.key.opt_namespace(value),
60            ..self
61        }
62    }
63
64    pub fn with_required(self, value: bool) -> Self {
65        Self {
66            required: value,
67            ..self
68        }
69    }
70
71    pub fn with_auto_refresh(self, value: bool) -> Self {
72        Self {
73            auto_refresh: value,
74            ..self
75        }
76    }
77
78    pub fn with_allow_in_snapshots(self, value: bool) -> Self {
79        Self {
80            allow_in_snapshots: value,
81            ..self
82        }
83    }
84
85    pub fn add_predefined_label(self, label: SecretMetadataLabel) -> Self {
86        let mut predefined_labels = self.predefined_labels;
87        predefined_labels.push(label);
88        Self {
89            predefined_labels,
90            ..self
91        }
92    }
93}
94
95#[derive(Debug, Clone, Eq, PartialEq, Hash, Builder)]
96pub struct SecretVaultKey {
97    pub secret_name: SecretName,
98    pub secret_version: Option<SecretVersion>,
99    pub namespace: Option<SecretNamespace>,
100}
101
102#[derive(Debug, Clone, Eq, PartialEq, Hash, Builder)]
103pub struct SecretMetadataLabel {
104    pub name: String,
105    pub value: Option<String>,
106}
107
108#[derive(Debug, Clone, Eq, PartialEq, Hash, Builder)]
109pub struct SecretMetadataAnnotation {
110    pub name: String,
111    pub value: Option<String>,
112}
113
114#[derive(Debug, Clone, Eq, PartialEq, Builder)]
115pub struct SecretMetadata {
116    #[default = "Utc::now()"]
117    pub cached_at: DateTime<Utc>,
118    pub key: SecretVaultKey,
119    pub labels: Option<Vec<SecretMetadataLabel>>,
120    pub annotations: Option<Vec<SecretMetadataAnnotation>>,
121    pub description: Option<String>,
122    pub expiration: Option<SecretExpiration>,
123    pub version: Option<SecretVersion>,
124    pub created_at: Option<DateTime<Utc>>,
125    pub updated_at: Option<DateTime<Utc>>,
126}
127
128impl SecretMetadata {
129    pub fn create_from_ref(secret_ref: &SecretVaultRef) -> Self {
130        let mut result = SecretMetadata::new(secret_ref.key.clone());
131        if !secret_ref.predefined_labels.is_empty() {
132            result.labels(secret_ref.predefined_labels.clone());
133        }
134        result
135    }
136
137    pub fn add_label(&mut self, label: SecretMetadataLabel) -> &Self {
138        if let Some(labels) = &mut self.labels {
139            labels.push(label);
140        } else {
141            self.labels = Some(vec![label]);
142        }
143        self
144    }
145
146    pub fn add_annotation(&mut self, annotation: SecretMetadataAnnotation) -> &Self {
147        if let Some(annotations) = &mut self.annotations {
148            annotations.push(annotation);
149        } else {
150            self.annotations = Some(vec![annotation]);
151        }
152        self
153    }
154}
155
156#[derive(Debug, Clone, Eq, PartialEq, Builder)]
157pub struct Secret {
158    pub value: SecretValue,
159    pub metadata: SecretMetadata,
160}
161
162#[derive(Debug, Clone, Eq, PartialEq)]
163pub enum SecretExpiration {
164    ExpireTime(chrono::DateTime<chrono::Utc>),
165    Ttl(chrono::Duration),
166}