From 11a51cadc9b09ff3168e32da1cb87139c7c2bff6 Mon Sep 17 00:00:00 2001 From: Andrew Plaza Date: Thu, 18 Jul 2024 14:34:46 -0400 Subject: [PATCH] add extra logging for SqlCipher (#906) * add logging for sqlcipher --- bindings_ffi/Cargo.lock | 1 + bindings_ffi/src/mls.rs | 5 ++- xmtp_mls/src/storage/encrypted_store/mod.rs | 44 +++++++++++++++++++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/bindings_ffi/Cargo.lock b/bindings_ffi/Cargo.lock index 391d1f797..3591f31d9 100644 --- a/bindings_ffi/Cargo.lock +++ b/bindings_ffi/Cargo.lock @@ -4907,6 +4907,7 @@ dependencies = [ "futures-core", "pin-project-lite", "tokio", + "tokio-util", ] [[package]] diff --git a/bindings_ffi/src/mls.rs b/bindings_ffi/src/mls.rs index 26145dc51..fd494850a 100644 --- a/bindings_ffi/src/mls.rs +++ b/bindings_ffi/src/mls.rs @@ -90,9 +90,10 @@ pub async fn create_client( let api_client = TonicApiClient::create(host.clone(), is_secure).await?; log::info!( - "Creating message store with path: {:?} and encryption key: {}", + "Creating message store with path: {:?} and encryption key: {} of length {:?}", db, - encryption_key.is_some() + encryption_key.is_some(), + encryption_key.as_ref().map(|k| k.len()) ); let storage_option = match db { diff --git a/xmtp_mls/src/storage/encrypted_store/mod.rs b/xmtp_mls/src/storage/encrypted_store/mod.rs index 64c5706d5..61522bd7a 100644 --- a/xmtp_mls/src/storage/encrypted_store/mod.rs +++ b/xmtp_mls/src/storage/encrypted_store/mod.rs @@ -31,9 +31,10 @@ use diesel::{ prelude::*, r2d2::{ConnectionManager, Pool, PoolTransactionManager, PooledConnection}, result::{DatabaseErrorKind, Error}, + sql_query, }; use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; -use log::warn; +use log::{log_enabled, warn}; use rand::RngCore; use xmtp_cryptography::utils as crypto_utils; @@ -48,6 +49,27 @@ pub type RawDbConnection = PooledConnection> pub type EncryptionKey = [u8; 32]; +// For PRAGMA query log statements +#[derive(QueryableByName, Debug)] +struct CipherVersion { + #[diesel(sql_type = diesel::sql_types::Text)] + cipher_version: String, +} + +// For PRAGMA query log statements +#[derive(QueryableByName, Debug)] +struct CipherProviderVersion { + #[diesel(sql_type = diesel::sql_types::Text)] + cipher_provider_version: String, +} + +// For PRAGMA query log statements +#[derive(QueryableByName, Debug)] +struct SqliteVersion { + #[diesel(sql_type = diesel::sql_types::Text)] + version: String, +} + #[derive(Default, Clone, Debug)] pub enum StorageOption { #[default] @@ -107,7 +129,6 @@ impl EncryptedMessageStore { // TODO: Validate that sqlite is correctly configured. Bad EncKey is not detected until the // migrations run which returns an unhelpful error. - let mut obj = Self { connect_opt: opts, pool: Arc::new(Some(pool).into()), @@ -127,6 +148,24 @@ impl EncryptedMessageStore { conn.run_pending_migrations(MIGRATIONS) .map_err(|e| StorageError::DbInit(e.to_string()))?; + let sqlite_version = + sql_query("SELECT sqlite_version() AS version").load::(conn)?; + log::info!("sqlite_version={}", sqlite_version[0].version); + + if self.enc_key.is_some() { + let cipher_version = sql_query("PRAGMA cipher_version").load::(conn)?; + let cipher_provider_version = + sql_query("PRAGMA cipher_provider_version").load::(conn)?; + log::info!( + "Sqlite cipher_version={}, cipher_provider_version={}", + cipher_version[0].cipher_version, + cipher_provider_version[0].cipher_provider_version, + ); + if log_enabled!(log::Level::Info) { + conn.batch_execute("PRAGMA cipher_log = stderr; PRAGMA cipher_log_level = INFO;")?; + } + } + log::info!("Migrations successful"); Ok(()) } @@ -141,7 +180,6 @@ impl EncryptedMessageStore { .ok_or(StorageError::PoolNeedsConnection)?; let mut conn = pool.get()?; - if let Some(ref key) = self.enc_key { conn.batch_execute(&format!("PRAGMA key = \"x'{}'\";", hex::encode(key)))?; }