Skip to content

Commit fffc900

Browse files
committed
crypto: Add a backup_version argument to group session backup methods
1 parent 67615fe commit fffc900

File tree

7 files changed

+71
-23
lines changed

7 files changed

+71
-23
lines changed

crates/matrix-sdk-crypto/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ Breaking changes:
1717
- Add new `dehydrated` property to `olm::account::PickledAccount`.
1818
([#3164](https://github.com/matrix-org/matrix-rust-sdk/pull/3164))
1919

20+
- Add a `backup_version` argument to `CryptoStore`'s
21+
`inbound_group_sessions_for_backup` and
22+
`mark_inbound_group_sessions_as_backed_up` methods.
23+
2024
Additions:
2125

2226
- Log more details about the Olm session after encryption and decryption.

crates/matrix-sdk-crypto/src/backups/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,12 @@ impl BackupMachine {
476476

477477
trace!(request_id = ?r.request_id, keys = ?r.sessions, "Marking room keys as backed up");
478478

479-
self.store.mark_inbound_group_sessions_as_backed_up(&room_and_session_ids).await?;
479+
self.store
480+
.mark_inbound_group_sessions_as_backed_up(
481+
&r.request.version,
482+
&room_and_session_ids,
483+
)
484+
.await?;
480485

481486
trace!(
482487
request_id = ?r.request_id,
@@ -514,7 +519,7 @@ impl BackupMachine {
514519
};
515520

516521
let sessions =
517-
self.store.inbound_group_sessions_for_backup(Self::BACKUP_BATCH_SIZE).await?;
522+
self.store.inbound_group_sessions_for_backup(&version, Self::BACKUP_BATCH_SIZE).await?;
518523

519524
if sessions.is_empty() {
520525
trace!(?backup_key, "No room keys need to be backed up");

crates/matrix-sdk-crypto/src/store/integration_tests.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ macro_rules! cryptostore_integration_tests {
326326
assert_eq!(store.inbound_group_session_counts().await.unwrap().total, 1);
327327
assert_eq!(store.inbound_group_session_counts().await.unwrap().backed_up, 0);
328328

329-
let to_back_up = store.inbound_group_sessions_for_backup(1).await.unwrap();
329+
let to_back_up = store.inbound_group_sessions_for_backup("bkpver", 1).await.unwrap();
330330
assert_eq!(to_back_up, vec![session])
331331
}
332332

@@ -342,14 +342,10 @@ macro_rules! cryptostore_integration_tests {
342342
}
343343
let changes = Changes { inbound_group_sessions: sessions.clone(), ..Default::default() };
344344
store.save_changes(changes).await.expect("Can't save group session");
345-
assert_eq!(store.inbound_group_sessions_for_backup(100).await.unwrap().len(), 10);
346-
347-
fn session_info(session: &InboundGroupSession) -> (&RoomId, &str) {
348-
(&session.room_id(), &session.session_id())
349-
}
345+
assert_eq!(store.inbound_group_sessions_for_backup("bkpver", 100).await.unwrap().len(), 10);
350346

351347
// When I mark some as backed up
352-
store.mark_inbound_group_sessions_as_backed_up(&[
348+
store.mark_inbound_group_sessions_as_backed_up("bkpver", &[
353349
session_info(&sessions[1]),
354350
session_info(&sessions[3]),
355351
session_info(&sessions[5]),
@@ -358,7 +354,7 @@ macro_rules! cryptostore_integration_tests {
358354
]).await.expect("Failed to mark sessions as backed up");
359355

360356
// And ask which still need backing up
361-
let to_back_up = store.inbound_group_sessions_for_backup(10).await.unwrap();
357+
let to_back_up = store.inbound_group_sessions_for_backup("bkpver", 10).await.unwrap();
362358
let needs_backing_up = |i: usize| to_back_up.iter().any(|s| s.session_id() == sessions[i].session_id());
363359

364360
// Then the sessions we said were backed up no longer need backing up
@@ -386,22 +382,30 @@ macro_rules! cryptostore_integration_tests {
386382
let room_id = &room_id!("!test:localhost");
387383
let (_, session) = account.create_group_session_pair_with_defaults(room_id).await;
388384

389-
session.mark_as_backed_up();
390-
391385
let changes =
392386
Changes { inbound_group_sessions: vec![session.clone()], ..Default::default() };
393387

394388
store.save_changes(changes).await.expect("Can't save group session");
395389

390+
// Given we have backed up our session
391+
store
392+
.mark_inbound_group_sessions_as_backed_up("bkpver1", &[session_info(&session)])
393+
.await
394+
.expect("Failed to mark_inbound_group_sessions_as_backed_up.");
395+
396396
assert_eq!(store.inbound_group_session_counts().await.unwrap().total, 1);
397397
assert_eq!(store.inbound_group_session_counts().await.unwrap().backed_up, 1);
398398

399-
let to_back_up = store.inbound_group_sessions_for_backup(1).await.unwrap();
399+
// Sanity: before resetting, we have nothing to back up
400+
let to_back_up = store.inbound_group_sessions_for_backup("bkpver1", 1).await.unwrap();
400401
assert_eq!(to_back_up, vec![]);
401402

403+
// When we reset the backup
402404
store.reset_backup_state().await.unwrap();
403405

404-
let to_back_up = store.inbound_group_sessions_for_backup(1).await.unwrap();
406+
// Then after resetting, even if we supply the same backup version number, we need
407+
// to back up the session
408+
let to_back_up = store.inbound_group_sessions_for_backup("bkpver1", 1).await.unwrap();
405409
assert_eq!(to_back_up, vec![session]);
406410
}
407411

@@ -960,6 +964,10 @@ macro_rules! cryptostore_integration_tests {
960964
let loaded_2 = store.get_custom_value("B").await.unwrap();
961965
assert_eq!(None, loaded_2);
962966
}
967+
968+
fn session_info(session: &InboundGroupSession) -> (&RoomId, &str) {
969+
(&session.room_id(), &session.session_id())
970+
}
963971
}
964972
};
965973
}

crates/matrix-sdk-crypto/src/store/memorystore.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ impl CryptoStore for MemoryStore {
279279

280280
async fn inbound_group_sessions_for_backup(
281281
&self,
282+
_backup_version: &str,
282283
limit: usize,
283284
) -> Result<Vec<InboundGroupSession>> {
284285
Ok(self
@@ -292,6 +293,7 @@ impl CryptoStore for MemoryStore {
292293

293294
async fn mark_inbound_group_sessions_as_backed_up(
294295
&self,
296+
_backup_version: &str,
295297
room_and_session_ids: &[(&RoomId, &str)],
296298
) -> Result<()> {
297299
for (room_id, session_id) in room_and_session_ids {
@@ -757,16 +759,20 @@ mod integration_tests {
757759

758760
async fn inbound_group_sessions_for_backup(
759761
&self,
762+
backup_version: &str,
760763
limit: usize,
761764
) -> Result<Vec<InboundGroupSession>, Self::Error> {
762-
self.0.inbound_group_sessions_for_backup(limit).await
765+
self.0.inbound_group_sessions_for_backup(backup_version, limit).await
763766
}
764767

765768
async fn mark_inbound_group_sessions_as_backed_up(
766769
&self,
770+
backup_version: &str,
767771
room_and_session_ids: &[(&RoomId, &str)],
768772
) -> Result<(), Self::Error> {
769-
self.0.mark_inbound_group_sessions_as_backed_up(room_and_session_ids).await
773+
self.0
774+
.mark_inbound_group_sessions_as_backed_up(backup_version, room_and_session_ids)
775+
.await
770776
}
771777

772778
async fn reset_backup_state(&self) -> Result<(), Self::Error> {

crates/matrix-sdk-crypto/src/store/traits.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,38 @@ pub trait CryptoStore: AsyncTraitDeps {
107107
/// backed up.
108108
async fn inbound_group_session_counts(&self) -> Result<RoomKeyCounts, Self::Error>;
109109

110-
/// Get all the inbound group sessions we have not backed up yet.
110+
/// Return a batch of ['InboundGroupSession'] ("room keys") that have not
111+
/// yet been backed up in the supplied backup version.
112+
///
113+
/// The size of the returned `Vec` is <= `limit`.
114+
///
115+
/// Note: some implementations ignore `backup_version` and assume the
116+
/// current backup version, which is normally the same.
111117
async fn inbound_group_sessions_for_backup(
112118
&self,
119+
backup_version: &str,
113120
limit: usize,
114121
) -> Result<Vec<InboundGroupSession>, Self::Error>;
115122

116-
/// Mark the inbound group sessions with the supplied room and session IDs
117-
/// as backed up
123+
/// Store the fact that the supplied sessions were backed up into the backup
124+
/// with version `backup_version`.
125+
///
126+
/// Note: some implementations ignore `backup_version` and assume the
127+
/// current backup version, which is normally the same.
118128
async fn mark_inbound_group_sessions_as_backed_up(
119129
&self,
130+
backup_version: &str,
120131
room_and_session_ids: &[(&RoomId, &str)],
121132
) -> Result<(), Self::Error>;
122133

123134
/// Reset the backup state of all the stored inbound group sessions.
135+
///
136+
/// Note: this is mostly implemented by stores that ignore the
137+
/// `backup_version` argument on `inbound_group_sessions_for_backup` and
138+
/// `mark_inbound_group_sessions_as_backed_up`. Implementations that
139+
/// pay attention to the supplied backup version probably don't need to
140+
/// update their storage when the current backup version changes, so have
141+
/// empty implementations of this method.
124142
async fn reset_backup_state(&self) -> Result<(), Self::Error>;
125143

126144
/// Get the backup keys we have stored.
@@ -331,20 +349,21 @@ impl<T: CryptoStore> CryptoStore for EraseCryptoStoreError<T> {
331349
async fn inbound_group_session_counts(&self) -> Result<RoomKeyCounts> {
332350
self.0.inbound_group_session_counts().await.map_err(Into::into)
333351
}
334-
335352
async fn inbound_group_sessions_for_backup(
336353
&self,
354+
backup_version: &str,
337355
limit: usize,
338356
) -> Result<Vec<InboundGroupSession>> {
339-
self.0.inbound_group_sessions_for_backup(limit).await.map_err(Into::into)
357+
self.0.inbound_group_sessions_for_backup(backup_version, limit).await.map_err(Into::into)
340358
}
341359

342360
async fn mark_inbound_group_sessions_as_backed_up(
343361
&self,
362+
backup_version: &str,
344363
room_and_session_ids: &[(&RoomId, &str)],
345364
) -> Result<()> {
346365
self.0
347-
.mark_inbound_group_sessions_as_backed_up(room_and_session_ids)
366+
.mark_inbound_group_sessions_as_backed_up(backup_version, room_and_session_ids)
348367
.await
349368
.map_err(Into::into)
350369
}

crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ impl_crypto_store! {
889889

890890
async fn inbound_group_sessions_for_backup(
891891
&self,
892+
_backup_version: &str,
892893
limit: usize,
893894
) -> Result<Vec<InboundGroupSession>> {
894895
let tx = self
@@ -933,7 +934,10 @@ impl_crypto_store! {
933934
Ok(result)
934935
}
935936

936-
async fn mark_inbound_group_sessions_as_backed_up(&self, room_and_session_ids: &[(&RoomId, &str)]) -> Result<()> {
937+
async fn mark_inbound_group_sessions_as_backed_up(&self,
938+
_backup_version: &str,
939+
room_and_session_ids: &[(&RoomId, &str)]
940+
) -> Result<()> {
937941
let tx = self
938942
.inner
939943
.transaction_on_one_with_mode(

crates/matrix-sdk-sqlite/src/crypto_store.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ impl CryptoStore for SqliteCryptoStore {
946946

947947
async fn inbound_group_sessions_for_backup(
948948
&self,
949+
_backup_version: &str,
949950
limit: usize,
950951
) -> Result<Vec<InboundGroupSession>> {
951952
self.acquire()
@@ -962,6 +963,7 @@ impl CryptoStore for SqliteCryptoStore {
962963

963964
async fn mark_inbound_group_sessions_as_backed_up(
964965
&self,
966+
_backup_version: &str,
965967
session_ids: &[(&RoomId, &str)],
966968
) -> Result<()> {
967969
Ok(self

0 commit comments

Comments
 (0)