Skip to content

Commit 684f228

Browse files
committed
refactor(sqlite): share implementations of the encode/decode/serialize/deserialize sqlite store helpers
1 parent 0f9faad commit 684f228

File tree

4 files changed

+124
-162
lines changed

4 files changed

+124
-162
lines changed

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

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
use std::{
16-
borrow::Cow,
1716
collections::HashMap,
1817
fmt,
1918
path::Path,
@@ -43,15 +42,14 @@ use ruma::{
4342
RoomId, TransactionId, UserId,
4443
};
4544
use rusqlite::{named_params, params_from_iter, OptionalExtension};
46-
use serde::{de::DeserializeOwned, Serialize};
4745
use tokio::{fs, sync::Mutex};
4846
use tracing::{debug, instrument, warn};
4947
use vodozemac::Curve25519PublicKey;
5048

5149
use crate::{
5250
error::{Error, Result},
5351
utils::{
54-
repeat_vars, Key, SqliteAsyncConnExt, SqliteKeyValueStoreAsyncConnExt,
52+
repeat_vars, EncryptableStore, Key, SqliteAsyncConnExt, SqliteKeyValueStoreAsyncConnExt,
5553
SqliteKeyValueStoreConnExt,
5654
},
5755
OpenStoreError, SqliteStoreConfig,
@@ -78,6 +76,12 @@ impl fmt::Debug for SqliteCryptoStore {
7876
}
7977
}
8078

79+
impl EncryptableStore for SqliteCryptoStore {
80+
fn get_cypher(&self) -> Option<&StoreCipher> {
81+
self.store_cipher.as_deref()
82+
}
83+
}
84+
8185
impl SqliteCryptoStore {
8286
/// Open the SQLite-based crypto store at the given path using the given
8387
/// passphrase to encrypt private data.
@@ -130,45 +134,6 @@ impl SqliteCryptoStore {
130134
})
131135
}
132136

133-
fn encode_value(&self, value: Vec<u8>) -> Result<Vec<u8>> {
134-
if let Some(key) = &self.store_cipher {
135-
let encrypted = key.encrypt_value_data(value)?;
136-
Ok(rmp_serde::to_vec_named(&encrypted)?)
137-
} else {
138-
Ok(value)
139-
}
140-
}
141-
142-
fn decode_value<'a>(&self, value: &'a [u8]) -> Result<Cow<'a, [u8]>> {
143-
if let Some(key) = &self.store_cipher {
144-
let encrypted = rmp_serde::from_slice(value)?;
145-
let decrypted = key.decrypt_value_data(encrypted)?;
146-
Ok(Cow::Owned(decrypted))
147-
} else {
148-
Ok(Cow::Borrowed(value))
149-
}
150-
}
151-
152-
fn serialize_json(&self, value: &impl Serialize) -> Result<Vec<u8>> {
153-
let serialized = serde_json::to_vec(value)?;
154-
self.encode_value(serialized)
155-
}
156-
157-
fn deserialize_json<T: DeserializeOwned>(&self, data: &[u8]) -> Result<T> {
158-
let decoded = self.decode_value(data)?;
159-
Ok(serde_json::from_slice(&decoded)?)
160-
}
161-
162-
fn serialize_value(&self, value: &impl Serialize) -> Result<Vec<u8>> {
163-
let serialized = rmp_serde::to_vec_named(value)?;
164-
self.encode_value(serialized)
165-
}
166-
167-
fn deserialize_value<T: DeserializeOwned>(&self, value: &[u8]) -> Result<T> {
168-
let decoded = self.decode_value(value)?;
169-
Ok(rmp_serde::from_slice(&decoded)?)
170-
}
171-
172137
fn deserialize_and_unpickle_inbound_group_session(
173138
&self,
174139
value: Vec<u8>,
@@ -193,15 +158,6 @@ impl SqliteCryptoStore {
193158
Ok(request)
194159
}
195160

196-
fn encode_key(&self, table_name: &str, key: impl AsRef<[u8]>) -> Key {
197-
let bytes = key.as_ref();
198-
if let Some(store_cipher) = &self.store_cipher {
199-
Key::Hashed(store_cipher.hash_key(table_name, bytes))
200-
} else {
201-
Key::Plain(bytes.to_owned())
202-
}
203-
}
204-
205161
fn get_static_account(&self) -> Option<StaticAccountData> {
206162
self.static_account.read().unwrap().clone()
207163
}

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

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
//! An SQLite-based backend for the [`EventCacheStore`].
1616
17-
use std::{borrow::Cow, fmt, iter::once, path::Path, sync::Arc};
17+
use std::{fmt, iter::once, path::Path, sync::Arc};
1818

1919
use async_trait::async_trait;
2020
use deadpool_sqlite::{Object as SqliteAsyncConn, Pool as SqlitePool, Runtime};
@@ -49,8 +49,8 @@ use tracing::{debug, error, trace};
4949
use crate::{
5050
error::{Error, Result},
5151
utils::{
52-
repeat_vars, time_to_timestamp, Key, SqliteAsyncConnExt, SqliteKeyValueStoreAsyncConnExt,
53-
SqliteKeyValueStoreConnExt, SqliteTransactionExt,
52+
repeat_vars, time_to_timestamp, EncryptableStore, Key, SqliteAsyncConnExt,
53+
SqliteKeyValueStoreAsyncConnExt, SqliteKeyValueStoreConnExt, SqliteTransactionExt,
5454
},
5555
OpenStoreError, SqliteStoreConfig,
5656
};
@@ -97,6 +97,12 @@ impl fmt::Debug for SqliteEventCacheStore {
9797
}
9898
}
9999

100+
impl EncryptableStore for SqliteEventCacheStore {
101+
fn get_cypher(&self) -> Option<&StoreCipher> {
102+
self.store_cipher.as_deref()
103+
}
104+
}
105+
100106
impl SqliteEventCacheStore {
101107
/// Open the SQLite-based event cache store at the given path using the
102108
/// given passphrase to encrypt private data.
@@ -148,34 +154,6 @@ impl SqliteEventCacheStore {
148154
Ok(Self { store_cipher, pool, media_service })
149155
}
150156

151-
fn encode_value(&self, value: Vec<u8>) -> Result<Vec<u8>> {
152-
if let Some(key) = &self.store_cipher {
153-
let encrypted = key.encrypt_value_data(value)?;
154-
Ok(rmp_serde::to_vec_named(&encrypted)?)
155-
} else {
156-
Ok(value)
157-
}
158-
}
159-
160-
fn decode_value<'a>(&self, value: &'a [u8]) -> Result<Cow<'a, [u8]>> {
161-
if let Some(key) = &self.store_cipher {
162-
let encrypted = rmp_serde::from_slice(value)?;
163-
let decrypted = key.decrypt_value_data(encrypted)?;
164-
Ok(Cow::Owned(decrypted))
165-
} else {
166-
Ok(Cow::Borrowed(value))
167-
}
168-
}
169-
170-
fn encode_key(&self, table_name: &str, key: impl AsRef<[u8]>) -> Key {
171-
let bytes = key.as_ref();
172-
if let Some(store_cipher) = &self.store_cipher {
173-
Key::Hashed(store_cipher.hash_key(table_name, bytes))
174-
} else {
175-
Key::Plain(bytes.to_owned())
176-
}
177-
}
178-
179157
async fn acquire(&self) -> Result<SqliteAsyncConn> {
180158
let connection = self.pool.get().await?;
181159

@@ -1709,7 +1687,11 @@ mod tests {
17091687
use tempfile::{tempdir, TempDir};
17101688

17111689
use super::SqliteEventCacheStore;
1712-
use crate::{event_cache_store::keys, utils::SqliteAsyncConnExt, SqliteStoreConfig};
1690+
use crate::{
1691+
event_cache_store::keys,
1692+
utils::{EncryptableStore as _, SqliteAsyncConnExt},
1693+
SqliteStoreConfig,
1694+
};
17131695

17141696
static TMP_DIR: Lazy<TempDir> = Lazy::new(|| tempdir().unwrap());
17151697
static NUM: AtomicU32 = AtomicU32::new(0);

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

Lines changed: 10 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ use ruma::{
3636
OwnedTransactionId, OwnedUserId, RoomId, TransactionId, UInt, UserId,
3737
};
3838
use rusqlite::{OptionalExtension, Transaction};
39-
use serde::{de::DeserializeOwned, Deserialize, Serialize};
39+
use serde::{Deserialize, Serialize};
4040
use tokio::fs;
41-
use tracing::{debug, error, warn};
41+
use tracing::{debug, warn};
4242

4343
use crate::{
4444
error::{Error, Result},
4545
utils::{
46-
repeat_vars, Key, SqliteAsyncConnExt, SqliteKeyValueStoreAsyncConnExt,
46+
repeat_vars, EncryptableStore, Key, SqliteAsyncConnExt, SqliteKeyValueStoreAsyncConnExt,
4747
SqliteKeyValueStoreConnExt,
4848
},
4949
OpenStoreError, SqliteStoreConfig,
@@ -359,79 +359,6 @@ impl SqliteStateStore {
359359
Ok(())
360360
}
361361

362-
fn encode_value(&self, value: Vec<u8>) -> Result<Vec<u8>> {
363-
if let Some(key) = &self.store_cipher {
364-
let encrypted = key.encrypt_value_data(value)?;
365-
Ok(rmp_serde::to_vec_named(&encrypted)?)
366-
} else {
367-
Ok(value)
368-
}
369-
}
370-
371-
fn serialize_value(&self, value: &impl Serialize) -> Result<Vec<u8>> {
372-
let serialized = rmp_serde::to_vec_named(value)?;
373-
self.encode_value(serialized)
374-
}
375-
376-
fn serialize_json(&self, value: &impl Serialize) -> Result<Vec<u8>> {
377-
let serialized = serde_json::to_vec(value)?;
378-
self.encode_value(serialized)
379-
}
380-
381-
fn decode_value<'a>(&self, value: &'a [u8]) -> Result<Cow<'a, [u8]>> {
382-
if let Some(key) = &self.store_cipher {
383-
let encrypted = rmp_serde::from_slice(value)?;
384-
let decrypted = key.decrypt_value_data(encrypted)?;
385-
Ok(Cow::Owned(decrypted))
386-
} else {
387-
Ok(Cow::Borrowed(value))
388-
}
389-
}
390-
391-
fn deserialize_json<T: DeserializeOwned>(&self, data: &[u8]) -> Result<T> {
392-
let decoded = self.decode_value(data)?;
393-
394-
let json_deserializer = &mut serde_json::Deserializer::from_slice(&decoded);
395-
396-
serde_path_to_error::deserialize(json_deserializer).map_err(|err| {
397-
let raw_json: Option<Raw<serde_json::Value>> = serde_json::from_slice(&decoded).ok();
398-
399-
let target_type = std::any::type_name::<T>();
400-
let serde_path = err.path().to_string();
401-
402-
error!(
403-
sentry = true,
404-
%err,
405-
"Failed to deserialize {target_type} in the state state: {serde_path}",
406-
);
407-
408-
if let Some(raw) = raw_json {
409-
if let Some(room_id) = raw.get_field::<OwnedRoomId>("room_id").ok().flatten() {
410-
warn!("Found a room id in the source data to deserialize: {room_id}");
411-
}
412-
if let Some(event_id) = raw.get_field::<OwnedEventId>("event_id").ok().flatten() {
413-
warn!("Found an event id in the source data to deserialize: {event_id}");
414-
}
415-
}
416-
417-
err.into_inner().into()
418-
})
419-
}
420-
421-
fn deserialize_value<T: DeserializeOwned>(&self, value: &[u8]) -> Result<T> {
422-
let decoded = self.decode_value(value)?;
423-
Ok(rmp_serde::from_slice(&decoded)?)
424-
}
425-
426-
fn encode_key(&self, table_name: &str, key: impl AsRef<[u8]>) -> Key {
427-
let bytes = key.as_ref();
428-
if let Some(store_cipher) = &self.store_cipher {
429-
Key::Hashed(store_cipher.hash_key(table_name, bytes))
430-
} else {
431-
Key::Plain(bytes.to_owned())
432-
}
433-
}
434-
435362
fn encode_state_store_data_key(&self, key: StateStoreDataKey<'_>) -> Key {
436363
let key_s = match key {
437364
StateStoreDataKey::SyncToken => Cow::Borrowed(StateStoreDataKey::SYNC_TOKEN),
@@ -494,6 +421,12 @@ impl SqliteStateStore {
494421
}
495422
}
496423

424+
impl EncryptableStore for SqliteStateStore {
425+
fn get_cypher(&self) -> Option<&StoreCipher> {
426+
self.store_cipher.as_deref()
427+
}
428+
}
429+
497430
/// Initialize the database.
498431
async fn init(conn: &SqliteAsyncConn) -> Result<()> {
499432
// First turn on WAL mode, this can't be done in the transaction, it fails with
@@ -2296,7 +2229,7 @@ mod migration_tests {
22962229
use super::{init, keys, SqliteStateStore, DATABASE_NAME};
22972230
use crate::{
22982231
error::{Error, Result},
2299-
utils::{SqliteAsyncConnExt, SqliteKeyValueStoreAsyncConnExt},
2232+
utils::{EncryptableStore as _, SqliteAsyncConnExt, SqliteKeyValueStoreAsyncConnExt},
23002233
OpenStoreError,
23012234
};
23022235

0 commit comments

Comments
 (0)