Bylaw

Gallery › Compute › Azure Community Policy

Allowed Disk states for Azure Disks

Converted Policy Audit Deny Disabled Mode Indexed Version 1.0.0

This policy audits or blocks certain disk states for Azure Disks. https://learn.microsoft.com/en-us/rest/api/compute/disks/get?tabs=HTTP#diskstate

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

Azure Policy JSONazurepolicy.json · 58 lines
{
  "name": "2e518796-a201-4c03-9c43-5f41ef36ee83",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Allowed Disk states for Azure Disks",
    "description": "This policy audits or blocks certain disk states for Azure Disks. https://learn.microsoft.com/en-us/rest/api/compute/disks/get?tabs=HTTP#diskstate",
    "metadata": {
      "version": "1.0.0",
      "category": "Compute"
    },
    "mode": "Indexed",
    "parameters": {
      "disallowedState": {
        "type": "array",
        "metaData": {
          "displayName": "Disallowed disk state",
          "description": "Controls what disk states are disallowed. Described at https://learn.microsoft.com/en-us/rest/api/compute/disks/get?tabs=HTTP#diskstate"
        },
        "allowedValues": [
          "ActiveSAS",
          "ActiveSASFrozen",
          "ActiveUpload",
          "Attached",
          "Frozen",
          "ReadyToUpload",
          "Reserved",
          "Unattached"
        ],
        "defaultValue": [
          "ActiveSAS",
          "ActiveSASFrozen"
        ]
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "field": "Microsoft.Compute/disks/diskState",
        "in": "[parameters('disallowedState')]"
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw2e518796-a201-4c03-9c43-5f41ef36ee83.bylaw.ps1 · 34 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '2e518796-a201-4c03-9c43-5f41ef36ee83' {

    Properties @{
        DisplayName = 'Allowed Disk states for Azure Disks'
        Description = 'This policy audits or blocks certain disk states for Azure Disks. https://learn.microsoft.com/en-us/rest/api/compute/disks/get?tabs=HTTP#diskstate'
        Version     = '1.0.0'
        Category    = 'Compute'
        Mode        = 'Indexed'
    }

    Parameter DisallowedState @{
        Type          = 'array'
        AllowedValues = @('ActiveSAS', 'ActiveSASFrozen', 'ActiveUpload', 'Attached', 'Frozen', 'ReadyToUpload', 'Reserved', 'Unattached')
        Default       = @('ActiveSAS', 'ActiveSASFrozen')
        DisplayName   = 'Disallowed disk state'
        Description   = 'Controls what disk states are disallowed. Described at https://learn.microsoft.com/en-us/rest/api/compute/disks/get?tabs=HTTP#diskstate'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource * {

        Require 'diskState is not one of the DisallowedState parameter' {
            Field 'Microsoft.Compute/disks/diskState' | Must -Not -BeIn (Param DisallowedState) -IfPresent
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Compute/allowed-disk-states-for-azure-disks/azurepolicy.json in Azure/Community-Policy, under the MIT licence.