11use super :: {
22 flattened_encrypted_attributes:: FlattenedEncryptedAttributes , normalized_protected_attributes:: NormalizedKey
33} ;
4- use crate :: crypto:: SealError ;
4+ use crate :: { crypto:: SealError , encrypted_table :: AttributeName } ;
55use cipherstash_client:: {
66 credentials:: { service_credentials:: ServiceToken , Credentials } ,
77 encryption:: { BytesWithDescriptor , Encryption , Plaintext } ,
@@ -35,7 +35,6 @@ impl FlattenedProtectedAttributes {
3535 chunk_into : usize ,
3636 ) -> Result < Vec < FlattenedEncryptedAttributes > , SealError > {
3737 let chunk_size = self . 0 . len ( ) / chunk_into;
38- println ! ( "Encrypting all attributes, chunk into: {}, chunk_size = {}" , chunk_into, chunk_size) ;
3938
4039 cipher
4140 . encrypt ( self . 0 . into_iter ( ) )
@@ -68,11 +67,11 @@ impl FromIterator<(Plaintext, String)> for FlattenedProtectedAttributes {
6867#[ derive( PartialEq , Debug ) ]
6968pub ( crate ) struct FlattenedProtectedAttribute {
7069 plaintext : Plaintext ,
71- key : FlattenedKey ,
70+ key : FlattenedAttrName ,
7271}
7372
7473impl FlattenedProtectedAttribute {
75- pub ( super ) fn new ( plaintext : impl Into < Plaintext > , key : impl Into < FlattenedKey > ) -> Self {
74+ pub ( super ) fn new ( plaintext : impl Into < Plaintext > , key : impl Into < FlattenedAttrName > ) -> Self {
7675 Self {
7776 plaintext : plaintext. into ( ) ,
7877 key : key. into ( ) ,
@@ -85,7 +84,10 @@ impl FlattenedProtectedAttribute {
8584 ( self . plaintext , normalized, subkey)
8685 }
8786
88- fn descriptor ( & self ) -> String {
87+ /// Returns the name of the attribute used to store the plaintext in the encrypted table.
88+ /// This is used as the descriptor when encrypting the attribute and
89+ /// is intended to tie the encrypted value to its location in a table.
90+ fn storage_descriptor ( & self ) -> String {
8991 self . key . descriptor ( )
9092 }
9193}
@@ -94,7 +96,7 @@ impl Into<BytesWithDescriptor> for FlattenedProtectedAttribute {
9496 fn into ( self ) -> BytesWithDescriptor {
9597 BytesWithDescriptor {
9698 bytes : self . plaintext . to_vec ( ) ,
97- descriptor : self . descriptor ( ) ,
99+ descriptor : self . storage_descriptor ( ) ,
98100 }
99101 }
100102}
@@ -105,17 +107,19 @@ impl Into<BytesWithDescriptor> for FlattenedProtectedAttribute {
105107/// A Map would have a key and a subkey, while a scalar would only have a key.
106108// TODO: Only implement Debug in tests
107109#[ derive( PartialEq , Hash , Eq , Clone , Debug ) ]
108- pub ( super ) struct FlattenedKey {
110+ pub ( super ) struct FlattenedAttrName {
111+ // TODO: Use a Cow to avoid copies during decryption
112+ // We may also never set this to None in which can we can remove the Option
109113 prefix : Option < String > ,
110- key : String ,
114+ name : AttributeName ,
111115 subkey : Option < String > ,
112116}
113117
114- impl FlattenedKey {
115- pub ( super ) fn new ( prefix : Option < String > , key : impl Into < String > ) -> Self {
118+ impl FlattenedAttrName {
119+ pub ( super ) fn new ( prefix : Option < String > , name : impl Into < AttributeName > ) -> Self {
116120 Self {
117121 prefix,
118- key : key . into ( ) ,
122+ name : name . into ( ) ,
119123 subkey : None ,
120124 }
121125 }
@@ -126,18 +130,18 @@ impl FlattenedKey {
126130 /// Prefix is discarded as it is not needed after decryption.
127131 pub ( super ) fn normalize ( self ) -> ( NormalizedKey , Option < String > ) {
128132 match self . subkey {
129- Some ( _) => ( NormalizedKey :: new_map ( self . key ) , self . subkey ) ,
130- None => ( NormalizedKey :: new_scalar ( self . key ) , None ) ,
133+ Some ( _) => ( NormalizedKey :: new_map ( self . name . as_external_name ( ) ) , self . subkey ) ,
134+ None => ( NormalizedKey :: new_scalar ( self . name . as_external_name ( ) ) , None ) ,
131135 }
132136 }
133137
134138 // TODO: Rename this to try_parse
135139 /// Parse a descriptor into a [FlattenedKey].
136140 pub ( super ) fn parse ( descriptor : & str ) -> Self {
137- fn split_subkey ( prefix : Option < String > , key : & str ) -> FlattenedKey {
141+ fn split_subkey ( prefix : Option < String > , key : & str ) -> FlattenedAttrName {
138142 match key. split_once ( "." ) {
139- None => FlattenedKey :: new ( prefix, key) ,
140- Some ( ( key, subkey) ) => FlattenedKey :: new ( prefix, key) . with_subkey ( subkey) ,
143+ None => FlattenedAttrName :: new ( prefix, key) ,
144+ Some ( ( key, subkey) ) => FlattenedAttrName :: new ( prefix, key) . with_subkey ( subkey) ,
141145 }
142146 }
143147 match descriptor. split_once ( "/" ) {
@@ -153,40 +157,40 @@ impl FlattenedKey {
153157
154158 pub ( crate ) fn descriptor ( & self ) -> String {
155159 match ( self . prefix . as_ref ( ) , self . subkey . as_ref ( ) ) {
156- ( Some ( prefix) , Some ( subkey) ) => format ! ( "{}/{}.{}" , prefix, self . key , subkey) ,
157- ( Some ( prefix) , None ) => format ! ( "{}/{}" , prefix, self . key ) ,
158- ( None , Some ( subkey) ) => format ! ( "{}.{}" , self . key , subkey) ,
159- ( None , None ) => self . key . to_string ( ) ,
160+ ( Some ( prefix) , Some ( subkey) ) => format ! ( "{}/{}.{}" , prefix, self . name . as_stored_name ( ) , subkey) ,
161+ ( Some ( prefix) , None ) => format ! ( "{}/{}" , prefix, self . name . as_stored_name ( ) ) ,
162+ ( None , Some ( subkey) ) => format ! ( "{}.{}" , self . name . as_stored_name ( ) , subkey) ,
163+ ( None , None ) => self . name . as_stored_name ( ) . to_string ( ) ,
160164 }
161165 }
162166
163167 /// Consume and return the parts of the key (not including the prefix).
164- pub fn into_key_parts ( self ) -> ( String , Option < String > ) {
165- ( self . key , self . subkey )
168+ pub fn into_parts ( self ) -> ( AttributeName , Option < String > ) {
169+ ( self . name , self . subkey )
166170 }
167171}
168172
169173// TODO: Change to TryFrom
170- impl From < String > for FlattenedKey {
174+ impl From < String > for FlattenedAttrName {
171175 fn from ( key : String ) -> Self {
172176 Self :: parse ( key. as_str ( ) )
173177 }
174178}
175179
176- impl From < & str > for FlattenedKey {
180+ impl From < & str > for FlattenedAttrName {
177181 fn from ( key : & str ) -> Self {
178182 Self :: parse ( key)
179183 }
180184}
181185
182- impl From < ( String , String ) > for FlattenedKey {
186+ impl From < ( String , String ) > for FlattenedAttrName {
183187 fn from ( ( prefix, key) : ( String , String ) ) -> Self {
184188 // TODO: Check that neither string is empty
185189 Self :: new ( Some ( prefix) , key)
186190 }
187191}
188192
189- impl From < ( & str , & str ) > for FlattenedKey {
193+ impl From < ( & str , & str ) > for FlattenedAttrName {
190194 fn from ( ( prefix, key) : ( & str , & str ) ) -> Self {
191195 Self :: new ( Some ( prefix. to_string ( ) ) , key)
192196 }
@@ -198,37 +202,38 @@ mod tests {
198202
199203 #[ test]
200204 fn test_flattened_key_from_string ( ) {
201- assert_eq ! ( FlattenedKey :: new( None , "foo" ) , "foo" . into( ) ) ;
205+ assert_eq ! ( FlattenedAttrName :: new( None , "foo" ) , "foo" . into( ) ) ;
202206 }
203207
204208 #[ test]
205209 fn test_flattened_key_from_tuple ( ) {
206210 assert_eq ! (
207- FlattenedKey :: new( Some ( "prefix" . to_string( ) ) , "foo" ) ,
211+ FlattenedAttrName :: new( Some ( "prefix" . to_string( ) ) , "foo" ) ,
208212 ( "prefix" , "foo" ) . into( )
209213 ) ;
210214 }
211215
216+ // TODO: Test that pk and sk are renamed to __pk and __sk respectively
212217 #[ test]
213218 fn test_flattened_key_descriptor ( ) {
214- assert_eq ! ( FlattenedKey :: new( None , "foo" ) . descriptor( ) , "foo" ) ;
219+ assert_eq ! ( FlattenedAttrName :: new( None , "foo" ) . descriptor( ) , "foo" ) ;
215220 assert_eq ! (
216- FlattenedKey :: new( Some ( "pref" . to_string( ) ) , "foo" ) . descriptor( ) ,
221+ FlattenedAttrName :: new( Some ( "pref" . to_string( ) ) , "foo" ) . descriptor( ) ,
217222 "pref/foo"
218223 ) ;
219224 assert_eq ! (
220- FlattenedKey :: new( None , "foo" ) . with_subkey( "x" ) . descriptor( ) ,
225+ FlattenedAttrName :: new( None , "foo" ) . with_subkey( "x" ) . descriptor( ) ,
221226 "foo.x"
222227 ) ;
223228 assert_eq ! (
224- FlattenedKey :: new( Some ( "pref" . to_string( ) ) , "foo" )
229+ FlattenedAttrName :: new( Some ( "pref" . to_string( ) ) , "foo" )
225230 . with_subkey( "x" )
226231 . descriptor( ) ,
227232 "pref/foo.x"
228233 ) ;
229234 }
230235
231- // TODO: Test normalize
236+ // TODO: Test normalize the FlattenedAttrName
232237
233238 #[ test]
234239 fn test_into_iter ( ) {
@@ -319,9 +324,9 @@ mod tests {
319324
320325 #[ test]
321326 fn test_flattened_key_parse ( ) {
322- assert_eq ! ( FlattenedKey :: parse( "key" ) , "key" . into( ) ) ;
323- assert_eq ! ( FlattenedKey :: parse( "prefix/key" ) , ( "prefix" , "key" ) . into( ) ) ;
324- assert_eq ! ( FlattenedKey :: parse( "key.subkey" ) , FlattenedKey :: from( "key" ) . with_subkey( "subkey" ) ) ;
325- assert_eq ! ( FlattenedKey :: parse( "prefix/key.subkey" ) , FlattenedKey :: from( ( "prefix" , "key" ) ) . with_subkey( "subkey" ) ) ;
327+ assert_eq ! ( FlattenedAttrName :: parse( "key" ) , "key" . into( ) ) ;
328+ assert_eq ! ( FlattenedAttrName :: parse( "prefix/key" ) , ( "prefix" , "key" ) . into( ) ) ;
329+ assert_eq ! ( FlattenedAttrName :: parse( "key.subkey" ) , FlattenedAttrName :: from( "key" ) . with_subkey( "subkey" ) ) ;
330+ assert_eq ! ( FlattenedAttrName :: parse( "prefix/key.subkey" ) , FlattenedAttrName :: from( ( "prefix" , "key" ) ) . with_subkey( "subkey" ) ) ;
326331 }
327332}
0 commit comments