diff --git a/CHANGELOG.md b/CHANGELOG.md index 99b81be..c0ff1f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,19 @@ All notable changes to LibreDevOpsHelpers are recorded here. +## 3.0.2 + +### Fixed +- `Add-LdoKeyVaultCurrentIpRule` and `Add-LdoStorageCurrentIpRule` now see a rule that is already + there. Both checked with a top-level filter (`[?ipAddress=='']`), but `az keyvault + network-rule list` and `az storage account network-rule list` return an object + (`{ ipRules: [...] }`), not a list, so the check never matched and the rule was added again on + every call. The Azure CLI deduplicated it, so nothing broke, but the log always claimed a new + rule. They now read `ipRules[].value` (Key Vault) and `ipRules[].ipAddressOrRange` (storage), + match the address with or without `/32` (Key Vault stores a single address as a /32), and log + "already present" when it is. Found during a live Key Vault dance from the azure-orchestration + build. + ## 3.0.1 ### Fixed diff --git a/LibreDevOpsHelpers/LibreDevOpsHelpers.AzureKeyVault/LibreDevOpsHelpers.AzureKeyVault.psm1 b/LibreDevOpsHelpers/LibreDevOpsHelpers.AzureKeyVault/LibreDevOpsHelpers.AzureKeyVault.psm1 index da4305e..926ece4 100644 --- a/LibreDevOpsHelpers/LibreDevOpsHelpers.AzureKeyVault/LibreDevOpsHelpers.AzureKeyVault.psm1 +++ b/LibreDevOpsHelpers/LibreDevOpsHelpers.AzureKeyVault/LibreDevOpsHelpers.AzureKeyVault.psm1 @@ -110,13 +110,19 @@ function Add-LdoKeyVaultCurrentIpRule { az @update | Out-Null Assert-LdoLastExitCode -Operation "az keyvault update ($KeyVaultName)" - $existing = az keyvault network-rule list -g $ResourceGroup -n $KeyVaultName --query "[?ipAddress=='$ip']" -o tsv - if (-not $existing) { + # az keyvault network-rule list returns an object ({ ipRules: [{ value }], virtualNetworkRules }), + # not a list, so a top-level filter never matches; and the vault stores a single address as a + # /32 CIDR, so match either form. + $rules = @(az keyvault network-rule list -g $ResourceGroup -n $KeyVaultName --query 'ipRules[].value' -o json | ConvertFrom-Json) + Assert-LdoLastExitCode -Operation "az keyvault network-rule list ($KeyVaultName)" + if (($rules -notcontains $ip) -and ($rules -notcontains "$ip/32")) { az keyvault network-rule add -g $ResourceGroup -n $KeyVaultName --ip-address $ip | Out-Null Assert-LdoLastExitCode -Operation "az keyvault network-rule add ($KeyVaultName)" + Write-LdoLog -Level INFO -Message "Added temporary Key Vault rule for $ip on $KeyVaultName." + } + else { + Write-LdoLog -Level INFO -Message "Key Vault rule for $ip already present on $KeyVaultName; skipping add." } - - Write-LdoLog -Level INFO -Message "Added temporary Key Vault rule for $ip on $KeyVaultName." } function Remove-LdoKeyVaultCurrentIpRule { diff --git a/LibreDevOpsHelpers/LibreDevOpsHelpers.AzureStorage/LibreDevOpsHelpers.AzureStorage.psm1 b/LibreDevOpsHelpers/LibreDevOpsHelpers.AzureStorage/LibreDevOpsHelpers.AzureStorage.psm1 index 11ce7cc..53fea1a 100644 --- a/LibreDevOpsHelpers/LibreDevOpsHelpers.AzureStorage/LibreDevOpsHelpers.AzureStorage.psm1 +++ b/LibreDevOpsHelpers/LibreDevOpsHelpers.AzureStorage/LibreDevOpsHelpers.AzureStorage.psm1 @@ -102,8 +102,12 @@ function Add-LdoStorageCurrentIpRule { az @update | Out-Null Assert-LdoLastExitCode -Operation "az storage account update ($StorageAccountName)" - $existing = az storage account network-rule list -g $ResourceGroup -n $StorageAccountName --query "[?ipAddress=='$ip']" -o tsv - if (-not $existing) { + # az storage account network-rule list returns an object ({ ipRules: [{ action, ipAddressOrRange }], + # virtualNetworkRules, ... }), not a list, so a top-level filter never matches. Match the address + # with or without a /32 suffix. + $rules = @(az storage account network-rule list -g $ResourceGroup -n $StorageAccountName --query 'ipRules[].ipAddressOrRange' -o json | ConvertFrom-Json) + Assert-LdoLastExitCode -Operation "az storage account network-rule list ($StorageAccountName)" + if (($rules -notcontains $ip) -and ($rules -notcontains "$ip/32")) { az storage account network-rule add -g $ResourceGroup -n $StorageAccountName --ip-address $ip | Out-Null Assert-LdoLastExitCode -Operation "az storage account network-rule add ($StorageAccountName)" Write-LdoLog -Level INFO -Message "Added temporary storage rule for $ip on $StorageAccountName." diff --git a/LibreDevOpsHelpers/LibreDevOpsHelpers.psd1 b/LibreDevOpsHelpers/LibreDevOpsHelpers.psd1 index 3b213e2..e2bb8fd 100644 --- a/LibreDevOpsHelpers/LibreDevOpsHelpers.psd1 +++ b/LibreDevOpsHelpers/LibreDevOpsHelpers.psd1 @@ -12,7 +12,7 @@ RootModule = '.\LibreDevOpsHelpers.psm1' # Version number of this module. - ModuleVersion = '3.0.1' + ModuleVersion = '3.0.2' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/tests/AzureKeyVault.Tests.ps1 b/tests/AzureKeyVault.Tests.ps1 index 7a0c9e1..10ca582 100644 --- a/tests/AzureKeyVault.Tests.ps1 +++ b/tests/AzureKeyVault.Tests.ps1 @@ -30,3 +30,67 @@ Describe 'SoftFail dance option' { (Get-Command $_).Parameters.ContainsKey('SoftFail') | Should -BeTrue } } + +Describe 'Add-LdoKeyVaultCurrentIpRule existing-rule detection' { + # az keyvault network-rule list returns { ipRules: [{ value }] }, and the vault stores a single + # address as a /32. The check used to filter the top level (never matching), so it re-added the + # rule every time; az only deduplicated it. + + It 'skips the add when the vault already allows <_>' -ForEach @('203.0.113.7/32', '203.0.113.7') { + InModuleScope LibreDevOpsHelpers.AzureKeyVault -Parameters @{ Existing = $_ } { + param($Existing) + Mock Test-LdoKeyVaultDanceTarget { $true } + Mock Get-LdoPublicIpAddress { '203.0.113.7' } + Mock Assert-LdoLastExitCode { } + Mock Write-LdoLog { } + # The canned az output is closed over, so the mock sees it wherever Pester runs it. + $show = '{"publicNetworkAccess":"Enabled","defaultAction":"Deny","bypass":"AzureServices"}' + $list = ConvertTo-Json -InputObject @($Existing) -Compress + $azMock = { + $joined = $args -join ' ' + if ($joined -like 'keyvault show*') { return $show } + if ($joined -like 'keyvault network-rule list*') { + # Like the real CLI: the command returns an object, so only the ipRules + # projection reaches the addresses; any other query (such as a top-level + # filter, the old bug) finds nothing. + $query = $args[[array]::IndexOf($args, '--query') + 1] + if ($query -eq 'ipRules[].value') { return $list } + return '' + } + return '' + }.GetNewClosure() + Mock az $azMock + Add-LdoKeyVaultCurrentIpRule -ResourceGroup rg -KeyVaultName kv + Should -Invoke az -Times 0 -Exactly -ParameterFilter { ($args -join ' ') -like 'keyvault network-rule add*' } + } + } + + It 'adds the rule when the vault does not allow the address' { + InModuleScope LibreDevOpsHelpers.AzureKeyVault -Parameters @{ Existing = '198.51.100.1/32' } { + param($Existing) + Mock Test-LdoKeyVaultDanceTarget { $true } + Mock Get-LdoPublicIpAddress { '203.0.113.7' } + Mock Assert-LdoLastExitCode { } + Mock Write-LdoLog { } + # The canned az output is closed over, so the mock sees it wherever Pester runs it. + $show = '{"publicNetworkAccess":"Enabled","defaultAction":"Deny","bypass":"AzureServices"}' + $list = ConvertTo-Json -InputObject @($Existing) -Compress + $azMock = { + $joined = $args -join ' ' + if ($joined -like 'keyvault show*') { return $show } + if ($joined -like 'keyvault network-rule list*') { + # Like the real CLI: the command returns an object, so only the ipRules + # projection reaches the addresses; any other query (such as a top-level + # filter, the old bug) finds nothing. + $query = $args[[array]::IndexOf($args, '--query') + 1] + if ($query -eq 'ipRules[].value') { return $list } + return '' + } + return '' + }.GetNewClosure() + Mock az $azMock + Add-LdoKeyVaultCurrentIpRule -ResourceGroup rg -KeyVaultName kv + Should -Invoke az -Times 1 -Exactly -ParameterFilter { ($args -join ' ') -like 'keyvault network-rule add*--ip-address 203.0.113.7*' } + } + } +} diff --git a/tests/AzureStorage.Tests.ps1 b/tests/AzureStorage.Tests.ps1 index de905e8..6e085ea 100644 --- a/tests/AzureStorage.Tests.ps1 +++ b/tests/AzureStorage.Tests.ps1 @@ -30,3 +30,66 @@ Describe 'SoftFail dance option' { (Get-Command $_).Parameters.ContainsKey('SoftFail') | Should -BeTrue } } + +Describe 'Add-LdoStorageCurrentIpRule existing-rule detection' { + # az storage account network-rule list returns { ipRules: [{ action, ipAddressOrRange }] }. The + # check used to filter the top level (never matching), so it re-added the rule every time. + + It 'skips the add when the account already allows <_>' -ForEach @('203.0.113.7', '203.0.113.7/32') { + InModuleScope LibreDevOpsHelpers.AzureStorage -Parameters @{ Existing = $_ } { + param($Existing) + Mock Test-LdoStorageDanceTarget { $true } + Mock Get-LdoPublicIpAddress { '203.0.113.7' } + Mock Assert-LdoLastExitCode { } + Mock Write-LdoLog { } + # The canned az output is closed over, so the mock sees it wherever Pester runs it. + $show = '{"publicNetworkAccess":"Enabled","networkRuleSet":{"defaultAction":"Deny","bypass":"AzureServices"}}' + $list = ConvertTo-Json -InputObject @($Existing) -Compress + $azMock = { + $joined = $args -join ' ' + if ($joined -like 'storage account show*') { return $show } + if ($joined -like 'storage account network-rule list*') { + # Like the real CLI: the command returns an object, so only the ipRules + # projection reaches the addresses; any other query (such as a top-level + # filter, the old bug) finds nothing. + $query = $args[[array]::IndexOf($args, '--query') + 1] + if ($query -eq 'ipRules[].ipAddressOrRange') { return $list } + return '' + } + return '' + }.GetNewClosure() + Mock az $azMock + Add-LdoStorageCurrentIpRule -ResourceGroup rg -StorageAccountName st + Should -Invoke az -Times 0 -Exactly -ParameterFilter { ($args -join ' ') -like 'storage account network-rule add*' } + } + } + + It 'adds the rule when the account does not allow the address' { + InModuleScope LibreDevOpsHelpers.AzureStorage -Parameters @{ Existing = '198.51.100.1' } { + param($Existing) + Mock Test-LdoStorageDanceTarget { $true } + Mock Get-LdoPublicIpAddress { '203.0.113.7' } + Mock Assert-LdoLastExitCode { } + Mock Write-LdoLog { } + # The canned az output is closed over, so the mock sees it wherever Pester runs it. + $show = '{"publicNetworkAccess":"Enabled","networkRuleSet":{"defaultAction":"Deny","bypass":"AzureServices"}}' + $list = ConvertTo-Json -InputObject @($Existing) -Compress + $azMock = { + $joined = $args -join ' ' + if ($joined -like 'storage account show*') { return $show } + if ($joined -like 'storage account network-rule list*') { + # Like the real CLI: the command returns an object, so only the ipRules + # projection reaches the addresses; any other query (such as a top-level + # filter, the old bug) finds nothing. + $query = $args[[array]::IndexOf($args, '--query') + 1] + if ($query -eq 'ipRules[].ipAddressOrRange') { return $list } + return '' + } + return '' + }.GetNewClosure() + Mock az $azMock + Add-LdoStorageCurrentIpRule -ResourceGroup rg -StorageAccountName st + Should -Invoke az -Times 1 -Exactly -ParameterFilter { ($args -join ' ') -like 'storage account network-rule add*--ip-address 203.0.113.7*' } + } + } +}