Skip to content

Commit eccb473

Browse files
authored
enable setting multiple SAS permissions at once (#432)
1 parent 2f1ca2e commit eccb473

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

sdk/storage/examples/copy_blob.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
6161
.with_resource_type(SasResourceType::Object)
6262
.with_start(now)
6363
.with_expiry(later)
64-
.with_permissions(SasPermissions::Read)
64+
.with_permissions(SasPermissions {
65+
read: true,
66+
..Default::default()
67+
})
6568
.with_protocol(SasProtocol::HttpHttps)
6669
.finalize();
6770
println!("token: '{}'", sas.token());

sdk/storage/examples/shared_access_signature.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ fn code() -> Result<(), Box<dyn Error + Sync + Send>> {
4040
.with_resource_type(SasResourceType::Object)
4141
.with_start(now)
4242
.with_expiry(later)
43-
.with_permissions(SasPermissions::Read)
43+
.with_permissions(SasPermissions {
44+
read: true,
45+
..Default::default()
46+
})
4447
.with_protocol(SasProtocol::HttpHttps)
4548
.finalize();
4649
println!("token: '{}'", sas.token());

sdk/storage/src/core/shared_access_signature.rs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,30 +99,49 @@ impl fmt::Display for SasResourceType {
9999
}
100100

101101
/// 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)).
102-
#[derive(Copy, Clone)]
103-
pub enum SasPermissions {
104-
Read,
105-
Write,
106-
Delete,
107-
List,
108-
Add,
109-
Create,
110-
Update,
111-
Process,
102+
#[derive(Copy, Clone, Default)]
103+
pub struct SasPermissions {
104+
pub read: bool,
105+
pub write: bool,
106+
pub delete: bool,
107+
pub list: bool,
108+
pub add: bool,
109+
pub create: bool,
110+
pub update: bool,
111+
pub process: bool,
112112
}
113113

114114
impl fmt::Display for SasPermissions {
115115
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116-
match *self {
117-
SasPermissions::Read => write!(f, "r"),
118-
SasPermissions::Write => write!(f, "w"),
119-
SasPermissions::Delete => write!(f, "d"),
120-
SasPermissions::List => write!(f, "l"),
121-
SasPermissions::Add => write!(f, "a"),
122-
SasPermissions::Create => write!(f, "c"),
123-
SasPermissions::Update => write!(f, "u"),
124-
SasPermissions::Process => write!(f, "p"),
116+
// NOTE: order *must* be `racwdxltmeop` per documentation:
117+
// https://docs.microsoft.com/en-us/rest/api/storageservices/create-service-sas#specifying-permissions
118+
119+
if self.read {
120+
write!(f, "r")?;
121+
}
122+
if self.add {
123+
write!(f, "a")?;
124+
}
125+
if self.create {
126+
write!(f, "c")?;
127+
}
128+
if self.write {
129+
write!(f, "w")?;
125130
}
131+
if self.delete {
132+
write!(f, "d")?;
133+
}
134+
if self.list {
135+
write!(f, "l")?;
136+
}
137+
if self.update {
138+
write!(f, "u")?;
139+
}
140+
if self.process {
141+
write!(f, "p")?;
142+
}
143+
144+
Ok(())
126145
}
127146
}
128147

0 commit comments

Comments
 (0)