Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add customer encryption key for upload and download blob request #1304

Merged
merged 4 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions sdk/core/src/headers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ impl Headers {
}
}

/// Add headers to the headers collection
pub fn add_ref<H>(&mut self, header: &H)
where
H: AsHeaders,
{
for (key, value) in header.as_headers() {
self.insert(key, value);
}
}

/// Iterate over all the header name/value pairs
pub fn iter(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue)> {
self.0.iter()
Expand Down Expand Up @@ -361,3 +371,6 @@ pub const USER: HeaderName = HeaderName::from_static("x-ms-user");
pub const USER_AGENT: HeaderName = HeaderName::from_static("user-agent");
pub const VERSION: HeaderName = HeaderName::from_static("x-ms-version");
pub const WWW_AUTHENTICATE: HeaderName = HeaderName::from_static("www-authenticate");
pub const ENCRYPTION_ALGORITHM: HeaderName = HeaderName::from_static("x-ms-encryption-algorithm");
pub const ENCRYPTION_KEY: HeaderName = HeaderName::from_static("x-ms-encryption-key");
pub const ENCRYPTION_KEY_SHA256: HeaderName = HeaderName::from_static("x-ms-encryption-key-sha256");
4 changes: 4 additions & 0 deletions sdk/storage_blobs/src/blob/operations/get_blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ operation! {
?blob_versioning: BlobVersioning,
?lease_id: LeaseId,
?chunk_size: u64,
?encryption_key: CPKInfo,
?if_modified_since: IfModifiedSinceCondition,
?if_match: IfMatchCondition,
?if_tags: IfTags,
Expand Down Expand Up @@ -43,6 +44,9 @@ impl GetBlobBuilder {
}

headers.add(this.lease_id);
if let Some(cpk_info) = &this.encryption_key {
headers.add_ref(cpk_info);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you discuss this choice, instead of using

Suggested change
if let Some(cpk_info) = &this.encryption_key {
headers.add_ref(cpk_info);
}
headers.add(this.encryption_key);

This is made possible by the following:

impl<T> AsHeaders for Option<T>
where
T: Header,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review.

The struct CPKInfo contains 3 headers: EncryptionKey, EncryptionKeySha256 and EncryptionAlgorithm. That's because once the EncryptionKey is specified, the other 2 headers must be provided. (from azure-sdk-for-go)

The this.encryption_key is wrapped by Option, for which I find there has been an implement:

// ./sdk/core/src/headers/mod.rs:L25
impl<T> AsHeaders for Option<T>
where
    T: Header,
{
    type Iter = std::option::IntoIter<(HeaderName, HeaderValue)>;

    fn as_headers(&self) -> Self::Iter {
        match self {
            Some(h) => h.as_headers(),
            None => None.into_iter(),
        }
    }
}

The T has a trait bound Header, which has the function name and value. But the trait Header can not be implemented by CPKInfo. So I cannot directly use headers.add(this.encryption_key);.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@demoray PTAL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can just specify the AsHeaders::Iter to expand the scope. See the code as followes:

impl<T> AsHeaders for Option<T>
where
    T: AsHeaders<Iter = std::option::IntoIter<(HeaderName, HeaderValue)>>,
{
    type Iter = T::Iter;

    fn as_headers(&self) -> Self::Iter {
        match self {
            Some(h) => h.as_headers(),
            None => None.into_iter(),
        }
    }
}

headers.add(this.if_modified_since);
headers.add(this.if_match.clone());
headers.add(this.if_tags.clone());
Expand Down
4 changes: 4 additions & 0 deletions sdk/storage_blobs/src/blob/operations/put_block_blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ operation! {
?access_tier: AccessTier,
?tags: Tags,
?lease_id: LeaseId,
?encryption_key: CPKInfo,
?encryption_scope: EncryptionScope,
?if_modified_since: IfModifiedSinceCondition,
?if_match: IfMatchCondition,
Expand All @@ -42,6 +43,9 @@ impl PutBlockBlobBuilder {
}
headers.add(self.access_tier);
headers.add(self.lease_id);
if let Some(cpk_info) = &self.encryption_key {
headers.add_ref(cpk_info);
}
headers.add(self.encryption_scope);
headers.add(self.if_modified_since);
headers.add(self.if_match);
Expand Down
59 changes: 59 additions & 0 deletions sdk/storage_blobs/src/options/encryption_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use azure_core::headers::{self, AsHeaders, HeaderName, HeaderValue};

const DEFAULT_ENCRYPTION_ALGORITHM: &'static str = "AES256";
demoray marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Clone, Debug)]
pub struct CPKInfo {
encryption_key: String,
encryption_key_sha256: String,

// only support AES256
encryption_algorithm: Option<String>,
}

impl CPKInfo {
pub fn new(key: String, key_sha256: String, algorithm: Option<String>) -> Self {
Self {
encryption_key: key,
encryption_key_sha256: key_sha256,

encryption_algorithm: algorithm,
}
}
}

impl From<(String, String)> for CPKInfo {
fn from(s: (String, String)) -> Self {
Self::new(s.0, s.1, None)
}
}

impl From<(String, String, String)> for CPKInfo {
fn from(s: (String, String, String)) -> Self {
Self::new(s.0, s.1, Some(s.2))
}
}

impl AsHeaders for CPKInfo {
type Iter = std::vec::IntoIter<(HeaderName, HeaderValue)>;

fn as_headers(&self) -> Self::Iter {
let algorithm = self
.encryption_algorithm
.as_deref()
.unwrap_or(DEFAULT_ENCRYPTION_ALGORITHM)
.to_owned();
let headers = vec![
(headers::ENCRYPTION_ALGORITHM, algorithm.into()),
(
headers::ENCRYPTION_KEY,
self.encryption_key.to_owned().into(),
),
(
headers::ENCRYPTION_KEY_SHA256,
self.encryption_key_sha256.to_owned().into(),
),
];
headers.into_iter()
}
}
2 changes: 2 additions & 0 deletions sdk/storage_blobs/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod block_id;
mod condition_append_position;
mod condition_max_size;
mod delete_snapshot_method;
mod encryption_key;
mod encryption_scope;
mod hash;
mod rehydrate_policy;
Expand All @@ -33,6 +34,7 @@ pub use block_id::BlockId;
pub use condition_append_position::ConditionAppendPosition;
pub use condition_max_size::ConditionMaxSize;
pub use delete_snapshot_method::DeleteSnapshotsMethod;
pub use encryption_key::CPKInfo;
pub use encryption_scope::EncryptionScope;
pub use hash::Hash;
pub use rehydrate_policy::RehydratePriority;
Expand Down