Bylaw

Gallery › Authorization › Azure Community Policy

Audit Specified Resource Type For Any Lock

Converted Policy AuditIfNotExists Disabled Mode All Version 1.0.0

This policy audits the specified resource type for any lock such as 'CanNotDelete' or 'ReadOnly'.

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

Azure Policy JSONazurepolicy.json · 73 lines
{
  "name": "90ed0d51-2642-442f-b50c-f9a1b287a09c",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Audit Specified Resource Type For Any Lock",
    "description": "This policy audits the specified resource type for any lock such as 'CanNotDelete' or 'ReadOnly'.",
    "metadata": {
      "version": "1.0.0",
      "category": "Authorization"
    },
    "mode": "All",
    "parameters": {
      "resourceTypes": {
        "type": "Array",
        "metadata": {
          "description": "Azure resource types to audit for any lock",
          "displayName": "resourceTypes"
        },
        "defaultValue": [
          "microsoft.network/expressroutecircuits",
          "microsoft.network/expressroutegateways",
          "microsoft.network/virtualnetworks",
          "microsoft.network/virtualnetworkgateways",
          "microsoft.network/vpngateways",
          "microsoft.network/p2svpngateways"
        ]
      },
      "lockLevel": {
        "type": "Array",
        "metadata": {
          "description": "The lock level to audit for",
          "displayName": "lockLevel"
        },
        "allowedValues": [
          "ReadOnly",
          "CanNotDelete"
        ],
        "defaultValue": [
          "ReadOnly",
          "CanNotDelete"
        ]
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "in": "[parameters('resourceTypes')]"
      },
      "then": {
        "details": {
          "existenceCondition": {
            "field": "Microsoft.Authorization/locks/level",
            "in": "[parameters('lockLevel')]"
          },
          "type": "Microsoft.Authorization/locks"
        },
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw90ed0d51-2642-442f-b50c-f9a1b287a09c.bylaw.ps1 · 44 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '90ed0d51-2642-442f-b50c-f9a1b287a09c' {

    Properties @{
        DisplayName = 'Audit Specified Resource Type For Any Lock'
        Description = 'This policy audits the specified resource type for any lock such as ''CanNotDelete'' or ''ReadOnly''.'
        Version     = '1.0.0'
        Category    = 'Authorization'
        Mode        = 'All'
    }

    Parameter ResourceTypes @{
        Type        = 'Array'
        Default     = @('microsoft.network/expressroutecircuits', 'microsoft.network/expressroutegateways', 'microsoft.network/virtualnetworks', 'microsoft.network/virtualnetworkgateways', 'microsoft.network/vpngateways', 'microsoft.network/p2svpngateways')
        DisplayName = 'resourceTypes'
        Description = 'Azure resource types to audit for any lock'
    }
    Parameter LockLevel @{
        Type          = 'Array'
        AllowedValues = @('ReadOnly', 'CanNotDelete')
        Default       = @('ReadOnly', 'CanNotDelete')
        DisplayName   = 'lockLevel'
        Description   = 'The lock level to audit for'
    }
    Parameter Effect @{
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource * {

        Require 'type is not one of the ResourceTypes parameter' {
            Field type | Must -Not -BeIn (Param ResourceTypes) -IfPresent
        }
    }

    AuditIfNotExists 'Microsoft.Authorization/locks' -Effect (Param Effect) {
        Require 'level is one of the LockLevel parameter' {
            Field level | Must -BeIn (Param LockLevel)
        }
    }
}

Source: policyDefinitions/Authorization/audit-specified-resource-type-for-any-lock/azurepolicy.json in Azure/Community-Policy, under the MIT licence.