Bylaw

Gallery › Network › Azure Landing Zones Library

Deny vNet peering to non-approved vNets

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This policy denies the creation of vNet Peerings to non-approved vNets under the assigned scope.

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

Azure Policy JSONDeny-VNET-Peering-To-Non-Approved-VNETs.alz_policy_definition.json · 86 lines
{
  "name": "Deny-VNET-Peering-To-Non-Approved-VNETs",
  "properties": {
    "description": "This policy denies the creation of vNet Peerings to non-approved vNets under the assigned scope.",
    "displayName": "Deny vNet peering to non-approved vNets",
    "metadata": {
      "alzCloudEnvironments": [
        "AzureCloud",
        "AzureChinaCloud",
        "AzureUSGovernment"
      ],
      "category": "Network",
      "source": "https://github.com/Azure/Enterprise-Scale/",
      "version": "1.0.0"
    },
    "mode": "All",
    "parameters": {
      "allowedVnets": {
        "defaultValue": [],
        "metadata": {
          "description": "Array of allowed vNets that can be peered with. Must be entered using their resource ID. Example: /subscriptions/{subId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{vnetName}",
          "displayName": "Allowed vNets to peer with"
        },
        "type": "Array"
      },
      "effect": {
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Deny",
        "metadata": {
          "description": "Enable or disable the execution of the policy",
          "displayName": "Effect"
        },
        "type": "String"
      }
    },
    "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')]"
      }
    },
    "policyType": "Custom"
  },
  "type": "Microsoft.Authorization/policyDefinitions"
}
BylawDeny-VNET-Peering-To-Non-Approved-VNETs.bylaw.ps1 · 52 lines
# Converted from Deny-VNET-Peering-To-Non-Approved-VNETs.alz_policy_definition.json by ConvertTo-Bylaw.
Policy 'Deny-VNET-Peering-To-Non-Approved-VNETs' {

    Properties @{
        DisplayName = 'Deny vNet peering to non-approved vNets'
        Description = 'This policy denies the creation of vNet Peerings to non-approved vNets under the assigned scope.'
        Category    = 'Network'
        Version     = '1.0.0'
        Mode        = 'All'
        Metadata    = [ordered]@{ alzCloudEnvironments = @('AzureCloud', 'AzureChinaCloud', 'AzureUSGovernment'); source = 'https://github.com/Azure/Enterprise-Scale/' }
    }

    Parameter AllowedVnets @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Allowed vNets to peer with'
        Description = 'Array of allowed vNets that can be peered with. Must be entered using their resource ID. Example: /subscriptions/{subId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{vnetName}'
    }
    Parameter Effect @{
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Deny'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable 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: policy_definitions/Deny-VNET-Peering-To-Non-Approved-VNETs.alz_policy_definition.json in Azure/Azure-Landing-Zones-Library, under the MIT licence.