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
}

src/Storage/Storage/Common/Util.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ public static BlobBaseClient GetTrack2BlobClient(Uri blobUri, AzureStorageContex
426426
{
427427
options = new BlobClientOptions();
428428
}
429-
if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials.IsToken) //Oauth
429+
if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsToken) //Oauth
430430
{
431431
if (blobType == null)
432432
{
@@ -448,7 +448,7 @@ public static BlobBaseClient GetTrack2BlobClient(Uri blobUri, AzureStorageContex
448448
}
449449
}
450450
}
451-
else if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials.IsSharedKey) //Shared Key
451+
else if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsSharedKey) //Shared Key
452452
{
453453
if (blobType == null)
454454
{
@@ -503,7 +503,7 @@ public static BlobBaseClient GetTrack2BlobClientWithType(BlobBaseClient blob, Az
503503
public static BlobServiceClient GetTrack2BlobServiceClient(AzureStorageContext context, BlobClientOptions options = null)
504504
{
505505
BlobServiceClient blobServiceClient;
506-
if (context.StorageAccount.Credentials.IsToken) //Oauth
506+
if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsToken) //Oauth
507507
{
508508
blobServiceClient = new BlobServiceClient(context.StorageAccount.BlobEndpoint, context.Track2OauthToken, options);
509509
}
@@ -512,7 +512,7 @@ public static BlobServiceClient GetTrack2BlobServiceClient(AzureStorageContext c
512512
string connectionString = context.ConnectionString;
513513

514514
// remove the "?" at the begin of SAS if any
515-
if (context.StorageAccount.Credentials.IsSAS)
515+
if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsSAS)
516516
{
517517
connectionString = connectionString.Replace("SharedAccessSignature=?", "SharedAccessSignature=");
518518
}
@@ -524,15 +524,15 @@ public static BlobServiceClient GetTrack2BlobServiceClient(AzureStorageContext c
524524
public static DataLakeServiceClient GetTrack2DataLakeServiceClient(AzureStorageContext context, DataLakeClientOptions options = null)
525525
{
526526
DataLakeServiceClient serviceClient;
527-
if (context.StorageAccount.Credentials.IsToken) //Oauth
527+
if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsToken) //Oauth
528528
{
529529
serviceClient = new DataLakeServiceClient(context.StorageAccount.BlobEndpoint, context.Track2OauthToken, options);
530530
}
531-
else if (context.StorageAccount.Credentials.IsSharedKey) //key
531+
else if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsSharedKey) //key
532532
{
533533
serviceClient = new DataLakeServiceClient(context.StorageAccount.BlobEndpoint, new StorageSharedKeyCredential(context.StorageAccountName, context.StorageAccount.Credentials.ExportBase64EncodedKey()), options);
534534
}
535-
else if (context.StorageAccount.Credentials.IsSAS) //sas
535+
else if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsSAS) //sas
536536
{
537537
serviceClient = new DataLakeServiceClient(new Uri(context.StorageAccount.BlobEndpoint.ToString() + context.StorageAccount.Credentials.SASToken), options);
538538
}
@@ -768,7 +768,7 @@ public static ShareServiceClient GetTrack2FileServiceClient(AzureStorageContext
768768
string connectionString = context.ConnectionString;
769769

770770
// remove the "?" at the begin of SAS if any
771-
if (context.StorageAccount.Credentials.IsSAS)
771+
if (context != null && context.StorageAccount != null && context.StorageAccount.Credentials != null && context.StorageAccount.Credentials.IsSAS)
772772
{
773773
connectionString = connectionString.Replace("SharedAccessSignature=?", "SharedAccessSignature=");
774774
}

src/Storage/Storage/DatalakeGen2/Cmdlet/GetAzDataLakeGen2FileContent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ public override void ExecuteCmdlet()
264264
{
265265
if (!InputObject.IsDirectory)
266266
{
267-
if (Channel.StorageContext.StorageAccount.Credentials.IsSAS)
267+
if (Channel.StorageContext.StorageAccount != null &&
268+
Channel.StorageContext.StorageAccount.Credentials != null && Channel.StorageContext.StorageAccount.Credentials.IsSAS)
268269
{
269270
// For SAS, the Uri already contains the sas token, so can't repeatedly inout the credential
270271
blob = new CloudBlockBlob(InputObject.File.Uri);

src/Storage/Storage/DatalakeGen2/Cmdlet/NewAzDataLakeGen2Item.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ public override void ExecuteCmdlet()
214214
if (ShouldProcess(GetDataLakeItemUriWithoutSas(fileClient), "Create File: "))
215215
{
216216
// Use SDK to upload directly when use SAS credential, and need set permission, since set permission after upload will fail with SAS
217-
if (Channel.StorageContext.StorageAccount.Credentials.IsSAS
218-
&& (!string.IsNullOrEmpty(this.Permission) || !string.IsNullOrEmpty(this.Umask)))
217+
if (Channel.StorageContext.StorageAccount != null && Channel.StorageContext.StorageAccount.Credentials != null &&
218+
Channel.StorageContext.StorageAccount.Credentials.IsSAS && (!string.IsNullOrEmpty(this.Permission) || !string.IsNullOrEmpty(this.Umask)))
219219
{
220220
Func<long, Task> taskGenerator = (taskId) => UploadDataLakeFile(taskId, fileClient, ResolvedFileName);
221221
RunTask(taskGenerator);

src/Storage/Storage/DatalakeGen2/Cmdlet/NewAzDataLakeGen2SasToken.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ public override void ExecuteCmdlet()
117117

118118
// When the input context is Oauth bases, can't generate normal SAS, but UserDelegationSas
119119
bool generateUserDelegationSas = false;
120-
if (Channel != null && Channel.StorageContext != null && Channel.StorageContext.StorageAccount.Credentials.IsToken)
120+
if (Channel != null && Channel.StorageContext != null && Channel.StorageContext.StorageAccount != null &&
121+
Channel.StorageContext.StorageAccount.Credentials != null && Channel.StorageContext.StorageAccount.Credentials.IsToken)
121122
{
122123
if (ShouldProcess(this.Path, "Generate User Delegation SAS, since input Storage Context is OAuth based."))
123124
{

src/Storage/Storage/File/Cmdlet/GetAzureStorageFileContent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ public override void ExecuteCmdlet()
225225
Resources.PrepareDownloadingFile);
226226

227227
// If not Oauth, use DMlib
228-
if (!(this.Channel != null && this.Channel.StorageContext != null && this.Channel.StorageContext.StorageAccount != null && this.Channel.StorageContext.StorageAccount.Credentials.IsToken))
228+
if (!(this.Channel != null && this.Channel.StorageContext != null && this.Channel.StorageContext.StorageAccount != null &&
229+
this.Channel.StorageContext.StorageAccount.Credentials != null && this.Channel.StorageContext.StorageAccount.Credentials.IsToken))
229230
{
230231
await DataMovementTransferHelper.DoTransfer(() =>
231232
{

src/Storage/Storage/Model/Contract/StorageBlobManagement.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,13 @@ public BlobServiceClient GetBlobServiceClient(BlobClientOptions options = null)
166166
{
167167
if (blobServiceClient == null)
168168
{
169-
if (this.StorageContext.StorageAccount.Credentials.IsToken) //Oauth
169+
if (this.StorageContext != null && this.StorageContext.StorageAccount != null &&
170+
this.StorageContext.StorageAccount.Credentials != null && this.StorageContext.StorageAccount.Credentials.IsToken) //Oauth
170171
{
171172
blobServiceClient = new BlobServiceClient(this.StorageContext.StorageAccount.BlobEndpoint, this.StorageContext.Track2OauthToken, options);
172173
}
173-
else if (this.StorageContext.StorageAccount.Credentials.IsSharedKey) //Shared Key
174+
else if (this.StorageContext != null && this.StorageContext.StorageAccount != null &&
175+
this.StorageContext.StorageAccount.Credentials != null && this.StorageContext.StorageAccount.Credentials.IsSharedKey) //Shared Key
174176
{
175177
blobServiceClient = new BlobServiceClient(this.StorageContext.StorageAccount.BlobEndpoint,
176178
new StorageSharedKeyCredential(this.StorageContext.StorageAccountName, this.StorageContext.StorageAccount.Credentials.ExportBase64EncodedKey()), options);

0 commit comments

Comments
 (0)