Bylaw

Gallery › Network › Azure Community Policy

Deny subnet delegation on subnets containing a key word

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This Policy will deny users from enabling subnet delegation on subnets containing a pre-defined key word.

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

Azure Policy JSONazurepolicy.json · 89 lines
{
  "name": "b7cc2562-35a5-4208-b20e-68adc73d4ada",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deny subnet delegation on subnets containing a key word",
    "description": "This Policy will deny users from enabling subnet delegation on subnets containing a pre-defined key word.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "subnetNamingConvention": {
        "type": "string",
        "metadata": {
          "displayName": "Subnets containing this word cannot enable subnet delegation"
        }
      },
      "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": [
              {
                "field": "type",
                "equals": "Microsoft.Network/virtualNetworks"
              },
              {
                "count": {
                  "field": "Microsoft.Network/virtualNetworks/subnets[*]",
                  "where": {
                    "allOf": [
                      {
                        "allOf": [
                          {
                            "value": "[empty(field('Microsoft.Network/virtualNetworks/subnets[*].delegations[*]'))]",
                            "equals": false
                          },
                          {
                            "field": "Microsoft.Network/virtualNetworks/subnets[*].name",
                            "contains": "[parameters('subnetNamingConvention')]"
                          }
                        ]
                      }
                    ]
                  }
                },
                "greater": 0
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Network/virtualNetworks/subnets"
              },
              {
                "value": "[empty(field('Microsoft.Network/virtualNetworks/subnets/delegations[*]'))]",
                "equals": false
              },
              {
                "field": "name",
                "contains": "[parameters('subnetNamingConvention')]"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylawb7cc2562-35a5-4208-b20e-68adc73d4ada.bylaw.ps1 · 45 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'b7cc2562-35a5-4208-b20e-68adc73d4ada' {

    Properties @{
        DisplayName = 'Deny subnet delegation on subnets containing a key word'
        Description = 'This Policy will deny users from enabling subnet delegation on subnets containing a pre-defined key word.'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter SubnetNamingConvention @{
        Type        = 'string'
        DisplayName = 'Subnets containing this word cannot enable subnet delegation'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource * {

        Require 'type, subnets, name and 1 more are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks' -IfPresent
                Count 'Microsoft.Network/virtualNetworks/subnets[*]' -Where {
                    Arm '[empty(field(''Microsoft.Network/virtualNetworks/subnets[*].delegations[*]''))]' | Must -Be $false
                    Field name | Must -ContainText (Param SubnetNamingConvention)
                } | Must -Be 0
            }
        }

        Require 'type, name and delegations are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks/subnets' -IfPresent
                Arm '[empty(field(''Microsoft.Network/virtualNetworks/subnets/delegations[*]''))]' | Must -Not -Be $false
                Field name | Must -Not -ContainText (Param SubnetNamingConvention) -IfPresent
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/deny-subnet-delegation-on-subnets-containing-a-key-word/azurepolicy.json in Azure/Community-Policy, under the MIT licence.