Bylaw

Gallery › Network › Azure built-in policies

Virtual machines should be connected to an approved virtual network

Converted Policy Audit Deny Disabled Mode Indexed Version 1.0.0

This policy audits any virtual machine connected to a virtual network that is not approved.

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

Azure Policy JSONApprovedVirtualNetwork_Audit.json · 59 lines
{
  "properties": {
    "displayName": "Virtual machines should be connected to an approved virtual network",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "This policy audits any virtual machine connected to a virtual network that is not approved.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "version": "1.0.0",
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "Audit",
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "The effect determines what happens when the policy rule is evaluated to match"
        }
      },
      "virtualNetworkId": {
        "type": "string",
        "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",
            "equals": "Microsoft.Network/networkInterfaces"
          },
          {
            "not": {
              "field": "Microsoft.Network/networkInterfaces/ipconfigurations[*].subnet.id",
              "like": "[concat(parameters('virtualNetworkId'),'/*')]"
            }
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/d416745a-506c-48b6-8ab1-83cb814bcaa3",
  "name": "d416745a-506c-48b6-8ab1-83cb814bcaa3"
}
Bylawd416745a-506c-48b6-8ab1-83cb814bcaa3.bylaw.ps1 · 40 lines
# Converted from ApprovedVirtualNetwork_Audit.json by ConvertTo-Bylaw.
Policy 'd416745a-506c-48b6-8ab1-83cb814bcaa3' {

    Properties @{
        DisplayName   = 'Virtual machines should be connected to an approved virtual network'
        Description   = 'This policy audits any virtual machine connected to a virtual network that is not approved.'
        Version       = '1.0.0'
        Category      = 'Network'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        Type          = 'string'
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'The effect determines what happens when the policy rule is evaluated to match'
    }
    Parameter VirtualNetworkId @{
        Type        = 'string'
        DisplayName = 'Virtual network Id'
        Description = 'Resource Id of the virtual network. Example: /subscriptions/YourSubscriptionId/resourceGroups/YourResourceGroupName/providers/Microsoft.Network/virtualNetworks/Name'
    }

    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(''virtualNetworkId''),''/*'')]') -Strict
            }
        }
    }

    Effect (Param Effect)
}

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