Skip to content

Commit 2e33f86

Browse files
committed
cargo fmt
1 parent ab5b393 commit 2e33f86

File tree

13 files changed

+123
-98
lines changed

13 files changed

+123
-98
lines changed

src/crypto/attrs/flattened_encrypted_attributes.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use crate::{
33
encrypted_table::{TableAttributes, ZeroKmsCipher},
44
traits::TableAttribute,
55
};
6-
use cipherstash_client::{
7-
encryption::Plaintext, zerokms::EncryptedRecord
8-
};
6+
use cipherstash_client::{encryption::Plaintext, zerokms::EncryptedRecord};
97
use itertools::Itertools;
108

119
use super::FlattenedProtectedAttributes;
@@ -45,11 +43,14 @@ impl FlattenedEncryptedAttributes {
4543
cipher
4644
.decrypt(self.attrs.into_iter())
4745
.await
48-
.map(|records| records
49-
.into_iter()
50-
// FIXME: We should change the decrypt method to return a plaintext and/or make a Plaintext::from_bytes method which consumes the bytes
51-
.map(|bytes| Plaintext::from_slice(&bytes).unwrap())
52-
.zip(descriptors.into_iter()).collect())
46+
.map(|records| {
47+
records
48+
.into_iter()
49+
// FIXME: We should change the decrypt method to return a plaintext and/or make a Plaintext::from_bytes method which consumes the bytes
50+
.map(|bytes| Plaintext::from_slice(&bytes).unwrap())
51+
.zip(descriptors.into_iter())
52+
.collect()
53+
})
5354
// FIXME: EncryptedRecord should return an error exposed in cipherstash_client
5455
.map_err(|_| SealError::AssertionFailed("FIXME".to_string()))
5556
}

src/crypto/attrs/flattened_protected_attributes.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ use super::{
22
flattened_encrypted_attributes::FlattenedEncryptedAttributes,
33
normalized_protected_attributes::NormalizedKey,
44
};
5-
use crate::{crypto::SealError, encrypted_table::{AttributeName, ScopedZeroKmsCipher}};
5+
use crate::{
6+
crypto::SealError,
7+
encrypted_table::{AttributeName, ScopedZeroKmsCipher},
8+
};
69
use cipherstash_client::{
7-
encryption::{BytesWithDescriptor, Plaintext}, zerokms::EncryptPayload,
10+
encryption::{BytesWithDescriptor, Plaintext},
11+
zerokms::EncryptPayload,
812
};
913
use itertools::Itertools;
1014

@@ -38,9 +42,7 @@ impl FlattenedProtectedAttributes {
3842
let payloads: Vec<BytesWithDescriptor> = self.0.into_iter().map(Into::into).collect();
3943

4044
cipher
41-
.encrypt(
42-
payloads.iter().map(EncryptPayload::from),
43-
)
45+
.encrypt(payloads.iter().map(EncryptPayload::from))
4446
.await?
4547
.into_iter()
4648
.chunks(chunk_size)

src/crypto/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ mod sealed;
44
mod sealer;
55
mod unsealed;
66
use crate::{
7-
traits::{PrimaryKeyError, PrimaryKeyParts, ReadConversionError, WriteConversionError}, Identifiable, IndexType, PrimaryKey
7+
traits::{PrimaryKeyError, PrimaryKeyParts, ReadConversionError, WriteConversionError},
8+
Identifiable, IndexType, PrimaryKey,
89
};
910
use cipherstash_client::{
10-
encryption::{
11-
EncryptionError, TypeParseError
12-
},zerokms
11+
encryption::{EncryptionError, TypeParseError},
12+
zerokms,
1313
};
1414
use miette::Diagnostic;
1515
use std::borrow::Cow;

src/crypto/sealed.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,21 @@ impl TryFrom<SealedTableEntry> for HashMap<String, AttributeValue> {
203203

204204
#[cfg(test)]
205205
mod tests {
206-
use crate::encrypted_table::{ZeroKmsCipher};
206+
use crate::encrypted_table::ZeroKmsCipher;
207207

208208
use super::SealedTableEntry;
209209
use cipherstash_client::{
210-
credentials::auto_refresh::AutoRefresh, ConsoleConfig, ZeroKMS, ZeroKMSConfig
210+
credentials::auto_refresh::AutoRefresh, ConsoleConfig, ZeroKMS, ZeroKMSConfig,
211211
};
212212
use miette::IntoDiagnostic;
213213
use std::{borrow::Cow, sync::Arc};
214214

215215
// FIXME: Use the test cipher from CipherStash Client when that's ready
216216
async fn get_cipher() -> Result<Arc<ZeroKmsCipher>, Box<dyn std::error::Error>> {
217-
let console_config = ConsoleConfig::builder().with_env().build().into_diagnostic()?;
217+
let console_config = ConsoleConfig::builder()
218+
.with_env()
219+
.build()
220+
.into_diagnostic()?;
218221
let zero_kms_config = ZeroKMSConfig::builder()
219222
.decryption_log(true)
220223
.with_env()

src/crypto/sealer.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
use super::{
2-
attrs::FlattenedProtectedAttributes, b64_encode, format_term_key, SealError, SealedTableEntry, Unsealed, MAX_TERMS_PER_INDEX
2+
attrs::FlattenedProtectedAttributes, b64_encode, format_term_key, SealError, SealedTableEntry,
3+
Unsealed, MAX_TERMS_PER_INDEX,
34
};
45
use crate::{
5-
encrypted_table::{AttributeName, ScopedZeroKmsCipher, TableAttribute, TableAttributes, TableEntry},
6+
encrypted_table::{
7+
AttributeName, ScopedZeroKmsCipher, TableAttribute, TableAttributes, TableEntry,
8+
},
69
traits::PrimaryKeyParts,
710
IndexType,
811
};
9-
use cipherstash_client::{
10-
encryption::{
11-
compound_indexer::{ComposableIndex, ComposablePlaintext},
12-
IndexTerm,
13-
},
12+
use cipherstash_client::encryption::{
13+
compound_indexer::{ComposableIndex, ComposablePlaintext},
14+
IndexTerm,
1415
};
1516
use itertools::Itertools;
1617
use std::{borrow::Cow, collections::HashMap};
@@ -51,10 +52,7 @@ impl RecordsWithTerms {
5152
}
5253
}
5354

54-
async fn encrypt(
55-
self,
56-
cipher: &ScopedZeroKmsCipher,
57-
) -> Result<Vec<Sealed>, SealError> {
55+
async fn encrypt(self, cipher: &ScopedZeroKmsCipher) -> Result<Vec<Sealed>, SealError> {
5856
let num_records = self.records.len();
5957
let mut pksks = Vec::with_capacity(num_records);
6058
let mut record_terms = Vec::with_capacity(num_records);
@@ -175,7 +173,9 @@ impl Sealer {
175173
.take(MAX_TERMS_PER_INDEX)
176174
.map(|x| (index_name.clone(), index_type, x))
177175
.collect()),
178-
x => Err(SealError::InvalidCiphertext(format!("Invalid index term: `{x:?}"))),
176+
x => Err(SealError::InvalidCiphertext(format!(
177+
"Invalid index term: `{x:?}"
178+
))),
179179
})
180180
.flatten_ok()
181181
.try_collect()?;
@@ -184,12 +184,10 @@ impl Sealer {
184184
.into_iter()
185185
.enumerate()
186186
.map(|(i, (index_name, index_type, value))| {
187-
let sk = b64_encode(
188-
cipher.mac::<32>(
189-
&format_term_key(sk.as_str(), &index_name, index_type, i),
190-
Some(pk.as_str()),
191-
),
192-
);
187+
let sk = b64_encode(cipher.mac::<32>(
188+
&format_term_key(sk.as_str(), &index_name, index_type, i),
189+
Some(pk.as_str()),
190+
));
193191

194192
Ok::<_, SealError>(Term { sk, value })
195193
})

src/encrypted_table/mod.rs

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@ use crate::{
1818
};
1919
use aws_sdk_dynamodb::types::{AttributeValue, Delete, Put, TransactWriteItem};
2020
use cipherstash_client::{
21-
config::{console_config::ConsoleConfig, cts_config::CtsConfig, zero_kms_config::ZeroKMSConfig}, credentials::{
22-
auto_refresh::AutoRefresh,
23-
service_credentials::ServiceCredentials,
24-
}, encryption::ScopedCipher, zerokms::{ZeroKMS, ZeroKMSWithClientKey}
21+
config::{
22+
console_config::ConsoleConfig, cts_config::CtsConfig, zero_kms_config::ZeroKMSConfig,
23+
},
24+
credentials::{auto_refresh::AutoRefresh, service_credentials::ServiceCredentials},
25+
encryption::ScopedCipher,
26+
zerokms::{ZeroKMS, ZeroKMSWithClientKey},
2527
};
2628
use log::info;
27-
use uuid::Uuid;
2829
use std::{
2930
borrow::Cow,
3031
collections::{HashMap, HashSet},
31-
ops::Deref, sync::Arc,
32+
ops::Deref,
33+
sync::Arc,
3234
};
35+
use uuid::Uuid;
3336

3437
pub struct Headless;
3538

@@ -274,7 +277,8 @@ impl<D> EncryptedTable<D> {
274277
&self,
275278
items: impl IntoIterator<Item = HashMap<String, AttributeValue>>,
276279
) -> Result<Vec<T>, DecryptError>
277-
where T: Decryptable + Identifiable,
280+
where
281+
T: Decryptable + Identifiable,
278282
{
279283
decrypt_all(&self.cipher, items).await
280284
}
@@ -284,13 +288,18 @@ impl<D> EncryptedTable<D> {
284288
delete: PreparedDelete,
285289
dataset_id: Option<Uuid>,
286290
) -> Result<DynamoRecordPatch, DeleteError> {
287-
let scoped_cipher = ScopedZeroKmsCipher::init(self.cipher.clone(), dataset_id).await.unwrap();
291+
let scoped_cipher = ScopedZeroKmsCipher::init(self.cipher.clone(), dataset_id)
292+
.await
293+
.unwrap();
288294

289-
let PrimaryKeyParts { pk, sk } = encrypt_primary_key_parts(&scoped_cipher, delete.primary_key)?;
295+
let PrimaryKeyParts { pk, sk } =
296+
encrypt_primary_key_parts(&scoped_cipher, delete.primary_key)?;
290297

291298
let delete_records = all_index_keys(&sk, delete.protected_indexes)
292299
.into_iter()
293-
.map(|x| Ok::<_, DeleteError>(b64_encode(scoped_cipher.mac::<32>(&x, Some(pk.as_str())))))
300+
.map(|x| {
301+
Ok::<_, DeleteError>(b64_encode(scoped_cipher.mac::<32>(&x, Some(pk.as_str()))))
302+
})
294303
.chain([Ok(sk)])
295304
.map(|sk| {
296305
let sk = sk?;
@@ -330,9 +339,7 @@ impl<D> EncryptedTable<D> {
330339
} = record;
331340

332341
// Do the encryption
333-
let sealed = sealer
334-
.seal(protected_attributes, &indexable_cipher)
335-
.await?;
342+
let sealed = sealer.seal(protected_attributes, &indexable_cipher).await?;
336343

337344
let mut put_records = Vec::with_capacity(sealed.len());
338345

@@ -395,20 +402,32 @@ impl EncryptedTable<Dynamo> {
395402
T: Decryptable + Identifiable,
396403
{
397404
// TODO: Don't unwrap
398-
let scoped_cipher = ScopedZeroKmsCipher::init(self.cipher.clone(), None).await.unwrap();
405+
let scoped_cipher = ScopedZeroKmsCipher::init(self.cipher.clone(), None)
406+
.await
407+
.unwrap();
399408
self.get_inner(k, scoped_cipher).await
400409
}
401410

402-
pub async fn get_via<T>(&self, k: impl Into<T::PrimaryKey>, dataset_id: Uuid) -> Result<Option<T>, GetError>
411+
pub async fn get_via<T>(
412+
&self,
413+
k: impl Into<T::PrimaryKey>,
414+
dataset_id: Uuid,
415+
) -> Result<Option<T>, GetError>
403416
where
404417
T: Decryptable + Identifiable,
405418
{
406419
// TODO: Don't unwrap
407-
let scoped_cipher = ScopedZeroKmsCipher::init(self.cipher.clone(), Some(dataset_id)).await.unwrap();
420+
let scoped_cipher = ScopedZeroKmsCipher::init(self.cipher.clone(), Some(dataset_id))
421+
.await
422+
.unwrap();
408423
self.get_inner(k, scoped_cipher).await
409424
}
410425

411-
async fn get_inner<T>(&self, k: impl Into<T::PrimaryKey>, cipher: ScopedZeroKmsCipher) -> Result<Option<T>, GetError>
426+
async fn get_inner<T>(
427+
&self,
428+
k: impl Into<T::PrimaryKey>,
429+
cipher: ScopedZeroKmsCipher,
430+
) -> Result<Option<T>, GetError>
412431
where
413432
T: Decryptable + Identifiable,
414433
{
@@ -513,7 +532,6 @@ impl EncryptedTable<Dynamo> {
513532
}
514533
}
515534

516-
517535
/// Take a prepared primary key and encrypt it to get the [`PrimaryKeyParts`] which can be used
518536
/// for retrieval.
519537
fn encrypt_primary_key_parts(
@@ -533,13 +551,16 @@ fn encrypt_primary_key_parts(
533551
Ok(PrimaryKeyParts { pk, sk })
534552
}
535553

536-
async fn decrypt<T>(scoped_cipher: &ZeroKmsCipher, item: HashMap<String, AttributeValue>) -> Result<T, DecryptError>
554+
async fn decrypt<T>(
555+
scoped_cipher: &ZeroKmsCipher,
556+
item: HashMap<String, AttributeValue>,
557+
) -> Result<T, DecryptError>
537558
where
538559
T: Decryptable + Identifiable,
539560
{
540561
let uspec = UnsealSpec::new_for_decryptable::<T>();
541562
let table_entry = SealedTableEntry::try_from(item)?;
542-
let result = table_entry.unseal(uspec, scoped_cipher).await?;
563+
let result = table_entry.unseal(uspec, scoped_cipher).await?;
543564

544565
Ok(result.into_value()?)
545566
}

src/encrypted_table/query.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use aws_sdk_dynamodb::{primitives::Blob, types::AttributeValue};
2-
use cipherstash_client::{
3-
encryption::{
4-
compound_indexer::{ComposableIndex, ComposablePlaintext},
5-
Plaintext
6-
},
2+
use cipherstash_client::encryption::{
3+
compound_indexer::{ComposableIndex, ComposablePlaintext},
4+
Plaintext,
75
};
86
use itertools::Itertools;
97
use std::{borrow::Cow, collections::HashMap, marker::PhantomData};
@@ -14,7 +12,7 @@ use crate::{
1412
};
1513
use cipherstash_client::encryption::IndexTerm;
1614

17-
use super::{Dynamo, EncryptedTable, ScopedZeroKmsCipher, QueryError, SealError};
15+
use super::{Dynamo, EncryptedTable, QueryError, ScopedZeroKmsCipher, SealError};
1816

1917
/// A builder for a query operation which returns records of type `S`.
2018
/// `B` is the storage backend used to store the data.
@@ -44,7 +42,9 @@ impl PreparedQuery {
4442
} = self;
4543

4644
let info = format!("{}#{}", type_name, index_name);
47-
let index_term = scoped_cipher.compound_query(composed_index, plaintext, info).map_err(SealError::from)?;
45+
let index_term = scoped_cipher
46+
.compound_query(composed_index, plaintext, info)
47+
.map_err(SealError::from)?;
4848

4949
// With DynamoDB queries must always return a single term
5050
let term = if let IndexTerm::Binary(x) = index_term {
@@ -132,7 +132,9 @@ where
132132
where
133133
T: Decryptable + Identifiable,
134134
{
135-
let scoped_cipher = ScopedZeroKmsCipher::init(self.storage.cipher.clone(), None).await.unwrap();
135+
let scoped_cipher = ScopedZeroKmsCipher::init(self.storage.cipher.clone(), None)
136+
.await
137+
.unwrap();
136138

137139
let storage = self.storage;
138140
let query = self.build()?;

src/errors/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ pub use crate::{
99
traits::{ReadConversionError, WriteConversionError},
1010
};
1111

12-
pub use cipherstash_client::{
13-
config::errors::ConfigError, encryption::EncryptionError,
14-
};
12+
pub use cipherstash_client::{config::errors::ConfigError, encryption::EncryptionError};
1513

1614
pub use aws_sdk_dynamodb::error::BuildError;
1715

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ pub use cipherstash_dynamodb_derive::{Decryptable, Encryptable, Identifiable, Se
2020

2121
// Re-exports
2222
pub use cipherstash_client::encryption;
23-
pub type Key = [u8; 32];
23+
pub type Key = [u8; 32];

tests/common.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ pub async fn create_table(client: &Client, table_name: &str) {
8484
.expect("Failed to create table");
8585
}
8686

87-
88-
8987
#[macro_export]
9088
macro_rules! assert_err {
9189
($cond:expr,) => {
@@ -130,4 +128,4 @@ macro_rules! assert_none {
130128
Err(e) => (),
131129
}
132130
};
133-
}
131+
}

0 commit comments

Comments
 (0)