Skip to content

Commit

Permalink
add extra logging for SqlCipher (#906)
Browse files Browse the repository at this point in the history
* add logging for sqlcipher
  • Loading branch information
insipx authored Jul 18, 2024
1 parent 79b6c7b commit 11a51ca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions bindings_ffi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 41 additions & 3 deletions xmtp_mls/src/storage/encrypted_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -48,6 +49,27 @@ pub type RawDbConnection = PooledConnection<ConnectionManager<SqliteConnection>>

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]
Expand Down Expand Up @@ -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()),
Expand All @@ -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::<SqliteVersion>(conn)?;
log::info!("sqlite_version={}", sqlite_version[0].version);

if self.enc_key.is_some() {
let cipher_version = sql_query("PRAGMA cipher_version").load::<CipherVersion>(conn)?;
let cipher_provider_version =
sql_query("PRAGMA cipher_provider_version").load::<CipherProviderVersion>(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(())
}
Expand All @@ -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)))?;
}
Expand Down

0 comments on commit 11a51ca

Please sign in to comment.