1- use cipherstash_client:: { credentials:: { service_credentials:: ServiceToken , Credentials } , encryption:: { Encryption , EncryptionError } , zero_kms:: EncryptedRecord } ;
1+ use crate :: {
2+ crypto:: { attrs:: flattened_protected_attributes:: FlattenedAttrName , SealError } ,
3+ encrypted_table:: TableAttributes ,
4+ traits:: TableAttribute ,
5+ } ;
6+ use cipherstash_client:: {
7+ credentials:: { service_credentials:: ServiceToken , Credentials } ,
8+ encryption:: { Encryption , EncryptionError } ,
9+ zero_kms:: EncryptedRecord ,
10+ } ;
211use itertools:: Itertools ;
3- use crate :: { crypto:: { attrs:: flattened_protected_attributes:: FlattenedAttrName , SealError } , encrypted_table:: TableAttributes , traits:: TableAttribute } ;
412
513use super :: FlattenedProtectedAttributes ;
614
@@ -14,7 +22,9 @@ pub(crate) struct FlattenedEncryptedAttributes {
1422
1523impl FlattenedEncryptedAttributes {
1624 pub ( crate ) fn with_capacity ( capacity : usize ) -> Self {
17- Self { attrs : Vec :: with_capacity ( capacity) }
25+ Self {
26+ attrs : Vec :: with_capacity ( capacity) ,
27+ }
1828 }
1929
2030 pub ( crate ) fn is_empty ( & self ) -> bool {
@@ -32,17 +42,16 @@ impl FlattenedEncryptedAttributes {
3242 self ,
3343 cipher : & Encryption < impl Credentials < Token = ServiceToken > > ,
3444 ) -> Result < FlattenedProtectedAttributes , SealError > {
35- let descriptors = self . attrs . iter ( ) . map ( |record| record. descriptor . clone ( ) ) . collect_vec ( ) ;
45+ let descriptors = self
46+ . attrs
47+ . iter ( )
48+ . map ( |record| record. descriptor . clone ( ) )
49+ . collect_vec ( ) ;
3650
3751 cipher
3852 . decrypt ( self . attrs . into_iter ( ) )
3953 . await
40- . map ( |records| {
41- records
42- . into_iter ( )
43- . zip ( descriptors. into_iter ( ) )
44- . collect ( )
45- } )
54+ . map ( |records| records. into_iter ( ) . zip ( descriptors. into_iter ( ) ) . collect ( ) )
4655 . map_err ( SealError :: from)
4756 }
4857
@@ -58,37 +67,42 @@ impl FlattenedEncryptedAttributes {
5867 . map ( |data| ( FlattenedAttrName :: parse ( & record. descriptor ) , data) )
5968 . map_err ( EncryptionError :: from)
6069 } )
61- . fold_ok ( Ok ( TableAttributes :: new ( ) ) , |acc , ( flattened_attr_name , bytes ) | {
62- let ( name , subkey ) = flattened_attr_name . into_parts ( ) ;
63- if let Some ( subkey ) = subkey {
64- acc
65- . and_then ( | mut acc| acc
66- . try_insert_map ( name, subkey, bytes)
67- . map ( |_| acc ) )
68- } else {
69- acc. map ( | mut acc| {
70- acc . insert ( name , bytes ) ;
71- acc
72- } )
73- }
74- } ) ?
70+ . fold_ok (
71+ Ok ( TableAttributes :: new ( ) ) ,
72+ |acc , ( flattened_attr_name , bytes ) | {
73+ let ( name , subkey ) = flattened_attr_name . into_parts ( ) ;
74+ if let Some ( subkey ) = subkey {
75+ acc . and_then ( | mut acc| acc . try_insert_map ( name, subkey, bytes) . map ( |_| acc ) )
76+ } else {
77+ acc . map ( | mut acc| {
78+ acc. insert ( name , bytes ) ;
79+ acc
80+ } )
81+ }
82+ } ,
83+ ) ?
7584 }
7685
7786 // TODO: Test this
7887 /// Normalize the TableAttributes into a set of encrypted records.
7988 /// An error will be returned if the TableAttributes contain an unsupported attribute type
8089 /// (only `Bytes` and `Map` are currently supported).
81- ///
90+ ///
8291 /// Bytes data is converted to an [EncryptedRecord] using [TableAttribute::as_encrypted_record]
8392 /// which validates that the descriptor matches the key and subkey.
84- ///
93+ ///
8594 /// This method is used during decrypt and load operations.
86- pub ( crate ) fn try_extend ( & mut self , attributes : TableAttributes , prefix : String ) -> Result < ( ) , SealError > {
95+ pub ( crate ) fn try_extend (
96+ & mut self ,
97+ attributes : TableAttributes ,
98+ prefix : String ,
99+ ) -> Result < ( ) , SealError > {
87100 for ( name, value) in attributes. into_iter ( ) {
88101 match value {
89102 TableAttribute :: Map ( map) => {
90103 for ( subkey, value) in map. into_iter ( ) {
91- let attr_key = FlattenedAttrName :: new ( Some ( prefix. clone ( ) ) , name. clone ( ) ) . with_subkey ( subkey) ;
104+ let attr_key = FlattenedAttrName :: new ( Some ( prefix. clone ( ) ) , name. clone ( ) )
105+ . with_subkey ( subkey) ;
92106 // Load the bytes and check for a confused deputy attack
93107 let record = value. as_encrypted_record ( & attr_key. descriptor ( ) ) ?;
94108 self . attrs . push ( record) ;
@@ -101,7 +115,9 @@ impl FlattenedEncryptedAttributes {
101115 self . attrs . push ( record) ;
102116 }
103117 _ => {
104- Err ( SealError :: AssertionFailed ( "Unsupported attribute type" . to_string ( ) ) ) ?;
118+ Err ( SealError :: AssertionFailed (
119+ "Unsupported attribute type" . to_string ( ) ,
120+ ) ) ?;
105121 }
106122 }
107123 }
@@ -118,6 +134,8 @@ impl From<Vec<EncryptedRecord>> for FlattenedEncryptedAttributes {
118134
119135impl FromIterator < EncryptedRecord > for FlattenedEncryptedAttributes {
120136 fn from_iter < T : IntoIterator < Item = EncryptedRecord > > ( iter : T ) -> Self {
121- Self { attrs : iter. into_iter ( ) . collect ( ) }
137+ Self {
138+ attrs : iter. into_iter ( ) . collect ( ) ,
139+ }
122140 }
123- }
141+ }
0 commit comments