Bylaw

Gallery › General › Azure Community Policy

Audit Resource Locks on Resource Groups based on Tags

Converted Policy AuditIfNotExists Disabled Mode All Version 1.0.0

Audits all Resource Groups that have a specific Tag, for the CanNotDelete Resource Lock.

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

Azure Policy JSONazurepolicy.json · 65 lines
{
  "name": "d55c656b-4caf-419d-adc1-aa5a2f99fb60",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Audit Resource Locks on Resource Groups based on Tags",
    "description": "Audits all Resource Groups that have a specific Tag, for the CanNotDelete Resource Lock.",
    "metadata": {
      "version": "1.0.0",
      "category": "General"
    },
    "mode": "All",
    "parameters": {
      "tagName": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Name",
          "description": "The Tag name to audit against (i.e. Environment, CostCenter, etc.)"
        }
      },
      "tagValue": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Value",
          "description": "Value of the tag to audit against (i.e. Prod/UAT/TEST, 12345, etc.)"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Resources/subscriptions/resourceGroups"
          },
          {
            "field": "[concat('tags[', parameters('tagName'), ']')]",
            "equals": "[parameters('tagValue')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Authorization/locks",
          "existenceCondition": {
            "field": "Microsoft.Authorization/locks/level",
            "equals": "CanNotDelete"
          }
        }
      }
    }
  }
}
Bylawd55c656b-4caf-419d-adc1-aa5a2f99fb60.bylaw.ps1 · 41 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'd55c656b-4caf-419d-adc1-aa5a2f99fb60' {

    Properties @{
        DisplayName = 'Audit Resource Locks on Resource Groups based on Tags'
        Description = 'Audits all Resource Groups that have a specific Tag, for the CanNotDelete Resource Lock.'
        Version     = '1.0.0'
        Category    = 'General'
        Mode        = 'All'
    }

    Parameter TagName @{
        Type        = 'String'
        DisplayName = 'Tag Name'
        Description = 'The Tag name to audit against (i.e. Environment, CostCenter, etc.)'
    }
    Parameter TagValue @{
        Type        = 'String'
        DisplayName = 'Tag Value'
        Description = 'Value of the tag to audit against (i.e. Prod/UAT/TEST, 12345, etc.)'
    }
    Parameter Effect @{
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Resources/subscriptions/resourceGroups' {

        Require 'tag (Param TagName) is not the TagValue parameter' {
            Tag (Param TagName) | Must -Not -Be (Param TagValue) -IfPresent
        }
    }

    AuditIfNotExists 'Microsoft.Authorization/locks' -Effect (Param Effect) {
        Require 'level is CanNotDelete' {
            Field level | Must -Be 'CanNotDelete'
        }
    }
}

Source: policyDefinitions/General/audit-resource-locks-on-resource-groups-based-on-tags/azurepolicy.json in Azure/Community-Policy, under the MIT licence.