Bylaw

Gallery › Network › Azure Landing Zones Library

Subnets without Private Endpoint Network Policies enabled should be denied

Converted Policy Audit Deny Disabled Mode All Version 1.2.0

This policy denies the creation of a subnet without Private Endpoint Netwotk Policies enabled. This policy is intended for 'workload' subnets, not 'central infrastructure' (aka, 'hub') subnets.

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

Azure Policy JSONDeny-Subnet-Without-Penp.alz_policy_definition.json · 114 lines
{
  "name": "Deny-Subnet-Without-Penp",
  "properties": {
    "description": "This policy denies the creation of a subnet without Private Endpoint Netwotk Policies enabled. This policy is intended for 'workload' subnets, not 'central infrastructure' (aka, 'hub') subnets.",
    "displayName": "Subnets without Private Endpoint Network Policies enabled should be denied",
    "metadata": {
      "alzCloudEnvironments": [
        "AzureCloud",
        "AzureChinaCloud",
        "AzureUSGovernment"
      ],
      "category": "Network",
      "source": "https://github.com/Azure/Enterprise-Scale/",
      "version": "1.2.0"
    },
    "mode": "All",
    "parameters": {
      "effect": {
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Deny",
        "metadata": {
          "description": "The effect determines what happens when the policy rule is evaluated to match",
          "displayName": "Effect"
        },
        "type": "String"
      },
      "excludedSubnets": {
        "defaultValue": [
          "GatewaySubnet",
          "AzureFirewallSubnet",
          "AzureFirewallManagementSubnet",
          "AzureBastionSubnet",
          "RouteServerSubnet"
        ],
        "metadata": {
          "description": "Array of subnet names that are excluded from this policy",
          "displayName": "Excluded Subnets"
        },
        "type": "Array"
      },
      "penpValue": {
        "allowedValues": [
          "Disabled",
          "NetworkSecurityGroupEnabled",
          "RouteTableEnabled",
          "Enabled"
        ],
        "defaultValue": "Enabled",
        "metadata": {
          "description": "The privateEndpointNetworkPolicies value required for evaluated subnets",
          "displayName": "Private Endpoint Network Policies Value"
        },
        "type": "String"
      }
    },
    "policyRule": {
      "if": {
        "anyOf": [
          {
            "allOf": [
              {
                "equals": "Microsoft.Network/virtualNetworks",
                "field": "type"
              },
              {
                "count": {
                  "field": "Microsoft.Network/virtualNetworks/subnets[*]",
                  "where": {
                    "allOf": [
                      {
                        "field": "Microsoft.Network/virtualNetworks/subnets[*].privateEndpointNetworkPolicies",
                        "notEquals": "[parameters('penpValue')]"
                      },
                      {
                        "field": "Microsoft.Network/virtualNetworks/subnets[*].name",
                        "notIn": "[parameters('excludedSubnets')]"
                      }
                    ]
                  }
                },
                "notEquals": 0
              }
            ]
          },
          {
            "allOf": [
              {
                "equals": "Microsoft.Network/virtualNetworks/subnets",
                "field": "type"
              },
              {
                "field": "name",
                "notIn": "[parameters('excludedSubnets')]"
              },
              {
                "field": "Microsoft.Network/virtualNetworks/subnets/privateEndpointNetworkPolicies",
                "notEquals": "[parameters('penpValue')]"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "policyType": "Custom"
  },
  "type": "Microsoft.Authorization/policyDefinitions"
}
BylawDeny-Subnet-Without-Penp.bylaw.ps1 · 54 lines
# Converted from Deny-Subnet-Without-Penp.alz_policy_definition.json by ConvertTo-Bylaw.
Policy 'Deny-Subnet-Without-Penp' {

    Properties @{
        DisplayName = 'Subnets without Private Endpoint Network Policies enabled should be denied'
        Description = 'This policy denies the creation of a subnet without Private Endpoint Netwotk Policies enabled. This policy is intended for ''workload'' subnets, not ''central infrastructure'' (aka, ''hub'') subnets.'
        Category    = 'Network'
        Version     = '1.2.0'
        Mode        = 'All'
        Metadata    = [ordered]@{ alzCloudEnvironments = @('AzureCloud', 'AzureChinaCloud', 'AzureUSGovernment'); source = 'https://github.com/Azure/Enterprise-Scale/' }
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Deny'
        DisplayName   = 'Effect'
        Description   = 'The effect determines what happens when the policy rule is evaluated to match'
    }
    Parameter ExcludedSubnets @{
        Type        = 'Array'
        Default     = @('GatewaySubnet', 'AzureFirewallSubnet', 'AzureFirewallManagementSubnet', 'AzureBastionSubnet', 'RouteServerSubnet')
        DisplayName = 'Excluded Subnets'
        Description = 'Array of subnet names that are excluded from this policy'
    }
    Parameter PenpValue @{
        AllowedValues = @('Disabled', 'NetworkSecurityGroupEnabled', 'RouteTableEnabled', 'Enabled')
        Default       = 'Enabled'
        DisplayName   = 'Private Endpoint Network Policies Value'
        Description   = 'The privateEndpointNetworkPolicies value required for evaluated subnets'
    }

    Resource * {

        Require 'type, subnets, privateEndpointNetworkPolicies and 1 more are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks' -IfPresent
                Count 'Microsoft.Network/virtualNetworks/subnets[*]' -Where {
                    Field privateEndpointNetworkPolicies | Must -Not -Be (Param PenpValue)
                    Field name | Must -Not -BeIn (Param ExcludedSubnets)
                } | Must -Be 0
            }
        }

        Require 'type, name and privateEndpointNetworkPolicies are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks/subnets' -IfPresent
                Field name | Must -BeIn (Param ExcludedSubnets)
                Field 'Microsoft.Network/virtualNetworks/subnets/privateEndpointNetworkPolicies' | Must -Be (Param PenpValue)
            }
        }
    }

    Effect (Param Effect)
}

Source: policy_definitions/Deny-Subnet-Without-Penp.alz_policy_definition.json in Azure/Azure-Landing-Zones-Library, under the MIT licence.