Bylaw

Gallery › Network › Azure Community Policy

Deny Private Endpoints in specific subnets based on a naming convention

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This Policy will deny the creation of Private Endpoints within subnets that contain a key word. This is useful when your subnets all follow a naming schema.

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

Azure Policy JSONazurepolicy.json · 52 lines
{
  "name": "24d34d23-dd31-4300-8d5c-92bd3bda6136",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deny Private Endpoints in specific subnets based on a naming convention",
    "description": "This Policy will deny the creation of Private Endpoints within subnets that contain a key word. This is useful when your subnets all follow a naming schema.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "subnetNamingConvention": {
        "type": "String",
        "metadata": {
          "displayName": "Not Allowed Subnet",
          "description": "This does not work on exact matches, but checks to see if the subnet contains the word you specify here."
        }
      },
      "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/privateEndpoints"
          },
          {
            "value": "[last(split(field('Microsoft.Network/privateEndpoints/subnet.id'), '/'))]",
            "contains": "[parameters('subnetNamingConvention')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw24d34d23-dd31-4300-8d5c-92bd3bda6136.bylaw.ps1 · 32 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '24d34d23-dd31-4300-8d5c-92bd3bda6136' {

    Properties @{
        DisplayName = 'Deny Private Endpoints in specific subnets based on a naming convention'
        Description = 'This Policy will deny the creation of Private Endpoints within subnets that contain a key word. This is useful when your subnets all follow a naming schema.'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter SubnetNamingConvention @{
        Type        = 'String'
        DisplayName = 'Not Allowed Subnet'
        Description = 'This does not work on exact matches, but checks to see if the subnet contains the word you specify here.'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Network/privateEndpoints' {

        Require 'subnet.id is as required' {
            Arm '[last(split(field(''Microsoft.Network/privateEndpoints/subnet.id''), ''/''))]' | Must -Not -ContainText (Param SubnetNamingConvention)
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/deny-private-endpoints-in-specific-subnets-based-on-a-naming-convention/azurepolicy.json in Azure/Community-Policy, under the MIT licence.