@@ -49,16 +49,16 @@ impl Deref for Dynamo {
4949 }
5050}
5151
52- pub type Cipher = ZeroKMSWithClientKey < AutoRefresh < ServiceCredentials > > ;
53- pub type ScopedCipherWithCreds = ScopedCipher < AutoRefresh < ServiceCredentials > > ;
52+ pub type ZeroKmsCipher = ZeroKMSWithClientKey < AutoRefresh < ServiceCredentials > > ;
53+ pub type ScopedZeroKmsCipher = ScopedCipher < AutoRefresh < ServiceCredentials > > ;
5454
5555pub struct EncryptedTable < D = Dynamo > {
5656 db : D ,
57- cipher : Arc < Cipher > ,
57+ cipher : Arc < ZeroKmsCipher > ,
5858}
5959
6060impl < D > EncryptedTable < D > {
61- pub fn cipher ( & self ) -> Arc < Cipher > {
61+ pub fn cipher ( & self ) -> Arc < ZeroKmsCipher > {
6262 self . cipher . clone ( )
6363 }
6464}
@@ -279,20 +279,15 @@ impl<D> EncryptedTable<D> {
279279 ) -> Result < Vec < T > , DecryptError >
280280 where T : Decryptable + Identifiable ,
281281 {
282- // TODO: Temporary obvs
283- let dataset_id = Uuid :: parse_str ( "93e10481-2692-4d65-a619-37e36a496e64" ) . unwrap ( ) ;
284- let scoped_cipher = ScopedCipherWithCreds :: init ( self . cipher . clone ( ) , dataset_id) . await . unwrap ( ) ;
285-
286- decrypt_all ( & scoped_cipher, items) . await
282+ decrypt_all ( & self . cipher , items) . await
287283 }
288284
289285 pub async fn create_delete_patch (
290286 & self ,
291287 delete : PreparedDelete ,
288+ dataset_id : Option < Uuid > ,
292289 ) -> Result < DynamoRecordPatch , DeleteError > {
293- // TODO: Temporary obvs
294- let dataset_id = Uuid :: parse_str ( "93e10481-2692-4d65-a619-37e36a496e64" ) . unwrap ( ) ;
295- let scoped_cipher = ScopedCipherWithCreds :: init ( self . cipher . clone ( ) , dataset_id) . await . unwrap ( ) ;
290+ let scoped_cipher = ScopedZeroKmsCipher :: init ( self . cipher . clone ( ) , dataset_id) . await . unwrap ( ) ;
296291
297292 let PrimaryKeyParts { pk, sk } = encrypt_primary_key_parts ( & scoped_cipher, delete. primary_key ) ?;
298293
@@ -323,14 +318,13 @@ impl<D> EncryptedTable<D> {
323318 pub async fn create_put_patch (
324319 & self ,
325320 record : PreparedRecord ,
321+ dataset_id : Option < Uuid > ,
326322 // TODO: Make sure the index_predicate is used correctly
327323 index_predicate : impl FnMut ( & AttributeName , & TableAttribute ) -> bool ,
328324 ) -> Result < DynamoRecordPatch , PutError > {
329325 let mut seen_sk = HashSet :: new ( ) ;
330326
331- // TODO: Temporary obvs
332- let dataset_id = Uuid :: parse_str ( "93e10481-2692-4d65-a619-37e36a496e64" ) . unwrap ( ) ;
333- let indexable_cipher = ScopedCipherWithCreds :: init ( self . cipher . clone ( ) , dataset_id) . await . unwrap ( ) ;
327+ let indexable_cipher = ScopedZeroKmsCipher :: init ( self . cipher . clone ( ) , dataset_id) . await . unwrap ( ) ;
334328
335329 let PreparedRecord {
336330 protected_attributes,
@@ -403,13 +397,26 @@ impl EncryptedTable<Dynamo> {
403397 where
404398 T : Decryptable + Identifiable ,
405399 {
406- // TODO: Temporary obvs
407- let dataset_id = Uuid :: parse_str ( "93e10481-2692-4d65-a619-37e36a496e64" ) . unwrap ( ) ;
408- let scoped_cipher = ScopedCipherWithCreds :: init ( self . cipher . clone ( ) , dataset_id) . await . unwrap ( ) ;
400+ // TODO: Don't unwrap
401+ let scoped_cipher = ScopedZeroKmsCipher :: init ( self . cipher . clone ( ) , None ) . await . unwrap ( ) ;
402+ self . get_inner ( k, scoped_cipher) . await
403+ }
409404
410- let PrimaryKeyParts { pk, sk } =
411- encrypt_primary_key_parts ( & scoped_cipher, PreparedPrimaryKey :: new :: < T > ( k) ) ?;
405+ pub async fn get_via < T > ( & self , k : impl Into < T :: PrimaryKey > , dataset_id : Uuid ) -> Result < Option < T > , GetError >
406+ where
407+ T : Decryptable + Identifiable ,
408+ {
409+ // TODO: Don't unwrap
410+ let scoped_cipher = ScopedZeroKmsCipher :: init ( self . cipher . clone ( ) , Some ( dataset_id) ) . await . unwrap ( ) ;
411+ self . get_inner ( k, scoped_cipher) . await
412+ }
412413
414+ async fn get_inner < T > ( & self , k : impl Into < T :: PrimaryKey > , cipher : ScopedZeroKmsCipher ) -> Result < Option < T > , GetError >
415+ where
416+ T : Decryptable + Identifiable ,
417+ {
418+ let PrimaryKeyParts { pk, sk } =
419+ encrypt_primary_key_parts ( & cipher, PreparedPrimaryKey :: new :: < T > ( k) ) ?;
413420
414421 let result = self
415422 . db
@@ -421,10 +428,8 @@ impl EncryptedTable<Dynamo> {
421428 . await
422429 . map_err ( |e| GetError :: Aws ( format ! ( "{e:?}" ) ) ) ?;
423430
424- println ! ( "RESULT {:?}" , result) ;
425-
426431 if let Some ( item) = result. item {
427- Ok ( Some ( decrypt ( & scoped_cipher , item) . await ?) )
432+ Ok ( Some ( decrypt ( & self . cipher , item) . await ?) )
428433 } else {
429434 Ok ( None )
430435 }
@@ -433,9 +438,25 @@ impl EncryptedTable<Dynamo> {
433438 pub async fn delete < E : Searchable + Identifiable > (
434439 & self ,
435440 k : impl Into < E :: PrimaryKey > ,
441+ ) -> Result < ( ) , DeleteError > {
442+ self . delete_inner :: < E > ( k. into ( ) , None ) . await
443+ }
444+
445+ pub async fn delete_via < E : Searchable + Identifiable > (
446+ & self ,
447+ k : impl Into < E :: PrimaryKey > ,
448+ dataset_id : Uuid ,
449+ ) -> Result < ( ) , DeleteError > {
450+ self . delete_inner :: < E > ( k. into ( ) , Some ( dataset_id) ) . await
451+ }
452+
453+ async fn delete_inner < E : Searchable + Identifiable > (
454+ & self ,
455+ k : E :: PrimaryKey ,
456+ dataset_id : Option < Uuid > ,
436457 ) -> Result < ( ) , DeleteError > {
437458 let transact_items = self
438- . create_delete_patch ( PreparedDelete :: new :: < E > ( k) )
459+ . create_delete_patch ( PreparedDelete :: new :: < E > ( k) , dataset_id )
439460 . await ?
440461 . into_transact_write_items ( & self . db . table_name ) ?;
441462
@@ -453,6 +474,20 @@ impl EncryptedTable<Dynamo> {
453474 }
454475
455476 pub async fn put < T > ( & self , record : T ) -> Result < ( ) , PutError >
477+ where
478+ T : Searchable + Identifiable ,
479+ {
480+ self . put_inner ( record, None ) . await
481+ }
482+
483+ pub async fn put_via < T > ( & self , record : T , dataset_id : Uuid ) -> Result < ( ) , PutError >
484+ where
485+ T : Searchable + Identifiable ,
486+ {
487+ self . put_inner ( record, Some ( dataset_id) ) . await
488+ }
489+
490+ async fn put_inner < T > ( & self , record : T , dataset_id : Option < Uuid > ) -> Result < ( ) , PutError >
456491 where
457492 T : Searchable + Identifiable ,
458493 {
@@ -461,6 +496,7 @@ impl EncryptedTable<Dynamo> {
461496 let transact_items = self
462497 . create_put_patch (
463498 record,
499+ dataset_id,
464500 // include all records in the indexes
465501 |_, _| true ,
466502 )
@@ -484,7 +520,7 @@ impl EncryptedTable<Dynamo> {
484520/// Take a prepared primary key and encrypt it to get the [`PrimaryKeyParts`] which can be used
485521/// for retrieval.
486522fn encrypt_primary_key_parts (
487- scoped_cipher : & ScopedCipherWithCreds ,
523+ scoped_cipher : & ScopedZeroKmsCipher ,
488524 prepared_primary_key : PreparedPrimaryKey ,
489525) -> Result < PrimaryKeyParts , PrimaryKeyError > {
490526 let PrimaryKeyParts { mut pk, mut sk } = prepared_primary_key. primary_key_parts ;
@@ -500,7 +536,7 @@ fn encrypt_primary_key_parts(
500536 Ok ( PrimaryKeyParts { pk, sk } )
501537}
502538
503- async fn decrypt < T > ( scoped_cipher : & ScopedCipherWithCreds , item : HashMap < String , AttributeValue > ) -> Result < T , DecryptError >
539+ async fn decrypt < T > ( scoped_cipher : & ZeroKmsCipher , item : HashMap < String , AttributeValue > ) -> Result < T , DecryptError >
504540where
505541 T : Decryptable + Identifiable ,
506542{
@@ -512,7 +548,7 @@ where
512548}
513549
514550async fn decrypt_all < T > (
515- scoped_cipher : & ScopedCipherWithCreds ,
551+ scoped_cipher : & ZeroKmsCipher ,
516552 items : impl IntoIterator < Item = HashMap < String , AttributeValue > > ,
517553) -> Result < Vec < T > , DecryptError >
518554where
0 commit comments