Skip to content

Commit 737607b

Browse files
authored
[Storage] Fix context issues - misleading StorageAccountName field and null reference issue when context has no Credentials field (#21741)
* Fix context issue * Add more checks
1 parent 2929d24 commit 737607b

16 files changed

+42
-29
lines changed

src/Storage/Storage.Management/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed an issue of StorageAccountName field in context object when the context is invalid
22+
- `New-AzStorageContext`
23+
* Fixed an issue when a context does not have Crendentials field
2124
* Added "$blobchangefeed" to be a valid container name
2225

2326
## Version 5.6.0

src/Storage/Storage/Blob/Cmdlet/NewAzureStorageBlobSasToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public override void ExecuteCmdlet()
180180

181181
// When the input context is Oauth bases, can't generate normal SAS, but UserDelegationSas
182182
bool generateUserDelegationSas = false;
183-
if (Channel != null && Channel.StorageContext != null && Channel.StorageContext.StorageAccount.Credentials.IsToken)
183+
if (Channel != null && Channel.StorageContext != null && Channel.StorageContext.StorageAccount.Credentials !=null && Channel.StorageContext.StorageAccount.Credentials.IsToken)
184184
{
185185
if (ShouldProcess(blob.Name, "Generate User Delegation SAS, since input Storage Context is OAuth based."))
186186
{

src/Storage/Storage/Blob/Cmdlet/NewAzureStorageContainerSasToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public override void ExecuteCmdlet()
127127

128128
// When the input context is Oauth bases, can't generate normal SAS, but UserDelegationSas
129129
bool generateUserDelegationSas = false;
130-
if (Channel!=null && Channel.StorageContext!= null && Channel.StorageContext.StorageAccount.Credentials.IsToken)
130+
if (Channel!=null && Channel.StorageContext!= null && Channel.StorageContext.StorageAccount.Credentials != null && Channel.StorageContext.StorageAccount.Credentials.IsToken)
131131
{
132132
if (ShouldProcess(Name, "Generate User Delegation SAS, since input Storage Context is OAuth based."))
133133
{

src/Storage/Storage/Blob/Cmdlet/StartAzureStorageBlobCopy.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,8 @@ private async Task StartCopyFromBlob(long taskId, IStorageBlobManagement destCha
677677
if (Channel!=null && destChannel != null &&
678678
Channel.StorageContext!= null && destChannel.StorageContext != null
679679
&& Channel.StorageContext.StorageAccountName == destChannel.StorageContext.StorageAccountName
680+
&& Channel.StorageContext.StorageAccount != null
681+
&& Channel.StorageContext.StorageAccount.Credentials != null
680682
&& Channel.StorageContext.StorageAccount.Credentials.IsToken)
681683
{
682684
// if inside same account, source blob can be anonumous
@@ -712,6 +714,8 @@ private async Task StartCopyFromBlob(long taskId, IStorageBlobManagement destCha
712714
if (Channel != null && destChannel != null &&
713715
Channel.StorageContext != null && destChannel.StorageContext != null
714716
&& Channel.StorageContext.StorageAccountName == destChannel.StorageContext.StorageAccountName
717+
&& Channel.StorageContext.StorageAccount != null
718+
&& Channel.StorageContext.StorageAccount.Credentials != null
715719
&& Channel.StorageContext.StorageAccount.Credentials.IsToken)
716720
{
717721
// if inside same account, source blob can be anonumous

src/Storage/Storage/Blob/StorageCloudBlobCmdletBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,15 +713,15 @@ internal DataLakeFileSystemClient GetFileSystemClientByName(IStorageBlobManageme
713713
Uri fileSystemUri = localChannel.StorageContext.StorageAccount.CreateCloudBlobClient().GetContainerReference(fileSystemName).Uri;
714714
DataLakeFileSystemClient fileSystem;
715715

716-
if (localChannel.StorageContext.StorageAccount.Credentials.IsToken) //Oauth
716+
if (localChannel.StorageContext.StorageAccount.Credentials != null && localChannel.StorageContext.StorageAccount.Credentials.IsToken) //Oauth
717717
{
718718
fileSystem = new DataLakeFileSystemClient(fileSystemUri, localChannel.StorageContext.Track2OauthToken, this.DataLakeClientOptions);
719719
}
720-
else if (localChannel.StorageContext.StorageAccount.Credentials.IsSAS) //SAS
720+
else if (localChannel.StorageContext.StorageAccount.Credentials != null && localChannel.StorageContext.StorageAccount.Credentials.IsSAS) //SAS
721721
{
722722
fileSystem = new DataLakeFileSystemClient(new Uri (fileSystemUri.ToString() + "?" + Util.GetSASStringWithoutQuestionMark(localChannel.StorageContext.StorageAccount.Credentials.SASToken)), this.DataLakeClientOptions);
723723
}
724-
else if (localChannel.StorageContext.StorageAccount.Credentials.IsSharedKey) //Shared Key
724+
else if (localChannel.StorageContext.StorageAccount.Credentials != null && localChannel.StorageContext.StorageAccount.Credentials.IsSharedKey) //Shared Key
725725
{
726726
fileSystem = new DataLakeFileSystemClient(fileSystemUri,
727727
new StorageSharedKeyCredential(localChannel.StorageContext.StorageAccountName, localChannel.StorageContext.StorageAccount.Credentials.ExportBase64EncodedKey()), this.DataLakeClientOptions);

src/Storage/Storage/Common/AzureStorageBlob.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public AzureStorageBlob(TaggedBlobItem blob, AzureStorageContext storageContext,
313313
BlobName = blob.BlobName
314314
};
315315
Uri blobUri = uriBuilder.ToUri();
316-
if (storageContext.StorageAccount.Credentials.IsSAS)
316+
if (storageContext.StorageAccount != null && storageContext.StorageAccount.Credentials != null && storageContext.StorageAccount.Credentials.IsSAS)
317317
{
318318
blobUri= new Uri(blobUri.ToString() + storageContext.StorageAccount.Credentials.SASToken);
319319
}
@@ -497,12 +497,12 @@ public static BlobClient GetTrack2BlobClient(BlobBaseClient blobBaseClient, Azur
497497
return (BlobClient)blobBaseClient;
498498
}
499499
BlobClient blobClient;
500-
if (context.StorageAccount.Credentials.IsToken) //Oauth
500+
if (context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsToken) //Oauth
501501
{
502502
blobClient = new BlobClient(blobBaseClient.Uri, context.Track2OauthToken, options);
503503

504504
}
505-
else if (context.StorageAccount.Credentials.IsSharedKey) //Shared Key
505+
else if (context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsSharedKey) //Shared Key
506506
{
507507
blobClient = new BlobClient(blobBaseClient.Uri,
508508
new StorageSharedKeyCredential(context.StorageAccountName, context.StorageAccount.Credentials.ExportBase64EncodedKey()), options);

src/Storage/Storage/Common/AzureStorageContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public AzureStorageContext(CloudStorageAccount account, string accountName = nul
186186
{
187187
StorageAccountName = "[AccessToken]";
188188
}
189-
else
189+
else if (account.Credentials != null && account.Credentials.IsAnonymous)
190190
{
191191
StorageAccountName = "[Anonymous]";
192192
}

src/Storage/Storage/Common/Cmdlet/NewAzureStorageAccountSasToken.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public NewAzureStorageAccountSasTokenCommand(IStorageBlobManagement channel)
9090
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
9191
public override void ExecuteCmdlet()
9292
{
93-
if (Channel != null && Channel.StorageContext != null && !Channel.StorageContext.StorageAccount.Credentials.IsSharedKey)
93+
if (Channel != null && Channel.StorageContext != null && Channel.StorageContext.StorageAccount != null
94+
&& Channel.StorageContext.StorageAccount.Credentials != null && !Channel.StorageContext.StorageAccount.Credentials.IsSharedKey)
9495
{
9596
throw new ArgumentException("Storage account SAS token must be secured with the storage account key.", "Context");
9697
}

src/Storage/Storage/Common/SasTokenHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public static ShareSasBuilder SetShareSasBuilder(string shareName,
448448
/// </summary>
449449
public static string GetFileSharedAccessSignature(AzureStorageContext context, ShareSasBuilder sasBuilder, CancellationToken cancelToken)
450450
{
451-
if (context != null && context.StorageAccount.Credentials.IsSharedKey)
451+
if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsSharedKey)
452452
{
453453
return sasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(context.StorageAccountName, context.StorageAccount.Credentials.ExportBase64EncodedKey())).ToString();
454454
}
@@ -687,7 +687,7 @@ public static BlobSasBuilder SetBlobPermission(BlobSasBuilder sasBuilder, string
687687
/// </summary>
688688
public static string GetBlobSharedAccessSignature(AzureStorageContext context, BlobSasBuilder sasBuilder, bool generateUserDelegationSas, BlobClientOptions ClientOptions, CancellationToken cancelToken)
689689
{
690-
if (context != null && context.StorageAccount.Credentials.IsSharedKey)
690+
if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsSharedKey)
691691
{
692692
return sasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(context.StorageAccountName, context.StorageAccount.Credentials.ExportBase64EncodedKey())).ToString();
693693
}
@@ -716,7 +716,7 @@ public static string GetBlobSharedAccessSignature(AzureStorageContext context, B
716716
/// </summary>
717717
public static string GetDatalakeGen2SharedAccessSignature(AzureStorageContext context, DataLakeSasBuilder sasBuilder, bool generateUserDelegationSas, DataLakeClientOptions clientOptions, CancellationToken cancelToken)
718718
{
719-
if (context != null && context.StorageAccount.Credentials.IsSharedKey)
719+
if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsSharedKey)
720720
{
721721
return sasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(context.StorageAccountName, context.StorageAccount.Credentials.ExportBase64EncodedKey())).ToString();
722722
}

src/Storage/Storage/Common/StorageExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ internal static Uri GenerateUriWithCredentials(
197197
{
198198
throw new ArgumentNullException("blob");
199199
}
200-
else if (context.StorageAccount.Credentials.IsSAS)
200+
else if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsSAS)
201201
{
202202
return blob.Uri;
203203
}

0 commit comments

Comments
 (0)