Skip to content

Commit

Permalink
[Storage] Fix file cmdlet context issue when current context doesn't …
Browse files Browse the repository at this point in the history
…match Track1 object context from input (Azure#21228)

* Fix file cmdlet context issue

* Update changelog message and fix context

* Create ChangeLog.md

---------

Co-authored-by: Yabo Hu <[email protected]>
  • Loading branch information
yifanz0 and VeryEarly authored Mar 20, 2023
1 parent a665723 commit 4494369
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 76 deletions.
13 changes: 13 additions & 0 deletions src/Storage/Storage.Management/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
- Additional information about change #1
-->
## Upcoming Release
* Fixed file cmdlets potential context issue when the current context doesn't match with the credential of input Azure File object
- `Close-AzStorageFileHandle`
- `Get-AzStorageFile`
- `Get-AzStorageFileContent`
- `Get-AzStorageFileHandle`
- `New-AzStorageDirectory`
- `New-AzStorageFileSASToken`
- `Remove-AzStorageDirectory`
- `Remove-AzStorageFile`
- `Remove-AzStorageShare`
- `Set-AzStorageFileContent`
- `Set-AzStorageShareQuota`
- `Start-AzStorageFileCopy`

## Version 5.4.1
* Updated Azure.Core to 1.28.0.
Expand Down
40 changes: 30 additions & 10 deletions src/Storage/Storage/File/AzureStorageFileCmdletBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace Microsoft.WindowsAzure.Commands.Storage.File
using System.Linq;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Azure.Storage.Blob;
using Microsoft.WindowsAzure.Commands.Storage.Adapters;

public abstract class AzureStorageFileCmdletBase : StorageCloudCmdletBase<IStorageFileManagement>
{
Expand Down Expand Up @@ -95,15 +96,34 @@ protected bool ShareIsEmpty(ShareClient share)
}
}

protected bool ShouldSetContext(IStorageContext context, CloudFileClient cloudFileClient)
{
if (context == null)
{
return true;
}
try
{
if (context.GetCloudStorageAccount().FileEndpoint.Host.Equals(cloudFileClient.BaseUri.Host, StringComparison.OrdinalIgnoreCase))
{
return false;
}
} catch (Exception)
{
return true;
}
return true;
}

/// <summary>
/// Write CloudFile to output using specified service channel
/// </summary>
/// <param name="taskId">Task id</param>
/// <param name="channel">IStorageFileManagement channel object</param>
/// <param name="context">AzureStorageContext object</param>
/// <param name="file">The output CloudFile object</param>
internal void WriteCloudFileObject(long taskId, IStorageFileManagement channel, CloudFile file)
internal void WriteCloudFileObject(long taskId, AzureStorageContext context, CloudFile file)
{
AzureStorageFile azureFile = new AzureStorageFile(file, channel.StorageContext);
AzureStorageFile azureFile = new AzureStorageFile(file, context);
OutputStream.WriteObject(taskId, azureFile);
}

Expand All @@ -112,11 +132,11 @@ internal void WriteCloudFileObject(long taskId, IStorageFileManagement channel,
/// Write CloudFileDirectory to output using specified service channel
/// </summary>
/// <param name="taskId">Task id</param>
/// <param name="channel">IStorageFileManagement channel object</param>
/// <param name="context">AzureStorageContext object</param>
/// <param name="fileDir">The output CloudFileDirectory object</param>
internal void WriteCloudFileDirectoryeObject(long taskId, IStorageFileManagement channel, CloudFileDirectory fileDir)
internal void WriteCloudFileDirectoryeObject(long taskId, AzureStorageContext context, CloudFileDirectory fileDir)
{
AzureStorageFileDirectory azureFileDir = new AzureStorageFileDirectory(fileDir, channel.StorageContext);
AzureStorageFileDirectory azureFileDir = new AzureStorageFileDirectory(fileDir, context);
OutputStream.WriteObject(taskId, azureFileDir);
}

Expand All @@ -136,17 +156,17 @@ internal void WriteCloudShareObject(long taskId, IStorageFileManagement channel,
/// Write IListFileItem to output using specified service channel
/// </summary>
/// <param name="taskId">Task id</param>
/// <param name="channel">IStorageFileManagement channel object</param>
/// <param name="context">AzureStorageContext object</param>
/// <param name="item">The output IListFileItem object</param>
internal void WriteListFileItemObject(long taskId, IStorageFileManagement channel, IListFileItem item)
internal void WriteListFileItemObject(long taskId, AzureStorageContext context, IListFileItem item)
{
if ((item as CloudFile) != null) // CloudFile
{
WriteCloudFileObject(taskId, channel, item as CloudFile);
WriteCloudFileObject(taskId, context, item as CloudFile);
}
else
{
WriteCloudFileDirectoryeObject(taskId, channel, item as CloudFileDirectory);
WriteCloudFileDirectoryeObject(taskId, context, item as CloudFileDirectory);
}
}

Expand Down
18 changes: 0 additions & 18 deletions src/Storage/Storage/File/Cmdlet/CloseAzureStorageFileHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,6 @@ public override void ExecuteCmdlet()
{
case DirectoryCloseAllParameterSetName:
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions);

// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext);
}
break;

case ShareNameCloseSingleParameterSetName:
Expand All @@ -202,22 +196,10 @@ public override void ExecuteCmdlet()
case ShareCloseSingleParameterSetName:
case ShareCloseAllParameterSetName:
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions);

// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext);
}
break;

case FileCloseAllParameterSetName:
targetFile = AzureStorageFile.GetTrack2FileClient(this.File, ClientOptions);

// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext);
}
break;

default:
Expand Down
10 changes: 6 additions & 4 deletions src/Storage/Storage/File/Cmdlet/GetAzureStorageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ public override void ExecuteCmdlet()
case Constants.DirectoryParameterSetName:
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions);

// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
// Build and set storage context for the output object when
// 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object
if (ShouldSetContext(this.Context, this.Directory.ServiceClient))
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext);
}
Expand All @@ -91,8 +92,9 @@ public override void ExecuteCmdlet()
case Constants.ShareParameterSetName:
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions);

// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
// Build and set storage context for the output object when
// 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object
if (ShouldSetContext(this.Context, this.Share.ServiceClient))
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext);
}
Expand Down
20 changes: 19 additions & 1 deletion src/Storage/Storage/File/Cmdlet/GetAzureStorageFileContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ public override void ExecuteCmdlet()
{
case LocalConstants.FileParameterSetName:
fileToBeDownloaded = this.File;
// Build and set storage context for the output object when
// 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object
if (ShouldSetContext(this.Context, this.File.ServiceClient))
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext);
}
break;

case LocalConstants.ShareNameParameterSetName:
Expand All @@ -164,10 +170,22 @@ public override void ExecuteCmdlet()

case LocalConstants.ShareParameterSetName:
fileToBeDownloaded = this.Share.GetRootDirectoryReference().GetFileReferenceByPath(path);
// Build and set storage context for the output object when
// 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object
if (ShouldSetContext(this.Context, this.Share.ServiceClient))
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext);
}
break;

case LocalConstants.DirectoryParameterSetName:
fileToBeDownloaded = this.Directory.GetFileReferenceByPath(path);
// Build and set storage context for the output object when
// 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object
if (ShouldSetContext(this.Context, this.Directory.ServiceClient))
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext);
}
break;

default:
Expand Down Expand Up @@ -264,7 +282,7 @@ await DataMovementTransferHelper.DoTransfer(() =>

if (this.PassThru)
{
WriteCloudFileObject(taskId, this.Channel, fileToBeDownloaded);
WriteCloudFileObject(taskId, (AzureStorageContext)this.Context, fileToBeDownloaded);
}
});
}
Expand Down
19 changes: 1 addition & 18 deletions src/Storage/Storage/File/Cmdlet/GetAzureStorageFileHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ public override void ExecuteCmdlet()
{
case Constants.DirectoryParameterSetName:
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions);

// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext);
}
break;

case Constants.ShareNameParameterSetName:
Expand All @@ -114,21 +108,10 @@ public override void ExecuteCmdlet()

case Constants.ShareParameterSetName:
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions);

// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext);
}
break;

case Constants.FileParameterSetName:
targetFile = AzureStorageFile.GetTrack2FileClient(this.File, ClientOptions);

// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext);
}
break;

default:
Expand Down
10 changes: 6 additions & 4 deletions src/Storage/Storage/File/Cmdlet/NewAzureStorageDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ public override void ExecuteCmdlet()
case Constants.DirectoryParameterSetName:
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions);

// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
// Build and set storage context for the output object when
// 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object
if (ShouldSetContext(this.Context, this.Directory.ServiceClient))
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext);
}
Expand All @@ -88,8 +89,9 @@ public override void ExecuteCmdlet()
case Constants.ShareParameterSetName:
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions);

// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
// Build and set storage context for the output object when
// 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object
if (ShouldSetContext(this.Context, this.Share.ServiceClient))
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ public override void ExecuteCmdlet()
ShareFileClient fileClient;
if (null != this.File)
{
// when only track1 object input, might miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
// Build and set storage context for the output object when
// 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object
if (ShouldSetContext(this.Context, this.File.ServiceClient))
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext);
}
Expand Down
10 changes: 6 additions & 4 deletions src/Storage/Storage/File/Cmdlet/RemoveAzureStorageDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public override void ExecuteCmdlet()
case Constants.DirectoryParameterSetName:
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions);

// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
// Build and set storage context for the output object when
// 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object
if (ShouldSetContext(this.Context, this.Directory.ServiceClient))
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext);
}
Expand All @@ -100,8 +101,9 @@ public override void ExecuteCmdlet()
case Constants.ShareParameterSetName:
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions);

// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
if (this.Context == null)
// Build and set storage context for the output object when
// 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object
if (ShouldSetContext(this.Context, this.Share.ServiceClient))
{
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext);
}
Expand Down
Loading

0 comments on commit 4494369

Please sign in to comment.