Bylaw

Gallery › Network › Azure Landing Zones Library

Subnets should have a User Defined Route

Converted Policy Audit Deny Disabled Mode All Version 2.1.0

This policy denies the creation of a subnet without a User Defined Route (UDR).

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

Azure Policy JSONDeny-Subnet-Without-Udr.alz_policy_definition.json · 97 lines
{
  "name": "Deny-Subnet-Without-Udr",
  "properties": {
    "description": "This policy denies the creation of a subnet without a User Defined Route (UDR).",
    "displayName": "Subnets should have a User Defined Route",
    "metadata": {
      "alzCloudEnvironments": [
        "AzureCloud",
        "AzureChinaCloud",
        "AzureUSGovernment"
      ],
      "category": "Network",
      "source": "https://github.com/Azure/Enterprise-Scale/",
      "version": "2.1.0"
    },
    "mode": "All",
    "parameters": {
      "effect": {
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Deny",
        "metadata": {
          "description": "Enable or disable the execution of the policy",
          "displayName": "Effect"
        },
        "type": "String"
      },
      "excludedSubnets": {
        "defaultValue": [
          "AzureBastionSubnet",
          "RouteServerSubnet"
        ],
        "metadata": {
          "description": "Array of subnet names that are excluded from this policy",
          "displayName": "Excluded Subnets"
        },
        "type": "Array"
      }
    },
    "policyRule": {
      "if": {
        "anyOf": [
          {
            "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": "[parameters('excludedSubnets')]"
                      }
                    ]
                  }
                },
                "notEquals": 0
              }
            ]
          },
          {
            "allOf": [
              {
                "equals": "Microsoft.Network/virtualNetworks/subnets",
                "field": "type"
              },
              {
                "field": "name",
                "notIn": "[parameters('excludedSubnets')]"
              },
              {
                "exists": "false",
                "field": "Microsoft.Network/virtualNetworks/subnets/routeTable.id"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "policyType": "Custom"
  },
  "type": "Microsoft.Authorization/policyDefinitions"
}
BylawDeny-Subnet-Without-Udr.bylaw.ps1 · 48 lines
# Converted from Deny-Subnet-Without-Udr.alz_policy_definition.json by ConvertTo-Bylaw.
Policy 'Deny-Subnet-Without-Udr' {

    Properties @{
        DisplayName = 'Subnets should have a User Defined Route'
        Description = 'This policy denies the creation of a subnet without a User Defined Route (UDR).'
        Category    = 'Network'
        Version     = '2.1.0'
        Mode        = 'All'
        Metadata    = [ordered]@{ alzCloudEnvironments = @('AzureCloud', 'AzureChinaCloud', 'AzureUSGovernment'); source = 'https://github.com/Azure/Enterprise-Scale/' }
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Deny'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter ExcludedSubnets @{
        Type        = 'Array'
        Default     = @('AzureBastionSubnet', 'RouteServerSubnet')
        DisplayName = 'Excluded Subnets'
        Description = 'Array of subnet names that are excluded from this policy'
    }

    Resource * {

        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 (Param ExcludedSubnets)
                } | Must -Be 0
            }
        }

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

    Effect (Param Effect)
}

Source: policy_definitions/Deny-Subnet-Without-Udr.alz_policy_definition.json in Azure/Azure-Landing-Zones-Library, under the MIT licence.