Bylaw

Gallery › Key Vault › Azure built-in policies

Azure Key Vault should have firewall enabled or public network access disabled

Converted Policy Audit Deny Disabled Mode Indexed Version 3.3.0

Enable the key vault firewall so that the key vault is not accessible by default to any public IPs or disable public network access for your key vault so that it's not accessible over the public internet. Optionally, you can configure specific IP ranges to limit access to those networks. Learn more at: https://docs.microsoft.com/azure/key-vault/general/network-security and https://aka.ms/akvprivatelink

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONFirewallEnabled_Audit.json · 179 lines
{
  "properties": {
    "displayName": "Azure Key Vault should have firewall enabled or public network access disabled",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Enable the key vault firewall so that the key vault is not accessible by default to any public IPs or disable public network access for your key vault so that it's not accessible over the public internet. Optionally, you can configure specific IP ranges to limit access to those networks. Learn more at: https://docs.microsoft.com/azure/key-vault/general/network-security and https://aka.ms/akvprivatelink",
    "metadata": {
      "version": "3.3.0",
      "category": "Key Vault"
    },
    "version": "3.3.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Audit"
      },
      "restrictIPAddresses": {
        "type": "String",
        "metadata": {
          "displayName": "Would you like to restrict specific IP addresses?",
          "description": "Select (Yes) to allow or forbid a list of IP addresses. If (No), the list of IP addresses won't have any effect in the policy enforcement"
        },
        "defaultValue": "No",
        "allowedValues": [
          "Yes",
          "No"
        ]
      },
      "allowedIPAddresses": {
        "type": "Array",
        "metadata": {
          "displayName": "Allowed IP addresses",
          "description": "Array with allowed public IP addresses. An empty array is evaluated as to allow all IPs."
        },
        "defaultValue": []
      },
      "forbiddenIPAddresses": {
        "type": "Array",
        "metadata": {
          "displayName": "Forbidden IP addresses",
          "description": "Array with forbidden public IP addresses. An empty array is evaluated as there are no forbidden IP addresses."
        },
        "defaultValue": []
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.KeyVault/vaults"
          },
          {
            "field": "Microsoft.KeyVault/vaults/createMode",
            "notEquals": "recover"
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.KeyVault/vaults/networkAcls.defaultAction",
                "notEquals": "Deny"
              },
              {
                "allOf": [
                  {
                    "value": "[parameters('restrictIPAddresses')]",
                    "equals": "Yes"
                  },
                  {
                    "anyOf": [
                      {
                        "allOf": [
                          {
                            "count": {
                              "value": "[parameters('allowedIPAddresses')]",
                              "name": "allowedIPAddresses"
                            },
                            "notEquals": 0
                          },
                          {
                            "not": {
                              "count": {
                                "field": "Microsoft.KeyVault/vaults/networkAcls.ipRules[*]",
                                "where": {
                                  "count": {
                                    "value": "[parameters('allowedIPAddresses')]",
                                    "name": "allowedIpAddress",
                                    "where": {
                                      "value": "[ipRangeContains(current('allowedIpAddress'), current('Microsoft.KeyVault/vaults/networkAcls.ipRules[*].value'))]",
                                      "equals": true
                                    }
                                  },
                                  "greater": 0
                                }
                              },
                              "equals": "[length(field('Microsoft.KeyVault/vaults/networkAcls.ipRules[*]'))]"
                            }
                          }
                        ]
                      },
                      {
                        "allOf": [
                          {
                            "count": {
                              "value": "[parameters('forbiddenIPAddresses')]",
                              "name": "forbiddenIPAddresses"
                            },
                            "notEquals": 0
                          },
                          {
                            "not": {
                              "count": {
                                "field": "Microsoft.KeyVault/vaults/networkAcls.ipRules[*]",
                                "where": {
                                  "count": {
                                    "value": "[parameters('forbiddenIPAddresses')]",
                                    "name": "forbiddenIPAddresses",
                                    "where": {
                                      "value": "[ipRangeContains(current('forbiddenIPAddresses'), current('Microsoft.KeyVault/vaults/networkAcls.ipRules[*].value'))]",
                                      "equals": true
                                    }
                                  },
                                  "greater": 0
                                }
                              },
                              "equals": 0
                            }
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "anyOf": [
              {
                "value": "[requestContext().apiVersion]",
                "less": "2021-06-01-preview"
              },
              {
                "allOf": [
                  {
                    "value": "[requestContext().apiVersion]",
                    "greaterOrEquals": "2021-06-01-preview"
                  },
                  {
                    "field": "Microsoft.KeyVault/vaults/publicNetworkAccess",
                    "notEquals": "Disabled"
                  }
                ]
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "3.3.0",
      "3.2.1"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/55615ac9-af46-4a59-874e-391cc3dfb490",
  "name": "55615ac9-af46-4a59-874e-391cc3dfb490"
}
Bylaw55615ac9-af46-4a59-874e-391cc3dfb490.bylaw.ps1 · 81 lines
# Converted from FirewallEnabled_Audit.json by ConvertTo-Bylaw.
Policy '55615ac9-af46-4a59-874e-391cc3dfb490' {

    Properties @{
        DisplayName   = 'Azure Key Vault should have firewall enabled or public network access disabled'
        Description   = 'Enable the key vault firewall so that the key vault is not accessible by default to any public IPs or disable public network access for your key vault so that it''s not accessible over the public internet. Optionally, you can configure specific IP ranges to limit access to those networks. Learn more at: https://docs.microsoft.com/azure/key-vault/general/network-security and https://aka.ms/akvprivatelink'
        Version       = '3.3.0'
        Category      = 'Key Vault'
        PolicyVersion = '3.3.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter RestrictIPAddresses @{
        AllowedValues = @('Yes', 'No')
        Default       = 'No'
        DisplayName   = 'Would you like to restrict specific IP addresses?'
        Description   = 'Select (Yes) to allow or forbid a list of IP addresses. If (No), the list of IP addresses won''t have any effect in the policy enforcement'
    }
    Parameter AllowedIPAddresses @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Allowed IP addresses'
        Description = 'Array with allowed public IP addresses. An empty array is evaluated as to allow all IPs.'
    }
    Parameter ForbiddenIPAddresses @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Forbidden IP addresses'
        Description = 'Array with forbidden public IP addresses. An empty array is evaluated as there are no forbidden IP addresses.'
    }

    Resource 'Microsoft.KeyVault/vaults' {

        When {
            Field createMode | Must -Not -Be 'recover'
            AnyOf {
                Field networkAcls.defaultAction | Must -Not -Be 'Deny'
                AllOf {
                    Param RestrictIPAddresses | Must -Be 'Yes'
                    AnyOf {
                        AllOf {
                            Count -Value (Param AllowedIPAddresses) -Name allowedIPAddresses | Must -Not -Be 0
                            Count 'networkAcls.ipRules[*]' -Where {
                                Count -Value (Param AllowedIPAddresses) -Name allowedIpAddress -Where {
                                    Arm '[ipRangeContains(current(''allowedIpAddress''), current(''Microsoft.KeyVault/vaults/networkAcls.ipRules[*].value''))]' | Must -Be $true
                                } | Must -BeGreaterThan 0
                            } | Must -Not -Be (Arm '[length(field(''Microsoft.KeyVault/vaults/networkAcls.ipRules[*]''))]')
                        }
                        AllOf {
                            Count -Value (Param ForbiddenIPAddresses) -Name forbiddenIPAddresses | Must -Not -Be 0
                            Count 'networkAcls.ipRules[*]' -Where {
                                Count -Value (Param ForbiddenIPAddresses) -Name forbiddenIPAddresses -Where {
                                    Arm '[ipRangeContains(current(''forbiddenIPAddresses''), current(''Microsoft.KeyVault/vaults/networkAcls.ipRules[*].value''))]' | Must -Be $true
                                } | Must -BeGreaterThan 0
                            } | Must -Not -Be 0
                        }
                    }
                }
            }
        }

        Require 'Requirement 1' {
            Arm '[requestContext().apiVersion]' | Must -BeGreaterOrEqual '2021-06-01-preview'
        }

        Require 'publicNetworkAccess is as required' {
            AnyOf {
                Arm '[requestContext().apiVersion]' | Must -BeLessThan '2021-06-01-preview'
                Field publicNetworkAccess | Must -Be 'Disabled'
            }
        }
    }

    Effect (Param Effect)
}

Source: built-in-policies/policyDefinitions/Key Vault/FirewallEnabled_Audit.json in Azure/azure-policy, under the MIT licence.