Skip to content

Commit 6943115

Browse files
authored
add customer encryption key for upload and download blob request (#1304)
1 parent be63d1e commit 6943115

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed

sdk/core/src/headers/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ impl<T> AsHeaders for T
1515
where
1616
T: Header,
1717
{
18-
type Iter = std::option::IntoIter<(HeaderName, HeaderValue)>;
18+
type Iter = std::vec::IntoIter<(HeaderName, HeaderValue)>;
1919

2020
fn as_headers(&self) -> Self::Iter {
21-
Some((self.name(), self.value())).into_iter()
21+
vec![(self.name(), self.value())].into_iter()
2222
}
2323
}
2424

2525
impl<T> AsHeaders for Option<T>
2626
where
27-
T: Header,
27+
T: AsHeaders<Iter = std::vec::IntoIter<(HeaderName, HeaderValue)>>,
2828
{
29-
type Iter = std::option::IntoIter<(HeaderName, HeaderValue)>;
29+
type Iter = T::Iter;
3030

3131
fn as_headers(&self) -> Self::Iter {
3232
match self {
3333
Some(h) => h.as_headers(),
34-
None => None.into_iter(),
34+
None => vec![].into_iter(),
3535
}
3636
}
3737
}
@@ -361,3 +361,6 @@ pub const USER: HeaderName = HeaderName::from_static("x-ms-user");
361361
pub const USER_AGENT: HeaderName = HeaderName::from_static("user-agent");
362362
pub const VERSION: HeaderName = HeaderName::from_static("x-ms-version");
363363
pub const WWW_AUTHENTICATE: HeaderName = HeaderName::from_static("www-authenticate");
364+
pub const ENCRYPTION_ALGORITHM: HeaderName = HeaderName::from_static("x-ms-encryption-algorithm");
365+
pub const ENCRYPTION_KEY: HeaderName = HeaderName::from_static("x-ms-encryption-key");
366+
pub const ENCRYPTION_KEY_SHA256: HeaderName = HeaderName::from_static("x-ms-encryption-key-sha256");

sdk/storage_blobs/src/blob/operations/get_blob.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ operation! {
1515
?blob_versioning: BlobVersioning,
1616
?lease_id: LeaseId,
1717
?chunk_size: u64,
18+
?encryption_key: CPKInfo,
1819
?if_modified_since: IfModifiedSinceCondition,
1920
?if_match: IfMatchCondition,
2021
?if_tags: IfTags,
@@ -43,6 +44,7 @@ impl GetBlobBuilder {
4344
}
4445

4546
headers.add(this.lease_id);
47+
headers.add(this.encryption_key.as_ref());
4648
headers.add(this.if_modified_since);
4749
headers.add(this.if_match.clone());
4850
headers.add(this.if_tags.clone());

sdk/storage_blobs/src/blob/operations/put_block_blob.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ operation! {
1616
?access_tier: AccessTier,
1717
?tags: Tags,
1818
?lease_id: LeaseId,
19+
?encryption_key: CPKInfo,
1920
?encryption_scope: EncryptionScope,
2021
?if_modified_since: IfModifiedSinceCondition,
2122
?if_match: IfMatchCondition,
@@ -42,6 +43,7 @@ impl PutBlockBlobBuilder {
4243
}
4344
headers.add(self.access_tier);
4445
headers.add(self.lease_id);
46+
headers.add(self.encryption_key);
4547
headers.add(self.encryption_scope);
4648
headers.add(self.if_modified_since);
4749
headers.add(self.if_match);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use azure_core::headers::{self, AsHeaders, HeaderName, HeaderValue};
2+
3+
const DEFAULT_ENCRYPTION_ALGORITHM: &str = "AES256";
4+
5+
#[derive(Clone, Debug)]
6+
pub struct CPKInfo {
7+
encryption_key: String,
8+
encryption_key_sha256: String,
9+
10+
// only support AES256
11+
encryption_algorithm: Option<String>,
12+
}
13+
14+
impl CPKInfo {
15+
pub fn new(key: String, key_sha256: String, algorithm: Option<String>) -> Self {
16+
Self {
17+
encryption_key: key,
18+
encryption_key_sha256: key_sha256,
19+
20+
encryption_algorithm: algorithm,
21+
}
22+
}
23+
}
24+
25+
impl From<(String, String)> for CPKInfo {
26+
fn from(s: (String, String)) -> Self {
27+
Self::new(s.0, s.1, None)
28+
}
29+
}
30+
31+
impl From<(String, String, String)> for CPKInfo {
32+
fn from(s: (String, String, String)) -> Self {
33+
Self::new(s.0, s.1, Some(s.2))
34+
}
35+
}
36+
37+
impl AsHeaders for CPKInfo {
38+
type Iter = std::vec::IntoIter<(HeaderName, HeaderValue)>;
39+
40+
fn as_headers(&self) -> Self::Iter {
41+
let algorithm = self
42+
.encryption_algorithm
43+
.as_deref()
44+
.unwrap_or(DEFAULT_ENCRYPTION_ALGORITHM)
45+
.to_owned();
46+
let headers = vec![
47+
(headers::ENCRYPTION_ALGORITHM, algorithm.into()),
48+
(
49+
headers::ENCRYPTION_KEY,
50+
self.encryption_key.to_owned().into(),
51+
),
52+
(
53+
headers::ENCRYPTION_KEY_SHA256,
54+
self.encryption_key_sha256.to_owned().into(),
55+
),
56+
];
57+
headers.into_iter()
58+
}
59+
}
60+
61+
impl AsHeaders for &CPKInfo {
62+
type Iter = <CPKInfo as AsHeaders>::Iter;
63+
64+
fn as_headers(&self) -> Self::Iter {
65+
(*self).as_headers()
66+
}
67+
}

sdk/storage_blobs/src/options/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod block_id;
1414
mod condition_append_position;
1515
mod condition_max_size;
1616
mod delete_snapshot_method;
17+
mod encryption_key;
1718
mod encryption_scope;
1819
mod hash;
1920
mod rehydrate_policy;
@@ -33,6 +34,7 @@ pub use block_id::BlockId;
3334
pub use condition_append_position::ConditionAppendPosition;
3435
pub use condition_max_size::ConditionMaxSize;
3536
pub use delete_snapshot_method::DeleteSnapshotsMethod;
37+
pub use encryption_key::CPKInfo;
3638
pub use encryption_scope::EncryptionScope;
3739
pub use hash::Hash;
3840
pub use rehydrate_policy::RehydratePriority;

0 commit comments

Comments
 (0)