Skip to content

Commit

Permalink
Handle cleanup for leased and encrypted storage accounts. Reduce thro…
Browse files Browse the repository at this point in the history
…ttling (#9212)
  • Loading branch information
benbp authored Oct 18, 2024
1 parent 4554792 commit 08a486a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
52 changes: 39 additions & 13 deletions eng/common/scripts/Helpers/Resource-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,40 @@ function Remove-WormStorageAccounts() {
if (!$hasContainers) { continue }

$ctx = New-AzStorageContext -StorageAccountName $account.StorageAccountName
$containers = $ctx | Get-AzStorageContainer
$blobs = $containers | Get-AzStorageBlob

$immutableBlobs = $ctx `
| Get-AzStorageContainer `
$immutableBlobs = $containers `
| Where-Object { $_.BlobContainerProperties.HasImmutableStorageWithVersioning } `
| Get-AzStorageBlob
try {
foreach ($blob in $immutableBlobs) {
Write-Host "Removing legal hold - blob: $($blob.Name), account: $($account.StorageAccountName), group: $($group.ResourceGroupName)"
$blob | Set-AzStorageBlobLegalHold -DisableLegalHold | Out-Null
# We can't edit blobs with customer encryption without using that key
# so just try to delete them fully instead. It is unlikely they
# will also have a legal hold enabled.
if (($blob | Get-Member 'ListBlobProperties') `
-and $blob.ListBlobProperties.Properties.CustomerProvidedKeySha256) {
Write-Host "Removing customer encrypted blob: $($blob.Name), account: $($account.StorageAccountName), group: $($group.ResourceGroupName)"
$blob | Remove-AzStorageBlob -Force
continue
}

if (!($blob | Get-Member 'BlobProperties')) {
continue
}

if ($blob.BlobProperties.LeaseState -eq 'Leased') {
Write-Host "Breaking blob lease: $($blob.Name), account: $($account.StorageAccountName), group: $($group.ResourceGroupName)"
$blob.ICloudBlob.BreakLease()
}

if ($blob.BlobProperties.HasLegalHold) {
Write-Host "Removing legal hold - blob: $($blob.Name), account: $($account.StorageAccountName), group: $($group.ResourceGroupName)"
$blob | Set-AzStorageBlobLegalHold -DisableLegalHold | Out-Null
}
}
}
catch {
Write-Warning "User must have 'Storage Blob Data Owner' RBAC permission on subscription or resource group"
} catch {
Write-Warning "Ensure user has 'Storage Blob Data Owner' RBAC permission on subscription or resource group"
Write-Error $_
throw
}
Expand All @@ -273,13 +294,19 @@ function Remove-WormStorageAccounts() {
}

try {
Write-Host "Removing immutability policies - account: $($ctx.StorageAccountName), group: $($group.ResourceGroupName)"
$null = $ctx | Get-AzStorageContainer | Get-AzStorageBlob | Remove-AzStorageBlobImmutabilityPolicy
foreach ($blob in $blobs) {
if ($blob.BlobProperties.ImmutabilityPolicy.PolicyMode) {
Write-Host "Removing immutability policy - blob: $($blob.Name), account: $($ctx.StorageAccountName), group: $($group.ResourceGroupName)"
$null = $blob | Remove-AzStorageBlobImmutabilityPolicy
}
}
}
catch {}

try {
$ctx | Get-AzStorageContainer | Get-AzStorageBlob | Remove-AzStorageBlob -Force
foreach ($blob in $blobs) {
$blob | Remove-AzStorageBlob -Force
}
$succeeded = $true
}
catch {
Expand All @@ -290,9 +317,8 @@ function Remove-WormStorageAccounts() {

try {
# Use AzRm cmdlet as deletion will only work through ARM with the immutability policies defined on the blobs
$ctx | Get-AzStorageContainer | ForEach-Object { Remove-AzRmStorageContainer -Name $_.Name -StorageAccountName $ctx.StorageAccountName -ResourceGroupName $group.ResourceGroupName -Force }
}
catch {
$containers | ForEach-Object { Remove-AzRmStorageContainer -Name $_.Name -StorageAccountName $ctx.StorageAccountName -ResourceGroupName $group.ResourceGroupName -Force }
} catch {
Write-Warning "Container removal failed. Ignoring the error and trying to delete the storage account."
Write-Warning $_
}
Expand Down
6 changes: 5 additions & 1 deletion eng/pipelines/live-test-cleanup-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ parameters:
- name: DryRun
type: boolean
default: false
- name: GroupFilter
type: string
default: '*'
- name: DisplayName
type: string
- name: ServiceConnection
Expand Down Expand Up @@ -87,7 +90,8 @@ steps:
@subscriptionConfiguration `
-Verbose `
${{ parameters.AdditionalParameters }} `
-WhatIf:$${{ parameters.DryRun }}
-WhatIf:$${{ parameters.DryRun }} `
-GroupFilter '${{ parameters.GroupFilter }}'
displayName: ${{ parameters.DisplayName }}
continueOnError: true
env:
Expand Down
4 changes: 4 additions & 0 deletions eng/pipelines/live-test-cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ parameters:
- name: DryRun
type: boolean
default: false
- name: GroupFilter
type: string
default: '*'
- name: Pool
type: string
default: azsdk-pool-mms-ubuntu-2204-general
Expand Down Expand Up @@ -97,6 +100,7 @@ stages:
- template: ./live-test-cleanup-template.yml
parameters:
DryRun: ${{ parameters.DryRun }}
GroupFilter: ${{ parameters.GroupFilter }}
DisplayName: ${{ subscription.DisplayName }}
ServiceConnection: ${{ subscription.ServiceConnection }}
SubscriptionConfigurationFilePaths: ${{ subscription.SubscriptionConfigurationFilePaths }}
Expand Down
6 changes: 4 additions & 2 deletions eng/scripts/live-test-resource-cleanup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ param (
[Parameter()]
[string] $AllowListPath = "$PSScriptRoot/cleanup-allowlist.txt",

[string] $GroupFilter = '*',

[Parameter()]
[switch] $Force,

Expand Down Expand Up @@ -351,7 +353,7 @@ function DeleteOrUpdateResourceGroups() {
}

Write-Verbose "Fetching groups"
[Array]$allGroups = Retry { Get-AzResourceGroup }
[Array]$allGroups = Retry { Get-AzResourceGroup } | Where-Object { $_.ResourceGroupName -like $GroupFilter }
$toDelete = @()
$toClean = @()
$toDeleteSoon = @()
Expand Down Expand Up @@ -449,7 +451,7 @@ function DeleteAndPurgeGroups([array]$toDelete) {
}

if (!$purgeableResources.Count) {
return
return $hasError
}
if ($Force -or $PSCmdlet.ShouldProcess("Purgable Resources", "Delete Purgeable Resources")) {
# Purge all the purgeable resources and get a list of resources (as a collection) we need to follow-up on.
Expand Down

0 comments on commit 08a486a

Please sign in to comment.