Skip to content

Commit

Permalink
Fix regenerate_olm_machine loosing backup state
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr committed Dec 20, 2023
1 parent 320b868 commit 71136e4
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions crates/matrix-sdk/src/encryption/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,10 @@ impl Encryption {
drop(olm_machine_guard);
// Recreate the OlmMachine.
self.client.base_client().regenerate_olm().await?;
// we need to trigger that so it gets back the cached key if known
// otherwise the new olm machine `BackupMachine#backup_key` will be out of sync
// and say backup is disabled.
self.client.encryption().backups().setup_and_resume().await?;
}
}
Ok(())
Expand Down Expand Up @@ -1452,6 +1456,21 @@ mod tests {
let initial_olm_machine =
client1.olm_machine().await.clone().expect("must have an olm machine");

// Also enable backup to check that new machine has the same backup keys.
let decryption_key = matrix_sdk_base::crypto::store::BackupDecryptionKey::new()
.expect("Can't create new recovery key");
let backup_key = decryption_key.megolm_v1_public_key();
backup_key.set_version("1".to_owned());
initial_olm_machine
.backup_machine()
.save_decryption_key(Some(decryption_key.to_owned()), Some("1".to_owned()))
.await
.expect("Should save");

initial_olm_machine.backup_machine().enable_backup_v1(backup_key.clone()).await.unwrap();

assert!(client1.encryption().backups().are_enabled().await);

// The other client can't take the lock too.
let acquired2 = client2.encryption().try_lock_store_once().await.unwrap();
assert!(acquired2.is_none());
Expand Down Expand Up @@ -1486,7 +1505,15 @@ mod tests {

// But now its olm machine has been invalidated and thus regenerated!
let olm_machine = client1.olm_machine().await.clone().expect("must have an olm machine");

let backup_key_new = olm_machine.backup_machine().get_backup_keys().await.unwrap();
assert!(!initial_olm_machine.same_as(&olm_machine));
assert!(backup_key_new.decryption_key.is_some());
assert_eq!(
backup_key_new.decryption_key.unwrap().megolm_v1_public_key().to_base64(),
backup_key.to_base64()
);
assert!(client1.encryption().backups().are_enabled().await);
}

#[cfg(feature = "sqlite")]
Expand Down

0 comments on commit 71136e4

Please sign in to comment.