Bylaw

Gallery › Network › Azure Community Policy

Deny Private Endpoints if not being deployed to a specific subnet

Uses Condition Policy Audit Deny Disabled Mode All Version 1.0.0

This Policy will deny the creation of Private Endpoints if not within subnets that contain a key word.

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 · 69 lines
{
  "name": "58300131-ba3b-4077-aafd-ea1c5499e9c2",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deny Private Endpoints if not being deployed to a specific subnet",
    "description": "This Policy will deny the creation of Private Endpoints if not within subnets that contain a key word.",
    "metadata": {
      "category": "Network",
      "version": "1.0.0"
    },
    "mode": "All",
    "parameters": {
      "subnetName": {
        "type": "string",
        "metadata": {
          "displayName": "Allowed Subnet prefix name (i.e. pls)",
          "description": "Name of subnet where Private Endpoints are allowed to be deployed into."
        }
      },
      "exemptedGroupIds": {
        "type": "array",
        "metadata": {
          "displayName": "Exempted Private Endpoint Group IDs",
          "description": "The Group IDs that are exempted from this Policy (i.e. blob)"
        }
      },
      "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"
          },
          {
            "field": "Microsoft.Network/privateEndpoints/subnet.id",
            "notContains": "[parameters('subnetName')]"
          },
          {
            "count": {
              "field": "Microsoft.Network/privateEndpoints/privateLinkServiceConnections[*].groupIds[*]",
              "where": {
                "field": "Microsoft.Network/privateEndpoints/privateLinkServiceConnections[*].groupIds[*]",
                "notIn": "[parameters('exemptedGroupIds')]"
              }
            },
            "greaterOrEquals": 1
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw58300131-ba3b-4077-aafd-ea1c5499e9c2.bylaw.ps1 · 41 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '58300131-ba3b-4077-aafd-ea1c5499e9c2' {

    Properties @{
        DisplayName = 'Deny Private Endpoints if not being deployed to a specific subnet'
        Description = 'This Policy will deny the creation of Private Endpoints if not within subnets that contain a key word.'
        Category    = 'Network'
        Version     = '1.0.0'
        Mode        = 'All'
    }

    Parameter SubnetName @{
        Type        = 'string'
        DisplayName = 'Allowed Subnet prefix name (i.e. pls)'
        Description = 'Name of subnet where Private Endpoints are allowed to be deployed into.'
    }
    Parameter ExemptedGroupIds @{
        Type        = 'array'
        DisplayName = 'Exempted Private Endpoint Group IDs'
        Description = 'The Group IDs that are exempted from this Policy (i.e. blob)'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Network/privateEndpoints' {

        When {
            Field subnet.id | Must -Not -ContainText (Param SubnetName)
        }

        Require 'privateLinkServiceConnections.groupIds is as required' {
            Condition -Violation '{ "count": { "field": "Microsoft.Network/privateEndpoints/privateLinkServiceConnections[*].groupIds[*]", "where": { "field": "Microsoft.Network/privateEndpoints/privateLinkServiceConnections[*].groupIds[*]", "notIn": "[parameters(''exemptedGroupIds'')]" } }, "greaterOrEquals": 1 }'
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/deny-private-endpoints-if-not-being-deployed-to-a-specific-subnet/azurepolicy.json in Azure/Community-Policy, under the MIT licence.