Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=='<ip>']`), 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
2 changes: 1 addition & 1 deletion LibreDevOpsHelpers/LibreDevOpsHelpers.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = '.\LibreDevOpsHelpers.psm1'

# Version number of this module.
ModuleVersion = '3.0.1'
ModuleVersion = '3.0.2'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
64 changes: 64 additions & 0 deletions tests/AzureKeyVault.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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*' }
}
}
}
63 changes: 63 additions & 0 deletions tests/AzureStorage.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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*' }
}
}
}
Loading