sos_core/events/
event_kind.rs

1//! Event types for audit and log events.
2
3use crate::Error;
4use serde::{Deserialize, Serialize};
5
6use std::fmt;
7use std::str::FromStr;
8
9/// Type identifier for a noop.
10const NOOP: u16 = 0;
11/// Type identifier for the create account operation.
12const CREATE_ACCOUNT: u16 = 1;
13/// Type identifier for the delete account operation.
14const DELETE_ACCOUNT: u16 = 2;
15/// Type identifier for a list vaults operation.
16const LIST_VAULTS: u16 = 3;
17/// Type identifier for the create vault operation.
18const CREATE_VAULT: u16 = 4;
19/// Type identifier for the read vault operation.
20const READ_VAULT: u16 = 5;
21/// Type identifier for the update vault operation.
22const UPDATE_VAULT: u16 = 6;
23/// Type identifier for the delete vault operation.
24const DELETE_VAULT: u16 = 7;
25/// Type identifier for the get vault name operation.
26const GET_VAULT_NAME: u16 = 8;
27/// Type identifier for the set vault name operation.
28const SET_VAULT_NAME: u16 = 9;
29/// Type identifier for the set vault meta operation.
30const SET_VAULT_META: u16 = 10;
31/// Type identifier for the create secret operation.
32const CREATE_SECRET: u16 = 11;
33/// Type identifier for the read secret operation.
34const READ_SECRET: u16 = 12;
35/// Type identifier for the update secret operation.
36const UPDATE_SECRET: u16 = 13;
37/// Type identifier for the delete secret operation.
38const DELETE_SECRET: u16 = 14;
39/// Type identifier for the move secret operation.
40const MOVE_SECRET: u16 = 15;
41/// Type identifier for the read log event (remote only).
42const READ_EVENT_LOG: u16 = 16;
43/// Type identifier for the export vault operation.
44const EXPORT_VAULT: u16 = 17;
45/// Type identifier for export account archive.
46const EXPORT_BACKUP_ARCHIVE: u16 = 18;
47/// Type identifier for restore account archive.
48const IMPORT_BACKUP_ARCHIVE: u16 = 19;
49/// Type identifier for exporting unencrypted secrets.
50const EXPORT_UNSAFE: u16 = 20;
51/// Type identifier for importing unencrypted secrets.
52const IMPORT_UNSAFE: u16 = 21;
53/// Type identifier for exporting contacts.
54const EXPORT_CONTACTS: u16 = 22;
55/// Type identifier for importing contacts.
56const IMPORT_CONTACTS: u16 = 23;
57/// Type identifier for creating a file.
58const CREATE_FILE: u16 = 24;
59/// Type identifier for moving a file.
60const MOVE_FILE: u16 = 25;
61/// Type identifier for deleting a file.
62const DELETE_FILE: u16 = 26;
63/// Type identifier for vault event compaction.
64const COMPACT_VAULT: u16 = 27;
65/// Type identifier for changing a password.
66const CHANGE_PASSWORD: u16 = 28;
67/// Type identifier for trusting a device.
68const TRUST_DEVICE: u16 = 29;
69/// Type identifier for revoking a device.
70const REVOKE_DEVICE: u16 = 30;
71/// Type identifier for updating an identity folder.
72const UPDATE_IDENTITY: u16 = 31;
73/// Type identifier for renaming an account.
74const RENAME_ACCOUNT: u16 = 32;
75/// Type identifier for the set vault flags operation.
76const SET_VAULT_FLAGS: u16 = 33;
77/// Type identifier for downloading a file buffer.
78const DOWNLOAD_FILE: u16 = 34;
79
80/// EventKind wraps an event type identifier and
81/// provides a `Display` implementation.
82#[derive(Debug, Serialize, Deserialize, Copy, Clone, Eq, PartialEq)]
83pub enum EventKind {
84    /// No operation.
85    Noop,
86    /// Event to create an account.
87    CreateAccount,
88    /// Event to delete an account.
89    DeleteAccount,
90    /// Event to represent a sign in.
91    ListVaults,
92    /// Event to create a vault.
93    CreateVault,
94    /// Event to read a vault.
95    ReadVault,
96    /// Event to update a vault.
97    UpdateVault,
98    /// Event to get vault name.
99    GetVaultName,
100    /// Event to set vault name.
101    SetVaultName,
102    /// Event to set vault flags.
103    SetVaultFlags,
104    /// Event to set vault meta data.
105    SetVaultMeta,
106    /// Event to delete a vault.
107    DeleteVault,
108    /// Event to create a secret.
109    CreateSecret,
110    /// Event to read a secret.
111    ReadSecret,
112    /// Event to update a secret.
113    UpdateSecret,
114    /// Event to delete a secret.
115    DeleteSecret,
116    /// Event to move a secret.
117    MoveSecret,
118    /// Event to read a log.
119    ReadEventLog,
120    /// Event to export a vault.
121    ExportVault,
122    /// Event to export an account archive.
123    ExportBackupArchive,
124    /// Event to import an account archive.
125    ImportBackupArchive,
126    /// Event to export unencrypted secrets.
127    ExportUnsafe,
128    /// Event to import unencrypted secrets.
129    ImportUnsafe,
130    /// Event to export contacts.
131    ExportContacts,
132    /// Event to import contacts.
133    ImportContacts,
134    /// Event for creating a file.
135    CreateFile,
136    /// Event for moving a file.
137    MoveFile,
138    /// Event for deleting a file.
139    DeleteFile,
140    /// Event for vault compaction.
141    CompactVault,
142    /// Event for changing a password.
143    ChangePassword,
144    /// Event for trusting a device.
145    TrustDevice,
146    /// Event for revoking a device.
147    RevokeDevice,
148    /// Event for when an identity folder is updated.
149    UpdateIdentity,
150    /// Event for when an account is renamed.
151    RenameAccount,
152    /// Event for when a file buffer is downloaded.
153    DownloadFile,
154}
155
156impl Default for EventKind {
157    fn default() -> Self {
158        Self::Noop
159    }
160}
161
162impl TryFrom<u16> for EventKind {
163    type Error = Error;
164    fn try_from(value: u16) -> std::result::Result<Self, Self::Error> {
165        Ok(match value {
166            NOOP => EventKind::Noop,
167            CREATE_ACCOUNT => EventKind::CreateAccount,
168            DELETE_ACCOUNT => EventKind::DeleteAccount,
169            LIST_VAULTS => EventKind::ListVaults,
170            CREATE_VAULT => EventKind::CreateVault,
171            READ_VAULT => EventKind::ReadVault,
172            UPDATE_VAULT => EventKind::UpdateVault,
173            DELETE_VAULT => EventKind::DeleteVault,
174            GET_VAULT_NAME => EventKind::GetVaultName,
175            SET_VAULT_NAME => EventKind::SetVaultName,
176            SET_VAULT_FLAGS => EventKind::SetVaultFlags,
177            SET_VAULT_META => EventKind::SetVaultMeta,
178            CREATE_SECRET => EventKind::CreateSecret,
179            READ_SECRET => EventKind::ReadSecret,
180            UPDATE_SECRET => EventKind::UpdateSecret,
181            DELETE_SECRET => EventKind::DeleteSecret,
182            MOVE_SECRET => EventKind::MoveSecret,
183            READ_EVENT_LOG => EventKind::ReadEventLog,
184            EXPORT_VAULT => EventKind::ExportVault,
185            EXPORT_BACKUP_ARCHIVE => EventKind::ExportBackupArchive,
186            IMPORT_BACKUP_ARCHIVE => EventKind::ImportBackupArchive,
187            EXPORT_UNSAFE => EventKind::ExportUnsafe,
188            IMPORT_UNSAFE => EventKind::ImportUnsafe,
189            EXPORT_CONTACTS => EventKind::ExportContacts,
190            IMPORT_CONTACTS => EventKind::ImportContacts,
191            CREATE_FILE => EventKind::CreateFile,
192            MOVE_FILE => EventKind::MoveFile,
193            DELETE_FILE => EventKind::DeleteFile,
194            COMPACT_VAULT => EventKind::CompactVault,
195            CHANGE_PASSWORD => EventKind::ChangePassword,
196            TRUST_DEVICE => EventKind::TrustDevice,
197            REVOKE_DEVICE => EventKind::RevokeDevice,
198            UPDATE_IDENTITY => EventKind::UpdateIdentity,
199            RENAME_ACCOUNT => EventKind::RenameAccount,
200            DOWNLOAD_FILE => EventKind::DownloadFile,
201            _ => return Err(Error::UnknownEventKind(value)),
202        })
203    }
204}
205
206impl From<&EventKind> for u16 {
207    fn from(value: &EventKind) -> Self {
208        match value {
209            EventKind::Noop => NOOP,
210            EventKind::CreateAccount => CREATE_ACCOUNT,
211            EventKind::DeleteAccount => DELETE_ACCOUNT,
212            EventKind::ListVaults => LIST_VAULTS,
213            EventKind::CreateVault => CREATE_VAULT,
214            EventKind::ReadVault => READ_VAULT,
215            EventKind::UpdateVault => UPDATE_VAULT,
216            EventKind::DeleteVault => DELETE_VAULT,
217            EventKind::GetVaultName => GET_VAULT_NAME,
218            EventKind::SetVaultName => SET_VAULT_NAME,
219            EventKind::SetVaultFlags => SET_VAULT_FLAGS,
220            EventKind::SetVaultMeta => SET_VAULT_META,
221            EventKind::CreateSecret => CREATE_SECRET,
222            EventKind::ReadSecret => READ_SECRET,
223            EventKind::UpdateSecret => UPDATE_SECRET,
224            EventKind::DeleteSecret => DELETE_SECRET,
225            EventKind::MoveSecret => MOVE_SECRET,
226            EventKind::ReadEventLog => READ_EVENT_LOG,
227            EventKind::ExportVault => EXPORT_VAULT,
228            EventKind::ExportBackupArchive => EXPORT_BACKUP_ARCHIVE,
229            EventKind::ImportBackupArchive => IMPORT_BACKUP_ARCHIVE,
230            EventKind::ExportUnsafe => EXPORT_UNSAFE,
231            EventKind::ImportUnsafe => IMPORT_UNSAFE,
232            EventKind::ExportContacts => EXPORT_CONTACTS,
233            EventKind::ImportContacts => IMPORT_CONTACTS,
234            EventKind::CreateFile => CREATE_FILE,
235            EventKind::MoveFile => MOVE_FILE,
236            EventKind::DeleteFile => DELETE_FILE,
237            EventKind::CompactVault => COMPACT_VAULT,
238            EventKind::ChangePassword => CHANGE_PASSWORD,
239            EventKind::TrustDevice => TRUST_DEVICE,
240            EventKind::RevokeDevice => REVOKE_DEVICE,
241            EventKind::UpdateIdentity => UPDATE_IDENTITY,
242            EventKind::RenameAccount => RENAME_ACCOUNT,
243            EventKind::DownloadFile => DOWNLOAD_FILE,
244        }
245    }
246}
247
248impl From<EventKind> for u16 {
249    fn from(value: EventKind) -> Self {
250        (&value).into()
251    }
252}
253
254impl fmt::Display for EventKind {
255    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
256        write!(f, "{}", {
257            match self {
258                EventKind::Noop => "NOOP",
259                EventKind::CreateAccount => "CREATE_ACCOUNT",
260                EventKind::DeleteAccount => "DELETE_ACCOUNT",
261                EventKind::ListVaults => "LIST_FOLDERS",
262                EventKind::CreateVault => "CREATE_FOLDER",
263                EventKind::ReadVault => "READ_FOLDER",
264                EventKind::UpdateVault => "UPDATE_FOLDER",
265                EventKind::DeleteVault => "DELETE_FOLDER",
266                EventKind::GetVaultName => "GET_FOLDER_NAME",
267                EventKind::SetVaultName => "SET_FOLDER_NAME",
268                EventKind::SetVaultFlags => "SET_FOLDER_FLAGS",
269                EventKind::SetVaultMeta => "SET_FOLDER_META",
270                EventKind::CreateSecret => "CREATE_SECRET",
271                EventKind::ReadSecret => "READ_SECRET",
272                EventKind::UpdateSecret => "UPDATE_SECRET",
273                EventKind::DeleteSecret => "DELETE_SECRET",
274                EventKind::MoveSecret => "MOVE_SECRET",
275                EventKind::ReadEventLog => "READ_EVENT_LOG",
276                EventKind::ExportVault => "EXPORT_FOLDER",
277                EventKind::ExportBackupArchive => "EXPORT_BACKUP_ARCHIVE",
278                EventKind::ImportBackupArchive => "IMPORT_BACKUP_ARCHIVE",
279                EventKind::ExportUnsafe => "EXPORT_UNSAFE",
280                EventKind::ImportUnsafe => "IMPORT_UNSAFE",
281                EventKind::ExportContacts => "EXPORT_CONTACTS",
282                EventKind::ImportContacts => "IMPORT_CONTACTS",
283                EventKind::CreateFile => "CREATE_FILE",
284                EventKind::MoveFile => "MOVE_FILE",
285                EventKind::DeleteFile => "DELETE_FILE",
286                EventKind::CompactVault => "COMPACT_FOLDER",
287                EventKind::ChangePassword => "CHANGE_PASSWORD",
288                EventKind::TrustDevice => "TRUST_DEVICE",
289                EventKind::RevokeDevice => "REVOKE_DEVICE",
290                EventKind::UpdateIdentity => "UPDATE_IDENTITY",
291                EventKind::RenameAccount => "RENAME_ACCOUNT",
292                EventKind::DownloadFile => "DOWNLOAD_FILE",
293            }
294        })
295    }
296}
297
298impl FromStr for EventKind {
299    type Err = Error;
300
301    fn from_str(s: &str) -> Result<Self, Self::Err> {
302        match s {
303            "NOOP" => Ok(EventKind::Noop),
304            "CREATE_ACCOUNT" => Ok(EventKind::CreateAccount),
305            "DELETE_ACCOUNT" => Ok(EventKind::DeleteAccount),
306            "LIST_FOLDERS" => Ok(EventKind::ListVaults),
307            "CREATE_FOLDER" => Ok(EventKind::CreateVault),
308            "READ_FOLDER" => Ok(EventKind::ReadVault),
309            "UPDATE_FOLDER" => Ok(EventKind::UpdateVault),
310            "DELETE_FOLDER" => Ok(EventKind::DeleteVault),
311            "GET_FOLDER_NAME" => Ok(EventKind::GetVaultName),
312            "SET_FOLDER_NAME" => Ok(EventKind::SetVaultName),
313            "SET_FOLDER_FLAGS" => Ok(EventKind::SetVaultFlags),
314            "SET_FOLDER_META" => Ok(EventKind::SetVaultMeta),
315            "CREATE_SECRET" => Ok(EventKind::CreateSecret),
316            "READ_SECRET" => Ok(EventKind::ReadSecret),
317            "UPDATE_SECRET" => Ok(EventKind::UpdateSecret),
318            "DELETE_SECRET" => Ok(EventKind::DeleteSecret),
319            "MOVE_SECRET" => Ok(EventKind::MoveSecret),
320            "READ_EVENT_LOG" => Ok(EventKind::ReadEventLog),
321            "EXPORT_FOLDER" => Ok(EventKind::ExportVault),
322            "EXPORT_BACKUP_ARCHIVE" => Ok(EventKind::ExportBackupArchive),
323            "IMPORT_BACKUP_ARCHIVE" => Ok(EventKind::ImportBackupArchive),
324            "EXPORT_UNSAFE" => Ok(EventKind::ExportUnsafe),
325            "IMPORT_UNSAFE" => Ok(EventKind::ImportUnsafe),
326            "EXPORT_CONTACTS" => Ok(EventKind::ExportContacts),
327            "IMPORT_CONTACTS" => Ok(EventKind::ImportContacts),
328            "CREATE_FILE" => Ok(EventKind::CreateFile),
329            "MOVE_FILE" => Ok(EventKind::MoveFile),
330            "DELETE_FILE" => Ok(EventKind::DeleteFile),
331            "COMPACT_FOLDER" => Ok(EventKind::CompactVault),
332            "CHANGE_PASSWORD" => Ok(EventKind::ChangePassword),
333            "TRUST_DEVICE" => Ok(EventKind::TrustDevice),
334            "REVOKE_DEVICE" => Ok(EventKind::RevokeDevice),
335            "UPDATE_IDENTITY" => Ok(EventKind::UpdateIdentity),
336            "RENAME_ACCOUNT" => Ok(EventKind::RenameAccount),
337            "DOWNLOAD_FILE" => Ok(EventKind::DownloadFile),
338            _ => Err(Error::UnknownEventType(s.to_string())),
339        }
340    }
341}