Skip to content

Commit 4494369

Browse files
yifanz0VeryEarly
andauthored
[Storage] Fix file cmdlet context issue when current context doesn't match Track1 object context from input (#21228)
* Fix file cmdlet context issue * Update changelog message and fix context * Create ChangeLog.md --------- Co-authored-by: Yabo Hu <[email protected]>
1 parent a665723 commit 4494369

14 files changed

+118
-76
lines changed

src/Storage/Storage.Management/ChangeLog.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed file cmdlets potential context issue when the current context doesn't match with the credential of input Azure File object
22+
- `Close-AzStorageFileHandle`
23+
- `Get-AzStorageFile`
24+
- `Get-AzStorageFileContent`
25+
- `Get-AzStorageFileHandle`
26+
- `New-AzStorageDirectory`
27+
- `New-AzStorageFileSASToken`
28+
- `Remove-AzStorageDirectory`
29+
- `Remove-AzStorageFile`
30+
- `Remove-AzStorageShare`
31+
- `Set-AzStorageFileContent`
32+
- `Set-AzStorageShareQuota`
33+
- `Start-AzStorageFileCopy`
2134

2235
## Version 5.4.1
2336
* Updated Azure.Core to 1.28.0.

src/Storage/Storage/File/AzureStorageFileCmdletBase.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace Microsoft.WindowsAzure.Commands.Storage.File
3131
using System.Linq;
3232
using Microsoft.Azure.Cosmos.Table;
3333
using Microsoft.Azure.Storage.Blob;
34+
using Microsoft.WindowsAzure.Commands.Storage.Adapters;
3435

3536
public abstract class AzureStorageFileCmdletBase : StorageCloudCmdletBase<IStorageFileManagement>
3637
{
@@ -95,15 +96,34 @@ protected bool ShareIsEmpty(ShareClient share)
9596
}
9697
}
9798

99+
protected bool ShouldSetContext(IStorageContext context, CloudFileClient cloudFileClient)
100+
{
101+
if (context == null)
102+
{
103+
return true;
104+
}
105+
try
106+
{
107+
if (context.GetCloudStorageAccount().FileEndpoint.Host.Equals(cloudFileClient.BaseUri.Host, StringComparison.OrdinalIgnoreCase))
108+
{
109+
return false;
110+
}
111+
} catch (Exception)
112+
{
113+
return true;
114+
}
115+
return true;
116+
}
117+
98118
/// <summary>
99119
/// Write CloudFile to output using specified service channel
100120
/// </summary>
101121
/// <param name="taskId">Task id</param>
102-
/// <param name="channel">IStorageFileManagement channel object</param>
122+
/// <param name="context">AzureStorageContext object</param>
103123
/// <param name="file">The output CloudFile object</param>
104-
internal void WriteCloudFileObject(long taskId, IStorageFileManagement channel, CloudFile file)
124+
internal void WriteCloudFileObject(long taskId, AzureStorageContext context, CloudFile file)
105125
{
106-
AzureStorageFile azureFile = new AzureStorageFile(file, channel.StorageContext);
126+
AzureStorageFile azureFile = new AzureStorageFile(file, context);
107127
OutputStream.WriteObject(taskId, azureFile);
108128
}
109129

@@ -112,11 +132,11 @@ internal void WriteCloudFileObject(long taskId, IStorageFileManagement channel,
112132
/// Write CloudFileDirectory to output using specified service channel
113133
/// </summary>
114134
/// <param name="taskId">Task id</param>
115-
/// <param name="channel">IStorageFileManagement channel object</param>
135+
/// <param name="context">AzureStorageContext object</param>
116136
/// <param name="fileDir">The output CloudFileDirectory object</param>
117-
internal void WriteCloudFileDirectoryeObject(long taskId, IStorageFileManagement channel, CloudFileDirectory fileDir)
137+
internal void WriteCloudFileDirectoryeObject(long taskId, AzureStorageContext context, CloudFileDirectory fileDir)
118138
{
119-
AzureStorageFileDirectory azureFileDir = new AzureStorageFileDirectory(fileDir, channel.StorageContext);
139+
AzureStorageFileDirectory azureFileDir = new AzureStorageFileDirectory(fileDir, context);
120140
OutputStream.WriteObject(taskId, azureFileDir);
121141
}
122142

@@ -136,17 +156,17 @@ internal void WriteCloudShareObject(long taskId, IStorageFileManagement channel,
136156
/// Write IListFileItem to output using specified service channel
137157
/// </summary>
138158
/// <param name="taskId">Task id</param>
139-
/// <param name="channel">IStorageFileManagement channel object</param>
159+
/// <param name="context">AzureStorageContext object</param>
140160
/// <param name="item">The output IListFileItem object</param>
141-
internal void WriteListFileItemObject(long taskId, IStorageFileManagement channel, IListFileItem item)
161+
internal void WriteListFileItemObject(long taskId, AzureStorageContext context, IListFileItem item)
142162
{
143163
if ((item as CloudFile) != null) // CloudFile
144164
{
145-
WriteCloudFileObject(taskId, channel, item as CloudFile);
165+
WriteCloudFileObject(taskId, context, item as CloudFile);
146166
}
147167
else
148168
{
149-
WriteCloudFileDirectoryeObject(taskId, channel, item as CloudFileDirectory);
169+
WriteCloudFileDirectoryeObject(taskId, context, item as CloudFileDirectory);
150170
}
151171
}
152172

src/Storage/Storage/File/Cmdlet/CloseAzureStorageFileHandle.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,6 @@ public override void ExecuteCmdlet()
184184
{
185185
case DirectoryCloseAllParameterSetName:
186186
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions);
187-
188-
// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
189-
if (this.Context == null)
190-
{
191-
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext);
192-
}
193187
break;
194188

195189
case ShareNameCloseSingleParameterSetName:
@@ -202,22 +196,10 @@ public override void ExecuteCmdlet()
202196
case ShareCloseSingleParameterSetName:
203197
case ShareCloseAllParameterSetName:
204198
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions);
205-
206-
// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
207-
if (this.Context == null)
208-
{
209-
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext);
210-
}
211199
break;
212200

213201
case FileCloseAllParameterSetName:
214202
targetFile = AzureStorageFile.GetTrack2FileClient(this.File, ClientOptions);
215-
216-
// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
217-
if (this.Context == null)
218-
{
219-
this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext);
220-
}
221203
break;
222204

223205
default:

src/Storage/Storage/File/Cmdlet/GetAzureStorageFile.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ public override void ExecuteCmdlet()
7575
case Constants.DirectoryParameterSetName:
7676
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions);
7777

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

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

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ public override void ExecuteCmdlet()
155155
{
156156
case LocalConstants.FileParameterSetName:
157157
fileToBeDownloaded = this.File;
158+
// Build and set storage context for the output object when
159+
// 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object
160+
if (ShouldSetContext(this.Context, this.File.ServiceClient))
161+
{
162+
this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext);
163+
}
158164
break;
159165

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

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

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

173191
default:
@@ -264,7 +282,7 @@ await DataMovementTransferHelper.DoTransfer(() =>
264282

265283
if (this.PassThru)
266284
{
267-
WriteCloudFileObject(taskId, this.Channel, fileToBeDownloaded);
285+
WriteCloudFileObject(taskId, (AzureStorageContext)this.Context, fileToBeDownloaded);
268286
}
269287
});
270288
}

src/Storage/Storage/File/Cmdlet/GetAzureStorageFileHandle.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ public override void ExecuteCmdlet()
9898
{
9999
case Constants.DirectoryParameterSetName:
100100
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions);
101-
102-
// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
103-
if (this.Context == null)
104-
{
105-
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext);
106-
}
107101
break;
108102

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

115109
case Constants.ShareParameterSetName:
116110
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions);
117-
118-
// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
119-
if (this.Context == null)
120-
{
121-
this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext);
122-
}
123111
break;
112+
124113
case Constants.FileParameterSetName:
125114
targetFile = AzureStorageFile.GetTrack2FileClient(this.File, ClientOptions);
126-
127-
// when only track1 object input, will miss storage context, so need to build storage context for prepare the output object.
128-
if (this.Context == null)
129-
{
130-
this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext);
131-
}
132115
break;
133116

134117
default:

src/Storage/Storage/File/Cmdlet/NewAzureStorageDirectory.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ public override void ExecuteCmdlet()
7272
case Constants.DirectoryParameterSetName:
7373
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions);
7474

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

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

src/Storage/Storage/File/Cmdlet/NewAzureStorageFileSasToken.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ public override void ExecuteCmdlet()
159159
ShareFileClient fileClient;
160160
if (null != this.File)
161161
{
162-
// when only track1 object input, might miss storage context, so need to build storage context for prepare the output object.
163-
if (this.Context == null)
162+
// Build and set storage context for the output object when
163+
// 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object
164+
if (ShouldSetContext(this.Context, this.File.ServiceClient))
164165
{
165166
this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext);
166167
}

src/Storage/Storage/File/Cmdlet/RemoveAzureStorageDirectory.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ public override void ExecuteCmdlet()
8383
case Constants.DirectoryParameterSetName:
8484
baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions);
8585

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

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

0 commit comments

Comments
 (0)