crypto: Rename the memory stores into caches and reorder the store module.

master
Damir Jelić 2020-09-04 12:42:40 +02:00
parent 7b3dfe2f27
commit adf8905d9f
12 changed files with 92 additions and 49 deletions

View File

@ -46,8 +46,8 @@ use matrix_sdk_common::{
}; };
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
use matrix_sdk_crypto::{ use matrix_sdk_crypto::{
CryptoStore, CryptoStoreError, Device, IncomingResponse, OlmError, OlmMachine, OutgoingRequest, store::{CryptoStore, CryptoStoreError},
Sas, UserDevices, Device, IncomingResponse, OlmError, OlmMachine, OutgoingRequest, Sas, UserDevices,
}; };
use zeroize::Zeroizing; use zeroize::Zeroizing;

View File

@ -57,8 +57,8 @@ pub use state::{AllRooms, ClientState};
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
#[cfg_attr(feature = "docs", doc(cfg(encryption)))] #[cfg_attr(feature = "docs", doc(cfg(encryption)))]
pub use matrix_sdk_crypto::{ pub use matrix_sdk_crypto::{
CryptoStoreError, Device, IncomingResponse, LocalTrust, OutgoingRequest, OutgoingRequests, store::CryptoStoreError, Device, IncomingResponse, LocalTrust, OutgoingRequest,
ReadOnlyDevice, Sas, UserDevices, OutgoingRequests, ReadOnlyDevice, Sas, UserDevices,
}; };
#[cfg(feature = "messages")] #[cfg(feature = "messages")]

View File

@ -41,9 +41,9 @@ use crate::{Account, OlmMachine};
use crate::{ use crate::{
error::{EventError, OlmError, OlmResult, SignatureError}, error::{EventError, OlmError, OlmResult, SignatureError},
identities::{OwnUserIdentity, UserIdentities}, identities::{OwnUserIdentity, UserIdentities},
store::Result as StoreResult, store::{caches::ReadOnlyUserDevices, Result as StoreResult},
verification::VerificationMachine, verification::VerificationMachine,
verify_json, ReadOnlyUserDevices, Sas, verify_json, Sas,
}; };
/// A read-only version of a `Device`. /// A read-only version of a `Device`.

View File

@ -513,7 +513,7 @@ mod test {
identities::{Device, ReadOnlyDevice}, identities::{Device, ReadOnlyDevice},
machine::test::response_from_file, machine::test::response_from_file,
olm::Account, olm::Account,
store::memorystore::MemoryStore, store::MemoryStore,
verification::VerificationMachine, verification::VerificationMachine,
}; };

View File

@ -30,10 +30,9 @@
mod error; mod error;
mod identities; mod identities;
mod machine; mod machine;
pub mod memory_stores;
pub mod olm; pub mod olm;
mod requests; mod requests;
mod store; pub mod store;
mod verification; mod verification;
pub use error::{MegolmError, OlmError}; pub use error::{MegolmError, OlmError};
@ -41,13 +40,9 @@ pub use identities::{
Device, LocalTrust, OwnUserIdentity, ReadOnlyDevice, UserDevices, UserIdentities, UserIdentity, Device, LocalTrust, OwnUserIdentity, ReadOnlyDevice, UserDevices, UserIdentities, UserIdentity,
}; };
pub use machine::OlmMachine; pub use machine::OlmMachine;
pub use memory_stores::ReadOnlyUserDevices;
pub(crate) use olm::Account; pub(crate) use olm::Account;
pub use olm::EncryptionSettings; pub use olm::EncryptionSettings;
pub use requests::{IncomingResponse, OutgoingRequest, OutgoingRequests}; pub use requests::{IncomingResponse, OutgoingRequest, OutgoingRequests};
#[cfg(feature = "sqlite_cryptostore")]
pub use store::sqlite::SqliteStore;
pub use store::{CryptoStore, CryptoStoreError};
pub use verification::Sas; pub use verification::Sas;
use error::SignatureError; use error::SignatureError;

View File

@ -63,9 +63,8 @@ use super::{
OlmMessage, OutboundGroupSession, OlmMessage, OutboundGroupSession,
}, },
requests::{IncomingResponse, OutgoingRequest}, requests::{IncomingResponse, OutgoingRequest},
store::{memorystore::MemoryStore, Result as StoreResult}, store::{CryptoStore, MemoryStore, Result as StoreResult},
verification::{Sas, VerificationMachine}, verification::{Sas, VerificationMachine},
CryptoStore,
}; };
/// State machine implementation of the Olm/Megolm encryption protocol used for /// State machine implementation of the Olm/Megolm encryption protocol used for

View File

@ -25,7 +25,7 @@ use matrix_sdk_common::{
locks::Mutex, locks::Mutex,
}; };
use super::{ use crate::{
identities::ReadOnlyDevice, identities::ReadOnlyDevice,
olm::{InboundGroupSession, Session}, olm::{InboundGroupSession, Session},
}; };
@ -209,8 +209,8 @@ impl DeviceStore {
mod test { mod test {
use crate::{ use crate::{
identities::device::test::get_device, identities::device::test::get_device,
memory_stores::{DeviceStore, GroupSessionStore, SessionStore},
olm::{test::get_account_and_session, InboundGroupSession}, olm::{test::get_account_and_session, InboundGroupSession},
store::caches::{DeviceStore, GroupSessionStore, SessionStore},
}; };
use matrix_sdk_common::identifiers::room_id; use matrix_sdk_common::identifiers::room_id;

View File

@ -21,11 +21,13 @@ use matrix_sdk_common::{
}; };
use matrix_sdk_common_macros::async_trait; use matrix_sdk_common_macros::async_trait;
use super::{Account, CryptoStore, InboundGroupSession, Result, Session}; use super::{
use crate::{ caches::{DeviceStore, GroupSessionStore, ReadOnlyUserDevices, SessionStore},
identities::{ReadOnlyDevice, UserIdentities}, Account, CryptoStore, InboundGroupSession, Result, Session,
memory_stores::{DeviceStore, GroupSessionStore, ReadOnlyUserDevices, SessionStore},
}; };
use crate::identities::{ReadOnlyDevice, UserIdentities};
/// An in-memory only store that will forget all the E2EE key once it's dropped.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct MemoryStore { pub struct MemoryStore {
sessions: SessionStore, sessions: SessionStore,
@ -36,8 +38,8 @@ pub struct MemoryStore {
identities: Arc<DashMap<UserId, UserIdentities>>, identities: Arc<DashMap<UserId, UserIdentities>>,
} }
impl MemoryStore { impl Default for MemoryStore {
pub fn new() -> Self { fn default() -> Self {
MemoryStore { MemoryStore {
sessions: SessionStore::new(), sessions: SessionStore::new(),
inbound_group_sessions: GroupSessionStore::new(), inbound_group_sessions: GroupSessionStore::new(),
@ -49,6 +51,13 @@ impl MemoryStore {
} }
} }
impl MemoryStore {
/// Create a new empty `MemoryStore`.
pub fn new() -> Self {
Self::default()
}
}
#[async_trait] #[async_trait]
impl CryptoStore for MemoryStore { impl CryptoStore for MemoryStore {
async fn load_account(&self) -> Result<Option<Account>> { async fn load_account(&self) -> Result<Option<Account>> {

View File

@ -12,8 +12,55 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//! Types and traits to implement the storage layer for the [`OlmMachine`]
//!
//! The storage layer for the [`OlmMachine`] can be customized using a trait.
//! Implementing your own [`CryptoStore`]
//!
//! An in-memory only store is provided as well as a SQLite based one, depending
//! on your needs and targets a custom store may be implemented, e.g. for
//! `wasm-unknown-unknown` an indexeddb store would be needed
//!
//! ```
//! # use matrix_sdk_crypto::{
//! # OlmMachine,
//! # store::MemoryStore,
//! # };
//! # use matrix_sdk_common::identifiers::{user_id, DeviceIdBox};
//! # let user_id = user_id!("@example:localhost");
//! # let device_id: DeviceIdBox = "TEST".into();
//! let store = Box::new(MemoryStore::new());
//!
//! let machine = OlmMachine::new_with_store(user_id, device_id, store);
//! ```
//!
//! [`OlmMachine`]: /matrix_sdk_crypto/struct.OlmMachine.html
//! [`CryptoStore`]: trait.Cryptostore.html
pub mod caches;
mod memorystore;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "sqlite_cryptostore")]
pub(crate) mod sqlite;
use caches::ReadOnlyUserDevices;
pub use memorystore::MemoryStore;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "sqlite_cryptostore")]
pub use sqlite::SqliteStore;
use std::{collections::HashSet, fmt::Debug, io::Error as IoError, sync::Arc}; use std::{collections::HashSet, fmt::Debug, io::Error as IoError, sync::Arc};
use olm_rs::errors::{OlmAccountError, OlmGroupSessionError, OlmSessionError};
use serde_json::Error as SerdeError;
use thiserror::Error;
use url::ParseError;
#[cfg_attr(feature = "docs", doc(cfg(r#sqlite_cryptostore)))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "sqlite_cryptostore")]
use sqlx::Error as SqlxError;
use matrix_sdk_common::{ use matrix_sdk_common::{
identifiers::{DeviceId, Error as IdentifierValidationError, RoomId, UserId}, identifiers::{DeviceId, Error as IdentifierValidationError, RoomId, UserId},
locks::Mutex, locks::Mutex,
@ -22,28 +69,15 @@ use matrix_sdk_common_macros::async_trait;
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
use matrix_sdk_common_macros::send_sync; use matrix_sdk_common_macros::send_sync;
use olm_rs::errors::{OlmAccountError, OlmGroupSessionError, OlmSessionError};
use serde_json::Error as SerdeError;
use thiserror::Error;
use url::ParseError;
use super::{ use super::{
identities::{ReadOnlyDevice, UserIdentities}, identities::{ReadOnlyDevice, UserIdentities},
memory_stores::ReadOnlyUserDevices,
olm::{Account, InboundGroupSession, Session}, olm::{Account, InboundGroupSession, Session},
}; };
use crate::error::SessionUnpicklingError; use crate::error::SessionUnpicklingError;
pub mod memorystore; /// A `CryptoStore` specific result type.
pub type Result<T> = std::result::Result<T, CryptoStoreError>;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "sqlite_cryptostore")]
pub mod sqlite;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "sqlite_cryptostore")]
use sqlx::Error as SqlxError;
#[derive(Error, Debug)] #[derive(Error, Debug)]
/// The crypto store's error type. /// The crypto store's error type.
@ -93,8 +127,6 @@ pub enum CryptoStoreError {
UrlParse(#[from] ParseError), UrlParse(#[from] ParseError),
} }
pub type Result<T> = std::result::Result<T, CryptoStoreError>;
/// Trait abstracting a store that the `OlmMachine` uses to store cryptographic /// Trait abstracting a store that the `OlmMachine` uses to store cryptographic
/// keys. /// keys.
#[async_trait] #[async_trait]

View File

@ -33,10 +33,12 @@ use sqlx::{query, query_as, sqlite::SqliteQueryAs, Connect, Executor, SqliteConn
use url::Url; use url::Url;
use zeroize::Zeroizing; use zeroize::Zeroizing;
use super::{CryptoStore, CryptoStoreError, Result}; use super::{
caches::{DeviceStore, GroupSessionStore, ReadOnlyUserDevices, SessionStore},
CryptoStore, CryptoStoreError, Result,
};
use crate::{ use crate::{
identities::{LocalTrust, ReadOnlyDevice, UserIdentities}, identities::{LocalTrust, ReadOnlyDevice, UserIdentities},
memory_stores::{DeviceStore, GroupSessionStore, ReadOnlyUserDevices, SessionStore},
olm::{ olm::{
Account, AccountPickle, IdentityKeys, InboundGroupSession, InboundGroupSessionPickle, Account, AccountPickle, IdentityKeys, InboundGroupSession, InboundGroupSessionPickle,
PickledAccount, PickledInboundGroupSession, PickledSession, PicklingMode, Session, PickledAccount, PickledInboundGroupSession, PickledSession, PicklingMode, Session,
@ -44,8 +46,9 @@ use crate::{
}, },
}; };
#[derive(Clone)]
/// SQLite based implementation of a `CryptoStore`. /// SQLite based implementation of a `CryptoStore`.
#[derive(Clone)]
#[cfg_attr(feature = "docs", doc(cfg(r#sqlite_cryptostore)))]
pub struct SqliteStore { pub struct SqliteStore {
user_id: Arc<UserId>, user_id: Arc<UserId>,
device_id: Arc<Box<DeviceId>>, device_id: Arc<Box<DeviceId>>,

View File

@ -26,7 +26,11 @@ use matrix_sdk_common::{
}; };
use super::sas::{content_to_request, Sas}; use super::sas::{content_to_request, Sas};
use crate::{requests::OutgoingRequest, Account, CryptoStore, CryptoStoreError, ReadOnlyDevice}; use crate::{
requests::OutgoingRequest,
store::{CryptoStore, CryptoStoreError},
Account, ReadOnlyDevice,
};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct VerificationMachine { pub struct VerificationMachine {
@ -229,9 +233,9 @@ mod test {
use super::{Sas, VerificationMachine}; use super::{Sas, VerificationMachine};
use crate::{ use crate::{
requests::OutgoingRequests, requests::OutgoingRequests,
store::memorystore::MemoryStore, store::{CryptoStore, MemoryStore},
verification::test::{get_content_from_request, wrap_any_to_device_content}, verification::test::{get_content_from_request, wrap_any_to_device_content},
Account, CryptoStore, ReadOnlyDevice, Account, ReadOnlyDevice,
}; };
fn alice_id() -> UserId { fn alice_id() -> UserId {

View File

@ -36,7 +36,8 @@ use matrix_sdk_common::{
use crate::{ use crate::{
identities::{LocalTrust, ReadOnlyDevice, UserIdentities}, identities::{LocalTrust, ReadOnlyDevice, UserIdentities},
Account, CryptoStore, CryptoStoreError, store::{CryptoStore, CryptoStoreError},
Account,
}; };
pub use helpers::content_to_request; pub use helpers::content_to_request;
@ -658,9 +659,9 @@ mod test {
}; };
use crate::{ use crate::{
store::memorystore::MemoryStore, store::{CryptoStore, MemoryStore},
verification::test::{get_content_from_request, wrap_any_to_device_content}, verification::test::{get_content_from_request, wrap_any_to_device_content},
Account, CryptoStore, ReadOnlyDevice, Account, ReadOnlyDevice,
}; };
use super::{Accepted, Created, Sas, SasState, Started}; use super::{Accepted, Created, Sas, SasState, Started};