Bylaw

Gallery › Network › Azure Community Policy

Use approved vNet for VM network interfaces

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This policy enforces VM network interfaces to use a specific vNet.

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

Azure Policy JSONazurepolicy.json · 54 lines
{
  "name": "34f97ac3-0f0a-495a-b135-1934397eb183",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Use approved vNet for VM network interfaces",
    "description": "This policy enforces VM network interfaces to use a specific vNet.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "vNetId": {
        "type": "string",
        "metadata": {
          "description": "Resource Id for the vNet",
          "displayName": "vNet Id"
        }
      },
      "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": [
          {
            "field": "type",
            "equals": "Microsoft.Network/networkInterfaces"
          },
          {
            "not": {
              "field": "Microsoft.Network/networkInterfaces/ipconfigurations[*].subnet.id",
              "like": "[concat(parameters('vNetId'),'*')]"
            }
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw34f97ac3-0f0a-495a-b135-1934397eb183.bylaw.ps1 · 38 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '34f97ac3-0f0a-495a-b135-1934397eb183' {

    Properties @{
        DisplayName = 'Use approved vNet for VM network interfaces'
        Description = 'This policy enforces VM network interfaces to use a specific vNet.'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter VNetId @{
        Type        = 'string'
        DisplayName = 'vNet Id'
        Description = 'Resource Id for the vNet'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Network/networkInterfaces' {

        Require 'ipConfigurations exists' {
            Field ipConfigurations | Must -Exist
        }

        Require 'ipconfigurations and subnet.id are as required' {
            Each 'ipconfigurations[*]' {
                Field subnet.id | Must -BeLike (Arm '[concat(parameters(''vNetId''),''*'')]') -Strict
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/use-approved-vnet-for-vm-network-interfaces/azurepolicy.json in Azure/Community-Policy, under the MIT licence.