Bylaw

Gallery › Network › Azure Landing Zones Library

User Defined Routes with 'Next Hop Type' set to 'Internet' or 'VirtualNetworkGateway' should be denied

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This policy denies the creation of a User Defined Route with 'Next Hop Type' set to 'Internet' or 'VirtualNetworkGateway'.

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

Azure Policy JSONDeny-UDR-With-Specific-NextHop.alz_policy_definition.json · 85 lines
{
  "name": "Deny-UDR-With-Specific-NextHop",
  "properties": {
    "description": "This policy denies the creation of a User Defined Route with 'Next Hop Type' set to 'Internet' or 'VirtualNetworkGateway'.",
    "displayName": "User Defined Routes with 'Next Hop Type' set to 'Internet' or 'VirtualNetworkGateway' should be denied",
    "metadata": {
      "alzCloudEnvironments": [
        "AzureCloud",
        "AzureChinaCloud",
        "AzureUSGovernment"
      ],
      "category": "Network",
      "source": "https://github.com/Azure/Enterprise-Scale/",
      "version": "1.0.0"
    },
    "mode": "All",
    "parameters": {
      "effect": {
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Deny",
        "metadata": {
          "description": "The effect determines what happens when the policy rule is evaluated to match",
          "displayName": "Effect"
        },
        "type": "String"
      },
      "excludedDestinations": {
        "defaultValue": [
          "Internet",
          "VirtualNetworkGateway"
        ],
        "metadata": {
          "description": "Array of route destinations that are to be denied",
          "displayName": "Excluded Destinations"
        },
        "type": "Array"
      }
    },
    "policyRule": {
      "if": {
        "anyOf": [
          {
            "allOf": [
              {
                "equals": "Microsoft.Network/routeTables",
                "field": "type"
              },
              {
                "count": {
                  "field": "Microsoft.Network/routeTables/routes[*]",
                  "where": {
                    "field": "Microsoft.Network/routeTables/routes[*].nextHopType",
                    "in": "[parameters('excludedDestinations')]"
                  }
                },
                "notEquals": 0
              }
            ]
          },
          {
            "allOf": [
              {
                "equals": "Microsoft.Network/routeTables/routes",
                "field": "type"
              },
              {
                "field": "Microsoft.Network/routeTables/routes/nextHopType",
                "in": "[parameters('excludedDestinations')]"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "policyType": "Custom"
  },
  "type": "Microsoft.Authorization/policyDefinitions"
}
BylawDeny-UDR-With-Specific-NextHop.bylaw.ps1 · 46 lines
# Converted from Deny-UDR-With-Specific-NextHop.alz_policy_definition.json by ConvertTo-Bylaw.
Policy 'Deny-UDR-With-Specific-NextHop' {

    Properties @{
        DisplayName = 'User Defined Routes with ''Next Hop Type'' set to ''Internet'' or ''VirtualNetworkGateway'' should be denied'
        Description = 'This policy denies the creation of a User Defined Route with ''Next Hop Type'' set to ''Internet'' or ''VirtualNetworkGateway''.'
        Category    = 'Network'
        Version     = '1.0.0'
        Mode        = 'All'
        Metadata    = [ordered]@{ alzCloudEnvironments = @('AzureCloud', 'AzureChinaCloud', 'AzureUSGovernment'); source = 'https://github.com/Azure/Enterprise-Scale/' }
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Deny'
        DisplayName   = 'Effect'
        Description   = 'The effect determines what happens when the policy rule is evaluated to match'
    }
    Parameter ExcludedDestinations @{
        Type        = 'Array'
        Default     = @('Internet', 'VirtualNetworkGateway')
        DisplayName = 'Excluded Destinations'
        Description = 'Array of route destinations that are to be denied'
    }

    Resource * {

        Require 'type, routes and nextHopType are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/routeTables' -IfPresent
                Count 'Microsoft.Network/routeTables/routes[*]' -Where {
                    Field nextHopType | Must -BeIn (Param ExcludedDestinations)
                } | Must -Be 0
            }
        }

        Require 'type and nextHopType are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/routeTables/routes' -IfPresent
                Field 'Microsoft.Network/routeTables/routes/nextHopType' | Must -Not -BeIn (Param ExcludedDestinations) -IfPresent
            }
        }
    }

    Effect (Param Effect)
}

Source: policy_definitions/Deny-UDR-With-Specific-NextHop.alz_policy_definition.json in Azure/Azure-Landing-Zones-Library, under the MIT licence.