0

I have attempted this using address prefix ranges, as we are aware of the address spaces used within the 'corp' VNETs. However, we do not have information on the ranges for the 'online' VNETs. Therefore, we can only check the ranges for the 'corp' VNETs in the condition

"parameters": {
  "corpVNetAddressPrefixes": {
    "type": "Array",
    "metadata": {
      "displayName": "Corp VNet Address Prefixes",
      "description": "The list of Corp Subscription VNets address prefixes."
    },
    "defaultValue": [
      "17.0.0.0/24"
    ]
  }
},
"policyRule": {
  "if": {
    "allOf": [
      {
        "field": "type",
        "equals": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings"
      },
      {
        "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings/remoteVirtualNetwork.id",
        "notcontains": "[subscription().id]"
      },
      {
        "anyOf": [
          {
            "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings/remoteVirtualNetworkAddressSpace.addressPrefixes[*]",
            "in": "[parameters('corpVNetAddressPrefixes')]"
          },
          {
            "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings/remoteAddressSpace.addressPrefixes[*]",
            "in": "[parameters('corpVNetAddressPrefixes')]"
          },
          {
            "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings/localVirtualNetworkAddressSpace.addressPrefixes[*]",
            "in": "[parameters('corpVNetAddressPrefixes')]"
          },
          {
            "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings/localAddressSpace.addressPrefixes[*]",
            "in": "[parameters('corpVNetAddressPrefixes')]"
          }
        ]
      }
    ]
  },
  "then": {
    "effect": "deny"
  }

however this solution working for one of the peering resource, but not for other (whenever we create a peering it creates two vnet-peering resources one in local VNet and other in selected RemoteVNet)

The issue here is the alias localVirtualNetworkAddressSpace.addressPrefixes[*] & virtualNetworkPeerings/localAddressSpace.addressPrefixes[*] is not working here in the policy rule and even not showing in resource json. due to which { "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings/localVirtualNetworkAddressSpace.addressPrefixes[*]", "in": "[parameters('corpVNetAddressPrefixes')]" } this condition is not working as expected

I have also tried with subscription tags but unfortunately we do not have remoteVirtualNtwork.tags alias available(can be requested raising a support ticket)

1
  • You need to ensure both VNet peerings are evaluated for address prefixes in both directions, modifying your policy conditions accordingly @aslambagawan Commented Dec 5, 2024 at 12:31

1 Answer 1

0

Challenges in creating custom policy rule to 'Deny VNet peering between subscriptions of "corp" & "online" management groups

To achive the configuration by defining an Azure Policy that stops virtual network peering between a corporate VNet and an online VNet unless the address prefixes match those allowed within a parameterized list.

In this the issue is when you establish VNet peering in Azure, it creates two peerings oneway in the source VNet and one in the destination VNet. These are different resources and also make a note to that the policy should account for both directions of the peering relationship.

Updated policy:

{
  "properties": {
    "displayName": "Deny unauthorized virtual network peerings",
    "description": "Denies the creation of virtual network peerings if they do not match the specified corporate address ranges.",
    "mode": "All",
    "parameters": {
      "corpVNetAddressPrefixes": {
        "type": "Array",
        "metadata": {
          "displayName": "Corp VNet Address Prefixes",
          "description": "The list of Corp Subscription VNets address prefixes."
        },
        "defaultValue": [
          "17.0.0.0/24"
        ]
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings"
          },
          {
            "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings/remoteVirtualNetwork.id",
            "notcontains": "[subscription().id]"
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings/remoteVirtualNetworkAddressSpace.addressPrefixes[*]",
                "in": "[parameters('corpVNetAddressPrefixes')]"
              },
              {
                "field": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings/localVirtualNetworkAddressSpace.addressPrefixes[*]",
                "in": "[parameters('corpVNetAddressPrefixes')]"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "deny"
      }
    }
  }
}

Demo configuration:

Here for the convience I have set the configuration using the same Subscription as dont have the privilage for mulitiple

resource "azurerm_virtual_network" "corp_vnet" {
  name                = "corp-vnet"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  address_space       = [var.corp_vnet_address_prefix]
}

resource "azurerm_virtual_network" "online_vnet" {
  name                = "online-vnet"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  address_space       = ["10.0.0.0/16"]
}

resource "azurerm_virtual_network_peering" "corp_to_online" {
  name                      = "corp-to-online-peering"
  resource_group_name       = azurerm_resource_group.example.name
  virtual_network_name      = azurerm_virtual_network.corp_vnet.name
  remote_virtual_network_id = azurerm_virtual_network.online_vnet.id
  allow_virtual_network_access = true
}

resource "azurerm_virtual_network_peering" "online_to_corp" {
  name                      = "online-to-corp-peering"
  resource_group_name       = azurerm_resource_group.example.name
  virtual_network_name      = azurerm_virtual_network.online_vnet.name
  remote_virtual_network_id = azurerm_virtual_network.corp_vnet.id
  allow_virtual_network_access = true
}

Deployment:

Refer:

https://learn.microsoft.com/en-us/azure/virtual-network/virtual-network-peering-overview

https://learn.microsoft.com/en-us/azure/governance/policy

Tutorial: Create a custom policy definition - Azure Policy | Microsoft Learn

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.