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

address clippy warnings for azure_storage #1406

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
81 changes: 39 additions & 42 deletions sdk/storage/src/authorization/authorization_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ impl Policy for AuthorizationPolicy {
let auth = generate_authorization(
request.headers(),
request.url(),
request.method(),
*request.method(),
account,
key,
ctx.get()
*ctx.get()
.expect("ServiceType must be in the Context at this point"),
)?;
request.insert_header(AUTHORIZATION, auth);
Expand Down Expand Up @@ -87,10 +87,10 @@ impl Policy for AuthorizationPolicy {
fn generate_authorization(
h: &Headers,
u: &Url,
method: &Method,
method: Method,
account: &str,
key: &str,
service_type: &ServiceType,
service_type: ServiceType,
) -> azure_core::Result<String> {
let str_to_sign = string_to_sign(h, u, method, account, service_type);
let auth = crate::hmac::sign(&str_to_sign, key).context(
Expand All @@ -108,46 +108,43 @@ fn add_if_exists<'a>(h: &'a Headers, key: &HeaderName) -> &'a str {
fn string_to_sign(
h: &Headers,
u: &Url,
method: &Method,
method: Method,
account: &str,
service_type: &ServiceType,
service_type: ServiceType,
) -> String {
match service_type {
ServiceType::Table => {
format!(
"{}\n{}\n{}\n{}\n{}",
method.as_ref(),
add_if_exists(h, &CONTENT_MD5),
add_if_exists(h, &CONTENT_TYPE),
add_if_exists(h, &MS_DATE),
canonicalized_resource_table(account, u)
)
}
_ => {
// content length must only be specified if != 0
// this is valid from 2015-02-21
let content_length = h
.get_optional_str(&CONTENT_LENGTH)
.filter(|&v| v != "0")
.unwrap_or_default();
format!(
"{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}{}",
method.as_ref(),
add_if_exists(h, &CONTENT_ENCODING),
add_if_exists(h, &CONTENT_LANGUAGE),
content_length,
add_if_exists(h, &CONTENT_MD5),
add_if_exists(h, &CONTENT_TYPE),
add_if_exists(h, &DATE),
add_if_exists(h, &IF_MODIFIED_SINCE),
add_if_exists(h, &IF_MATCH),
add_if_exists(h, &IF_NONE_MATCH),
add_if_exists(h, &IF_UNMODIFIED_SINCE),
add_if_exists(h, &RANGE),
canonicalize_header(h),
canonicalized_resource(account, u)
)
}
if matches!(service_type, ServiceType::Table) {
format!(
"{}\n{}\n{}\n{}\n{}",
method.as_ref(),
add_if_exists(h, &CONTENT_MD5),
add_if_exists(h, &CONTENT_TYPE),
add_if_exists(h, &MS_DATE),
canonicalized_resource_table(account, u)
)
} else {
// content length must only be specified if != 0
// this is valid from 2015-02-21
let content_length = h
.get_optional_str(&CONTENT_LENGTH)
.filter(|&v| v != "0")
.unwrap_or_default();
format!(
"{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}{}",
method.as_ref(),
add_if_exists(h, &CONTENT_ENCODING),
add_if_exists(h, &CONTENT_LANGUAGE),
content_length,
add_if_exists(h, &CONTENT_MD5),
add_if_exists(h, &CONTENT_TYPE),
add_if_exists(h, &DATE),
add_if_exists(h, &IF_MODIFIED_SINCE),
add_if_exists(h, &IF_MATCH),
add_if_exists(h, &IF_NONE_MATCH),
add_if_exists(h, &IF_UNMODIFIED_SINCE),
add_if_exists(h, &RANGE),
canonicalize_header(h),
canonicalized_resource(account, u)
)
}
}

Expand Down
6 changes: 3 additions & 3 deletions sdk/storage/src/cloud_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ impl CloudLocation {

pub fn credentials(&self) -> &StorageCredentials {
match self {
CloudLocation::Public { credentials, .. } => credentials,
CloudLocation::China { credentials, .. } => credentials,
CloudLocation::Public { credentials, .. }
| CloudLocation::China { credentials, .. }
| CloudLocation::Custom { credentials, .. } => credentials,
CloudLocation::Emulator { .. } => &EMULATOR_CREDENTIALS,
CloudLocation::Custom { credentials, .. } => credentials,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage/src/connection_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl<'a> ConnectionString<'a> {
account_name: Some(account),
account_key: Some(key),
..
} => Ok(StorageCredentials::Key(account.to_string(), key.to_string())),
} => Ok(StorageCredentials::Key((*account).to_string(), (*key).to_string())),
_ => {
Err(Error::message(ErrorKind::Credential,
"Could not create a `StorageCredentail` from the provided connection string. Please validate that you have specified a means of authentication (key, SAS, etc.)."
Expand Down
7 changes: 3 additions & 4 deletions sdk/storage/src/parsing_xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ pub fn traverse<'a>(
if vec.is_empty() {
if (x + 1) >= path.len() && ignore_empty_leaf {
return Ok(vec);
} else {
return Err(Error::with_message(ErrorKind::Other, || {
format!("path not found: {}", *item)
}));
}
return Err(Error::with_message(ErrorKind::Other, || {
format!("path not found: {}", *item)
}));
}

if vec.len() > 1 && (x + 1) < path.len() {
Expand Down
5 changes: 3 additions & 2 deletions sdk/storage/src/shared_access_signature/account_sas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl fmt::Display for AccountSasResourceType {
}

/// Indicate which operations a `key_client` may perform on the resource ([Azure documentation](https://docs.microsoft.com/rest/api/storageservices/create-service-sas#specifying-permissions)).
#[allow(clippy::struct_excessive_bools)]
#[derive(Copy, Clone, Default)]
pub struct AccountSasPermissions {
pub read: bool,
Expand Down Expand Up @@ -181,12 +182,12 @@ impl AccountSharedAccessSignature {
self.permissions,
self.resource,
self.resource_type,
self.start.map_or("".to_string(), format_date),
self.start.map_or(String::new(), format_date),
format_date(self.expiry),
self.ip.clone().unwrap_or_default(),
self.protocol
.as_ref()
.map_or("".to_string(), |v| v.to_string()),
.map_or(String::new(), ToString::to_string),
self.version,
);

Expand Down
19 changes: 10 additions & 9 deletions sdk/storage/src/shared_access_signature/service_sas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl fmt::Display for BlobSignedResource {
}
}

#[allow(clippy::struct_excessive_bools)]
#[derive(Default)]
pub struct BlobSasPermissions {
pub read: bool, // r - Container | Directory | Blob
Expand Down Expand Up @@ -136,23 +137,23 @@ impl BlobSharedAccessSignature {
fn sign(&self) -> String {
let content = vec![
self.permissions.to_string(),
self.start.map_or("".to_string(), format_date),
self.start.map_or(String::new(), format_date),
format_date(self.expiry),
self.canonicalized_resource.clone(),
self.identifier
.as_ref()
.unwrap_or(&"".to_string())
.unwrap_or(&String::new())
.to_string(),
self.ip.as_ref().unwrap_or(&"".to_string()).to_string(),
self.ip.as_ref().unwrap_or(&String::new()).to_string(),
self.protocol.map(|x| x.to_string()).unwrap_or_default(),
SERVICE_SAS_VERSION.to_string(),
self.resource.to_string(),
"".to_string(), // snapshot time
"".to_string(), // rscd
"".to_string(), // rscc
"".to_string(), // rsce
"".to_string(), // rscl
"".to_string(), // rsct
String::new(), // snapshot time
String::new(), // rscd
String::new(), // rscc
String::new(), // rsce
String::new(), // rscl
String::new(), // rsct
];

hmac::sign(&content.join("\n"), &self.key).expect("HMAC signing failed")
Expand Down