Bylaw

Gallery › Network › Azure Community Policy

VNet Peering is only allowed to approved VNets

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This policy denied you from peering to a VNet that's not on the list of approved VNets.

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

Azure Policy JSONazurepolicy.json · 82 lines
{
  "name": "ffa5aa5d-a9da-4439-b01e-0b9f3df73ff7",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "VNet Peering is only allowed to approved VNets",
    "description": "This policy denied you from peering to a VNet that's not on the list of approved VNets.",
    "metadata": {
      "category": "Network",
      "createdBy": "",
      "updatedBy": "",
      "version": "1.0.0"
    },
    "mode": "All",
    "parameters": {
      "allowedVnets": {
        "type": "array",
        "metadata": {
          "description": "Enter list of allowed VNets using a semi-coling ';' seperated list. VNets must be entered using their resource ID. Example: /subscriptions/{SubID}/resourceGroups/resourceGroupName/providers/Microsoft.Network/virtualNetworks/resourceGroupName/virtualNetworkPeerings/vnetName",
          "displayName": "Allowed VNets",
          "strongType": "Microsoft.Network/virtualNetworks"
        },
        "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": {
        "anyOf": [
          {
            "allOf": [
              {
                "equals": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
                "field": "type"
              },
              {
                "not": {
                  "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings/remoteVirtualNetwork.id",
                  "in": "[parameters('allowedVnets')]"
                }
              }
            ]
          },
          {
            "allOf": [
              {
                "equals": "Microsoft.Network/virtualNetworks",
                "field": "type"
              },
              {
                "not": {
                  "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings[*].remoteVirtualNetwork.id",
                  "in": "[parameters('allowedVnets')]"
                }
              },
              {
                "not": {
                  "exists": false,
                  "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings[*].remoteVirtualNetwork.id"
                }
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylawffa5aa5d-a9da-4439-b01e-0b9f3df73ff7.bylaw.ps1 · 52 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'ffa5aa5d-a9da-4439-b01e-0b9f3df73ff7' {

    Properties @{
        DisplayName = 'VNet Peering is only allowed to approved VNets'
        Description = 'This policy denied you from peering to a VNet that''s not on the list of approved VNets.'
        Category    = 'Network'
        Version     = '1.0.0'
        Mode        = 'All'
    }

    Parameter AllowedVnets @{
        Type        = 'array'
        Default     = @()
        DisplayName = 'Allowed VNets'
        Description = 'Enter list of allowed VNets using a semi-coling '';'' seperated list. VNets must be entered using their resource ID. Example: /subscriptions/{SubID}/resourceGroups/resourceGroupName/providers/Microsoft.Network/virtualNetworks/resourceGroupName/virtualNetworkPeerings/vnetName'
        Metadata    = [ordered]@{ strongType = 'Microsoft.Network/virtualNetworks' }
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource * {

        Require 'type and remoteVirtualNetwork.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks/virtualNetworkPeerings' -IfPresent
                Field 'Microsoft.Network/virtualNetworks/virtualNetworkPeerings/remoteVirtualNetwork.id' | Must -BeIn (Param AllowedVnets)
            }
        }

        Require 'type, virtualNetworkPeerings and remoteVirtualNetwork.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks' -IfPresent
                AllOf {
                    Field 'Microsoft.Network/virtualNetworks/virtualNetworkPeerings' | Must -Exist
                    Each 'Microsoft.Network/virtualNetworks/virtualNetworkPeerings[*]' {
                        Field remoteVirtualNetwork.id | Must -BeIn (Param AllowedVnets) -Strict
                    }
                }
                Each 'Microsoft.Network/virtualNetworks/virtualNetworkPeerings[*]' {
                    Field remoteVirtualNetwork.id | Must -Not -Exist
                }
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/vnet-peering-is-only-allowed-to-approved-vnets/azurepolicy.json in Azure/Community-Policy, under the MIT licence.