sos-server-storage 0.17.1

Server storage for the Save Our Secrets SDK.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//! Server storage backed by a database.
use crate::{Error, Result, ServerAccountStorage};
use async_trait::async_trait;
use indexmap::IndexSet;
use sos_backend::{
    extract_vault, AccountEventLog, BackendTarget, DeviceEventLog,
    FolderEventLog, VaultWriter,
};
use sos_core::{
    decode,
    device::{DevicePublicKey, TrustedDevice},
    encode,
    events::{
        patch::{FolderDiff, FolderPatch},
        AccountEvent, EventLog,
    },
    AccountId, Paths, VaultFlags, VaultId,
};
use sos_database::async_sqlite::Client;
use sos_database::entity::{
    AccountEntity, AccountRow, FolderEntity, FolderRecord, FolderRow,
};
use sos_reducers::{DeviceReducer, FolderReducer};
use sos_sync::{CreateSet, StorageEventLogs};
use sos_vault::{EncryptedEntry, Summary, Vault};
use sos_vfs as vfs;
use std::{
    collections::{HashMap, HashSet},
    sync::Arc,
};
use tokio::sync::RwLock;

#[cfg(feature = "files")]
use sos_backend::FileEventLog;

#[cfg(feature = "audit")]
use {sos_audit::AuditEvent, sos_backend::audit::append_audit_events};

/// Server folders loaded into memory and mirrored to the database.
pub struct ServerDatabaseStorage {
    /// Account identifier.
    pub(super) account_id: AccountId,

    /// Account database id.
    pub(super) account_row_id: i64,

    /// Directories for storage.
    pub(super) paths: Arc<Paths>,

    /// Database client.
    pub(super) client: Client,

    /// Backend target.
    pub(super) target: BackendTarget,

    /// Identity folder event log.
    pub(super) identity_log: Arc<RwLock<FolderEventLog>>,

    /// Account event log.
    pub(super) account_log: Arc<RwLock<AccountEventLog>>,

    /// Device event log.
    pub(super) device_log: Arc<RwLock<DeviceEventLog>>,

    /// File event log.
    #[cfg(feature = "files")]
    pub(super) file_log: Arc<RwLock<FileEventLog>>,

    /// Folder event logs.
    pub(super) folders: HashMap<VaultId, Arc<RwLock<FolderEventLog>>>,

    /// Reduced collection of devices.
    pub(super) devices: IndexSet<TrustedDevice>,
}

impl ServerDatabaseStorage {
    /// Create database storage for server-side access.
    ///
    /// Events are loaded into memory.
    pub async fn new(
        mut target: BackendTarget,
        account_id: &AccountId,
        identity_log: Arc<RwLock<FolderEventLog>>,
    ) -> Result<Self> {
        let (paths, client, account_row) = {
            let BackendTarget::Database(paths, client) = &mut target else {
                panic!("database backend expected");
            };
            debug_assert!(!paths.is_global());

            if !vfs::metadata(paths.documents_dir()).await?.is_dir() {
                return Err(Error::NotDirectory(
                    paths.documents_dir().to_path_buf(),
                )
                .into());
            }

            let account_row =
                Self::lookup_account(client, account_id).await?;
            (paths, client, account_row)
        };

        let paths = paths.clone();
        let client = client.clone();

        let (device_log, devices) =
            Self::initialize_device_log(&target, account_id).await?;

        let mut event_log =
            AccountEventLog::new_account(target.clone(), account_id).await?;
        event_log.load_tree().await?;

        #[cfg(feature = "files")]
        let file_log = {
            let mut file_log =
                FileEventLog::new_file(target.clone(), account_id).await?;
            file_log.load_tree().await?;
            file_log
        };

        let mut storage = Self {
            account_id: *account_id,
            account_row_id: account_row.row_id,
            paths,
            client,
            target,
            identity_log,
            account_log: Arc::new(RwLock::new(event_log)),
            device_log: Arc::new(RwLock::new(device_log)),
            #[cfg(feature = "files")]
            file_log: Arc::new(RwLock::new(file_log)),
            folders: Default::default(),
            devices,
        };

        storage.load_folders().await?;

        Ok(storage)
    }

    async fn initialize_device_log(
        target: &BackendTarget,
        account_id: &AccountId,
    ) -> Result<(DeviceEventLog, IndexSet<TrustedDevice>)> {
        let mut event_log =
            DeviceEventLog::new_device(target.clone(), account_id).await?;
        event_log.load_tree().await?;

        let reducer = DeviceReducer::new(&event_log);
        let devices = reducer.reduce().await?;

        Ok((event_log, devices))
    }

    /// Create new event log cache entries.
    async fn create_folder_entry(&mut self, id: &VaultId) -> Result<()> {
        let mut event_log = FolderEventLog::new_folder(
            self.target.clone(),
            &self.account_id,
            id,
        )
        .await?;
        event_log.load_tree().await?;
        self.folders.insert(*id, Arc::new(RwLock::new(event_log)));
        Ok(())
    }

    /// Remove a folder.
    async fn remove_vault_file(&self, folder_id: &VaultId) -> Result<()> {
        let folder_id = *folder_id;
        self.client
            .conn(move |conn| {
                let folder = FolderEntity::new(&conn);
                folder.delete_folder(&folder_id)
            })
            .await
            .map_err(sos_database::Error::from)?;
        Ok(())
    }

    /// Create a new account.
    pub async fn initialize_account(
        target: &BackendTarget,
        account_id: &AccountId,
        identity_patch: &FolderPatch,
    ) -> Result<FolderEventLog> {
        let BackendTarget::Database(paths, client) = &target else {
            panic!("database backend expected");
        };

        let vault = extract_vault(identity_patch.records())
            .await?
            .ok_or(Error::NoVaultEvent)?;

        let account_row =
            AccountRow::new_insert(account_id, vault.name().to_string())?;
        let folder_row = FolderRow::new_insert(&vault).await?;
        client
            .conn_mut(move |conn| {
                let tx = conn.transaction()?;

                // Create the account
                let account = AccountEntity::new(&tx);
                let account_id = account.insert(&account_row)?;

                // Create the folder
                let folder = FolderEntity::new(&tx);
                let folder_id =
                    folder.insert_folder(account_id, &folder_row)?;

                // Create the join
                account.insert_login_folder(account_id, folder_id)?;

                tx.commit()?;
                Ok(())
            })
            .await
            .map_err(sos_database::Error::from)?;

        let mut event_log = FolderEventLog::new_folder(
            BackendTarget::Database(paths.clone(), client.clone()),
            account_id,
            vault.id(),
        )
        .await?;
        event_log.clear().await?;
        event_log.patch_unchecked(identity_patch).await?;
        Ok(event_log)
    }

    async fn lookup_account(
        client: &mut Client,
        account_id: &AccountId,
    ) -> Result<AccountRow> {
        let account_id = *account_id;
        Ok(client
            .conn(move |conn| {
                let account = AccountEntity::new(&conn);
                Ok(account.find_one(&account_id)?)
            })
            .await
            .map_err(sos_database::Error::from)?)
    }
}

#[async_trait]
impl ServerAccountStorage for ServerDatabaseStorage {
    fn account_id(&self) -> &AccountId {
        &self.account_id
    }

    fn list_device_keys(&self) -> HashSet<&DevicePublicKey> {
        self.devices.iter().map(|d| d.public_key()).collect()
    }

    fn paths(&self) -> Arc<Paths> {
        self.paths.clone()
    }

    fn folders(&self) -> &HashMap<VaultId, Arc<RwLock<FolderEventLog>>> {
        &self.folders
    }

    fn folders_mut(
        &mut self,
    ) -> &mut HashMap<VaultId, Arc<RwLock<FolderEventLog>>> {
        &mut self.folders
    }

    fn set_devices(&mut self, devices: IndexSet<TrustedDevice>) {
        self.devices = devices;
    }

    async fn rename_account(&self, name: &str) -> Result<()> {
        // Rename the folder (v1 logic)
        let account_id = self.account_row_id.clone();
        let login_folder = self
            .client
            .conn_and_then(move |conn| {
                let folder = FolderEntity::new(&conn);
                folder.find_login_folder(account_id)
            })
            .await?;
        let login_folder = FolderRecord::from_row(login_folder).await?;

        let mut file =
            VaultWriter::new(self.target.clone(), login_folder.summary.id());
        file.set_vault_name(name.to_owned()).await?;

        // Update the accounts table (v2 logic)
        let account_id = self.account_row_id.clone();
        let name = name.to_owned();
        self.client
            .conn_and_then(move |conn| {
                let account = AccountEntity::new(&conn);
                account.rename_account(account_id, &name)
            })
            .await?;

        Ok(())
    }

    async fn read_vault(&self, folder_id: &VaultId) -> Result<Vault> {
        Ok(FolderEntity::compute_folder_vault(&self.client, folder_id)
            .await?)
    }

    async fn write_vault(&self, vault: &Vault) -> Result<()> {
        let identity_id = *vault.id();
        let identity_row = FolderRow::new_update(vault).await?;
        self.client
            .conn(move |conn| {
                let folder = FolderEntity::new(&conn);
                Ok(folder.update_folder(&identity_id, &identity_row)?)
            })
            .await?;
        Ok(())
    }

    /*
    async fn read_login_vault(&self) -> Result<Vault> {
        let account_row_id = self.account_row_id;
        let folder_row = self
            .client
            .conn_and_then(move |conn| {
                let folder_entity = FolderEntity::new(&conn);
                folder_entity.find_login_folder(account_row_id)
            })
            .await?;
        let record = FolderRecord::from_row(folder_row).await?;
        Ok(FolderEntity::compute_folder_vault(
            &self.client,
            record.summary.id(),
        )
        .await?)
    }
    */

    async fn write_login_vault(&self, vault: &Vault) -> Result<()> {
        AccountEntity::upsert_login_folder(
            &self.client,
            &self.account_id,
            &vault,
        )
        .await?;
        Ok(())
    }

    async fn replace_folder(
        &self,
        folder_id: &VaultId,
        diff: &FolderDiff,
    ) -> Result<(FolderEventLog, Vault)> {
        let mut event_log = FolderEventLog::new_folder(
            BackendTarget::Database(self.paths.clone(), self.client.clone()),
            &self.account_id,
            folder_id,
        )
        .await?;
        event_log.replace_all_events(&diff).await?;

        let vault = FolderReducer::new()
            .reduce(&event_log)
            .await?
            .build(false)
            .await?;

        FolderEntity::replace_all_secrets(
            self.client.clone(),
            folder_id,
            &vault,
        )
        .await?;

        Ok((event_log, vault))
    }

    async fn set_folder_flags(
        &self,
        folder_id: &VaultId,
        flags: VaultFlags,
    ) -> Result<()> {
        let mut writer = VaultWriter::new(self.target.clone(), folder_id);
        writer.set_vault_flags(flags).await?;
        Ok(())
    }

    async fn import_account(
        &mut self,
        account_data: &CreateSet,
    ) -> Result<()> {
        let account_id = self.account_row_id;

        {
            let mut writer = self.account_log.write().await;
            writer.patch_unchecked(&account_data.account).await?;
        }

        {
            let mut writer = self.device_log.write().await;
            writer.patch_unchecked(&account_data.device).await?;
            let reducer = DeviceReducer::new(&*writer);
            self.devices = reducer.reduce().await?;
        }

        #[cfg(feature = "files")]
        {
            let mut writer = self.file_log.write().await;
            writer.patch_unchecked(&account_data.files).await?;
        }

        for (id, folder) in &account_data.folders {
            if let Some(vault) = extract_vault(folder.records()).await? {
                let folder_row = FolderRow::new_insert(&vault).await?;

                self.client
                    .conn(move |conn| {
                        let folder = FolderEntity::new(&conn);
                        folder.insert_folder(account_id, &folder_row)
                    })
                    .await
                    .map_err(sos_database::Error::from)?;

                let mut event_log = FolderEventLog::new_folder(
                    BackendTarget::Database(
                        self.paths.clone(),
                        self.client.clone(),
                    ),
                    &self.account_id,
                    id,
                )
                .await?;
                event_log.patch_unchecked(folder).await?;

                self.folders.insert(*id, Arc::new(RwLock::new(event_log)));
            }
        }

        Ok(())
    }

    async fn load_folders(&mut self) -> Result<Vec<Summary>> {
        let account_id = self.account_row_id;
        let rows = self
            .client
            .conn_and_then(move |conn| {
                let folders = FolderEntity::new(&conn);
                Ok::<_, sos_database::Error>(
                    folders.list_user_folders(account_id)?,
                )
            })
            .await?;

        let mut folders = Vec::new();
        for row in rows {
            let record = FolderRecord::from_row(row).await?;
            folders.push(record.summary);
        }

        // Create a cache entry for each summary if it does not
        // already exist.
        for summary in &folders {
            // Ensure we don't overwrite existing data
            if self.folders.get(summary.id()).is_none() {
                self.create_folder_entry(summary.id()).await?;
            }
        }

        Ok(folders)
    }

    async fn import_folder(
        &mut self,
        id: &VaultId,
        buffer: &[u8],
    ) -> Result<()> {
        let exists = self.folders.get(id).is_some();

        let vault: Vault = decode(buffer).await?;
        let (vault, events) = FolderReducer::split::<Error>(vault).await?;

        if id != vault.id() {
            return Err(
                Error::VaultIdentifierMismatch(*id, *vault.id()).into()
            );
        }

        FolderEntity::upsert_folder_and_secrets(
            &self.client,
            self.account_row_id,
            &vault,
        )
        .await?;

        self.create_folder_entry(id).await?;

        {
            let event_log = self.folders.get_mut(id).unwrap();
            let mut event_log = event_log.write().await;
            event_log.clear().await?;
            event_log.apply(events.as_slice()).await?;
        }

        #[cfg(feature = "audit")]
        {
            let buffer = encode(&vault).await?;
            // If there is an existing folder
            // and we are overwriting then log the update
            // folder event
            let account_event = if exists {
                AccountEvent::UpdateFolder(*id, buffer)
            // Otherwise a create event
            } else {
                AccountEvent::CreateFolder(*id, buffer)
            };

            let audit_event: AuditEvent =
                (self.account_id(), &account_event).into();
            append_audit_events(&[audit_event]).await?;
        }

        Ok(())
    }

    async fn delete_folder(&mut self, id: &VaultId) -> Result<()> {
        // Remove from the database
        self.remove_vault_file(id).await?;

        // Remove local state
        self.folders.remove(id);

        #[cfg(feature = "files")]
        {
            let blob_folder = self.paths.into_file_folder_path(&id);
            if vfs::try_exists(&blob_folder).await? {
                vfs::remove_dir_all(&blob_folder).await?;
            }
        }

        #[cfg(feature = "audit")]
        {
            let account_event = AccountEvent::DeleteFolder(*id);
            let audit_event: AuditEvent =
                (self.account_id(), &account_event).into();
            append_audit_events(&[audit_event]).await?;
        }

        Ok(())
    }

    async fn rename_folder(
        &mut self,
        id: &VaultId,
        name: &str,
    ) -> Result<()> {
        let mut access = VaultWriter::new(self.target.clone(), id);
        access.set_vault_name(name.to_owned()).await?;

        #[cfg(feature = "audit")]
        {
            let account_event =
                AccountEvent::RenameFolder(*id, name.to_owned());
            let audit_event: AuditEvent =
                (self.account_id(), &account_event).into();
            append_audit_events(&[audit_event]).await?;
        }

        Ok(())
    }

    async fn delete_account(&mut self) -> Result<()> {
        // Remove all account data from the database
        let account_id = self.account_id.clone();
        self.client
            .conn(move |conn| {
                let account = AccountEntity::new(&conn);
                account.delete_account(&account_id)
            })
            .await
            .map_err(sos_database::Error::from)?;

        // Delete all file blobs for the account
        let blobs_dir = self.paths.into_files_dir();
        if vfs::try_exists(&blobs_dir).await? {
            vfs::remove_dir_all(&blobs_dir).await?;
        }

        Ok(())
    }
}

#[async_trait]
impl StorageEventLogs for ServerDatabaseStorage {
    type Error = Error;

    async fn identity_log(&self) -> Result<Arc<RwLock<FolderEventLog>>> {
        Ok(self.identity_log.clone())
    }

    async fn account_log(&self) -> Result<Arc<RwLock<AccountEventLog>>> {
        Ok(self.account_log.clone())
    }

    async fn device_log(&self) -> Result<Arc<RwLock<DeviceEventLog>>> {
        Ok(self.device_log.clone())
    }

    #[cfg(feature = "files")]
    async fn file_log(&self) -> Result<Arc<RwLock<FileEventLog>>> {
        Ok(self.file_log.clone())
    }

    async fn folder_details(&self) -> Result<IndexSet<Summary>> {
        let ids = self.folders.keys().copied().collect::<Vec<_>>();
        let mut output = IndexSet::new();
        // TODO: we could use a find_many() with "IN (id1, id2, ..)" here
        for id in ids {
            let row = self
                .client
                .conn(move |conn| {
                    let folder = FolderEntity::new(&conn);
                    Ok(folder.find_one(&id)?)
                })
                .await?;
            let record = FolderRecord::from_row(row).await?;
            output.insert(record.summary);
        }
        Ok(output)
    }

    async fn folder_log(
        &self,
        id: &VaultId,
    ) -> Result<Arc<RwLock<FolderEventLog>>> {
        Ok(Arc::clone(
            self.folders
                .get(id)
                .ok_or(sos_backend::StorageError::FolderNotFound(*id))?,
        ))
    }
}