Bylaw

Gallery › Compute › Azure Community Policy

Only approved VMSS extensions should be installed

Converted Policy Audit Deny Disabled Mode Indexed Version 1.0.0

This policy governs the virtual machine scale set extensions that are not approved.

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

Azure Policy JSONazurepolicy.json · 71 lines
{
  "name": "017def49-4d59-46c7-9740-5b5a5127e0a7",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Only approved VMSS extensions should be installed",
    "description": "This policy governs the virtual machine scale set extensions that are not approved.",
    "metadata": {
      "category": "Compute",
      "createdBy": "",
      "updatedBy": null,
      "updatedOn": null,
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "approvedExtensions": {
        "type": "Array",
        "metadata": {
          "description": "The list of approved extension types that can be installed. Example: AzureDiskEncryption",
          "displayName": "Approved extensions"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "anyOf": [
          {
            "allOf": [
              {
                "equals": "Microsoft.Compute/virtualMachineScaleSets/extensions",
                "field": "type"
              },
              {
                "field": "Microsoft.Compute/virtualMachineScaleSets/extensions/type",
                "notIn": "[parameters('approvedExtensions')]"
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.extensionProfile.extensions[*].type",
                "notIn": "[parameters('approvedExtensions')]"
              },
              {
                "exists": true,
                "field": "Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.extensionProfile.extensions[*].type"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw017def49-4d59-46c7-9740-5b5a5127e0a7.bylaw.ps1 · 49 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '017def49-4d59-46c7-9740-5b5a5127e0a7' {

    Properties @{
        DisplayName = 'Only approved VMSS extensions should be installed'
        Description = 'This policy governs the virtual machine scale set extensions that are not approved.'
        Category    = 'Compute'
        Version     = '1.0.0'
        Mode        = 'Indexed'
    }

    Parameter ApprovedExtensions @{
        Type        = 'Array'
        DisplayName = 'Approved extensions'
        Description = 'The list of approved extension types that can be installed. Example: AzureDiskEncryption'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource * {

        Require 'type is as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/virtualMachineScaleSets/extensions' -IfPresent
                Field 'Microsoft.Compute/virtualMachineScaleSets/extensions/type' | Must -BeIn (Param ApprovedExtensions)
            }
        }

        Require 'virtualMachineProfile.extensionProfile.extensions and type are as required' {
            AnyOf {
                Count 'Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.extensionProfile.extensions[*]' -Where {
                    Field type | Must -BeIn (Param ApprovedExtensions)
                } | Must -Not -Be 0
                AnyOf {
                    Field 'Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.extensionProfile.extensions' | Must -Not -Exist
                    Count 'Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.extensionProfile.extensions[*]' -Where {
                        Field type | Must -Not -Exist
                    } | Must -Not -Be 0
                }
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Compute/only-approved-vmss-extensions-should-be-installed/azurepolicy.json in Azure/Community-Policy, under the MIT licence.