-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Database migration for HMAC Key Records #1377
Closed
+181
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
xmtp_mls/migrations/2024-12-04-194513_hmac-keys-records-table/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS "hmac_key_records"; |
11 changes: 11 additions & 0 deletions
11
xmtp_mls/migrations/2024-12-04-194513_hmac-keys-records-table/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CREATE TABLE "hmac_key_records"( | ||
-- Group ID that the Hmac keys are associated with | ||
"group_id" BLOB NOT NULL, | ||
-- Dm ID that the Hmac keys are associated with | ||
"dm_id" TEXT, | ||
-- The hmac key | ||
"hmac_key" BLOB NOT NULL, | ||
-- The number of 30 day periods since epoch | ||
"thirty_day_periods_since_epoch" INT NOT NULL, | ||
PRIMARY KEY ("group_id", "hmac_key") | ||
); |
159 changes: 159 additions & 0 deletions
159
xmtp_mls/src/storage/encrypted_store/hmac_key_record.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
use crate::{impl_store, storage::StorageError}; | ||
|
||
use super::Sqlite; | ||
use super::{ | ||
db_connection::DbConnection, | ||
schema::consent_records::{self, dsl}, | ||
}; | ||
use diesel::{ | ||
backend::Backend, | ||
deserialize::{self, FromSql, FromSqlRow}, | ||
expression::AsExpression, | ||
prelude::*, | ||
serialize::{self, IsNull, Output, ToSql}, | ||
sql_types::Integer, | ||
upsert::excluded, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use xmtp_id::associations::DeserializationError; | ||
use xmtp_proto::xmtp::mls::message_contents::{ | ||
ConsentEntityType, ConsentState as ConsentStateProto, ConsentUpdate as ConsentUpdateProto, | ||
}; | ||
|
||
/// StoredConsentRecord holds a serialized ConsentRecord | ||
#[derive(Insertable, Queryable, Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] | ||
#[diesel(table_name = hmac_key_records)] | ||
#[diesel(primary_key(group_id, hmac_key))] | ||
pub struct StoredHmacKeyRecord { | ||
/// The group id associate with these hmac keys | ||
pub group_id: Vec<u8>, | ||
/// The dm id for stitching | ||
pub dm_id: Option<String>, | ||
/// The hmac key | ||
pub hmac_key: Vec<u8>, | ||
/// The number of 30 day periods since epoch | ||
pub thirty_day_periods_since_epoch: i32, | ||
} | ||
|
||
impl StoredHmacKeyRecord { | ||
pub fn new(group_id: Vec<u8>, dm_id: Option<String>, hmac_key: Vec<u8>, thirty_day_periods_since_epoch: i32) -> Self { | ||
Self { | ||
group_id, | ||
dm_id, | ||
hmac_key, | ||
thirty_day_periods_since_epoch | ||
} | ||
} | ||
} | ||
|
||
impl_store!(StoredHmacKeyRecord, hmac_key_records); | ||
|
||
impl DbConnection { | ||
/// Returns all hmac_key_records for the given group_id | ||
pub fn get_hmac_key_records( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sugesstion: does |
||
&self, | ||
group_id: Vec<u8>, | ||
) -> Result<Vec<StoredHmacKeyRecord>, StorageError> { | ||
Ok(self.raw_query(|conn| -> diesel::QueryResult<_> { | ||
dsl::hmac_key_records | ||
.filter(dsl::group_id.eq(group_id)) | ||
.load::<StoredHmacKeyRecord>(conn) | ||
})?) | ||
} | ||
|
||
/// Insert hmac_key_records without replacing existing ones | ||
pub fn insert_hmac_key_records( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: can we use |
||
&self, | ||
records: &[StoredHmacKeyRecord], | ||
) -> Result<(), StorageError> { | ||
self.raw_query(|conn| -> diesel::QueryResult<_> { | ||
conn.transaction::<_, diesel::result::Error, _>(|conn| { | ||
for record in records.iter() { | ||
diesel::insert_into(dsl::hmac_key_records) | ||
.values(record) | ||
.on_conflict_do_nothing() | ||
.execute(conn)?; | ||
} | ||
Ok(()) | ||
}) | ||
})?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::storage::encrypted_store::tests::with_connection; | ||
#[cfg(target_arch = "wasm32")] | ||
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_dedicated_worker); | ||
|
||
use super::*; | ||
|
||
fn generate_hmac_key_record( | ||
group_id: Vec<u8>, | ||
hmac_key: Vec<u8>, | ||
thirty_day_periods_since_epoch: i32, | ||
) -> StoredHmacKeyRecord { | ||
StoredHmacKeyRecord { | ||
group_id, | ||
None, | ||
hmac_key, | ||
thirty_day_periods_since_epoch, | ||
} | ||
} | ||
|
||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] | ||
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)] | ||
async fn insert_and_read_hmac_key_records() { | ||
with_connection(|conn| { | ||
// Prepare test data | ||
let group_id = b"group_id".to_vec(); | ||
let hmac_key = b"hmac_key".to_vec(); | ||
let thirty_day_periods_since_epoch = 123; | ||
|
||
let hmac_record = generate_hmac_key_record( | ||
group_id.clone(), | ||
hmac_key.clone(), | ||
thirty_day_periods_since_epoch, | ||
); | ||
|
||
// Insert the record | ||
conn.insert_hmac_key_records(&[hmac_record.clone()]) | ||
.expect("should insert hmac_key_record without error"); | ||
|
||
// Read back the inserted record | ||
let records = conn | ||
.get_hmac_key_records(group_id.clone()) | ||
.expect("query should work"); | ||
|
||
// Ensure the records match | ||
assert_eq!(records.len(), 1, "There should be exactly one record"); | ||
let retrieved_record = &records[0]; | ||
|
||
assert_eq!(retrieved_record.group_id, hmac_record.group_id); | ||
assert_eq!(retrieved_record.hmac_key, hmac_record.hmac_key); | ||
assert_eq!( | ||
retrieved_record.thirty_day_periods_since_epoch, | ||
hmac_record.thirty_day_periods_since_epoch | ||
); | ||
|
||
// Insert a second record (same group_id and thirty_day_periods_since_epoch) | ||
let conflict_record = generate_hmac_key_record( | ||
group_id.clone(), | ||
b"new_hmac_key".to_vec(), // Different hmac_key | ||
thirty_day_periods_since_epoch, | ||
); | ||
|
||
conn.insert_hmac_key_records(&[conflict_record]) | ||
.expect("should insert second record without error"); | ||
|
||
let records = conn | ||
.get_hmac_key_records(group_id.clone()) | ||
.expect("query should work"); | ||
|
||
assert_eq!(records.len(), 2, "Both records should exist"); | ||
}) | ||
.await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this based off of the hmac keys work in V2 but @codabrink mentioned we might be doing this based on inboxId now. So maybe this is irrelevant in V3?