Bylaw

Gallery › Network › Azure Community Policy

deny-vm-vmss-and-load-balancer-from-subnet

Uses Condition Policy Audit Deny Disabled Mode All Version 1.0.0

need to add description

ConvertTo-Bylaw converted this policy, and proved it compiles back to the same definition. Conditions the language can't express yet are written as raw JSON with Condition.

Azure Policy JSONazurepolicy.json · 86 lines
{
  "name": "bbb6be92-8628-4d86-a2b7-744b6d94ee51",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "deny-vm-vmss-and-load-balancer-from-subnet",
    "description": "need to add description",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "deniedSubnetName": {
        "type": "String",
        "metadata": {
          "displayName": "Name of subnet where VM, VMSS, and LB are denied.",
          "description": "The creation of VM, VMSS, and LB in the defined subnet will be denied"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "anyOf": [
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/VirtualMachineScaleSets"
              },
              {
                "field": "Microsoft.Compute/VirtualMachineScaleSets/networkInterfaceConfigurations[*].ipConfigurations[*].subnet.id",
                "Contains": "[parameters('deniedSubnetName')]"
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Network/networkInterfaces"
              },
              {
                "field": "Microsoft.Network/networkInterfaces/ipconfigurations[*].subnet.id",
                "Contains": "[parameters('deniedSubnetName')]"
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Network/loadBalancers"
              },
              {
                "count": {
                  "field": "Microsoft.Network/loadBalancers/frontendIPConfigurations[*]",
                  "where": {
                    "field": "Microsoft.Network/loadBalancers/frontendIPConfigurations[*].subnet.id",
                    "like": "[concat('*',parameters('deniedSubnetName'))]"
                  }
                },
                "greater": 0
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylawbbb6be92-8628-4d86-a2b7-744b6d94ee51.bylaw.ps1 · 56 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'bbb6be92-8628-4d86-a2b7-744b6d94ee51' {

    Properties @{
        DisplayName = 'deny-vm-vmss-and-load-balancer-from-subnet'
        Description = 'need to add description'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter DeniedSubnetName @{
        Type        = 'String'
        DisplayName = 'Name of subnet where VM, VMSS, and LB are denied.'
        Description = 'The creation of VM, VMSS, and LB in the defined subnet will be denied'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource * {

        Require 'type and networkInterfaceConfigurations.ipConfigurations.subnet.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/VirtualMachineScaleSets' -IfPresent
                Condition -Violation '{ "field": "Microsoft.Compute/VirtualMachineScaleSets/networkInterfaceConfigurations[*].ipConfigurations[*].subnet.id", "Contains": "[parameters(''deniedSubnetName'')]" }'
            }
        }

        Require 'type, ipconfigurations and subnet.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/networkInterfaces' -IfPresent
                AnyOf {
                    Field 'Microsoft.Network/networkInterfaces/ipconfigurations' | Must -Not -Exist
                    Count 'Microsoft.Network/networkInterfaces/ipconfigurations[*]' -Where {
                        Field subnet.id | Must -Not -ContainText (Param DeniedSubnetName)
                    } | Must -Not -Be 0
                }
            }
        }

        Require 'type, frontendIPConfigurations and subnet.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/loadBalancers' -IfPresent
                Each 'Microsoft.Network/loadBalancers/frontendIPConfigurations[*]' {
                    Field subnet.id | Must -Not -BeLike (Arm '[concat(''*'',parameters(''deniedSubnetName''))]')
                }
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/deny-vm-vmss-and-load-balancer-from-subnet/azurepolicy.json in Azure/Community-Policy, under the MIT licence.