fraiseql_core/security/kms/
models.rs1use std::{collections::HashMap, fmt};
7
8use serde::{Deserialize, Serialize};
9use zeroize::Zeroizing;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
13#[serde(rename_all = "snake_case")]
14#[non_exhaustive]
15pub enum KeyPurpose {
16 EncryptDecrypt,
18 SignVerify,
20 Mac,
22}
23
24impl fmt::Display for KeyPurpose {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 match self {
27 Self::EncryptDecrypt => write!(f, "encrypt_decrypt"),
28 Self::SignVerify => write!(f, "sign_verify"),
29 Self::Mac => write!(f, "mac"),
30 }
31 }
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
36#[serde(rename_all = "snake_case")]
37#[non_exhaustive]
38pub enum KeyState {
39 Enabled,
41 Disabled,
43 PendingDeletion,
45 Destroyed,
47}
48
49impl fmt::Display for KeyState {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 match self {
52 Self::Enabled => write!(f, "enabled"),
53 Self::Disabled => write!(f, "disabled"),
54 Self::PendingDeletion => write!(f, "pending_deletion"),
55 Self::Destroyed => write!(f, "destroyed"),
56 }
57 }
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct KeyReference {
63 pub provider: String,
65 pub key_id: String,
67 pub key_alias: Option<String>,
69 pub purpose: KeyPurpose,
71 pub created_at: i64,
73}
74
75impl KeyReference {
76 #[must_use]
78 pub const fn new(
79 provider: String,
80 key_id: String,
81 purpose: KeyPurpose,
82 created_at: i64,
83 ) -> Self {
84 Self {
85 provider,
86 key_id,
87 key_alias: None,
88 purpose,
89 created_at,
90 }
91 }
92
93 #[must_use]
95 pub fn with_alias(mut self, alias: String) -> Self {
96 self.key_alias = Some(alias);
97 self
98 }
99
100 #[must_use]
102 pub fn qualified_id(&self) -> String {
103 format!("{}:{}", self.provider, self.key_id)
104 }
105}
106
107#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct EncryptedData {
110 pub ciphertext: String,
112 pub key_reference: KeyReference,
114 pub algorithm: String,
116 pub encrypted_at: i64,
118 pub context: HashMap<String, String>,
120}
121
122impl EncryptedData {
123 #[must_use]
125 pub const fn new(
126 ciphertext: String,
127 key_reference: KeyReference,
128 algorithm: String,
129 encrypted_at: i64,
130 context: HashMap<String, String>,
131 ) -> Self {
132 Self {
133 ciphertext,
134 key_reference,
135 algorithm,
136 encrypted_at,
137 context,
138 }
139 }
140}
141
142#[derive(Debug, Clone)]
144pub struct DataKeyPair {
145 pub plaintext_key: Zeroizing<Vec<u8>>,
147 pub encrypted_key: EncryptedData,
149 pub key_reference: KeyReference,
151}
152
153impl DataKeyPair {
154 #[must_use]
156 pub fn new(
157 plaintext_key: Vec<u8>,
158 encrypted_key: EncryptedData,
159 key_reference: KeyReference,
160 ) -> Self {
161 Self {
162 plaintext_key: Zeroizing::new(plaintext_key),
163 encrypted_key,
164 key_reference,
165 }
166 }
167}
168
169#[derive(Debug, Clone, Serialize, Deserialize)]
171pub struct RotationPolicy {
172 pub enabled: bool,
174 pub rotation_period_days: u32,
176 pub last_rotation: Option<i64>,
178 pub next_rotation: Option<i64>,
180}
181
182impl RotationPolicy {
183 #[must_use]
185 pub const fn new(enabled: bool, rotation_period_days: u32) -> Self {
186 Self {
187 enabled,
188 rotation_period_days,
189 last_rotation: None,
190 next_rotation: None,
191 }
192 }
193}