Bylaw

Gallery › Key Vault › Azure built-in policies

Key vaults should have deletion protection enabled

Converted Policy Audit Deny Disabled Mode Indexed Version 2.1.0

Malicious deletion of a key vault can lead to permanent data loss. You can prevent permanent data loss by enabling purge protection and soft delete. Purge protection protects you from insider attacks by enforcing a mandatory retention period for soft deleted key vaults. No one inside your organization or Microsoft will be able to purge your key vaults during the soft delete retention period. Keep in mind that key vaults created after September 1st 2019 have soft-delete enabled by default.

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

Azure Policy JSONRecoverable_Audit.json · 94 lines
{
  "properties": {
    "displayName": "Key vaults should have deletion protection enabled",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Malicious deletion of a key vault can lead to permanent data loss. You can prevent permanent data loss by enabling purge protection and soft delete. Purge protection protects you from insider attacks by enforcing a mandatory retention period for soft deleted key vaults. No one inside your organization or Microsoft will be able to purge your key vaults during the soft delete retention period. Keep in mind that key vaults created after September 1st 2019 have soft-delete enabled by default.",
    "metadata": {
      "version": "2.1.0",
      "category": "Key Vault"
    },
    "version": "2.1.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.KeyVault/vaults"
          },
          {
            "field": "Microsoft.KeyVault/vaults/createMode",
            "notEquals": "recover"
          },
          {
            "anyOf": [
              {
                "allOf": [
                  {
                    "value": "[requestContext().apiVersion]",
                    "less": "2019-09-01"
                  },
                  {
                    "anyOf": [
                      {
                        "field": "Microsoft.KeyVault/vaults/enableSoftDelete",
                        "equals": "false"
                      },
                      {
                        "field": "Microsoft.KeyVault/vaults/enableSoftDelete",
                        "exists": "false"
                      }
                    ]
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "value": "[requestContext().apiVersion]",
                    "greaterOrEquals": "2019-09-01"
                  },
                  {
                    "field": "Microsoft.KeyVault/vaults/enableSoftDelete",
                    "equals": "false"
                  }
                ]
              },
              {
                "field": "Microsoft.KeyVault/vaults/enablePurgeProtection",
                "exists": "false"
              },
              {
                "field": "Microsoft.KeyVault/vaults/enablePurgeProtection",
                "equals": "false"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "2.1.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/0b60c0b2-2dc2-4e1c-b5c9-abbed971de53",
  "name": "0b60c0b2-2dc2-4e1c-b5c9-abbed971de53"
}
Bylaw0b60c0b2-2dc2-4e1c-b5c9-abbed971de53.bylaw.ps1 · 53 lines
# Converted from Recoverable_Audit.json by ConvertTo-Bylaw.
Policy '0b60c0b2-2dc2-4e1c-b5c9-abbed971de53' {

    Properties @{
        DisplayName   = 'Key vaults should have deletion protection enabled'
        Description   = 'Malicious deletion of a key vault can lead to permanent data loss. You can prevent permanent data loss by enabling purge protection and soft delete. Purge protection protects you from insider attacks by enforcing a mandatory retention period for soft deleted key vaults. No one inside your organization or Microsoft will be able to purge your key vaults during the soft delete retention period. Keep in mind that key vaults created after September 1st 2019 have soft-delete enabled by default.'
        Version       = '2.1.0'
        Category      = 'Key Vault'
        PolicyVersion = '2.1.0'
        Mode          = 'Indexed'
    }

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

    Resource 'Microsoft.KeyVault/vaults' {

        When {
            Field createMode | Must -Not -Be 'recover'
        }

        Require 'enableSoftDelete is as required' {
            AnyOf {
                Arm '[requestContext().apiVersion]' | Must -BeGreaterOrEqual '2019-09-01'
                AllOf {
                    Field enableSoftDelete | Must -Not -Be 'false' -IfPresent
                    Field enableSoftDelete | Must -Exist
                }
            }
        }

        Require 'enableSoftDelete is as required' {
            AnyOf {
                Arm '[requestContext().apiVersion]' | Must -BeLessThan '2019-09-01'
                Field enableSoftDelete | Must -Not -Be 'false' -IfPresent
            }
        }

        Require 'enablePurgeProtection exists' {
            Field enablePurgeProtection | Must -Exist
        }

        Require 'enablePurgeProtection is not false' {
            Field enablePurgeProtection | Must -Not -Be 'false' -IfPresent
        }
    }

    Effect (Param Effect)
}

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