Bylaw

Gallery › Network › Azure Community Policy

Prevent subnets without Route Table

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This policy prevents subnets without a UDR attached to them.

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

Azure Policy JSONazurepolicy.json · 86 lines
{
  "name": "43177425-57f6-442d-ab5b-6fc57f6f4ab3",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Prevent subnets without Route Table",
    "description": "This policy prevents subnets without a UDR attached to them.",
    "metadata": {
      "category": "Network",
      "createdBy": "",
      "updatedBy": null,
      "updatedOn": null,
      "version": "1.0.0"
    },
    "mode": "All",
    "parameters": {
      "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": [
              {
                "equals": "Microsoft.Network/virtualNetworks/subnets",
                "field": "type"
              },
              {
                "exists": "false",
                "field": "Microsoft.Network/virtualNetworks/subnets/routeTable.id"
              },
              {
                "field": "name",
                "notIn": [
                  "AzureFirewallManagementSubnet"
                ]
              }
            ]
          },
          {
            "allOf": [
              {
                "equals": "Microsoft.Network/virtualNetworks",
                "field": "type"
              },
              {
                "count": {
                  "field": "Microsoft.Network/virtualNetworks/subnets[*]",
                  "where": {
                    "allOf": [
                      {
                        "exists": "false",
                        "field": "Microsoft.Network/virtualNetworks/subnets[*].routeTable.id"
                      },
                      {
                        "field": "Microsoft.Network/virtualNetworks/subnets[*].name",
                        "notIn": [
                          "AzureFirewallManagementSubnet"
                        ]
                      }
                    ]
                  }
                },
                "notEquals": 0
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw43177425-57f6-442d-ab5b-6fc57f6f4ab3.bylaw.ps1 · 41 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '43177425-57f6-442d-ab5b-6fc57f6f4ab3' {

    Properties @{
        DisplayName = 'Prevent subnets without Route Table'
        Description = 'This policy prevents subnets without a UDR attached to them.'
        Category    = 'Network'
        Version     = '1.0.0'
        Mode        = 'All'
    }

    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource * {

        Require 'type, routeTable.id and name are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks/subnets' -IfPresent
                Field 'Microsoft.Network/virtualNetworks/subnets/routeTable.id' | Must -Exist
                Field name | Must -BeIn @('AzureFirewallManagementSubnet')
            }
        }

        Require 'type, subnets, routeTable.id and 1 more are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks' -IfPresent
                Count 'Microsoft.Network/virtualNetworks/subnets[*]' -Where {
                    Field routeTable.id | Must -Not -Exist
                    Field name | Must -Not -BeIn @('AzureFirewallManagementSubnet')
                } | Must -Be 0
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/prevent-subnets-without-route-table/azurepolicy.json in Azure/Community-Policy, under the MIT licence.