Skip to content

Commit f237327

Browse files
authored
[Storage] Fix parser logic when downloading a managed disk using SAS and bearer token (#24113)
* Fix parser logic when downloading a managed disk * Fix parser logic of downloading managed disk * Update changelog * update debug log and parsing logic
1 parent 6d8cc0c commit f237327

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

src/Storage/Storage.Management/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed parser logic when downloading blob from managed disk account with Sas Uri and bearer token on Linux and MacOS
22+
- `Get-AzStorageBlobContent`
2123

2224
## Version 6.1.1
2325
* Removed some code branches referencing Microsoft.Azure.Storage.Blob

src/Storage/Storage/Blob/Cmdlet/GetAzureStorageBlobContent.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ internal void GetBlobContent(string blobUri, string fileName)
375375
}
376376
catch (global::Azure.RequestFailedException e) when (e.Status == 401) // need diskRP bearer token
377377
{
378-
string audience = Util.GetAudienceFrom401ExceptionMessage(e.Message);
378+
string audience = GetAudienceFrom401ExceptionMessage(e);
379379
if (audience != null)
380380
{
381381
WriteDebugLog(string.Format("Need bearer token with audience {0} to access the blob, so will generate bearer token and resend the request.", audience));
@@ -433,6 +433,39 @@ internal string GetFullReceiveFilePath(string fileName, string blobName, DateTim
433433
return filePath;
434434
}
435435

436+
/// <summary>
437+
/// When request doesn't container a proper bearer token, server will return 401 error include the audience of the required bearer token.
438+
/// This function will get the audience of bearer token from SDK exception message.
439+
/// If server not return audience, will output null.
440+
/// </summary>
441+
private string GetAudienceFrom401ExceptionMessage(global::Azure.RequestFailedException exception)
442+
{
443+
string authenticateHeaderName = "WWW-Authenticate";
444+
string audience = null;
445+
foreach (var header in exception.GetRawResponse().Headers)
446+
{
447+
string headerName = header.Name;
448+
if (headerName.StartsWith(authenticateHeaderName))
449+
{
450+
string headerValue = header.Value;
451+
WriteDebugLog(string.Format("Found header name: {0}, value: {1}", headerName, headerValue));
452+
string audienceName = "resource_id=";
453+
try
454+
{
455+
string authText = headerValue.Split(new string[] { audienceName }, StringSplitOptions.None)[1];
456+
audience = authText.Split(new string[] { " " }, StringSplitOptions.None)[0];
457+
WriteDebugLog(string.Format("Found audience: {0}", audience));
458+
return audience;
459+
}
460+
catch
461+
{
462+
WriteDebugLog(string.Format("Failed to parse the header. The parsing result has length of {0}", audience.Length));
463+
}
464+
}
465+
}
466+
return null;
467+
}
468+
436469
/// <summary>
437470
/// Create blob client and storage service management channel if need to.
438471
/// </summary>

src/Storage/Storage/Common/Util.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -722,32 +722,6 @@ public static string GetSASStringWithoutQuestionMark(string sas)
722722
return sas;
723723
}
724724

725-
/// <summary>
726-
/// When request doesn't container a proper bearer token, server will return 401 error include the audience of the required bearer token.
727-
/// This function will get the audience of bearer token from SDK exception message.
728-
/// If server not return audience, will output null.
729-
/// </summary>
730-
public static string GetAudienceFrom401ExceptionMessage(string exceptionMessage)
731-
{
732-
string authenticateHeaderName = "WWW-Authenticate";
733-
string audienceName = "resource_id=";
734-
string[] exceptionMessageTexts = exceptionMessage.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
735-
foreach (string messageText in exceptionMessageTexts)
736-
{
737-
if (messageText.StartsWith(authenticateHeaderName))
738-
{
739-
string[] authTexts = messageText.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
740-
foreach (string authText in authTexts)
741-
{
742-
if (authText.StartsWith(audienceName))
743-
{
744-
return authText.Substring(audienceName.Length);
745-
}
746-
}
747-
}
748-
}
749-
return null;
750-
}
751725

752726
public static ShareServiceClient GetTrack2FileServiceClient(AzureStorageContext context, ShareClientOptions options = null)
753727
{

0 commit comments

Comments
 (0)