Skip to content

Commit

Permalink
Restrict live test storage account access to client IP
Browse files Browse the repository at this point in the history
  • Loading branch information
benbp authored and azure-sdk committed Jun 18, 2024
1 parent 9270e83 commit b640e7e
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions eng/common/TestResources/New-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ function MergeHashes([hashtable] $source, [psvariable] $dest)
function BuildBicepFile([System.IO.FileSystemInfo] $file)
{
if (!(Get-Command bicep -ErrorAction Ignore)) {
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"
Write-Error "A bicep file was found at '$($file.FullName)' but the Azure Bicep CLI is not installed. See aka.ms/bicep-install"
throw
}

Expand Down Expand Up @@ -758,7 +758,8 @@ try {
if ($TestApplicationSecret -and $ServicePrincipalAuth) {
$templateParameters.Add('testApplicationSecret', $TestApplicationSecret)
}
if ($CI -and $Environment -eq 'AzureCloud') {
# Only add subnets when running in an azure pipeline context
if ($env:SYSTEM_TEAMPROJECTID -and $Environment -eq 'AzureCloud') {
$templateParameters.Add('azsdkPipelineSubnetList', $azsdkPipelineSubnets)
}

Expand Down Expand Up @@ -838,6 +839,28 @@ try {
-templateFile $templateFile `
-environmentVariables $EnvironmentVariables

$storageAccounts = Retry { Get-AzResource -ResourceGroupName $ResourceGroupName -ResourceType "Microsoft.Storage/storageAccounts" }
# Add client IP to storage account when running as local user. Pipeline's have their own vnet with access
if ($storageAccounts) {
foreach ($account in $storageAccounts) {
$rules = Get-AzStorageAccountNetworkRuleSet -ResourceGroupName $ResourceGroupName -AccountName $account.Name
if ($rules -and $rules.DefaultAction -eq "Allow") {
Write-Host "Restricting network rules in storage account '$($account.Name)' to deny access by default"
Retry { Update-AzStorageAccountNetworkRuleSet -ResourceGroupName $ResourceGroupName -Name $account.Name -DefaultAction Deny }
if ($env:SYSTEM_TEAMPROJECTID) {
Write-Host "Enabling access to '$($account.Name)' from pipeline subnets"
foreach ($subnet in $azsdkPipelineSubnets) {
Retry { Add-AzStorageAccountNetworkRule -ResourceGroupName $ResourceGroupName -Name $account.Name -VirtualNetworkResourceId $subnet }
}
} else {
Write-Host "Enabling access to '$($account.Name)' from client IP"
$clientIp ??= Retry { Invoke-RestMethod -Uri 'https://icanhazip.com/' } # cloudflare owned ip site
Retry { Add-AzStorageAccountNetworkRule -ResourceGroupName $ResourceGroupName -Name $account.Name -IPAddressOrRange $clientIp | Out-Null }
}
}
}
}

$postDeploymentScript = $templateFile.originalFilePath | Split-Path | Join-Path -ChildPath "$ResourceType-resources-post.ps1"
if (Test-Path $postDeploymentScript) {
Log "Invoking post-deployment script '$postDeploymentScript'"
Expand All @@ -852,7 +875,6 @@ try {
Write-Host "Deleting ARM deployment as it may contain secrets. Deployed resources will not be affected."
$null = $deployment | Remove-AzResourceGroupDeployment
}

} finally {
$exitActions.Invoke()
}
Expand Down

0 comments on commit b640e7e

Please sign in to comment.