Skip to content

Commit 4ad1464

Browse files
authored
[Storage] Fix Bug 10483492: Remove-AzDataLakeGen2Item fails with readonly sas (#15582)
* [Storage] Fix Bug 10483492: Remove-AzDataLakeGen2Item fails with readonly sas * Revised destination existing check in move datalakegen2 item
1 parent 061923b commit 4ad1464

File tree

5 files changed

+90
-78
lines changed

5 files changed

+90
-78
lines changed

src/Storage/Storage.Management.Test/ScenarioTests/StorageDataPlaneTests.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,9 +900,11 @@ function Test-DatalakeGen2
900900
Assert-AreEqual 2 $result.TotalFilesSuccessfulCount
901901
Assert-AreEqual 3 $result.TotalDirectoriesSuccessfulCount
902902

903-
# Remove Items
904-
Remove-AzDataLakeGen2Item -Context $storageContext -FileSystem $filesystemName -Path $filePath1 -Force
905-
Remove-AzDataLakeGen2Item -Context $storageContext -FileSystem $filesystemName -Path $directoryPath1 -Force
903+
# Remove Items with delete only SAS
904+
$sas = New-AzStorageContainerSASToken -Name $filesystemName -Permission d -Context $storageContext
905+
$storageContextSas = New-AzStorageContext -StorageAccountName $storageContext.StorageAccountName -SasToken $sas
906+
Remove-AzDataLakeGen2Item -Context $storageContextSas -FileSystem $filesystemName -Path $filePath1 -Force
907+
Remove-AzDataLakeGen2Item -Context $storageContextSas -FileSystem $filesystemName -Path $directoryPath1 -Force
906908

907909
# Clean Storage Account
908910
Get-AzDataLakeGen2ChildItem -Context $storageContext -FileSystem $filesystemName | Remove-AzDataLakeGen2Item -Force

src/Storage/Storage.Management/ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed remove datalakegen2 item fail with readonly SAS token
22+
- `Remove-AzDataLakeGen2Item`
23+
* Revised destination existing check in move datalakegen2 item
24+
- `Move-AzDataLakeGen2Item`
2125

2226
## Version 3.10.0
2327
* Supported Blob Last Access Time

src/Storage/Storage/Common/AzureDataLakeGen2Item.cs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class AzureDataLakeGen2Item : AzureStorageBase
102102
/// </summary>
103103
[Ps1Xml(Label = "Group", Target = ViewControl.Table, Position = 7, TableColumnWidth = 10)]
104104
public string Group { get; set; }
105-
105+
106106
/// <summary>
107107
/// Azure DataLakeGen2 Item constructor
108108
/// </summary>
@@ -112,16 +112,30 @@ public AzureDataLakeGen2Item(DataLakeFileClient fileClient)
112112
Name = fileClient.Name;
113113
Path = fileClient.Path;
114114
File = fileClient;
115-
Properties = fileClient.GetProperties();
116-
AccessControl = File.GetAccessControl();
117-
Length = Properties.ContentLength;
118-
ContentType = Properties.ContentType;
119-
LastModified = Properties.LastModified;
120115
IsDirectory = false;
121-
Permissions = AccessControl.Permissions;
122-
ACL = PSPathAccessControlEntry.ParsePSPathAccessControlEntrys(AccessControl.AccessControlList);
123-
Owner = AccessControl.Owner;
124-
Group = AccessControl.Group;
116+
try
117+
{
118+
Properties = fileClient.GetProperties();
119+
Length = Properties.ContentLength;
120+
ContentType = Properties.ContentType;
121+
LastModified = Properties.LastModified;
122+
}
123+
catch (global::Azure.RequestFailedException e) when (e.Status == 403 || e.Status == 404)
124+
{
125+
// skip get file properties if don't have read permission
126+
}
127+
try
128+
{
129+
AccessControl = File.GetAccessControl();
130+
Permissions = AccessControl.Permissions;
131+
ACL = PSPathAccessControlEntry.ParsePSPathAccessControlEntrys(AccessControl.AccessControlList);
132+
Owner = AccessControl.Owner;
133+
Group = AccessControl.Group;
134+
}
135+
catch (global::Azure.RequestFailedException e) when (e.Status == 403 || e.Status == 404)
136+
{
137+
// skip get file ACL if don't have read permission
138+
}
125139
}
126140

127141
/// <summary>
@@ -136,16 +150,35 @@ public AzureDataLakeGen2Item(DataLakeDirectoryClient directoryClient)
136150
IsDirectory = true;
137151
if (directoryClient.Path != "/" || string.IsNullOrEmpty(directoryClient.Path)) //if root directory, GetProperties() will fail. Skip until this is fixed.
138152
{
139-
Properties = directoryClient.GetProperties();
140-
Length = Properties.ContentLength;
141-
ContentType = Properties.ContentType;
142-
LastModified = Properties.LastModified;
153+
try
154+
{
155+
156+
157+
Properties = directoryClient.GetProperties();
158+
Length = Properties.ContentLength;
159+
ContentType = Properties.ContentType;
160+
LastModified = Properties.LastModified;
161+
}
162+
catch (global::Azure.RequestFailedException e) when (e.Status == 403 || e.Status == 404)
163+
{
164+
// skip get dir properties if don't have read permission
165+
}
166+
}
167+
168+
try
169+
{
170+
171+
AccessControl = directoryClient.GetAccessControl();
172+
Permissions = AccessControl.Permissions;
173+
ACL = PSPathAccessControlEntry.ParsePSPathAccessControlEntrys(AccessControl.AccessControlList);
174+
Owner = AccessControl.Owner;
175+
Group = AccessControl.Group;
176+
}
177+
catch (global::Azure.RequestFailedException e) when (e.Status == 403 || e.Status == 404)
178+
{
179+
// skip get dir ACL if don't have read permission
180+
143181
}
144-
AccessControl = directoryClient.GetAccessControl();
145-
Permissions = AccessControl.Permissions;
146-
ACL = PSPathAccessControlEntry.ParsePSPathAccessControlEntrys(AccessControl.AccessControlList);
147-
Owner = AccessControl.Owner;
148-
Group = AccessControl.Group;
149182
}
150183

151184

src/Storage/Storage/DatalakeGen2/Cmdlet/MoveAzDataLakeGen2Item.cs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public MoveAzDataLakeGen2ItemCommand(IStorageBlobManagement channel)
9797
public override void ExecuteCmdlet()
9898
{
9999
IStorageBlobManagement localChannel = Channel;
100-
BlobRequestOptions requestOptions = RequestOptions;
101100

102101
bool foundAFolder = false;
103102
DataLakeFileClient srcBlob = null;
@@ -124,20 +123,10 @@ public override void ExecuteCmdlet()
124123
{
125124
if (ShouldProcess(GetDataLakeItemUriWithoutSas(srcBlobDir), "Move Directory: "))
126125
{
127-
// check dest exist
128-
bool destExist = true;
129126
DataLakeFileSystemClient destFileSystem = GetFileSystemClientByName(localChannel, this.DestFileSystem != null ? this.DestFileSystem : this.FileSystem);
130127
DataLakeDirectoryClient destBlobDir = destFileSystem.GetDirectoryClient(this.DestPath);
131-
try
132-
{
133-
destBlobDir.GetProperties();
134-
}
135-
catch (RequestFailedException e) when (e.Status == 404)
136-
{
137-
destExist = false;
138-
}
139128

140-
if (this.Force || !destExist || ShouldContinue(string.Format("Overwrite destination {0}", GetDataLakeItemUriWithoutSas(destBlobDir)), ""))
129+
if (this.Force || !destBlobDir.Exists() || ShouldContinue(string.Format("Overwrite destination {0}", GetDataLakeItemUriWithoutSas(destBlobDir)), ""))
141130
{
142131
destBlobDir = srcBlobDir.Rename(this.DestPath, this.DestFileSystem).Value;
143132
WriteDataLakeGen2Item(localChannel, destBlobDir);
@@ -148,20 +137,10 @@ public override void ExecuteCmdlet()
148137
{
149138
if (ShouldProcess(GetDataLakeItemUriWithoutSas(srcBlob), "Move File: "))
150139
{
151-
// check dest exist
152-
bool destExist = true;
153140
DataLakeFileSystemClient destFileSystem = GetFileSystemClientByName(localChannel, this.DestFileSystem != null ? this.DestFileSystem : this.FileSystem);
154141
DataLakeFileClient destFile = destFileSystem.GetFileClient(this.DestPath);
155-
try
156-
{
157-
destFile.GetProperties();
158-
}
159-
catch (RequestFailedException e) when (e.Status == 404)
160-
{
161-
destExist = false;
162-
}
163142

164-
if (this.Force || !destExist || ShouldContinue(string.Format("Overwrite destination {0}", GetDataLakeItemUriWithoutSas(destFile)), ""))
143+
if (this.Force || !destFile.Exists() || ShouldContinue(string.Format("Overwrite destination {0}", GetDataLakeItemUriWithoutSas(destFile)), ""))
165144
{
166145
destFile = srcBlob.Rename(this.DestPath, this.DestFileSystem).Value;
167146
WriteDataLakeGen2Item(localChannel, destFile);

src/Storage/Storage/DatalakeGen2/Cmdlet/RemoveAzDataLakeGen2Item.cs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -103,49 +103,43 @@ protected override void BeginProcessing()
103103
public override void ExecuteCmdlet()
104104
{
105105
IStorageBlobManagement localChannel = Channel;
106-
BlobRequestOptions requestOptions = RequestOptions;
107106

108-
bool foundAFolder = false;
109-
110-
DataLakeFileClient fileClient = null;
111-
DataLakeDirectoryClient dirClient = null;
112-
if (ParameterSetName == ManualParameterSet)
113-
{
114-
DataLakeFileSystemClient fileSystem = GetFileSystemClientByName(localChannel, this.FileSystem);
115-
foundAFolder = GetExistDataLakeGen2Item(fileSystem, this.Path, out fileClient, out dirClient);
116-
}
117-
else //BlobParameterSet
107+
if (ShouldProcess(ParameterSetName == ManualParameterSet ? this.Path : InputObject.Path, "remove"))
118108
{
119-
if (!InputObject.IsDirectory)
109+
if (ParameterSetName == ManualParameterSet)
120110
{
121-
fileClient = InputObject.File;
111+
DataLakeFileSystemClient fileSystem = GetFileSystemClientByName(localChannel, this.FileSystem);
112+
DataLakePathClient pathClient = new DataLakePathClient(fileSystem, this.Path);
113+
if (force || ShouldContinue(string.Format("Remove DatalakeGen2 Item: {0}", GetDataLakeItemUriWithoutSas(pathClient)), ""))
114+
{
115+
pathClient.Delete(true, cancellationToken: this.CmdletCancellationToken);
116+
}
122117
}
123-
else
118+
else //ItemPipeline
124119
{
125-
dirClient = InputObject.Directory;
126-
foundAFolder = true;
120+
if (!InputObject.IsDirectory)
121+
{
122+
DataLakeFileClient fileClient = InputObject.File;
123+
if (force || ShouldContinue(string.Format("Remove File: {0}", GetDataLakeItemUriWithoutSas(fileClient)), ""))
124+
{
125+
fileClient.Delete(cancellationToken: this.CmdletCancellationToken);
126+
}
127+
}
128+
else
129+
{
130+
DataLakeDirectoryClient dirClient = InputObject.Directory;
131+
if (force || ShouldContinue(string.Format("Remove Directory: {0}", GetDataLakeItemUriWithoutSas(dirClient)), ""))
132+
{
133+
dirClient.Delete(true, cancellationToken: this.CmdletCancellationToken);
134+
}
135+
}
127136
}
128-
}
129137

130-
if (foundAFolder)
131-
{
132-
if (force || ShouldContinue(string.Format("Remove Directory: {0}", GetDataLakeItemUriWithoutSas(dirClient)), ""))
133-
{
134-
dirClient.Delete(true);
135-
}
136-
}
137-
else
138-
{
139-
if (force || ShouldContinue(string.Format("Remove File: {0}", GetDataLakeItemUriWithoutSas(fileClient)), ""))
138+
if (PassThru)
140139
{
141-
fileClient.Delete();
140+
WriteObject(true);
142141
}
143142
}
144-
145-
if (PassThru)
146-
{
147-
WriteObject(true);
148-
}
149143
}
150144
}
151145
}

0 commit comments

Comments
 (0)