Skip to content

Commit 3e6216a

Browse files
committed
cargo fmt
1 parent 61bf2d9 commit 3e6216a

16 files changed

Lines changed: 191 additions & 151 deletions

src/crypto/attrs/flattened_encrypted_attributes.rs

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
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+
};
211
use itertools::Itertools;
3-
use crate::{crypto::{attrs::flattened_protected_attributes::FlattenedAttrName, SealError}, encrypted_table::TableAttributes, traits::TableAttribute};
412

513
use super::FlattenedProtectedAttributes;
614

@@ -14,7 +22,9 @@ pub(crate) struct FlattenedEncryptedAttributes {
1422

1523
impl 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

119135
impl 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+
}

src/crypto/attrs/flattened_protected_attributes.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{
2-
flattened_encrypted_attributes::FlattenedEncryptedAttributes, normalized_protected_attributes::NormalizedKey
2+
flattened_encrypted_attributes::FlattenedEncryptedAttributes,
3+
normalized_protected_attributes::NormalizedKey,
34
};
45
use crate::{crypto::SealError, encrypted_table::AttributeName};
56
use cipherstash_client::{
@@ -56,7 +57,11 @@ impl Extend<FlattenedProtectedAttribute> for FlattenedProtectedAttributes {
5657
/// Allows us to collect a list of (Plaintext, String) tuples into a [FlattenedProtectedAttributes] object.
5758
impl FromIterator<(Plaintext, String)> for FlattenedProtectedAttributes {
5859
fn from_iter<T: IntoIterator<Item = (Plaintext, String)>>(iter: T) -> Self {
59-
Self(iter.into_iter().map(|(plaintext, key)| FlattenedProtectedAttribute::new(plaintext, key)).collect())
60+
Self(
61+
iter.into_iter()
62+
.map(|(plaintext, key)| FlattenedProtectedAttribute::new(plaintext, key))
63+
.collect(),
64+
)
6065
}
6166
}
6267

@@ -130,8 +135,14 @@ impl FlattenedAttrName {
130135
/// Prefix is discarded as it is not needed after decryption.
131136
pub(super) fn normalize(self) -> (NormalizedKey, Option<String>) {
132137
match self.subkey {
133-
Some(_) => (NormalizedKey::new_map(self.name.as_external_name()), self.subkey),
134-
None => (NormalizedKey::new_scalar(self.name.as_external_name()), None),
138+
Some(_) => (
139+
NormalizedKey::new_map(self.name.as_external_name()),
140+
self.subkey,
141+
),
142+
None => (
143+
NormalizedKey::new_scalar(self.name.as_external_name()),
144+
None,
145+
),
135146
}
136147
}
137148

@@ -157,7 +168,9 @@ impl FlattenedAttrName {
157168

158169
pub(crate) fn descriptor(&self) -> String {
159170
match (self.prefix.as_ref(), self.subkey.as_ref()) {
160-
(Some(prefix), Some(subkey)) => format!("{}/{}.{}", prefix, self.name.as_stored_name(), subkey),
171+
(Some(prefix), Some(subkey)) => {
172+
format!("{}/{}.{}", prefix, self.name.as_stored_name(), subkey)
173+
}
161174
(Some(prefix), None) => format!("{}/{}", prefix, self.name.as_stored_name()),
162175
(None, Some(subkey)) => format!("{}.{}", self.name.as_stored_name(), subkey),
163176
(None, None) => self.name.as_stored_name().to_string(),
@@ -222,7 +235,9 @@ mod tests {
222235
"pref/foo"
223236
);
224237
assert_eq!(
225-
FlattenedAttrName::new(None, "foo").with_subkey("x").descriptor(),
238+
FlattenedAttrName::new(None, "foo")
239+
.with_subkey("x")
240+
.descriptor(),
226241
"foo.x"
227242
);
228243
assert_eq!(
@@ -325,8 +340,17 @@ mod tests {
325340
#[test]
326341
fn test_flattened_key_parse() {
327342
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"));
343+
assert_eq!(
344+
FlattenedAttrName::parse("prefix/key"),
345+
("prefix", "key").into()
346+
);
347+
assert_eq!(
348+
FlattenedAttrName::parse("key.subkey"),
349+
FlattenedAttrName::from("key").with_subkey("subkey")
350+
);
351+
assert_eq!(
352+
FlattenedAttrName::parse("prefix/key.subkey"),
353+
FlattenedAttrName::from(("prefix", "key")).with_subkey("subkey")
354+
);
331355
}
332356
}

src/crypto/attrs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod flattened_encrypted_attributes;
22
mod flattened_protected_attributes;
33
mod normalized_protected_attributes;
4+
pub(crate) use flattened_encrypted_attributes::FlattenedEncryptedAttributes;
45
pub(crate) use flattened_protected_attributes::FlattenedProtectedAttributes;
56
pub(crate) use normalized_protected_attributes::NormalizedProtectedAttributes;
6-
pub(crate) use flattened_encrypted_attributes::FlattenedEncryptedAttributes;

src/crypto/attrs/normalized_protected_attributes.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::collections::HashMap;
2-
use cipherstash_client::encryption::Plaintext;
31
use super::flattened_protected_attributes::{
42
FlattenedAttrName, FlattenedProtectedAttribute, FlattenedProtectedAttributes,
53
};
4+
use cipherstash_client::encryption::Plaintext;
5+
use std::collections::HashMap;
66

77
// FIXME: Remove this (only used for debugging)
88
#[derive(Debug)]
@@ -87,14 +87,16 @@ impl NormalizedProtectedAttributes {
8787
impl FromIterator<(NormalizedKey, NormalizedValue)> for NormalizedProtectedAttributes {
8888
fn from_iter<T: IntoIterator<Item = (NormalizedKey, NormalizedValue)>>(iter: T) -> Self {
8989
let values = iter.into_iter().collect();
90-
Self { values, prefix: None }
90+
Self {
91+
values,
92+
prefix: None,
93+
}
9194
}
9295
}
9396

9497
impl FromIterator<FlattenedProtectedAttribute> for NormalizedProtectedAttributes {
9598
fn from_iter<T: IntoIterator<Item = FlattenedProtectedAttribute>>(iter: T) -> Self {
96-
iter
97-
.into_iter()
99+
iter.into_iter()
98100
.fold(Self::new(), |mut acc, fpa| {
99101
match fpa.normalize_into_parts() {
100102
(plaintext, key, Some(subkey)) => {
@@ -104,7 +106,7 @@ impl FromIterator<FlattenedProtectedAttribute> for NormalizedProtectedAttributes
104106
acc.insert(key, plaintext);
105107
}
106108
}
107-
acc
109+
acc
108110
})
109111
.into()
110112
}

src/crypto/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ mod b64_encode;
33
mod sealed;
44
mod sealer;
55
mod unsealed;
6-
use std::borrow::Cow;
76
use crate::{
87
traits::{PrimaryKeyError, PrimaryKeyParts, ReadConversionError, WriteConversionError},
98
Identifiable, IndexType, PrimaryKey,
@@ -13,9 +12,11 @@ use cipherstash_client::{
1312
encryption::{
1413
compound_indexer::{CompoundIndex, ExactIndex},
1514
Encryption, EncryptionError, Plaintext, TypeParseError,
16-
}, vitur_client::DecryptError,
15+
},
16+
vitur_client::DecryptError,
1717
};
1818
use miette::Diagnostic;
19+
use std::borrow::Cow;
1920
use thiserror::Error;
2021

2122
// Re-exports

src/crypto/sealed.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::{
2-
crypto::attrs::FlattenedEncryptedAttributes, encrypted_table::TableEntry, traits::{ReadConversionError, WriteConversionError}, Decryptable, Identifiable
2+
crypto::attrs::FlattenedEncryptedAttributes,
3+
encrypted_table::TableEntry,
4+
traits::{ReadConversionError, WriteConversionError},
5+
Decryptable, Identifiable,
36
};
47
use aws_sdk_dynamodb::{primitives::Blob, types::AttributeValue};
58
use cipherstash_client::{
@@ -31,10 +34,16 @@ pub struct UnsealSpec<'a> {
3134
}
3235

3336
impl UnsealSpec<'static> {
34-
pub fn new_for_decryptable<D>() -> Self where D: Decryptable + Identifiable {
37+
pub fn new_for_decryptable<D>() -> Self
38+
where
39+
D: Decryptable + Identifiable,
40+
{
3541
Self {
3642
protected_attributes: D::protected_attributes(),
37-
sort_key_prefix: D::sort_key_prefix().as_deref().map(ToOwned::to_owned).unwrap_or(D::type_name().to_string()),
43+
sort_key_prefix: D::sort_key_prefix()
44+
.as_deref()
45+
.map(ToOwned::to_owned)
46+
.unwrap_or(D::type_name().to_string()),
3847
}
3948
}
4049
}
@@ -69,7 +78,6 @@ impl SealedTableEntry {
6978
spec: UnsealSpec<'_>,
7079
cipher: &Encryption<impl Credentials<Token = ServiceToken>>,
7180
) -> Result<Vec<Unsealed>, SealError> {
72-
7381
let UnsealSpec {
7482
protected_attributes,
7583
sort_key_prefix,
@@ -96,7 +104,10 @@ impl SealedTableEntry {
96104
.into_iter()
97105
.map(|unprotected| {
98106
// TODO: Create a new_from_unprotected method
99-
Ok(Unsealed::new_from_parts(NormalizedProtectedAttributes::new(), unprotected))
107+
Ok(Unsealed::new_from_parts(
108+
NormalizedProtectedAttributes::new(),
109+
unprotected,
110+
))
100111
})
101112
.collect()
102113
} else {
@@ -113,9 +124,7 @@ impl SealedTableEntry {
113124
.map(|fpa| fpa.into_iter().collect::<NormalizedProtectedAttributes>())
114125
.into_iter()
115126
.zip_eq(unprotected_items.into_iter())
116-
.map(|(fpa, unprotected)| {
117-
Ok(Unsealed::new_from_parts(fpa, unprotected))
118-
})
127+
.map(|(fpa, unprotected)| Ok(Unsealed::new_from_parts(fpa, unprotected)))
119128
.collect()
120129
}
121130
}
@@ -190,10 +199,7 @@ impl TryFrom<SealedTableEntry> for HashMap<String, AttributeValue> {
190199
}
191200

192201
item.0.attributes.into_iter().for_each(|(k, v)| {
193-
map.insert(
194-
k.into_stored_name(),
195-
v.into(),
196-
);
202+
map.insert(k.into_stored_name(), v.into());
197203
});
198204

199205
Ok(map)

0 commit comments

Comments
 (0)