Bylaw

Gallery › Compute › Azure built-in policies

OS and data disks should be encrypted with a customer-managed key

Converted Policy Audit Deny Disabled Mode Indexed Version 3.0.0

Use customer-managed keys to manage the encryption at rest of the contents of your managed disks. By default, the data is encrypted at rest with platform-managed keys, but customer-managed keys are commonly required to meet regulatory compliance standards. Customer-managed keys enable the data to be encrypted with an Azure Key Vault key created and owned by you. You have full control and responsibility for the key lifecycle, including rotation and management. Learn more at https://aka.ms/disks-cmk.

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

Azure Policy JSONOSAndDataDiskCMKRequired_Deny.json · 192 lines
{
  "properties": {
    "displayName": "OS and data disks should be encrypted with a customer-managed key",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Use customer-managed keys to manage the encryption at rest of the contents of your managed disks. By default, the data is encrypted at rest with platform-managed keys, but customer-managed keys are commonly required to meet regulatory compliance standards. Customer-managed keys enable the data to be encrypted with an Azure Key Vault key created and owned by you. You have full control and responsibility for the key lifecycle, including rotation and management. Learn more at https://aka.ms/disks-cmk.",
    "metadata": {
      "category": "Compute",
      "version": "3.0.0"
    },
    "version": "3.0.0",
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "Audit",
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      }
    },
    "policyRule": {
      "if": {
        "anyOf": [
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/virtualMachines"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/storageProfile.osDisk.managedDisk.diskEncryptionSet.id",
                "exists": "False"
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/virtualMachines"
              },
              {
                "value": "[length(field('Microsoft.Compute/virtualMachines/storageProfile.dataDisks'))]",
                "greater": 0
              },
              {
                "field": "Microsoft.Compute/virtualMachines/storageProfile.dataDisks[*].managedDisk.id",
                "exists": "False"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/storageProfile.dataDisks[*].managedDisk.diskEncryptionSet.id",
                "exists": "False"
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/virtualMachineScaleSets"
              },
              {
                "field": "Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.storageProfile.osDisk.managedDisk.diskEncryptionSet.id",
                "exists": "False"
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/virtualMachineScaleSets"
              },
              {
                "count": {
                  "field": "Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.storageProfile.dataDisks[*]"
                },
                "greater": 0
              },
              {
                "not": {
                  "field": "Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.storageProfile.dataDisks[*].managedDisk.diskEncryptionSet.id",
                  "exists": "true"
                }
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/disks"
              },
              {
                "field": "Microsoft.Compute/disks/encryption.diskEncryptionSetId",
                "exists": "False"
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/galleries/images/versions"
              },
              {
                "value": "[length(field('Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*].encryption.osDiskImage.diskEncryptionSetId'))]",
                "notEquals": "[length(field('Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*]'))]"
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/galleries/images/versions"
              },
              {
                "value": "[length(field('Microsoft.Compute/galleries/images/versions/storageProfile.dataDiskImages[*]'))]",
                "greater": 0
              },
              {
                "anyOf": [
                  {
                    "count": {
                      "field": "Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*]",
                      "where": {
                        "value": "[length(current('Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*].encryption.dataDiskImages[*].diskEncryptionSetId'))]",
                        "notEquals": "[length(field('Microsoft.Compute/galleries/images/versions/storageProfile.dataDiskImages[*]'))]"
                      }
                    },
                    "greater": 0
                  },
                  {
                    "not": {
                      "field": "Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*].encryption.dataDiskImages[*].diskEncryptionSetId",
                      "exists": "true"
                    }
                  }
                ]
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/images"
              },
              {
                "field": "Microsoft.Compute/images/storageProfile.osDisk.diskEncryptionSet.id",
                "exists": "False"
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/images"
              },
              {
                "value": "[length(field('Microsoft.Compute/images/storageProfile.dataDisks[*]'))]",
                "greater": 0
              },
              {
                "not": {
                  "field": "Microsoft.Compute/images/storageProfile.dataDisks[*].diskEncryptionSet.id",
                  "exists": "true"
                }
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "3.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/702dd420-7fcc-42c5-afe8-4026edd20fe0",
  "name": "702dd420-7fcc-42c5-afe8-4026edd20fe0"
}
Bylaw702dd420-7fcc-42c5-afe8-4026edd20fe0.bylaw.ps1 · 118 lines
# Converted from OSAndDataDiskCMKRequired_Deny.json by ConvertTo-Bylaw.
Policy '702dd420-7fcc-42c5-afe8-4026edd20fe0' {

    Properties @{
        DisplayName   = 'OS and data disks should be encrypted with a customer-managed key'
        Description   = 'Use customer-managed keys to manage the encryption at rest of the contents of your managed disks. By default, the data is encrypted at rest with platform-managed keys, but customer-managed keys are commonly required to meet regulatory compliance standards. Customer-managed keys enable the data to be encrypted with an Azure Key Vault key created and owned by you. You have full control and responsibility for the key lifecycle, including rotation and management. Learn more at https://aka.ms/disks-cmk.'
        Category      = 'Compute'
        Version       = '3.0.0'
        PolicyVersion = '3.0.0'
        Mode          = 'Indexed'
    }

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

    Resource * {

        Require 'type and storageProfile.osDisk.managedDisk.diskEncryptionSet.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/virtualMachines' -IfPresent
                Field 'Microsoft.Compute/virtualMachines/storageProfile.osDisk.managedDisk.diskEncryptionSet.id' | Must -Exist
            }
        }

        Require 'type, storageProfile.dataDisks, managedDisk.id and 1 more are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/virtualMachines' -IfPresent
                Arm '[length(field(''Microsoft.Compute/virtualMachines/storageProfile.dataDisks''))]' | Must -BeLessOrEqual 0
                Count 'Microsoft.Compute/virtualMachines/storageProfile.dataDisks[*]' -Where {
                    Field managedDisk.id | Must -Exist
                } | Must -Not -Be 0
                Count 'Microsoft.Compute/virtualMachines/storageProfile.dataDisks[*]' -Where {
                    Field managedDisk.diskEncryptionSet.id | Must -Exist
                } | Must -Not -Be 0
            }
        }

        Require 'type and virtualMachineProfile.storageProfile.osDisk.managedDisk.diskEncryptionSet.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/virtualMachineScaleSets' -IfPresent
                Field 'Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.storageProfile.osDisk.managedDisk.diskEncryptionSet.id' | Must -Exist
            }
        }

        Require 'type, virtualMachineProfile.storageProfile.dataDisks and managedDisk.diskEncryptionSet.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/virtualMachineScaleSets' -IfPresent
                Count 'Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.storageProfile.dataDisks[*]' | Must -Be 0
                AllOf {
                    Field 'Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.storageProfile.dataDisks' | Must -Exist
                    Each 'Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.storageProfile.dataDisks[*]' {
                        Field managedDisk.diskEncryptionSet.id | Must -Exist
                    }
                }
            }
        }

        Require 'type and encryption.diskEncryptionSetId are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/disks' -IfPresent
                Field 'Microsoft.Compute/disks/encryption.diskEncryptionSetId' | Must -Exist
            }
        }

        Require 'type, publishingProfile.targetRegions.encryption.osDiskImage.diskEncryptionSetId and publishingProfile.targetRegions are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/galleries/images/versions' -IfPresent
                Arm '[length(field(''Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*].encryption.osDiskImage.diskEncryptionSetId''))]' | Must -Be (Arm '[length(field(''Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*]''))]')
            }
        }

        Require 'type, publishingProfile.targetRegions, encryption.dataDiskImages and 2 more are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/galleries/images/versions' -IfPresent
                Arm '[length(field(''Microsoft.Compute/galleries/images/versions/storageProfile.dataDiskImages[*]''))]' | Must -BeLessOrEqual 0
                AllOf {
                    Count 'Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*]' -Where {
                        Arm '[length(current(''Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*].encryption.dataDiskImages[*].diskEncryptionSetId''))]' | Must -Not -Be (Arm '[length(field(''Microsoft.Compute/galleries/images/versions/storageProfile.dataDiskImages[*]''))]')
                    } | Must -Be 0
                    Field 'Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions' | Must -Exist
                    Each 'Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*]' {
                        Field encryption.dataDiskImages | Must -Exist
                        Each 'encryption.dataDiskImages[*]' {
                            Field diskEncryptionSetId | Must -Exist
                        }
                    }
                }
            }
        }

        Require 'type and storageProfile.osDisk.diskEncryptionSet.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/images' -IfPresent
                Field 'Microsoft.Compute/images/storageProfile.osDisk.diskEncryptionSet.id' | Must -Exist
            }
        }

        Require 'type, storageProfile.dataDisks and diskEncryptionSet.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/images' -IfPresent
                Arm '[length(field(''Microsoft.Compute/images/storageProfile.dataDisks[*]''))]' | Must -BeLessOrEqual 0
                AllOf {
                    Field 'Microsoft.Compute/images/storageProfile.dataDisks' | Must -Exist
                    Each 'Microsoft.Compute/images/storageProfile.dataDisks[*]' {
                        Field diskEncryptionSet.id | Must -Exist
                    }
                }
            }
        }
    }

    Effect (Param Effect)
}

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