Bylaw

Gallery › Storage › Azure built-in policies

Configure your Storage account to enable blob versioning

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

You can enable Blob storage versioning to automatically maintain previous versions of an object. When blob versioning is enabled, you can access earlier versions of a blob to recover your data if it's modified or deleted.

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

Azure Policy JSONStorageAccountEnableBlobVersion_Audit.json · 66 lines
{
  "properties": {
    "displayName": "Configure your Storage account to enable blob versioning",
    "policyType": "BuiltIn",
    "mode": "All",
    "description": "You can enable Blob storage versioning to automatically maintain previous versions of an object. When blob versioning is enabled, you can access earlier versions of a blob to recover your data if it's modified or deleted.",
    "metadata": {
      "version": "1.0.0",
      "category": "Storage"
    },
    "version": "1.0.0",
    "parameters": {
      "effect": {
        "type": "String",
        "defaultValue": "Audit",
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "The effect determines what happens when the policy rule is evaluated to match"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Storage/storageAccounts/blobServices"
          },
          {
            "anyOf": [
              {
                "allOf": [
                  {
                    "value": "[requestContext().apiVersion]",
                    "less": "2019-10-10"
                  },
                  {
                    "field": "Microsoft.Storage/storageAccounts/blobServices/isVersioningEnabled",
                    "exists": "true"
                  }
                ]
              },
              {
                "field": "Microsoft.Storage/storageAccounts/blobServices/isVersioningEnabled",
                "equals": "false"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/c36a325b-ae04-4863-ad4f-19c6678f8e08",
  "name": "c36a325b-ae04-4863-ad4f-19c6678f8e08"
}
Bylawc36a325b-ae04-4863-ad4f-19c6678f8e08.bylaw.ps1 · 35 lines
# Converted from StorageAccountEnableBlobVersion_Audit.json by ConvertTo-Bylaw.
Policy 'c36a325b-ae04-4863-ad4f-19c6678f8e08' {

    Properties @{
        DisplayName   = 'Configure your Storage account to enable blob versioning'
        Description   = 'You can enable Blob storage versioning to automatically maintain previous versions of an object. When blob versioning is enabled, you can access earlier versions of a blob to recover your data if it''s modified or deleted.'
        Version       = '1.0.0'
        Category      = 'Storage'
        PolicyVersion = '1.0.0'
        Mode          = 'All'
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'The effect determines what happens when the policy rule is evaluated to match'
    }

    Resource 'Microsoft.Storage/storageAccounts/blobServices' {

        Require 'isVersioningEnabled is as required' {
            AnyOf {
                Arm '[requestContext().apiVersion]' | Must -BeGreaterOrEqual '2019-10-10'
                Field isVersioningEnabled | Must -Not -Exist
            }
        }

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

    Effect (Param Effect)
}

Source: built-in-policies/policyDefinitions/Storage/StorageAccountEnableBlobVersion_Audit.json in Azure/azure-policy, under the MIT licence.