Bylaw

Gallery › Compute › Azure Community Policy

VM use allowed Images

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This policy prevents unauthorized images for VMs.

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

Azure Policy JSONazurepolicy.json · 138 lines
{
  "name": "29750e03-2a09-4e55-9b40-a99920cb63b1",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "VM use allowed Images",
    "description": "This policy prevents unauthorized images for VMs.",
    "metadata": {
      "category": "Compute",
      "createdBy": "",
      "updatedBy": null,
      "updatedOn": null,
      "version": "1.0.0"
    },
    "mode": "All",
    "parameters": {
      "allowedImageOffers": {
        "type": "Array",
        "metadata": {
          "description": "Offers listed here are the only ones approved to be deployed. Use this command to get a list of Offers: Get-AzVMImageOffer -Location '' -PublisherName ''",
          "displayName": "Allowed Image Offers"
        },
        "defaultValue": []
      },
      "allowedImagePublishers": {
        "type": "Array",
        "metadata": {
          "description": "Publishers listed here are the only ones approved to be deployed. Use this command to get a list of Publishers: Get-AzVMImagePublisher -Location ''",
          "displayName": "Allowed Image Publishers"
        },
        "defaultValue": []
      },
      "allowedImageSkus": {
        "type": "Array",
        "metadata": {
          "description": "SKUs listed here are the only ones approved to be deployed. Use this command to get a list of SKUs: Get-AzVMImageSku -Location '' -PublisherName ''  -Offer ''",
          "displayName": "Allowed Image SKUs"
        },
        "defaultValue": []
      },
      "allowedImageVersions": {
        "type": "Array",
        "metadata": {
          "description": "Versions listed here are the only ones approved to be deployed. Use this command to get a list of Versions: Get-AzVMImage -Location '' -PublisherName '' -Offer '' -Skus ''",
          "displayName": "Allowed Image Versions"
        },
        "defaultValue": []
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "equals": "Microsoft.Compute/virtualMachines",
            "field": "type"
          },
          {
            "allOf": [
              {
                "exists": false,
                "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.id"
              },
              {
                "not": {
                  "allOf": [
                    {
                      "anyOf": [
                        {
                          "field": "Microsoft.Compute/virtualMachines/imagePublisher",
                          "in": "[parameters('allowedImagePublishers')]"
                        },
                        {
                          "equals": 0,
                          "value": "[length(parameters('allowedImagePublishers'))]"
                        }
                      ]
                    },
                    {
                      "anyOf": [
                        {
                          "field": "Microsoft.Compute/virtualMachines/imageOffer",
                          "in": "[parameters('allowedImageOffers')]"
                        },
                        {
                          "equals": 0,
                          "value": "[length(parameters('allowedImageOffers'))]"
                        }
                      ]
                    },
                    {
                      "anyOf": [
                        {
                          "field": "Microsoft.Compute/virtualMachines/imageSku",
                          "in": "[parameters('allowedImageSkus')]"
                        },
                        {
                          "equals": 0,
                          "value": "[length(parameters('allowedImageSkus'))]"
                        }
                      ]
                    },
                    {
                      "anyOf": [
                        {
                          "field": "Microsoft.Compute/virtualMachines/imageVersion",
                          "in": "[parameters('allowedImageVersions')]"
                        },
                        {
                          "equals": 0,
                          "value": "[length(parameters('allowedImageVersions'))]"
                        }
                      ]
                    }
                  ]
                }
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw29750e03-2a09-4e55-9b40-a99920cb63b1.bylaw.ps1 · 79 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '29750e03-2a09-4e55-9b40-a99920cb63b1' {

    Properties @{
        DisplayName = 'VM use allowed Images'
        Description = 'This policy prevents unauthorized images for VMs.'
        Category    = 'Compute'
        Version     = '1.0.0'
        Mode        = 'All'
    }

    Parameter AllowedImageOffers @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Allowed Image Offers'
        Description = 'Offers listed here are the only ones approved to be deployed. Use this command to get a list of Offers: Get-AzVMImageOffer -Location '''' -PublisherName '''''
    }
    Parameter AllowedImagePublishers @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Allowed Image Publishers'
        Description = 'Publishers listed here are the only ones approved to be deployed. Use this command to get a list of Publishers: Get-AzVMImagePublisher -Location '''''
    }
    Parameter AllowedImageSkus @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Allowed Image SKUs'
        Description = 'SKUs listed here are the only ones approved to be deployed. Use this command to get a list of SKUs: Get-AzVMImageSku -Location '''' -PublisherName ''''  -Offer '''''
    }
    Parameter AllowedImageVersions @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Allowed Image Versions'
        Description = 'Versions listed here are the only ones approved to be deployed. Use this command to get a list of Versions: Get-AzVMImage -Location '''' -PublisherName '''' -Offer '''' -Skus '''''
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Compute/virtualMachines' {

        When {
            Field storageProfile.imageReference.id | Must -Not -Exist
        }

        Require 'imagePublisher is as required' {
            AnyOf {
                Field imagePublisher | Must -BeIn (Param AllowedImagePublishers)
                Param AllowedImagePublishers -Length | Must -Be 0
            }
        }

        Require 'imageOffer is as required' {
            AnyOf {
                Field imageOffer | Must -BeIn (Param AllowedImageOffers)
                Param AllowedImageOffers -Length | Must -Be 0
            }
        }

        Require 'imageSku is as required' {
            AnyOf {
                Field imageSku | Must -BeIn (Param AllowedImageSkus)
                Param AllowedImageSkus -Length | Must -Be 0
            }
        }

        Require 'imageVersion is as required' {
            AnyOf {
                Field imageVersion | Must -BeIn (Param AllowedImageVersions)
                Param AllowedImageVersions -Length | Must -Be 0
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Compute/vm-use-allowed-images/azurepolicy.json in Azure/Community-Policy, under the MIT licence.