Bylaw

Gallery › Compute › Azure built-in policies

Managed disks should use a specific set of disk encryption sets for the customer-managed key encryption

Converted Policy Audit Deny Disabled Mode Indexed Version 2.0.0

Requiring a specific set of disk encryption sets to be used with managed disks give you control over the keys used for encryption at rest. You are able to select the allowed encrypted sets and all others are rejected when attached to a disk. 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 JSONManagedDiskEncryptionSetsAllowed_Deny.json · 170 lines
{
  "properties": {
    "displayName": "Managed disks should use a specific set of disk encryption sets for the customer-managed key encryption",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Requiring a specific set of disk encryption sets to be used with managed disks give you control over the keys used for encryption at rest. You are able to select the allowed encrypted sets and all others are rejected when attached to a disk. Learn more at https://aka.ms/disks-cmk.",
    "metadata": {
      "category": "Compute",
      "version": "2.0.0"
    },
    "version": "2.0.0",
    "parameters": {
      "allowedEncryptionSets": {
        "type": "Array",
        "metadata": {
          "displayName": "Allowed disk encryption set",
          "description": "The list of allowed disk encryption sets for managed disks.",
          "strongType": "Microsoft.Compute/diskEncryptionSets"
        }
      },
      "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/disks"
              },
              {
                "field": "Microsoft.Compute/disks/managedBy",
                "exists": "False"
              },
              {
                "field": "Microsoft.Compute/disks/encryption.diskEncryptionSetId",
                "notIn": "[parameters('allowedEncryptionSets')]"
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/virtualMachines"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/storageProfile.osDisk.managedDisk.diskEncryptionSet.id",
                "notIn": "[parameters('allowedEncryptionSets')]"
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/virtualMachineScaleSets"
              },
              {
                "field": "Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.storageProfile.osDisk.managedDisk.diskEncryptionSet.id",
                "notIn": "[parameters('allowedEncryptionSets')]"
              }
            ]
          },
          {
            "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",
                  "in": "[parameters('allowedEncryptionSets')]"
                }
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/galleries/images/versions"
              },
              {
                "not": {
                  "field": "Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*].encryption.osDiskImage.diskEncryptionSetId",
                  "in": "[parameters('allowedEncryptionSets')]"
                }
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/galleries/images/versions"
              },
              {
                "value": "[length(field('Microsoft.Compute/galleries/images/versions/storageProfile.dataDiskImages[*]'))]",
                "greater": 0
              },
              {
                "not": {
                  "field": "Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*].encryption.dataDiskImages[*].diskEncryptionSetId",
                  "in": "[parameters('allowedEncryptionSets')]"
                }
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/images"
              },
              {
                "field": "Microsoft.Compute/images/storageProfile.osDisk.diskEncryptionSet.id",
                "notIn": "[parameters('allowedEncryptionSets')]"
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/images"
              },
              {
                "value": "[length(field('Microsoft.Compute/images/storageProfile.dataDisks[*]'))]",
                "greater": 0
              },
              {
                "field": "Microsoft.Compute/images/storageProfile.dataDisks[*].diskEncryptionSet.id",
                "notIn": "[parameters('allowedEncryptionSets')]"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "2.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/d461a302-a187-421a-89ac-84acdb4edc04",
  "name": "d461a302-a187-421a-89ac-84acdb4edc04"
}
Bylawd461a302-a187-421a-89ac-84acdb4edc04.bylaw.ps1 · 111 lines
# Converted from ManagedDiskEncryptionSetsAllowed_Deny.json by ConvertTo-Bylaw.
Policy 'd461a302-a187-421a-89ac-84acdb4edc04' {

    Properties @{
        DisplayName   = 'Managed disks should use a specific set of disk encryption sets for the customer-managed key encryption'
        Description   = 'Requiring a specific set of disk encryption sets to be used with managed disks give you control over the keys used for encryption at rest. You are able to select the allowed encrypted sets and all others are rejected when attached to a disk. Learn more at https://aka.ms/disks-cmk.'
        Category      = 'Compute'
        Version       = '2.0.0'
        PolicyVersion = '2.0.0'
        Mode          = 'Indexed'
    }

    Parameter AllowedEncryptionSets @{
        Type        = 'Array'
        DisplayName = 'Allowed disk encryption set'
        Description = 'The list of allowed disk encryption sets for managed disks.'
        Metadata    = [ordered]@{ strongType = 'Microsoft.Compute/diskEncryptionSets' }
    }
    Parameter Effect @{
        Type          = 'string'
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource * {

        Require 'type, managedBy and encryption.diskEncryptionSetId are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/disks' -IfPresent
                Field 'Microsoft.Compute/disks/managedBy' | Must -Exist
                Field 'Microsoft.Compute/disks/encryption.diskEncryptionSetId' | Must -BeIn (Param AllowedEncryptionSets)
            }
        }

        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 -BeIn (Param AllowedEncryptionSets)
            }
        }

        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 -BeIn (Param AllowedEncryptionSets)
            }
        }

        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 -BeIn (Param AllowedEncryptionSets) -Strict
                    }
                }
            }
        }

        Require 'type, publishingProfile.targetRegions and encryption.osDiskImage.diskEncryptionSetId are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/galleries/images/versions' -IfPresent
                AllOf {
                    Field 'Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions' | Must -Exist
                    Each 'Microsoft.Compute/galleries/images/versions/publishingProfile.targetRegions[*]' {
                        Field encryption.osDiskImage.diskEncryptionSetId | Must -BeIn (Param AllowedEncryptionSets) -Strict
                    }
                }
            }
        }

        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 {
                    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 -BeIn (Param AllowedEncryptionSets) -Strict
                        }
                    }
                }
            }
        }

        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 -BeIn (Param AllowedEncryptionSets)
            }
        }

        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
                Count 'Microsoft.Compute/images/storageProfile.dataDisks[*]' -Where {
                    Field diskEncryptionSet.id | Must -BeIn (Param AllowedEncryptionSets)
                } | Must -Not -Be 0
            }
        }
    }

    Effect (Param Effect)
}

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