Bylaw

Gallery › Mission › Azure built-in policies

Virtual Network should Specify the allowed service delegations for subnets

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This policy applies exclusively to Mission Platform products and services; usage outside of these scopes is not supported. Restricts which Azure services can be delegated control of subnets to ensure only approved services can inject resources into the virtual network.

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

Azure Policy JSONVNETAllowedServiceDelegations_Audit.json · 103 lines
{
  "properties": {
    "displayName": "Virtual Network should Specify the allowed service delegations for subnets",
    "policyType": "BuiltIn",
    "mode": "All",
    "description": "This policy applies exclusively to Mission Platform products and services; usage outside of these scopes is not supported. Restricts which Azure services can be delegated control of subnets to ensure only approved services can inject resources into the virtual network.",
    "metadata": {
      "version": "1.0.0",
      "category": "Mission"
    },
    "version": "1.0.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Virtual Network should Specify the allowed service delegations for subnets Effect",
          "description": " Specify the allowed service delegations for subnets Effect"
        },
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Audit"
      },
      "allowedServiceNames": {
        "type": "Array",
        "metadata": {
          "displayName": "Allowed Service Names for Delegation",
          "description": " Allowed Service Names for Delegation. Example: Microsoft.Web/serverFarms, Microsoft.Synapse/workspaces"
        },
        "defaultValue": []
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "in": [
              "Microsoft.Network/virtualNetworks",
              "Microsoft.Network/virtualNetworks/subnets"
            ]
          },
          {
            "anyOf": [
              {
                "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.Network/virtualNetworks/subnets"
                  },
                  {
                    "count": {
                      "field": "Microsoft.Network/virtualNetworks/subnets/delegations[*]",
                      "where": {
                        "value": "[current('Microsoft.Network/virtualNetworks/subnets/delegations[*].serviceName')]",
                        "notIn": "[parameters('allowedServiceNames')]"
                      }
                    },
                    "greater": 0
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.Network/virtualNetworks"
                  },
                  {
                    "count": {
                      "field": "Microsoft.Network/virtualNetworks/subnets[*]",
                      "where": {
                        "count": {
                          "field": "Microsoft.Network/virtualNetworks/subnets[*].delegations[*]",
                          "where": {
                            "value": "[current('Microsoft.Network/virtualNetworks/subnets[*].delegations[*].serviceName')]",
                            "notIn": "[parameters('allowedServiceNames')]"
                          }
                        },
                        "greater": 0
                      }
                    },
                    "greater": 0
                  }
                ]
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/7ecf8dcc-8f39-4225-9ff8-06a8e75d778b",
  "name": "7ecf8dcc-8f39-4225-9ff8-06a8e75d778b"
}
Bylaw7ecf8dcc-8f39-4225-9ff8-06a8e75d778b.bylaw.ps1 · 54 lines
# Converted from VNETAllowedServiceDelegations_Audit.json by ConvertTo-Bylaw.
Policy '7ecf8dcc-8f39-4225-9ff8-06a8e75d778b' {

    Properties @{
        DisplayName   = 'Virtual Network should Specify the allowed service delegations for subnets'
        Description   = 'This policy applies exclusively to Mission Platform products and services; usage outside of these scopes is not supported. Restricts which Azure services can be delegated control of subnets to ensure only approved services can inject resources into the virtual network.'
        Version       = '1.0.0'
        Category      = 'Mission'
        PolicyVersion = '1.0.0'
        Mode          = 'All'
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Virtual Network should Specify the allowed service delegations for subnets Effect'
        Description   = ' Specify the allowed service delegations for subnets Effect'
    }
    Parameter AllowedServiceNames @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Allowed Service Names for Delegation'
        Description = ' Allowed Service Names for Delegation. Example: Microsoft.Web/serverFarms, Microsoft.Synapse/workspaces'
    }

    Resource * {

        When {
            Field type | Must -BeIn @('Microsoft.Network/virtualNetworks', 'Microsoft.Network/virtualNetworks/subnets')
        }

        Require 'type and delegations are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks/subnets' -IfPresent
                Count 'Microsoft.Network/virtualNetworks/subnets/delegations[*]' -Where {
                    Arm '[current(''Microsoft.Network/virtualNetworks/subnets/delegations[*].serviceName'')]' | Must -Not -BeIn (Param AllowedServiceNames)
                } | Must -Be 0
            }
        }

        Require 'type, subnets and delegations are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks' -IfPresent
                Each 'Microsoft.Network/virtualNetworks/subnets[*]' {
                    Count 'delegations[*]' -Where {
                        Arm '[current(''Microsoft.Network/virtualNetworks/subnets[*].delegations[*].serviceName'')]' | Must -Not -BeIn (Param AllowedServiceNames)
                    } | Must -Be 0
                }
            }
        }
    }

    Effect (Param Effect)
}

Source: built-in-policies/policyDefinitions/Mission/VNETAllowedServiceDelegations_Audit.json in Azure/azure-policy, under the MIT licence.