Bylaw

Gallery › Network › Azure Community Policy

Deny changing VNet DNS settings from pre-defined value

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This Policy will prevent users from changing DNS settings on a VNet

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

Azure Policy JSONazurepolicy.json · 60 lines
{
  "name": "e2b0f415-c0e3-4ac9-bdfb-c1d4c3579c9b",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deny changing VNet DNS settings from pre-defined value",
    "description": "This Policy will prevent users from changing DNS settings on a VNet",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "dnsSettings": {
        "type": "array",
        "metadata": {
          "displayname": "Enforced DNS Settings",
          "description": "Users will be unable to change the DNS settings on a VNet from the values defined in this array."
        }
      },
      "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": [
          {
            "field": "type",
            "equals": "Microsoft.Network/virtualNetworks"
          },
          {
            "anyOf": [
              {
                "value": "[if(empty(field('Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers')), bool('false'), equals(length(intersection(parameters('dnsSettings'), field('Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers'))), length(parameters('dnsSettings'))))]",
                "equals": false
              },
              {
                "value": "[if(empty(field('Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers')), bool('false'), equals(length(field('Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers')),length(parameters('dnsSettings'))))]",
                "equals": false
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylawe2b0f415-c0e3-4ac9-bdfb-c1d4c3579c9b.bylaw.ps1 · 36 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'e2b0f415-c0e3-4ac9-bdfb-c1d4c3579c9b' {

    Properties @{
        DisplayName = 'Deny changing VNet DNS settings from pre-defined value'
        Description = 'This Policy will prevent users from changing DNS settings on a VNet'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter DnsSettings @{
        Type        = 'array'
        DisplayName = 'Enforced DNS Settings'
        Description = 'Users will be unable to change the DNS settings on a VNet from the values defined in this array.'
    }
    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' {
            Arm '[if(empty(field(''Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers'')), bool(''false''), equals(length(intersection(parameters(''dnsSettings''), field(''Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers''))), length(parameters(''dnsSettings''))))]' | Must -Not -Be $false
        }

        Require 'dhcpOptions.dnsServers is as required' {
            Arm '[if(empty(field(''Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers'')), bool(''false''), equals(length(field(''Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers'')),length(parameters(''dnsSettings''))))]' | Must -Not -Be $false
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/deny-changing-vnet-dns-settings-from-pre-defined-value/azurepolicy.json in Azure/Community-Policy, under the MIT licence.