Bylaw

Gallery › Storage › Azure built-in policies

Storage accounts should use customer-managed key for encryption

Converted Policy Audit Disabled Mode Indexed Version 1.1.0

Secure your blob and file storage account with greater flexibility using customer-managed keys. When you specify a customer-managed key, that key is used to protect and control access to the key that encrypts your data. Using customer-managed keys provides additional capabilities to control rotation of the key encryption key or cryptographically erase data.

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

Azure Policy JSONStorageAccountCustomerManagedKeyEnabled_Audit.json · 114 lines
{
  "properties": {
    "displayName": "Storage accounts should use customer-managed key for encryption",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Secure your blob and file storage account with greater flexibility using customer-managed keys. When you specify a customer-managed key, that key is used to protect and control access to the key that encrypts your data. Using customer-managed keys provides additional capabilities to control rotation of the key encryption key or cryptographically erase data.",
    "metadata": {
      "version": "1.1.0",
      "category": "Storage"
    },
    "version": "1.1.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      },
      "excludedManagedByResourceProviders": {
        "type": "Array",
        "metadata": {
          "displayName": "Managed services to exclude",
          "description": "Select managed services whose storage accounts should be excluded from this policy, or select all managed resource groups. Leave empty to evaluate all storage accounts."
        },
        "allowedValues": [
          "Azure Red Hat OpenShift",
          "Azure Databricks",
          "Microsoft Purview",
          "All managed resource groups"
        ],
        "defaultValue": []
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Storage/storageAccounts"
          },
          {
            "anyOf": [
              {
                "value": "[contains(parameters('excludedManagedByResourceProviders'), 'Azure Red Hat OpenShift')]",
                "equals": false
              },
              {
                "value": "[resourcegroup().managedBy]",
                "notContains": "/providers/Microsoft.RedHatOpenShift/"
              }
            ]
          },
          {
            "anyOf": [
              {
                "value": "[contains(parameters('excludedManagedByResourceProviders'), 'Azure Databricks')]",
                "equals": false
              },
              {
                "value": "[resourcegroup().managedBy]",
                "notContains": "/providers/Microsoft.Databricks/"
              }
            ]
          },
          {
            "anyOf": [
              {
                "value": "[contains(parameters('excludedManagedByResourceProviders'), 'Microsoft Purview')]",
                "equals": false
              },
              {
                "value": "[resourcegroup().managedBy]",
                "notContains": "/providers/Microsoft.Purview/"
              }
            ]
          },
          {
            "anyOf": [
              {
                "value": "[contains(parameters('excludedManagedByResourceProviders'), 'All managed resource groups')]",
                "equals": false
              },
              {
                "value": "[empty(resourcegroup().managedBy)]",
                "equals": true
              }
            ]
          },
          {
            "not": {
              "field": "Microsoft.Storage/storageAccounts/encryption.keySource",
              "equals": "Microsoft.Keyvault"
            }
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "1.1.0",
      "1.0.3"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/6fac406b-40ca-413b-bf8e-0bf964659c25",
  "name": "6fac406b-40ca-413b-bf8e-0bf964659c25"
}
Bylaw6fac406b-40ca-413b-bf8e-0bf964659c25.bylaw.ps1 · 54 lines
# Converted from StorageAccountCustomerManagedKeyEnabled_Audit.json by ConvertTo-Bylaw.
Policy '6fac406b-40ca-413b-bf8e-0bf964659c25' {

    Properties @{
        DisplayName   = 'Storage accounts should use customer-managed key for encryption'
        Description   = 'Secure your blob and file storage account with greater flexibility using customer-managed keys. When you specify a customer-managed key, that key is used to protect and control access to the key that encrypts your data. Using customer-managed keys provides additional capabilities to control rotation of the key encryption key or cryptographically erase data.'
        Version       = '1.1.0'
        Category      = 'Storage'
        PolicyVersion = '1.1.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter ExcludedManagedByResourceProviders @{
        Type          = 'Array'
        AllowedValues = @('Azure Red Hat OpenShift', 'Azure Databricks', 'Microsoft Purview', 'All managed resource groups')
        Default       = @()
        DisplayName   = 'Managed services to exclude'
        Description   = 'Select managed services whose storage accounts should be excluded from this policy, or select all managed resource groups. Leave empty to evaluate all storage accounts.'
    }

    Resource 'Microsoft.Storage/storageAccounts' {

        When {
            AnyOf {
                Arm '[contains(parameters(''excludedManagedByResourceProviders''), ''Azure Red Hat OpenShift'')]' | Must -Be $false
                Arm '[resourcegroup().managedBy]' | Must -Not -ContainText '/providers/Microsoft.RedHatOpenShift/'
            }
            AnyOf {
                Arm '[contains(parameters(''excludedManagedByResourceProviders''), ''Azure Databricks'')]' | Must -Be $false
                Arm '[resourcegroup().managedBy]' | Must -Not -ContainText '/providers/Microsoft.Databricks/'
            }
            AnyOf {
                Arm '[contains(parameters(''excludedManagedByResourceProviders''), ''Microsoft Purview'')]' | Must -Be $false
                Arm '[resourcegroup().managedBy]' | Must -Not -ContainText '/providers/Microsoft.Purview/'
            }
            AnyOf {
                Arm '[contains(parameters(''excludedManagedByResourceProviders''), ''All managed resource groups'')]' | Must -Be $false
                Arm '[empty(resourcegroup().managedBy)]' | Must -Be $true
            }
        }

        Require 'encryption.keySource is Microsoft.Keyvault' {
            Field encryption.keySource | Must -Be 'Microsoft.Keyvault'
        }
    }

    Effect (Param Effect)
}

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