Bylaw

Gallery › Mission › Azure built-in policies

Virtual Network should Specify allowed Vnet Peers

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This policy applies exclusively to Mission Platform products and services; usage outside of these scopes is not supported. Controls which virtual networks can be peered together to prevent unauthorized network connectivity and ensure network isolation between workloads.

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

Azure Policy JSONVNETAllowedPeers_Audit.json · 90 lines
{
  "properties": {
    "displayName": "Virtual Network should Specify allowed Vnet Peers",
    "policyType": "BuiltIn",
    "mode": "All",
    "description": "This policy applies exclusively to Mission Platform products and services; usage outside of these scopes is not supported. Controls which virtual networks can be peered together to prevent unauthorized network connectivity and ensure network isolation between workloads.",
    "metadata": {
      "version": "1.0.0",
      "category": "Mission"
    },
    "version": "1.0.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Virtual Network Specified the allowed peers for Virtual Networks Effect",
          "description": " Specified allowed virtual networks Effect"
        },
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Audit"
      },
      "allowedVnetPeers": {
        "type": "Array",
        "metadata": {
          "displayName": "Virtual network Id",
          "description": " Resource Id of the virtual network. Example: /subscriptions/YourSubscriptionId/resourceGroups/YourResourceGroupName/providers/Microsoft.Network/virtualNetworks/Name"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "in": [
              "Microsoft.Network/virtualNetworks",
              "Microsoft.Network/virtualNetworks/virtualNetworkPeerings"
            ]
          },
          {
            "anyOf": [
              {
                "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.Network/virtualNetworks"
                  },
                  {
                    "count": {
                      "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings[*]",
                      "where": {
                        "value": "[current('Microsoft.Network/virtualNetworks/virtualNetworkPeerings[*].remoteVirtualNetwork.id')]",
                        "notIn": "[parameters('allowedVnetPeers')]"
                      }
                    },
                    "greater": 0
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings"
                  },
                  {
                    "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings/remoteVirtualNetwork.id",
                    "notIn": "[parameters('allowedVnetPeers')]"
                  }
                ]
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/9de54b0f-202f-41c3-8c4f-c27a89bc3b57",
  "name": "9de54b0f-202f-41c3-8c4f-c27a89bc3b57"
}
Bylaw9de54b0f-202f-41c3-8c4f-c27a89bc3b57.bylaw.ps1 · 49 lines
# Converted from VNETAllowedPeers_Audit.json by ConvertTo-Bylaw.
Policy '9de54b0f-202f-41c3-8c4f-c27a89bc3b57' {

    Properties @{
        DisplayName   = 'Virtual Network should Specify allowed Vnet Peers'
        Description   = 'This policy applies exclusively to Mission Platform products and services; usage outside of these scopes is not supported. Controls which virtual networks can be peered together to prevent unauthorized network connectivity and ensure network isolation between workloads.'
        Version       = '1.0.0'
        Category      = 'Mission'
        PolicyVersion = '1.0.0'
        Mode          = 'All'
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Virtual Network Specified the allowed peers for Virtual Networks Effect'
        Description   = ' Specified allowed virtual networks Effect'
    }
    Parameter AllowedVnetPeers @{
        Type        = 'Array'
        DisplayName = 'Virtual network Id'
        Description = ' Resource Id of the virtual network. Example: /subscriptions/YourSubscriptionId/resourceGroups/YourResourceGroupName/providers/Microsoft.Network/virtualNetworks/Name'
    }

    Resource * {

        When {
            Field type | Must -BeIn @('Microsoft.Network/virtualNetworks', 'Microsoft.Network/virtualNetworks/virtualNetworkPeerings')
        }

        Require 'type and virtualNetworkPeerings are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks' -IfPresent
                Count 'Microsoft.Network/virtualNetworks/virtualNetworkPeerings[*]' -Where {
                    Arm '[current(''Microsoft.Network/virtualNetworks/virtualNetworkPeerings[*].remoteVirtualNetwork.id'')]' | Must -Not -BeIn (Param AllowedVnetPeers)
                } | Must -Be 0
            }
        }

        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 AllowedVnetPeers)
            }
        }
    }

    Effect (Param Effect)
}

Source: built-in-policies/policyDefinitions/Mission/VNETAllowedPeers_Audit.json in Azure/azure-policy, under the MIT licence.