Bylaw

Gallery › Network › Azure Community Policy

Address space must be pre-allocated for region

Converted Policy Audit Deny Disabled Mode Indexed Version 0.0.1-preview

This policy ensures that the address space allocated to a VNET has been pre-allocated for use within Azure, preventing peerings being utilised as an attack vector for null-routing traffic on the platform.

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

Azure Policy JSONazurepolicy.json · 126 lines
{
  "name": "839b1912-7f98-40f7-b83e-2153e2e4b0fc",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Address space must be pre-allocated for region",
    "description": "This policy ensures that the address space allocated to a VNET has been pre-allocated for use within Azure, preventing peerings being utilised as an attack vector for null-routing traffic on the platform.",
    "metadata": {
      "version": "0.0.1-preview",
      "category": "Network",
      "portalReview": "true"
    },
    "mode": "Indexed",
    "parameters": {
      "spokeAllocations": {
        "type": "Array",
        "metadata": {
          "displayName": "Regional CIDR Allocations",
          "description": "An array of CIDR objects (environment, location and CIDR), to be compared against CIDRs allocated to VNET resources."
        },
        "defaultValue": [
          {
            "environment": "DEV",
            "location": "westeurope",
            "CIDR": "10.2.0.0/16"
          },
          {
            "environment": "PROD",
            "location": "northeurope",
            "CIDR": "10.1.0.0/16"
          }
        ]
      },
      "hubSubscriptions": {
        "type": "Array",
        "metadata": {
          "displayName": "(Optional) Hub Subscription IDs",
          "description": "An array of subscriptionIDs to which VNETs are peered; will cause the policy to only evaluate VNETs associated directly with your hubs."
        },
        "defaultValue": []
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Network/virtualNetworks"
              },
              {
                "field": "Microsoft.Network/virtualNetworks/addressSpace",
                "exists": true
              },
              {
                "value": "[length(field('Microsoft.Network/virtualNetworks/addressSpace'))]",
                "greater": 0
              }
            ]
          },
          {
            "allOf": [
              {
                "value": "[length(parameters('hubSubscriptions'))]",
                "greater": 0
              },
              {
                "count": {
                  "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings[*]",
                  "where": {
                    "allOf": [
                      {
                        "value": "[ split( string( field('Microsoft.Network/virtualNetworks/virtualNetworkPeerings[*].remoteVirtualNetwork.id') ), '/' )[2] ]",
                        "in": "[parameters('hubSubscriptions')]"
                      }
                    ]
                  }
                },
                "greater": 0
              }
            ]
          },
          {
            "count": {
              "value": "[parameters('spokeAllocations')]",
              "name": "spokeAllocation",
              "where": {
                "allOf": [
                  {
                    "value": "[subscription().displayName]",
                    "contains": "[current('spokeAllocation').environment]"
                  },
                  {
                    "field": "location",
                    "equals": "[current('spokeAllocation').location]"
                  },
                  {
                    "value": "[ipRangeContains(current('spokeAllocation').CIDR, first(field('Microsoft.Network/virtualNetworks/addressSpace.addressPrefixes[*]')))]",
                    "equals": true
                  }
                ]
              }
            },
            "notequals": "[length(field('Microsoft.Network/virtualNetworks/addressSpace.addressPrefixes'))]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw839b1912-7f98-40f7-b83e-2153e2e4b0fc.bylaw.ps1 · 53 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '839b1912-7f98-40f7-b83e-2153e2e4b0fc' {

    Properties @{
        DisplayName = 'Address space must be pre-allocated for region'
        Description = 'This policy ensures that the address space allocated to a VNET has been pre-allocated for use within Azure, preventing peerings being utilised as an attack vector for null-routing traffic on the platform.'
        Version     = '0.0.1-preview'
        Category    = 'Network'
        Mode        = 'Indexed'
        Metadata    = [ordered]@{ portalReview = 'true' }
    }

    Parameter SpokeAllocations @{
        Type        = 'Array'
        Default     = @([ordered]@{ environment = 'DEV'; location = 'westeurope'; CIDR = '10.2.0.0/16' }, [ordered]@{ environment = 'PROD'; location = 'northeurope'; CIDR = '10.1.0.0/16' })
        DisplayName = 'Regional CIDR Allocations'
        Description = 'An array of CIDR objects (environment, location and CIDR), to be compared against CIDRs allocated to VNET resources.'
    }
    Parameter HubSubscriptions @{
        Type        = 'Array'
        Default     = @()
        DisplayName = '(Optional) Hub Subscription IDs'
        Description = 'An array of subscriptionIDs to which VNETs are peered; will cause the policy to only evaluate VNETs associated directly with your hubs.'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Network/virtualNetworks' {

        When {
            Count -Value (Param SpokeAllocations) -Name spokeAllocation -Where {
                Arm '[subscription().displayName]' | Must -ContainText (Arm '[current(''spokeAllocation'').environment]')
                Field location | Must -Be (Arm '[current(''spokeAllocation'').location]')
                Arm '[ipRangeContains(current(''spokeAllocation'').CIDR, first(field(''Microsoft.Network/virtualNetworks/addressSpace.addressPrefixes[*]'')))]' | Must -Be $true
            } | Must -Not -Be (Arm '[length(field(''Microsoft.Network/virtualNetworks/addressSpace.addressPrefixes''))]')
            Field addressSpace | Must -Exist
            Arm '[length(field(''Microsoft.Network/virtualNetworks/addressSpace''))]' | Must -BeGreaterThan 0
            Param HubSubscriptions -Length | Must -BeGreaterThan 0
        }

        Require 'virtualNetworkPeerings and virtualNetworkPeerings.remoteVirtualNetwork.id are as required' {
            Count 'virtualNetworkPeerings[*]' -Where {
                Arm '[ split( string( field(''Microsoft.Network/virtualNetworks/virtualNetworkPeerings[*].remoteVirtualNetwork.id'') ), ''/'' )[2] ]' | Must -BeIn (Param HubSubscriptions)
            } | Must -Be 0
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/address-space-must-be-pre-allocated-for-region/azurepolicy.json in Azure/Community-Policy, under the MIT licence.