Skip to content

Commit

Permalink
add logging for sqlcipher
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Jul 18, 2024
1 parent 53ad89f commit 86a6565
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
2 changes: 2 additions & 0 deletions bindings_ffi/Cargo.lock

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

6 changes: 4 additions & 2 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,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 All @@ -103,6 +104,7 @@ pub async fn create_client(

let store = match encryption_key {
Some(key) => {
log::info!("Key exists -- creating encrypted database");
let key: EncryptionKey = key
.try_into()
.map_err(|_| "Malformed 32 byte encryption key".to_string())?;
Expand Down
41 changes: 39 additions & 2 deletions xmtp_mls/src/storage/encrypted_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use diesel::{
prelude::*,
r2d2::{ConnectionManager, Pool, PoolTransactionManager, PooledConnection},
result::{DatabaseErrorKind, Error},
sql_query,
};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use log::warn;
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 @@ -95,6 +117,10 @@ impl EncryptedMessageStore {
enc_key: Option<EncryptionKey>,
) -> Result<Self, StorageError> {
log::info!("Setting up DB connection pool");
log::info!(
"Creating new EncryptedMessageStore with enc_key:{}",
enc_key.is_some()
);
let pool =
match opts {
StorageOption::Ephemeral => Pool::builder()
Expand All @@ -107,7 +133,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 +152,19 @@ impl EncryptedMessageStore {
conn.run_pending_migrations(MIGRATIONS)
.map_err(|e| StorageError::DbInit(e.to_string()))?;

let cipher_version = sql_query("PRAGMA cipher_version").load::<CipherVersion>(conn)?;
let cipher_provider_version =
sql_query("PRAGMA cipher_provider_version").load::<CipherProviderVersion>(conn)?;
let sqlite_version =
sql_query("SELECT sqlite_version() AS version").load::<SqliteVersion>(conn)?;
log::info!(
"Sql cipher version={}, cipher provider version={}, sqlite_version={}",
cipher_version[0].cipher_version,
cipher_provider_version[0].cipher_provider_version,
sqlite_version[0].version,
);
conn.batch_execute("PRAGMA cipher_log = stderr; PRAGMA cipher_log_level = INFO;")?;

log::info!("Migrations successful");
Ok(())
}
Expand All @@ -141,7 +179,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 86a6565

Please sign in to comment.