Bylaw

Gallery › App Service › Azure Community Policy

Configure private DNS zones for private endpoints connected to Static Web App

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Use private DNS zones to override the DNS resolution for a private endpoint. This policy automatically selects the private DNS zone with the corresponding partition id used by the Static Web App.

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

Azure Policy JSONazurepolicy.json · 128 lines
{
  "name": "ee8e785b-1d5a-4e73-971e-e54bb75450f8",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Configure private DNS zones for private endpoints connected to Static Web App",
    "description": "Use private DNS zones to override the DNS resolution for a private endpoint. This policy automatically selects the private DNS zone with the corresponding partition id used by the Static Web App.",
    "metadata": {
      "version": "1.0.0",
      "category": "App Service"
    },
    "mode": "Indexed",
    "parameters": {
      "privateDnsZoneId": {
        "type": "String",
        "metadata": {
          "displayName": "Private DNS zone",
          "description": "Specifies the private DNS zone to use to configure private endpoint. Make sure to have a dedicated DNS zone for each possible partition id, i.e. 'privatelink.2.azurestaticapps.net' for partition id 2 and so on.",
          "strongType": "Microsoft.Network/privateDnsZones"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Network/privateEndpoints"
          },
          {
            "count": {
              "field": "Microsoft.Network/privateEndpoints/privateLinkServiceConnections[*]",
              "where": {
                "allOf": [
                  {
                    "field": "Microsoft.Network/privateEndpoints/privateLinkServiceConnections[*].privateLinkServiceId",
                    "contains": "Microsoft.Web/staticSites"
                  },
                  {
                    "field": "Microsoft.Network/privateEndpoints/privateLinkServiceConnections[*].groupIds[*]",
                    "equals": "staticSites"
                  }
                ]
              }
            },
            "greaterOrEquals": 1
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Network/privateEndpoints/privateDnsZoneGroups",
          "evaluationDelay": "AfterProvisioning",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/4d97b98b-1d4f-4787-a291-c67834d212e7"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "privateDnsZoneId": {
                    "type": "string"
                  },
                  "privateEndpointName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  },
                  "privateLinkServiceId": {
                    "type": "array"
                  }
                },
                "resources": [
                  {
                    "apiVersion": "2023-04-01",
                    "location": "[parameters('location')]",
                    "name": "[concat(parameters('privateEndpointName'), '/deployedByPolicy')]",
                    "properties": {
                      "privateDnsZoneConfigs": [
                        {
                          "name": "privatelink-staticWebApp",
                          "properties": {
                            "privateDnsZoneId": "[if(greaterOrEquals(length(split(reference(parameters('privateLinkServiceId')[0], '2023-12-01').defaultHostname, '.')), 4), replace(parameters('privateDnsZoneId'),'azurestaticapps.net', concat(split(reference(parameters('privateLinkServiceId')[0], '2023-12-01').defaultHostname, '.')[1],'.azurestaticapps.net')), parameters('privateDnsZoneId'))]"
                          }
                        }
                      ]
                    },
                    "type": "Microsoft.Network/privateEndpoints/privateDnsZoneGroups"
                  }
                ]
              },
              "parameters": {
                "privateDnsZoneId": {
                  "value": "[parameters('privateDnsZoneId')]"
                },
                "privateEndpointName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "privateLinkServiceId": {
                  "value": "[field('Microsoft.Network/privateEndpoints/privateLinkServiceConnections[*].privateLinkServiceId')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylawee8e785b-1d5a-4e73-971e-e54bb75450f8.bylaw.ps1 · 46 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'ee8e785b-1d5a-4e73-971e-e54bb75450f8' {

    Properties @{
        DisplayName = 'Configure private DNS zones for private endpoints connected to Static Web App'
        Description = 'Use private DNS zones to override the DNS resolution for a private endpoint. This policy automatically selects the private DNS zone with the corresponding partition id used by the Static Web App.'
        Version     = '1.0.0'
        Category    = 'App Service'
        Mode        = 'Indexed'
    }

    Parameter PrivateDnsZoneId @{
        Type        = 'String'
        DisplayName = 'Private DNS zone'
        Description = 'Specifies the private DNS zone to use to configure private endpoint. Make sure to have a dedicated DNS zone for each possible partition id, i.e. ''privatelink.2.azurestaticapps.net'' for partition id 2 and so on.'
        Metadata    = [ordered]@{ strongType = 'Microsoft.Network/privateDnsZones' }
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource 'Microsoft.Network/privateEndpoints' {

        Require 'privateLinkServiceConnections, privateLinkServiceId and groupIds are as required' {
            Count 'privateLinkServiceConnections[*]' -Where {
                Field privateLinkServiceId | Must -ContainText 'Microsoft.Web/staticSites'
                Field groupIds | Must -Exist
                Count 'groupIds[*]' -Where {
                    Element | Must -Not -Be 'staticSites'
                } | Must -Be 0
            } | Must -Be 0
        }
    }

    DeployIfNotExists 'Microsoft.Network/privateEndpoints/privateDnsZoneGroups' -Role 'Network Contributor' -EvaluationDelay AfterProvisioning -Effect (Param Effect) {
        Deployment -TemplateFile './templates/ee8e785b-1d5a-4e73-971e-e54bb75450f8.json' -Parameters ([ordered]@{
                privateDnsZoneId     = (Param PrivateDnsZoneId)
                privateEndpointName  = (Arm '[field(''name'')]')
                location             = (Arm '[field(''location'')]')
                privateLinkServiceId = (Arm '[field(''Microsoft.Network/privateEndpoints/privateLinkServiceConnections[*].privateLinkServiceId'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/App Service/configure-a-private-dns-zone-for-static-webapp/azurepolicy.json in Azure/Community-Policy, under the MIT licence.