Bylaw

Gallery › Key Vault › Azure Community Policy

Enforce key vault firewall blocking public access

Converted Policy Audit Deny Disabled Mode Indexed Version 1.0.0

This policy prevents setting key vault public firewall as allow all or have any vnet/ip rules.

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

Azure Policy JSONazurepolicy.json · 72 lines
{
  "name": "8d207454-abad-446e-a5fd-2e4279b663ef",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Enforce key vault firewall blocking public access",
    "description": "This policy prevents setting key vault public firewall as allow all or have any vnet/ip rules.",
    "metadata": {
      "category": "Key Vault",
      "createdBy": "",
      "updatedBy": null,
      "updatedOn": null,
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "equals": "Microsoft.KeyVault/Vaults",
            "field": "type"
          },
          {
            "anyOf": [
              {
                "exists": "false",
                "field": "Microsoft.KeyVault/Vaults/networkAcls.defaultAction"
              },
              {
                "equals": "Allow",
                "field": "Microsoft.KeyVault/Vaults/networkAcls.defaultAction"
              },
              {
                "exists": "false",
                "field": "Microsoft.KeyVault/Vaults/networkAcls.virtualNetworkRules"
              },
              {
                "notequals": "0",
                "value": "[length(field('Microsoft.KeyVault/Vaults/networkAcls.virtualNetworkRules'))]"
              },
              {
                "exists": "false",
                "field": "Microsoft.KeyVault/Vaults/networkAcls.ipRules"
              },
              {
                "notequals": "0",
                "value": "[length(field('Microsoft.KeyVault/Vaults/networkAcls.ipRules'))]"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw8d207454-abad-446e-a5fd-2e4279b663ef.bylaw.ps1 · 47 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '8d207454-abad-446e-a5fd-2e4279b663ef' {

    Properties @{
        DisplayName = 'Enforce key vault firewall blocking public access'
        Description = 'This policy prevents setting key vault public firewall as allow all or have any vnet/ip rules.'
        Category    = 'Key Vault'
        Version     = '1.0.0'
        Mode        = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.KeyVault/Vaults' {

        Require 'networkAcls.defaultAction exists' {
            Field networkAcls.defaultAction | Must -Exist
        }

        Require 'networkAcls.defaultAction is not Allow' {
            Field networkAcls.defaultAction | Must -Not -Be 'Allow' -IfPresent
        }

        Require 'networkAcls.virtualNetworkRules exists' {
            Field networkAcls.virtualNetworkRules | Must -Exist
        }

        Require 'networkAcls.virtualNetworkRules is as required' {
            Arm '[length(field(''Microsoft.KeyVault/Vaults/networkAcls.virtualNetworkRules''))]' | Must -Be '0'
        }

        Require 'networkAcls.ipRules exists' {
            Field networkAcls.ipRules | Must -Exist
        }

        Require 'networkAcls.ipRules is as required' {
            Arm '[length(field(''Microsoft.KeyVault/Vaults/networkAcls.ipRules''))]' | Must -Be '0'
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Key Vault/enforce-key-vault-firewall-blocking-public-access/azurepolicy.json in Azure/Community-Policy, under the MIT licence.