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

Fix SAS token generation for directory-scoped access #1277

Merged
Merged
Changes from 2 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
57 changes: 57 additions & 0 deletions sdk/storage/src/shared_access_signature/service_sas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub struct BlobSharedAccessSignature {
identifier: Option<String>,
ip: Option<String>,
protocol: Option<SasProtocol>,
signed_directory_depth: Option<u32>, // sdd
dmweis marked this conversation as resolved.
Show resolved Hide resolved
}

impl BlobSharedAccessSignature {
Expand All @@ -120,6 +121,7 @@ impl BlobSharedAccessSignature {
identifier: None,
ip: None,
protocol: None,
signed_directory_depth: None,
}
}

Expand All @@ -128,6 +130,7 @@ impl BlobSharedAccessSignature {
identifier: String => Some(identifier),
ip: String => Some(ip),
protocol: SasProtocol => Some(protocol),
signed_directory_depth: u32 => Some(signed_directory_depth),
dmweis marked this conversation as resolved.
Show resolved Hide resolved
}

fn sign(&self) -> String {
Expand Down Expand Up @@ -179,9 +182,63 @@ impl SasToken for BlobSharedAccessSignature {
elements.push(format!("spr={protocol}"))
}

if let Some(signed_directory_depth) = &self.signed_directory_depth {
elements.push(format!("sdd={signed_directory_depth}"))
}

let sig = self.sign();
elements.push(format!("sig={}", format_form(sig)));

elements.join("&")
}
}

#[cfg(test)]
mod test {
use super::*;
use time::Duration;

const MOCK_SECRET_KEY: &str = "RZfi3m1W7eyQ5zD4ymSmGANVdJ2SDQmg4sE89SW104s=";
const MOCK_CANONICALIZED_RESOURCE: &str = "/blob/STORAGE_ACCOUNT_NAME/CONTAINER_NAME/";

#[test]
fn test_blob_scoped_sas_token() {
let permissions = BlobSasPermissions {
read: true,
..Default::default()
};
let signed_token = BlobSharedAccessSignature::new(
String::from(MOCK_SECRET_KEY),
String::from(MOCK_CANONICALIZED_RESOURCE),
permissions,
OffsetDateTime::UNIX_EPOCH + Duration::days(7),
BlobSignedResource::Blob,
)
.token();
// BlobSignedResource::Blob
assert!(signed_token.contains("sr=b"));
// this assert will be a bad idea if we ever add a new field that ends with "sdd"
assert!(!signed_token.contains("sdd="));
Copy link
Contributor

Choose a reason for hiding this comment

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

As this example uses a constant expire date of 1970-01-08, the signed token result should always be the same. What are your thoughts for just having a const for the expected result?

Otherwise, to address your comment, this could also naively split on &, as the SasToken in the example should not result in a & except as a delim.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea!

I added both better splitting and an assert against a hardcoded string. This way you get more clear failures if things break but have a wider test too.

If people add more tests later or other things break the hardcoded tests they can remove one or the other

}

#[test]
fn test_directory_scoped_sas_token() {
let permissions = BlobSasPermissions {
read: true,
..Default::default()
};
let signed_token = BlobSharedAccessSignature::new(
String::from(MOCK_SECRET_KEY),
String::from(MOCK_CANONICALIZED_RESOURCE),
permissions,
OffsetDateTime::UNIX_EPOCH + Duration::days(7),
BlobSignedResource::Directory,
)
.signed_directory_depth(2_u32)
dmweis marked this conversation as resolved.
Show resolved Hide resolved
.token();
// BlobSignedResource::Blob
assert!(signed_token.contains("sr=d"));
// signed_directory_depth = 2
assert!(signed_token.contains("sdd=2"));
}
}