Skip to content

Commit 93beeb8

Browse files
fsackurwyunchi-ms
andauthored
Fix invalid SAS token from New-AzServiceBusAuthorizationRuleSASToken and New-AzEventHubAuthorizationRuleSASToken (#14535)
* Fix #12975 New-AzServiceBusAuthorizationRuleSASToken returns invalid token * Tidy * Update changelog * Fix #14534 New-AzEventHubAuthorizationRuleSASToken returns invalid token when StartTime is provided * Tidy * Update changelog * Update ChangeLog.md * Update ChangeLog.md * Fix syntax errors Co-authored-by: Yunchi Wang <[email protected]>
1 parent 4ade22f commit 93beeb8

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

src/EventHub/EventHub/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed that `New-AzServiceBusAuthorizationRuleSASToken` returns invalid token. [#12975]
2122

2223
## Version 1.7.1
2324
* Fixed Cluster commands for EventHub cluster without tags

src/EventHub/EventHub/Cmdlets/AuthorizationRule/NewAzureEventhubAuthorizationRuleSASToken.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,24 @@ public override void ExecuteCmdlet()
9191
}
9292
}
9393

94-
TimeSpan secondsFromBaseTime = ExpiryTime.Value.Subtract(EpochTime);
95-
long seconds = Convert.ToInt64(secondsFromBaseTime.TotalSeconds, CultureInfo.InvariantCulture);
96-
string stringToSign = StartTime.HasValue ? StartTime.ToString() + "\n" + System.Web.HttpUtility.UrlEncode(resourceUri) + "\n" + seconds : System.Web.HttpUtility.UrlEncode(resourceUri) + "\n" + seconds;
94+
var encodedResourceUri = System.Web.HttpUtility.UrlEncode(resourceUri);
95+
var expiry = Convert.ToInt64(ExpiryTime.Value.Subtract(EpochTime).TotalSeconds, CultureInfo.InvariantCulture);
96+
var stringToSign = StartTime == null ? "" : Convert.ToInt64(StartTime.Value.Subtract(EpochTime).TotalSeconds, CultureInfo.InvariantCulture) + "\n";
97+
stringToSign = stringToSign + encodedResourceUri + "\n" + expiry;
98+
9799
HMACSHA256 hmac = new HMACSHA256(System.Text.Encoding.UTF8.GetBytes(sakey));
98100
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
99-
string sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), seconds, KeyType);
101+
102+
string sasToken = String.Format(CultureInfo.InvariantCulture,
103+
"SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
104+
HttpUtility.UrlEncode(resourceUri),
105+
HttpUtility.UrlEncode(signature),
106+
expiry,
107+
KeyType);
108+
100109
PSSharedAccessSignatureAttributes psSastoken = new PSSharedAccessSignatureAttributes(sasToken);
101-
WriteObject(psSastoken, true);
102110

111+
WriteObject(psSastoken, true);
103112
}
104113
catch (Management.EventHub.Models.ErrorResponseException ex)
105114
{

src/ServiceBus/ServiceBus/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Additional information about change #1
2020
-->
2121
## Upcoming Release
22+
* Fixed that `New-AzServiceBusAuthorizationRuleSASToken` returns invalid token. [#12975]
2223

2324
## Version 1.4.1
2425
* Update references in .psd1 to use relative path

src/ServiceBus/ServiceBus/Cmdlets/AuthorizationRule/NewAzureServiceBusAuthorizationRuleSASToken.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ public override void ExecuteCmdlet()
5555
{
5656
LocalResourceIdentifier identifier = new LocalResourceIdentifier(AuthorizationRuleId);
5757
string resourceUri = string.Empty, strPolicyName = string.Empty, sakey = string.Empty;
58+
DateTime EpochTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
5859

5960
PSListKeysAttributes listkeys;
6061
if (identifier.ParentResource1 != null && AuthorizationRuleId.Contains("topics"))
61-
{
62+
{
6263
listkeys = Client.GetTopicKey(identifier.ResourceGroupName, identifier.ParentResource, identifier.ParentResource1, identifier.ResourceName);
6364
}
6465
else if (identifier.ParentResource1 != null && AuthorizationRuleId.Contains("queues"))
@@ -94,13 +95,24 @@ public override void ExecuteCmdlet()
9495
}
9596
}
9697

97-
string stringToSign = StartTime.HasValue ? StartTime.ToString() + "\n" + System.Web.HttpUtility.UrlEncode(resourceUri) + "\n" + ExpiryTime.ToString() : System.Web.HttpUtility.UrlEncode(resourceUri) + "\n" + ExpiryTime.ToString();
98+
var encodedResourceUri = System.Web.HttpUtility.UrlEncode(resourceUri);
99+
var expiry = Convert.ToInt64(ExpiryTime.Value.Subtract(EpochTime).TotalSeconds, CultureInfo.InvariantCulture);
100+
var stringToSign = StartTime == null ? "" : Convert.ToInt64(StartTime.Value.Subtract(EpochTime).TotalSeconds, CultureInfo.InvariantCulture) + "\n";
101+
stringToSign = stringToSign + encodedResourceUri + "\n" + expiry;
102+
98103
HMACSHA256 hmac = new HMACSHA256(System.Text.Encoding.UTF8.GetBytes(sakey));
99104
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
100-
string sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), ExpiryTime, KeyType);
105+
106+
string sasToken = String.Format(CultureInfo.InvariantCulture,
107+
"SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
108+
HttpUtility.UrlEncode(resourceUri),
109+
HttpUtility.UrlEncode(signature),
110+
ExpiryTime,
111+
KeyType);
112+
101113
PSSharedAccessSignatureAttributes psSastoken = new PSSharedAccessSignatureAttributes(sasToken);
102-
WriteObject(psSastoken, true);
103114

115+
WriteObject(psSastoken, true);
104116
}
105117
catch (Management.ServiceBus.Models.ErrorResponseException ex)
106118
{

0 commit comments

Comments
 (0)