Bylaw

Gallery › Network › Azure Community Policy

Deny Service Endpoints on Subnets Based on Naming Convention

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This Policy will deny the enablement of Service Endpoints on subnets that contain the string you define in the Policy. This will also prevent PaaS resources such as Azure Storage from being able to add this subnet to their firewall allowed list.

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

Azure Policy JSONazurepolicy.json · 90 lines
{
  "name": "0baf3b25-fb0f-4160-842c-ace6e622feda",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deny Service Endpoints on Subnets Based on Naming Convention",
    "description": "This Policy will deny the enablement of Service Endpoints on subnets that contain the string you define in the Policy. This will also prevent PaaS resources such as Azure Storage from being able to add this subnet to their firewall allowed list.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "targetSubnetName": {
        "type": "String",
        "metadata": {
          "displayName": "Target Subnet Naming Convention",
          "description": "This is the keyword the policy will look for in order to trigger an effect. If you were to enter the value of 'example' any subnets that contained this word would Service Endpoint enablement denied. This is not case sensitive."
        }
      },
      "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.Network/virtualNetworks"
              },
              {
                "count": {
                  "field": "Microsoft.Network/virtualNetworks/subnets[*]",
                  "where": {
                    "allOf": [
                      {
                        "not": {
                          "field": "Microsoft.Network/virtualNetworks/subnets[*].serviceEndpoints[*].service",
                          "notLike": "*"
                        }
                      },
                      {
                        "field": "Microsoft.Network/virtualNetworks/subnets[*].name",
                        "contains": "[parameters('targetSubnetName')]"
                      }
                    ]
                  }
                },
                "greater": 0
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Network/virtualNetworks/subnets"
              },
              {
                "field": "name",
                "contains": "[parameters('targetSubnetName')]"
              },
              {
                "not": {
                  "field": "Microsoft.Network/virtualNetworks/subnets/serviceEndpoints[*].service",
                  "notLike": "*"
                }
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw0baf3b25-fb0f-4160-842c-ace6e622feda.bylaw.ps1 · 51 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '0baf3b25-fb0f-4160-842c-ace6e622feda' {

    Properties @{
        DisplayName = 'Deny Service Endpoints on Subnets Based on Naming Convention'
        Description = 'This Policy will deny the enablement of Service Endpoints on subnets that contain the string you define in the Policy. This will also prevent PaaS resources such as Azure Storage from being able to add this subnet to their firewall allowed list.'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter TargetSubnetName @{
        Type        = 'String'
        DisplayName = 'Target Subnet Naming Convention'
        Description = 'This is the keyword the policy will look for in order to trigger an effect. If you were to enter the value of ''example'' any subnets that contained this word would Service Endpoint enablement denied. This is not case sensitive.'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource * {

        Require 'type, subnets, name and 2 more are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks' -IfPresent
                Each 'Microsoft.Network/virtualNetworks/subnets[*]' -Where {
                    Field name | Must -ContainText (Param TargetSubnetName)
                } {
                    Each 'serviceEndpoints[*]' {
                        Field service | Must -Not -BeLike '*'
                    }
                }
            }
        }

        Require 'type, name, serviceEndpoints and 1 more are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks/subnets' -IfPresent
                Field name | Must -Not -ContainText (Param TargetSubnetName) -IfPresent
                Each 'Microsoft.Network/virtualNetworks/subnets/serviceEndpoints[*]' {
                    Field service | Must -Not -BeLike '*'
                }
            }
        }
    }

    Effect (Param Effect)
}

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