Bylaw

Gallery › Network › Azure Community Policy

Enforce VNET DNS servers

Converted Policy Audit Deny Disabled Mode Indexed Version 1.0.0

This policy prevent settings non authorized dns servers for vnets.

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

Azure Policy JSONazurepolicy.json · 69 lines
{
  "name": "d8ebd9c5-3b41-41bc-845d-5069a808d3ea",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Enforce VNET DNS servers",
    "description": "This policy prevent settings non authorized dns servers for vnets.",
    "metadata": {
      "category": "Network",
      "createdBy": "",
      "updatedBy": null,
      "updatedOn": null,
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "servers": {
        "type": "Array",
        "metadata": {
          "description": "The list of DNS servers to enforce exclusive existance of.",
          "displayName": "DNS Servers list"
        }
      },
      "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": [
          {
            "equals": "Microsoft.Network/virtualNetworks",
            "field": "type"
          },
          {
            "anyOf": [
              {
                "exists": false,
                "field": "Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers[*]"
              },
              {
                "count": {
                  "field": "Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers[*]",
                  "where": {
                    "field": "Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers[*]",
                    "notIn": "[parameters('servers')]"
                  }
                },
                "greater": 0
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylawd8ebd9c5-3b41-41bc-845d-5069a808d3ea.bylaw.ps1 · 40 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'd8ebd9c5-3b41-41bc-845d-5069a808d3ea' {

    Properties @{
        DisplayName = 'Enforce VNET DNS servers'
        Description = 'This policy prevent settings non authorized dns servers for vnets.'
        Category    = 'Network'
        Version     = '1.0.0'
        Mode        = 'Indexed'
    }

    Parameter Servers @{
        Type        = 'Array'
        DisplayName = 'DNS Servers list'
        Description = 'The list of DNS servers to enforce exclusive existance of.'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Network/virtualNetworks' {

        Require 'dhcpOptions.dnsServers is as required' {
            Count 'dhcpOptions.dnsServers[*]' -Where {
                Element | Must -Exist
            } | Must -Not -Be 0
        }

        Require 'dhcpOptions.dnsServers is as required' {
            Each 'dhcpOptions.dnsServers[*]' {
                Element | Must -BeIn (Param Servers)
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/enforce-vnet-dns-servers/azurepolicy.json in Azure/Community-Policy, under the MIT licence.