2020-06-16 10:11:38 +00:00
|
|
|
use crate::{utils, Error, Result};
|
|
|
|
use ruma::{
|
|
|
|
api::client::{
|
|
|
|
error::ErrorKind,
|
2021-02-27 21:09:22 +00:00
|
|
|
r0::backup::{BackupAlgorithm, KeyBackupData, RoomKeyBackup},
|
2020-06-16 10:11:38 +00:00
|
|
|
},
|
2020-07-29 18:28:40 +00:00
|
|
|
RoomId, UserId,
|
2020-06-16 10:11:38 +00:00
|
|
|
};
|
2021-06-08 16:10:00 +00:00
|
|
|
use std::{collections::BTreeMap, convert::TryFrom, sync::Arc};
|
|
|
|
|
|
|
|
use super::abstraction::Tree;
|
2020-06-16 10:11:38 +00:00
|
|
|
|
|
|
|
pub struct KeyBackups {
|
2021-06-08 16:10:00 +00:00
|
|
|
pub(super) backupid_algorithm: Arc<dyn Tree>, // BackupId = UserId + Version(Count)
|
|
|
|
pub(super) backupid_etag: Arc<dyn Tree>, // BackupId = UserId + Version(Count)
|
|
|
|
pub(super) backupkeyid_backup: Arc<dyn Tree>, // BackupKeyId = UserId + Version + RoomId + SessionId
|
2020-06-16 10:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyBackups {
|
|
|
|
pub fn create_backup(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
|
|
|
backup_metadata: &BackupAlgorithm,
|
|
|
|
globals: &super::globals::Globals,
|
|
|
|
) -> Result<String> {
|
|
|
|
let version = globals.next_count()?.to_string();
|
|
|
|
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
2020-06-16 10:11:38 +00:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(&version.as_bytes());
|
|
|
|
|
|
|
|
self.backupid_algorithm.insert(
|
|
|
|
&key,
|
2021-06-08 16:10:00 +00:00
|
|
|
&serde_json::to_vec(backup_metadata).expect("BackupAlgorithm::to_vec always works"),
|
2020-06-16 10:11:38 +00:00
|
|
|
)?;
|
|
|
|
self.backupid_etag
|
|
|
|
.insert(&key, &globals.next_count()?.to_be_bytes())?;
|
|
|
|
Ok(version)
|
|
|
|
}
|
|
|
|
|
2020-08-27 12:48:20 +00:00
|
|
|
pub fn delete_backup(&self, user_id: &UserId, version: &str) -> Result<()> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
2020-08-27 12:48:20 +00:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(&version.as_bytes());
|
|
|
|
|
|
|
|
self.backupid_algorithm.remove(&key)?;
|
|
|
|
self.backupid_etag.remove(&key)?;
|
|
|
|
|
|
|
|
key.push(0xff);
|
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
for (outdated_key, _) in self.backupkeyid_backup.scan_prefix(key) {
|
|
|
|
self.backupkeyid_backup.remove(&outdated_key)?;
|
2020-08-27 12:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-06-16 10:11:38 +00:00
|
|
|
pub fn update_backup(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
|
|
|
version: &str,
|
|
|
|
backup_metadata: &BackupAlgorithm,
|
|
|
|
globals: &super::globals::Globals,
|
|
|
|
) -> Result<String> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
2020-06-16 10:11:38 +00:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(&version.as_bytes());
|
|
|
|
|
|
|
|
if self.backupid_algorithm.get(&key)?.is_none() {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"Tried to update nonexistent backup.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.backupid_algorithm.insert(
|
|
|
|
&key,
|
2021-06-08 16:10:00 +00:00
|
|
|
&serde_json::to_string(backup_metadata)
|
|
|
|
.expect("BackupAlgorithm::to_string always works")
|
|
|
|
.as_bytes(),
|
2020-06-16 10:11:38 +00:00
|
|
|
)?;
|
|
|
|
self.backupid_etag
|
|
|
|
.insert(&key, &globals.next_count()?.to_be_bytes())?;
|
|
|
|
Ok(version.to_string())
|
|
|
|
}
|
|
|
|
|
2021-08-31 17:14:37 +00:00
|
|
|
pub fn get_latest_backup_version(&self, user_id: &UserId) -> Result<Option<String>> {
|
|
|
|
let mut prefix = user_id.as_bytes().to_vec();
|
|
|
|
prefix.push(0xff);
|
|
|
|
let mut last_possible_key = prefix.clone();
|
|
|
|
last_possible_key.extend_from_slice(&u64::MAX.to_be_bytes());
|
|
|
|
|
|
|
|
self.backupid_algorithm
|
|
|
|
.iter_from(&last_possible_key, true)
|
|
|
|
.take_while(move |(k, _)| k.starts_with(&prefix))
|
|
|
|
.next()
|
|
|
|
.map_or(Ok(None), |(key, _)| {
|
|
|
|
utils::string_from_bytes(
|
|
|
|
key.rsplit(|&b| b == 0xff)
|
|
|
|
.next()
|
|
|
|
.expect("rsplit always returns an element"),
|
|
|
|
)
|
|
|
|
.map_err(|_| Error::bad_database("backupid_algorithm key is invalid."))
|
|
|
|
.map(Some)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-16 10:11:38 +00:00
|
|
|
pub fn get_latest_backup(&self, user_id: &UserId) -> Result<Option<(String, BackupAlgorithm)>> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut prefix = user_id.as_bytes().to_vec();
|
2020-06-16 10:11:38 +00:00
|
|
|
prefix.push(0xff);
|
2021-06-08 16:10:00 +00:00
|
|
|
let mut last_possible_key = prefix.clone();
|
|
|
|
last_possible_key.extend_from_slice(&u64::MAX.to_be_bytes());
|
|
|
|
|
2020-06-16 10:11:38 +00:00
|
|
|
self.backupid_algorithm
|
2021-06-08 16:10:00 +00:00
|
|
|
.iter_from(&last_possible_key, true)
|
|
|
|
.take_while(move |(k, _)| k.starts_with(&prefix))
|
|
|
|
.next()
|
|
|
|
.map_or(Ok(None), |(key, value)| {
|
2020-06-16 10:11:38 +00:00
|
|
|
let version = utils::string_from_bytes(
|
|
|
|
key.rsplit(|&b| b == 0xff)
|
|
|
|
.next()
|
|
|
|
.expect("rsplit always returns an element"),
|
|
|
|
)
|
|
|
|
.map_err(|_| Error::bad_database("backupid_algorithm key is invalid."))?;
|
|
|
|
|
|
|
|
Ok(Some((
|
|
|
|
version,
|
|
|
|
serde_json::from_slice(&value).map_err(|_| {
|
|
|
|
Error::bad_database("Algorithm in backupid_algorithm is invalid.")
|
|
|
|
})?,
|
|
|
|
)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_backup(&self, user_id: &UserId, version: &str) -> Result<Option<BackupAlgorithm>> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
2020-06-16 10:11:38 +00:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(version.as_bytes());
|
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
self.backupid_algorithm
|
|
|
|
.get(&key)?
|
|
|
|
.map_or(Ok(None), |bytes| {
|
2021-06-17 18:34:14 +00:00
|
|
|
serde_json::from_slice(&bytes)
|
|
|
|
.map_err(|_| Error::bad_database("Algorithm in backupid_algorithm is invalid."))
|
2021-06-08 16:10:00 +00:00
|
|
|
})
|
2020-06-16 10:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_key(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
|
|
|
version: &str,
|
|
|
|
room_id: &RoomId,
|
|
|
|
session_id: &str,
|
2021-02-27 21:09:22 +00:00
|
|
|
key_data: &KeyBackupData,
|
2020-06-16 10:11:38 +00:00
|
|
|
globals: &super::globals::Globals,
|
|
|
|
) -> Result<()> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
2020-06-16 10:11:38 +00:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(version.as_bytes());
|
|
|
|
|
|
|
|
if self.backupid_algorithm.get(&key)?.is_none() {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"Tried to update nonexistent backup.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.backupid_etag
|
|
|
|
.insert(&key, &globals.next_count()?.to_be_bytes())?;
|
|
|
|
|
|
|
|
key.push(0xff);
|
2021-04-05 19:44:21 +00:00
|
|
|
key.extend_from_slice(room_id.as_bytes());
|
2020-06-16 10:11:38 +00:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(session_id.as_bytes());
|
|
|
|
|
|
|
|
self.backupkeyid_backup.insert(
|
|
|
|
&key,
|
2021-06-08 16:10:00 +00:00
|
|
|
&serde_json::to_vec(&key_data).expect("KeyBackupData::to_vec always works"),
|
2020-06-16 10:11:38 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn count_keys(&self, user_id: &UserId, version: &str) -> Result<usize> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut prefix = user_id.as_bytes().to_vec();
|
2020-06-16 10:11:38 +00:00
|
|
|
prefix.push(0xff);
|
|
|
|
prefix.extend_from_slice(version.as_bytes());
|
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
Ok(self.backupkeyid_backup.scan_prefix(prefix).count())
|
2020-06-16 10:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_etag(&self, user_id: &UserId, version: &str) -> Result<String> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
2020-06-16 10:11:38 +00:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(&version.as_bytes());
|
|
|
|
|
|
|
|
Ok(utils::u64_from_bytes(
|
|
|
|
&self
|
|
|
|
.backupid_etag
|
|
|
|
.get(&key)?
|
|
|
|
.ok_or_else(|| Error::bad_database("Backup has no etag."))?,
|
|
|
|
)
|
|
|
|
.map_err(|_| Error::bad_database("etag in backupid_etag invalid."))?
|
|
|
|
.to_string())
|
|
|
|
}
|
|
|
|
|
2021-02-27 21:09:22 +00:00
|
|
|
pub fn get_all(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
|
|
|
version: &str,
|
|
|
|
) -> Result<BTreeMap<RoomId, RoomKeyBackup>> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut prefix = user_id.as_bytes().to_vec();
|
2020-06-16 10:11:38 +00:00
|
|
|
prefix.push(0xff);
|
|
|
|
prefix.extend_from_slice(version.as_bytes());
|
2020-08-27 12:48:20 +00:00
|
|
|
prefix.push(0xff);
|
2020-06-16 10:11:38 +00:00
|
|
|
|
2021-02-27 21:09:22 +00:00
|
|
|
let mut rooms = BTreeMap::<RoomId, RoomKeyBackup>::new();
|
2020-06-16 10:11:38 +00:00
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
for result in self
|
|
|
|
.backupkeyid_backup
|
|
|
|
.scan_prefix(prefix)
|
|
|
|
.map(|(key, value)| {
|
|
|
|
let mut parts = key.rsplit(|&b| b == 0xff);
|
2020-06-16 10:11:38 +00:00
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
let session_id =
|
|
|
|
utils::string_from_bytes(&parts.next().ok_or_else(|| {
|
|
|
|
Error::bad_database("backupkeyid_backup key is invalid.")
|
|
|
|
})?)
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database("backupkeyid_backup session_id is invalid.")
|
|
|
|
})?;
|
2020-06-16 10:11:38 +00:00
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
let room_id = RoomId::try_from(
|
|
|
|
utils::string_from_bytes(&parts.next().ok_or_else(|| {
|
|
|
|
Error::bad_database("backupkeyid_backup key is invalid.")
|
|
|
|
})?)
|
|
|
|
.map_err(|_| Error::bad_database("backupkeyid_backup room_id is invalid."))?,
|
2020-06-16 10:11:38 +00:00
|
|
|
)
|
2021-06-08 16:10:00 +00:00
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database("backupkeyid_backup room_id is invalid room id.")
|
|
|
|
})?;
|
2020-06-16 10:11:38 +00:00
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
let key_data = serde_json::from_slice(&value).map_err(|_| {
|
|
|
|
Error::bad_database("KeyBackupData in backupkeyid_backup is invalid.")
|
|
|
|
})?;
|
2020-06-16 10:11:38 +00:00
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
Ok::<_, Error>((room_id, session_id, key_data))
|
|
|
|
})
|
|
|
|
{
|
2020-06-16 10:11:38 +00:00
|
|
|
let (room_id, session_id, key_data) = result?;
|
|
|
|
rooms
|
|
|
|
.entry(room_id)
|
2021-02-27 21:09:22 +00:00
|
|
|
.or_insert_with(|| RoomKeyBackup {
|
2020-06-16 10:11:38 +00:00
|
|
|
sessions: BTreeMap::new(),
|
|
|
|
})
|
|
|
|
.sessions
|
|
|
|
.insert(session_id, key_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(rooms)
|
|
|
|
}
|
2020-08-27 12:48:20 +00:00
|
|
|
|
|
|
|
pub fn get_room(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
|
|
|
version: &str,
|
|
|
|
room_id: &RoomId,
|
2021-06-08 16:10:00 +00:00
|
|
|
) -> Result<BTreeMap<String, KeyBackupData>> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut prefix = user_id.as_bytes().to_vec();
|
2020-08-27 12:48:20 +00:00
|
|
|
prefix.push(0xff);
|
|
|
|
prefix.extend_from_slice(version.as_bytes());
|
|
|
|
prefix.push(0xff);
|
|
|
|
prefix.extend_from_slice(room_id.as_bytes());
|
|
|
|
prefix.push(0xff);
|
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
Ok(self
|
|
|
|
.backupkeyid_backup
|
|
|
|
.scan_prefix(prefix)
|
|
|
|
.map(|(key, value)| {
|
2020-08-27 12:48:20 +00:00
|
|
|
let mut parts = key.rsplit(|&b| b == 0xff);
|
|
|
|
|
|
|
|
let session_id =
|
|
|
|
utils::string_from_bytes(&parts.next().ok_or_else(|| {
|
|
|
|
Error::bad_database("backupkeyid_backup key is invalid.")
|
|
|
|
})?)
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database("backupkeyid_backup session_id is invalid.")
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let key_data = serde_json::from_slice(&value).map_err(|_| {
|
2021-02-27 21:09:22 +00:00
|
|
|
Error::bad_database("KeyBackupData in backupkeyid_backup is invalid.")
|
2020-08-27 12:48:20 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok::<_, Error>((session_id, key_data))
|
|
|
|
})
|
|
|
|
.filter_map(|r| r.ok())
|
2021-06-08 16:10:00 +00:00
|
|
|
.collect())
|
2020-08-27 12:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_session(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
|
|
|
version: &str,
|
|
|
|
room_id: &RoomId,
|
|
|
|
session_id: &str,
|
2021-02-27 21:09:22 +00:00
|
|
|
) -> Result<Option<KeyBackupData>> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
2020-08-27 12:48:20 +00:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(version.as_bytes());
|
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(room_id.as_bytes());
|
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(session_id.as_bytes());
|
|
|
|
|
|
|
|
self.backupkeyid_backup
|
|
|
|
.get(&key)?
|
|
|
|
.map(|value| {
|
2021-02-27 21:09:22 +00:00
|
|
|
serde_json::from_slice(&value).map_err(|_| {
|
|
|
|
Error::bad_database("KeyBackupData in backupkeyid_backup is invalid.")
|
|
|
|
})
|
2020-08-27 12:48:20 +00:00
|
|
|
})
|
|
|
|
.transpose()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_all_keys(&self, user_id: &UserId, version: &str) -> Result<()> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
2020-08-27 12:48:20 +00:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(&version.as_bytes());
|
|
|
|
key.push(0xff);
|
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
for (outdated_key, _) in self.backupkeyid_backup.scan_prefix(key) {
|
|
|
|
self.backupkeyid_backup.remove(&outdated_key)?;
|
2020-08-27 12:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_room_keys(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
|
|
|
version: &str,
|
|
|
|
room_id: &RoomId,
|
|
|
|
) -> Result<()> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
2020-08-27 12:48:20 +00:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(&version.as_bytes());
|
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(&room_id.as_bytes());
|
|
|
|
key.push(0xff);
|
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
for (outdated_key, _) in self.backupkeyid_backup.scan_prefix(key) {
|
|
|
|
self.backupkeyid_backup.remove(&outdated_key)?;
|
2020-08-27 12:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_room_key(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
|
|
|
version: &str,
|
|
|
|
room_id: &RoomId,
|
|
|
|
session_id: &str,
|
|
|
|
) -> Result<()> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
2020-08-27 12:48:20 +00:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(&version.as_bytes());
|
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(&room_id.as_bytes());
|
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(&session_id.as_bytes());
|
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
for (outdated_key, _) in self.backupkeyid_backup.scan_prefix(key) {
|
|
|
|
self.backupkeyid_backup.remove(&outdated_key)?;
|
2020-08-27 12:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-06-16 10:11:38 +00:00
|
|
|
}
|