Skip to content

Commit

Permalink
fix(azure): API Version should be specified by clients
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo committed May 31, 2023
1 parent 2c2a771 commit 00b18e3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 40 deletions.
4 changes: 0 additions & 4 deletions src/azure/constants.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// Headers used in azure services.
pub const X_MS_DATE: &str = "x-ms-date";
pub const X_MS_VERSION: &str = "x-ms-version";
pub const CONTENT_MD5: &str = "content-md5";

// Env values used in azure services.
pub const AZURE_VERSION: &str = "2019-12-12";
41 changes: 5 additions & 36 deletions src/azure/storage/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ use crate::time::DateTime;
/// - [Authorize with Shared Key](https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key)
#[derive(Debug, Default)]
pub struct Signer {
/// whether to omit service version or not
omit_service_version: bool,
time: Option<DateTime>,
}

Expand All @@ -36,12 +34,6 @@ impl Signer {
Self::default()
}

/// set the signer to omitting service version
pub fn omit_service_version(mut self) -> Self {
self.omit_service_version = true;
self
}

/// Specify the signing time.
///
/// # Note
Expand Down Expand Up @@ -73,11 +65,7 @@ impl Signer {
}
SigningMethod::Header => {
ctx.headers
.insert(X_MS_VERSION, AZURE_VERSION.to_string().parse()?);
if self.omit_service_version {
ctx.headers
.insert(X_MS_DATE, format_http_date(time::now()).parse()?);
}
.insert(X_MS_DATE, format_http_date(time::now()).parse()?);
ctx.headers.insert(AUTHORIZATION, {
let mut value: HeaderValue = format!("Bearer {}", token).parse()?;
value.set_sensitive(true);
Expand All @@ -100,8 +88,7 @@ impl Signer {
}
SigningMethod::Header => {
let now = self.time.unwrap_or_else(time::now);
let string_to_sign =
string_to_sign(&mut ctx, ak, now, self.omit_service_version)?;
let string_to_sign = string_to_sign(&mut ctx, ak, now)?;
let signature =
base64_hmac_sha256(&base64_decode(sk), string_to_sign.as_bytes());

Expand Down Expand Up @@ -192,12 +179,7 @@ impl Signer {
/// ## Reference
///
/// - [Blob, Queue, and File Services (Shared Key authorization)](https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key)
fn string_to_sign(
ctx: &mut SigningContext,
ak: &str,
now: DateTime,
omit_service_version: bool,
) -> Result<String> {
fn string_to_sign(ctx: &mut SigningContext, ak: &str, now: DateTime) -> Result<String> {
let mut s = String::with_capacity(128);

writeln!(&mut s, "{}", ctx.method.as_str())?;
Expand Down Expand Up @@ -225,11 +207,7 @@ fn string_to_sign(
ctx.header_get_or_default(&IF_UNMODIFIED_SINCE)?
)?;
writeln!(&mut s, "{}", ctx.header_get_or_default(&RANGE)?)?;
writeln!(
&mut s,
"{}",
canonicalize_header(ctx, now, omit_service_version)?
)?;
writeln!(&mut s, "{}", canonicalize_header(ctx, now)?)?;
write!(&mut s, "{}", canonicalize_resource(ctx, ak))?;

debug!("string to sign: {}", &s);
Expand All @@ -240,18 +218,9 @@ fn string_to_sign(
/// ## Reference
///
/// - [Constructing the canonicalized headers string](https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key#constructing-the-canonicalized-headers-string)
fn canonicalize_header(
ctx: &mut SigningContext,
now: DateTime,
omit_service_version: bool,
) -> Result<String> {
fn canonicalize_header(ctx: &mut SigningContext, now: DateTime) -> Result<String> {
ctx.headers
.insert(X_MS_DATE, format_http_date(now).parse()?);
if !omit_service_version {
// Insert x_ms_version header.
ctx.headers
.insert(X_MS_VERSION, AZURE_VERSION.to_string().parse()?);
}

Ok(SigningContext::header_to_string(
ctx.header_to_vec_with_prefix("x-ms-"),
Expand Down

1 comment on commit 00b18e3

@github-actions
Copy link

Choose a reason for hiding this comment

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

Deploy preview for reqsign ready!

✅ Preview
https://reqsign-f4rfqlwqp-xuanwo.vercel.app

Built with commit 00b18e3.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.