-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcertificates.rs
734 lines (656 loc) · 25.5 KB
/
certificates.rs
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS
//! Certificate storage relies on the upper layer to do the actual certification
//! validation work and takes care of handling concurrency issues.
//!
//! Hence no unique violation should occur under normal circumstances here.
//!
//! On top of that, storage is only responsible for storing/fetching encrypted items:
//! - No cache is handled at this level: better let the higher level components
//! do that since they have a better idea of what should be cached.
//! - No encryption/serialization: simplify code (no need for type dispatching)
//! and testing (no need to build valid certificates objects.
use paste::paste;
use std::{collections::HashMap, path::Path};
use libparsec_types::prelude::*;
use crate::platform::certificates::{
PlatformCertificatesStorage, PlatformCertificatesStorageForUpdateGuard,
};
#[derive(Debug, Clone, Copy)]
pub enum UpTo {
Current,
Timestamp(DateTime),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PerTopicLastTimestamps {
pub common: Option<DateTime>,
pub sequester: Option<DateTime>,
pub realm: HashMap<VlobID, DateTime>,
pub shamir_recovery: Option<DateTime>,
}
impl PerTopicLastTimestamps {
pub fn is_empty(&self) -> bool {
self.common.is_none()
&& self.sequester.is_none()
&& self.realm.is_empty()
&& self.shamir_recovery.is_none()
}
pub fn new_for_common(common_timestamp: DateTime) -> Self {
Self {
common: Some(common_timestamp),
sequester: None,
realm: HashMap::default(),
shamir_recovery: None,
}
}
pub fn new_for_realm(realm_id: VlobID, realm_timestamp: DateTime) -> Self {
Self {
common: None,
sequester: None,
realm: HashMap::from([(realm_id, realm_timestamp)]),
shamir_recovery: None,
}
}
pub fn new_for_common_and_realm(
common_timestamp: DateTime,
realm_id: VlobID,
realm_timestamp: DateTime,
) -> Self {
Self {
common: Some(common_timestamp),
sequester: None,
realm: HashMap::from([(realm_id, realm_timestamp)]),
shamir_recovery: None,
}
}
}
impl PerTopicLastTimestamps {
/// Ensure we know about all topics specified in other and we have a younger or
/// equal timestamp on each one of them.
pub fn is_up_to_date(&self, other: &PerTopicLastTimestamps) -> bool {
if self.common < other.common
|| self.sequester < other.sequester
|| self.shamir_recovery < other.shamir_recovery
{
return false;
}
for (realm_id, other_realm_timestamp) in other.realm.iter() {
match self.realm.get(realm_id) {
// We don't know about this realm
None => return false,
// We are not up to date with this realm
Some(self_realm_timestamp) if self_realm_timestamp < other_realm_timestamp => {
return false
}
_ => (),
}
}
true
}
}
pub trait StorableCertificate {
/// Values for `certificate_type` column in `certificates` table
/// Note the fact their value is similar to the `type` field in certificates, this
/// is purely for simplicity as the two are totally decorrelated.
const TYPE: &'static str;
fn filters(&self) -> (FilterKind, FilterKind);
fn timestamp(&self) -> DateTime;
}
#[derive(Debug, Clone)]
pub enum FilterKind<'a> {
Null,
Bytes(&'a [u8]),
U64([u8; 8]),
}
impl FilterKind<'_> {
pub fn from_u64(value: u64) -> Self {
Self::U64(value.to_le_bytes())
}
}
/// Certificates are grouped into topic, each of which being provided by the server
/// with their timestamp strictly growing.
/// We need this group of information in the storage so that we can retrieve
/// the last timestamp of each topic when polling the server for new ones.
pub(super) trait StorableCertificateTopic {
const TYPES: &'static [&'static str];
/// This method is not meant to be executed, instead it acts as a canary by making
/// the compilation fails if a new certificate is added to a topic without updating
/// the implementation of `StorableCertificateTopic` trait for said topic.
#[cfg(test)]
fn topic_certifs_correspondance_canary(certif: Self);
}
macro_rules! impl_storable_certificate_topic {
($topic_struct:ident, [ $( $certif:ident ),* $(,)? ]) => {
impl StorableCertificateTopic for $topic_struct {
const TYPES: &'static [&'static str] = &[
$(
<paste!{ [< $certif Certificate >] } as StorableCertificate>::TYPE,
)*
];
#[cfg(test)]
fn topic_certifs_correspondance_canary(certif: Self) {
match certif {
$(
$topic_struct::$certif(_) => (),
)*
}
}
}
};
}
impl_storable_certificate_topic!(
CommonTopicArcCertificate,
[User, Device, RevokedUser, UserUpdate,]
);
impl_storable_certificate_topic!(
SequesterTopicArcCertificate,
[
SequesterAuthority,
SequesterService,
SequesterRevokedService
]
);
impl_storable_certificate_topic!(
RealmTopicArcCertificate,
[RealmRole, RealmName, RealmKeyRotation, RealmArchiving]
);
impl_storable_certificate_topic!(
ShamirRecoveryTopicArcCertificate,
[ShamirRecoveryBrief, ShamirRecoveryShare]
);
impl StorableCertificate for UserCertificate {
const TYPE: &'static str = "user_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
let filter1 = FilterKind::Bytes(self.user_id.as_bytes());
let filter2 = FilterKind::Null;
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
impl StorableCertificate for DeviceCertificate {
const TYPE: &'static str = "device_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
let filter1 = FilterKind::Bytes(self.device_id.as_bytes());
let filter2 = FilterKind::Bytes(self.user_id.as_bytes());
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
impl StorableCertificate for RevokedUserCertificate {
const TYPE: &'static str = "revoked_user_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
let filter1 = FilterKind::Bytes(self.user_id.as_bytes());
let filter2 = FilterKind::Null;
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
impl StorableCertificate for UserUpdateCertificate {
const TYPE: &'static str = "user_update_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
let filter1 = FilterKind::Bytes(self.user_id.as_bytes());
let filter2 = FilterKind::Null;
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
impl StorableCertificate for RealmRoleCertificate {
const TYPE: &'static str = "realm_role_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
let filter1 = FilterKind::Bytes(self.realm_id.as_bytes());
let filter2 = FilterKind::Bytes(self.user_id.as_bytes());
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
impl StorableCertificate for RealmNameCertificate {
const TYPE: &'static str = "realm_name_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
let filter1 = FilterKind::Bytes(self.realm_id.as_bytes());
let filter2 = FilterKind::Null;
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
impl StorableCertificate for RealmKeyRotationCertificate {
const TYPE: &'static str = "realm_key_rotation_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
let filter1 = FilterKind::Bytes(self.realm_id.as_bytes());
let filter2 = FilterKind::from_u64(self.key_index);
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
impl StorableCertificate for RealmArchivingCertificate {
const TYPE: &'static str = "realm_archiving_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
let filter1 = FilterKind::Bytes(self.realm_id.as_bytes());
let filter2 = FilterKind::Null;
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
impl StorableCertificate for SequesterAuthorityCertificate {
const TYPE: &'static str = "sequester_authority_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
// No filter is needed as there is a most one authority certificate
let filter1 = FilterKind::Null;
let filter2 = FilterKind::Null;
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
impl StorableCertificate for SequesterServiceCertificate {
const TYPE: &'static str = "sequester_service_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
let filter1 = FilterKind::Bytes(self.service_id.as_bytes());
let filter2 = FilterKind::Null;
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
impl StorableCertificate for SequesterRevokedServiceCertificate {
const TYPE: &'static str = "sequester_revoked_service_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
let filter1 = FilterKind::Bytes(self.service_id.as_bytes());
let filter2 = FilterKind::Null;
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
impl StorableCertificate for ShamirRecoveryBriefCertificate {
const TYPE: &'static str = "shamir_recovery_brief_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
let filter1 = FilterKind::Bytes(self.user_id.as_bytes());
let filter2 = FilterKind::Null;
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
impl StorableCertificate for ShamirRecoveryShareCertificate {
const TYPE: &'static str = "shamir_recovery_share_certificate";
fn filters(&self) -> (FilterKind, FilterKind) {
let filter1 = FilterKind::Bytes(self.user_id.as_bytes());
let filter2 = FilterKind::Bytes(self.recipient.as_bytes());
(filter1, filter2)
}
fn timestamp(&self) -> DateTime {
self.timestamp
}
}
#[derive(Debug, thiserror::Error)]
pub enum GetCertificateError {
#[error("Certificate doesn't exist")]
NonExisting,
#[error("Certificate exists but is more recent than the provided timestamp")]
ExistButTooRecent { certificate_timestamp: DateTime },
#[error(transparent)]
Internal(#[from] anyhow::Error),
}
#[derive(Debug, Clone)]
pub enum GetCertificateQuery<'a> {
/// SELECT * FROM certificates
NoFilter { certificate_type: &'static str },
/// SELECT * FROM certificates WHERE type = ? AND filter1 = ?
Filter1 {
certificate_type: &'static str,
filter1: FilterKind<'a>,
},
/// SELECT * FROM certificates WHERE type = ? AND filter2 = ?
Filter2 {
certificate_type: &'static str,
filter2: FilterKind<'a>,
},
/// SELECT * FROM certificates WHERE type = ? AND filter1 = ? AND filter2 = ?
BothFilters {
certificate_type: &'static str,
filter1: FilterKind<'a>,
filter2: FilterKind<'a>,
},
/// SELECT * FROM certificates WHERE type = ? AND filter1 = (SELECT filter2 FROM certificates WHERE type = ? AND filter1 = ?)
Filter1EqFilter2WhereFilter1 {
certificate_type: &'static str,
subquery_certificate_type: &'static str,
filter1: FilterKind<'a>,
},
/// SELECT * FROM certificates WHERE type = ? AND filter1 = (SELECT filter1 FROM certificates WHERE type = ? AND filter2 = ?)
Filter1EqFilter1WhereFilter2 {
certificate_type: &'static str,
subquery_certificate_type: &'static str,
filter2: FilterKind<'a>,
},
/// SELECT * FROM certificates WHERE type = ? AND filter2 = (SELECT filter1 FROM certificates WHERE type = ? AND filter2 = ?)
Filter2EqFilter1WhereFilter2 {
certificate_type: &'static str,
subquery_certificate_type: &'static str,
filter2: FilterKind<'a>,
},
/// SELECT * FROM certificates WHERE type = ? AND filter2 = (SELECT filter2 FROM certificates WHERE type = ? AND filter1 = ?)
Filter2EqFilter2WhereFilter1 {
certificate_type: &'static str,
subquery_certificate_type: &'static str,
filter1: FilterKind<'a>,
},
}
impl<'a> GetCertificateQuery<'a> {
/// Get all users
pub fn users_certificates() -> Self {
Self::NoFilter {
certificate_type: <UserCertificate as StorableCertificate>::TYPE,
}
}
pub fn user_certificate(user_id: &'a UserID) -> Self {
Self::Filter1 {
certificate_type: <UserCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(user_id.as_bytes()),
}
}
pub fn user_certificate_from_device_id(device_id: &'a DeviceID) -> Self {
Self::Filter1EqFilter2WhereFilter1 {
certificate_type: <UserCertificate as StorableCertificate>::TYPE,
subquery_certificate_type: <DeviceCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(device_id.as_bytes()),
}
}
pub fn revoked_user_certificate(user_id: &'a UserID) -> Self {
Self::Filter1 {
certificate_type: <RevokedUserCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(user_id.as_bytes()),
}
}
pub fn revoked_user_certificate_from_device_id(device_id: &'a DeviceID) -> Self {
Self::Filter1EqFilter2WhereFilter1 {
certificate_type: <RevokedUserCertificate as StorableCertificate>::TYPE,
subquery_certificate_type: <DeviceCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(device_id.as_bytes()),
}
}
/// Get all user update certificates for a given user
pub fn user_update_certificates(user_id: &'a UserID) -> Self {
Self::Filter1 {
certificate_type: <UserUpdateCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(user_id.as_bytes()),
}
}
/// Get all user update certificates for a given user
pub fn user_update_certificates_from_device_id(device_id: &'a DeviceID) -> Self {
Self::Filter1EqFilter2WhereFilter1 {
certificate_type: <UserUpdateCertificate as StorableCertificate>::TYPE,
subquery_certificate_type: <DeviceCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(device_id.as_bytes()),
}
}
pub fn device_certificate(device_id: &'a DeviceID) -> Self {
Self::Filter1 {
certificate_type: <DeviceCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(device_id.as_bytes()),
}
}
/// Get all device certificates for a given user
pub fn user_devices_certificates(user_id: &'a UserID) -> Self {
Self::Filter2 {
certificate_type: <DeviceCertificate as StorableCertificate>::TYPE,
filter2: FilterKind::Bytes(user_id.as_bytes()),
}
}
pub fn realm_role_certificate(realm_id: &'a VlobID, user_id: &'a UserID) -> Self {
Self::BothFilters {
certificate_type: <RealmRoleCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(realm_id.as_bytes()),
filter2: FilterKind::Bytes(user_id.as_bytes()),
}
}
/// Get all realm name certificates for a given realm
pub fn realm_name_certificates(realm_id: &'a VlobID) -> Self {
Self::Filter1 {
certificate_type: <RealmNameCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(realm_id.as_bytes()),
}
}
pub fn realm_key_rotation_certificate(realm_id: &'a VlobID, key_index: IndexInt) -> Self {
Self::BothFilters {
certificate_type: <RealmKeyRotationCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(realm_id.as_bytes()),
filter2: FilterKind::from_u64(key_index),
}
}
/// Get all realm name certificates for a given realm
pub fn realm_key_rotation_certificates(realm_id: &'a VlobID) -> Self {
Self::Filter1 {
certificate_type: <RealmKeyRotationCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(realm_id.as_bytes()),
}
}
/// Get all realm archiving certificates for a given realm
pub fn realm_archiving_certificates(realm_id: &'a VlobID) -> Self {
Self::Filter1 {
certificate_type: <RealmArchivingCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(realm_id.as_bytes()),
}
}
/// Get all realm role certificates for a given realm
pub fn realm_role_certificates(realm_id: &'a VlobID) -> Self {
Self::Filter1 {
certificate_type: <RealmRoleCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(realm_id.as_bytes()),
}
}
/// Get all realm role certificates for a given user
pub fn user_realm_role_certificates(user_id: &'a UserID) -> Self {
Self::Filter2 {
certificate_type: <RealmRoleCertificate as StorableCertificate>::TYPE,
filter2: FilterKind::Bytes(user_id.as_bytes()),
}
}
/// Get all shamir recovery brief certificates we know about
pub fn shamir_recovery_brief_certificates() -> Self {
Self::NoFilter {
certificate_type: <ShamirRecoveryBriefCertificate as StorableCertificate>::TYPE,
}
}
/// Get all shamir recovery share certificates we know about
pub fn shamir_recovery_share_certificates() -> Self {
Self::NoFilter {
certificate_type: <ShamirRecoveryShareCertificate as StorableCertificate>::TYPE,
}
}
/// Get all shamir recovery brief certificates for a given user
pub fn user_shamir_recovery_brief_certificates(user_id: &'a UserID) -> Self {
Self::Filter1 {
certificate_type: <ShamirRecoveryBriefCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(user_id.as_bytes()),
}
}
/// Get the recipient's shamir recovery share certificate to recover author
pub fn user_recipient_shamir_recovery_share_certificates(
user_id: &'a UserID,
recipient: &'a UserID,
) -> Self {
Self::BothFilters {
certificate_type: <ShamirRecoveryShareCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(user_id.as_bytes()),
filter2: FilterKind::Bytes(recipient.as_bytes()),
}
}
pub fn sequester_authority_certificate() -> Self {
// No filter is needed as there is a most one authority certificate
Self::NoFilter {
certificate_type: <SequesterAuthorityCertificate as StorableCertificate>::TYPE,
}
}
pub fn sequester_service_certificate(service_id: &'a SequesterServiceID) -> Self {
Self::Filter1 {
certificate_type: <SequesterServiceCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(service_id.as_bytes()),
}
}
// Get all sequester service certificates
pub fn sequester_service_certificates() -> Self {
Self::NoFilter {
certificate_type: <SequesterServiceCertificate as StorableCertificate>::TYPE,
}
}
pub fn sequester_revoked_service_certificate(service_id: &'a SequesterServiceID) -> Self {
Self::Filter1 {
certificate_type: <SequesterRevokedServiceCertificate as StorableCertificate>::TYPE,
filter1: FilterKind::Bytes(service_id.as_bytes()),
}
}
// Get all sequester service certificates
pub fn sequester_revoked_service_certificates() -> Self {
Self::NoFilter {
certificate_type: <SequesterRevokedServiceCertificate as StorableCertificate>::TYPE,
}
}
}
#[derive(Debug)]
pub struct CertificatesStorage {
platform: PlatformCertificatesStorage,
}
impl CertificatesStorage {
pub async fn start(data_base_dir: &Path, device: &LocalDevice) -> anyhow::Result<Self> {
// `maybe_populate_certificate_storage` needs to start a `CertificatesStorage`,
// leading to a recursive call which is not supported for async functions.
// Hence `no_populate_start` which breaks the recursion.
//
// Also note we don't try to return the `CertificatesStorage` that has been
// used during the populate as it would change the internal state of the
// storage (typically caches) depending of if populate has been needed or not.
#[cfg(feature = "test-with-testbed")]
crate::testbed::maybe_populate_certificate_storage(data_base_dir, device).await;
Self::no_populate_start(data_base_dir, device).await
}
pub(crate) async fn no_populate_start(
data_base_dir: &Path,
device: &LocalDevice,
) -> anyhow::Result<Self> {
let platform =
PlatformCertificatesStorage::no_populate_start(data_base_dir, device).await?;
Ok(Self { platform })
}
pub async fn stop(self) -> anyhow::Result<()> {
self.platform.stop().await
}
pub async fn for_update(&mut self) -> anyhow::Result<CertificatesStorageUpdater> {
self.platform
.for_update()
.await
.map(|platform| CertificatesStorageUpdater { platform })
}
/// Return the last certificate timestamp we know about for the given topic, or `None`
/// if the storage is empty regarding this topic (and the topics it depends on).
pub async fn get_last_timestamps(&mut self) -> anyhow::Result<PerTopicLastTimestamps> {
self.platform.get_last_timestamps().await
}
/// Note if multiple results are possible, the latest certificate is kept
pub async fn get_certificate_encrypted<'a>(
&mut self,
query: GetCertificateQuery<'a>,
up_to: UpTo,
) -> Result<(DateTime, Vec<u8>), GetCertificateError> {
self.platform.get_certificate_encrypted(query, up_to).await
}
/// Certificates are returned ordered by timestamp in increasing order (i.e. oldest first)
pub async fn get_multiple_certificates_encrypted<'a>(
&mut self,
query: GetCertificateQuery<'a>,
up_to: UpTo,
offset: Option<u32>,
limit: Option<u32>,
) -> anyhow::Result<Vec<(DateTime, Vec<u8>)>> {
self.platform
.get_multiple_certificates_encrypted(query, up_to, offset, limit)
.await
}
/// Only used for debugging tests
#[allow(unused)]
pub async fn debug_dump(&mut self) -> anyhow::Result<String> {
self.platform.debug_dump().await
}
}
#[derive(Debug)]
pub struct CertificatesStorageUpdater<'a> {
platform: PlatformCertificatesStorageForUpdateGuard<'a>,
}
impl<'a> CertificatesStorageUpdater<'a> {
/// Finish the update and commit the underlying transaction to database.
/// If the guard is dropped without calling this method, the transaction
/// is rollback.
pub async fn commit(self) -> anyhow::Result<()> {
self.platform.commit().await
}
/// Remove all certificates from the database
/// There is no data loss from this as certificates can be re-obtained from
/// the server, however it is only needed when switching from/to redacted
/// certificates
pub async fn forget_all_certificates(&mut self) -> anyhow::Result<()> {
self.platform.forget_all_certificates().await
}
/// Add a (already validated) certificate.
pub async fn add_certificate<C: StorableCertificate>(
&mut self,
certif: &C,
encrypted: Vec<u8>,
) -> anyhow::Result<()> {
let (filter1, filter2) = certif.filters();
self.platform
.add_certificate(C::TYPE, filter1, filter2, certif.timestamp(), encrypted)
.await
}
/// Return the last certificate timestamp we know about for the given topic, or `None`
/// if the storage is empty regarding this topic (and the topics it depends on).
pub async fn get_last_timestamps(&mut self) -> anyhow::Result<PerTopicLastTimestamps> {
self.platform.get_last_timestamps().await
}
/// Note if multiple results are possible, the latest certificate is kept
pub async fn get_certificate_encrypted<'b>(
&mut self,
query: GetCertificateQuery<'b>,
up_to: UpTo,
) -> Result<(DateTime, Vec<u8>), GetCertificateError> {
self.platform.get_certificate_encrypted(query, up_to).await
}
/// Certificates are returned ordered by timestamp in increasing order (i.e. oldest first)
pub async fn get_multiple_certificates_encrypted<'b>(
&mut self,
query: GetCertificateQuery<'b>,
up_to: UpTo,
offset: Option<u32>,
limit: Option<u32>,
) -> anyhow::Result<Vec<(DateTime, Vec<u8>)>> {
self.platform
.get_multiple_certificates_encrypted(query, up_to, offset, limit)
.await
}
/// Only used for debugging tests
#[allow(unused)]
pub async fn debug_dump(&mut self) -> anyhow::Result<String> {
self.platform.debug_dump().await
}
}
#[cfg(test)]
#[path = "../tests/unit/certificates.rs"]
mod test;