Skip to content

Commit 52d3d42

Browse files
committed
Restrict live test storage account access to client IP
1 parent 7510ce4 commit 52d3d42

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

eng/common/TestResources/New-TestResources.ps1

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function MergeHashes([hashtable] $source, [psvariable] $dest)
262262
function BuildBicepFile([System.IO.FileSystemInfo] $file)
263263
{
264264
if (!(Get-Command bicep -ErrorAction Ignore)) {
265-
Write-Error "A bicep file was found at '$($file.FullName)' but the Azure Bicep CLI is not installed. See https://aka.ms/install-bicep-pwsh"
265+
Write-Error "A bicep file was found at '$($file.FullName)' but the Azure Bicep CLI is not installed. See aka.ms/bicep-install"
266266
throw
267267
}
268268

@@ -758,7 +758,8 @@ try {
758758
if ($TestApplicationSecret -and $ServicePrincipalAuth) {
759759
$templateParameters.Add('testApplicationSecret', $TestApplicationSecret)
760760
}
761-
if ($CI -and $Environment -eq 'AzureCloud') {
761+
# Only add subnets when running in an azure pipeline context
762+
if ($env:SYSTEM_TEAMPROJECTID -and $Environment -eq 'AzureCloud') {
762763
$templateParameters.Add('azsdkPipelineSubnetList', $azsdkPipelineSubnets)
763764
}
764765

@@ -838,6 +839,28 @@ try {
838839
-templateFile $templateFile `
839840
-environmentVariables $EnvironmentVariables
840841

842+
$storageAccounts = Retry { Get-AzResource -ResourceGroupName $ResourceGroupName -ResourceType "Microsoft.Storage/storageAccounts" }
843+
# Add client IP to storage account when running as local user. Pipeline's have their own vnet with access
844+
if ($storageAccounts) {
845+
foreach ($account in $storageAccounts) {
846+
$rules = Get-AzStorageAccountNetworkRuleSet -ResourceGroupName $ResourceGroupName -AccountName $account.Name
847+
if ($rules -and $rules.DefaultAction -eq "Allow") {
848+
Write-Host "Restricting network rules in storage account '$($account.Name)' to deny access by default"
849+
Retry { Update-AzStorageAccountNetworkRuleSet -ResourceGroupName $ResourceGroupName -Name $account.Name -DefaultAction Deny }
850+
if ($env:SYSTEM_TEAMPROJECTID) {
851+
Write-Host "Enabling access to '$($account.Name)' from pipeline subnets"
852+
foreach ($subnet in $azsdkPipelineSubnets) {
853+
Retry { Add-AzStorageAccountNetworkRule -ResourceGroupName $ResourceGroupName -Name $account.Name -VirtualNetworkResourceId $subnet }
854+
}
855+
} else {
856+
Write-Host "Enabling access to '$($account.Name)' from client IP"
857+
$clientIp ??= Retry { Invoke-RestMethod -Uri 'https://icanhazip.com/' } # cloudflare owned ip site
858+
Retry { Add-AzStorageAccountNetworkRule -ResourceGroupName $ResourceGroupName -Name $account.Name -IPAddressOrRange $clientIp | Out-Null }
859+
}
860+
}
861+
}
862+
}
863+
841864
$postDeploymentScript = $templateFile.originalFilePath | Split-Path | Join-Path -ChildPath "$ResourceType-resources-post.ps1"
842865
if (Test-Path $postDeploymentScript) {
843866
Log "Invoking post-deployment script '$postDeploymentScript'"
@@ -852,7 +875,6 @@ try {
852875
Write-Host "Deleting ARM deployment as it may contain secrets. Deployed resources will not be affected."
853876
$null = $deployment | Remove-AzResourceGroupDeployment
854877
}
855-
856878
} finally {
857879
$exitActions.Invoke()
858880
}

0 commit comments

Comments
 (0)