Bylaw

Gallery › Network › Azure Community Policy

Prevent subnets without NSG

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This policy prevents subnets without an NSG attached to them.

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

Azure Policy JSONazurepolicy.json · 98 lines
{
  "name": "74c23fa8-5cea-413e-bf2c-8226f7728a46",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Prevent subnets without NSG",
    "description": "This policy prevents subnets without an NSG attached to them.",
    "metadata": {
      "category": "Network",
      "createdBy": "",
      "updatedBy": null,
      "updatedOn": null,
      "version": "1.0.0"
    },
    "mode": "All",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "anyOf": [
          {
            "allOf": [
              {
                "equals": "Microsoft.Network/virtualNetworks/subnets",
                "field": "type"
              },
              {
                "exists": "false",
                "field": "Microsoft.Network/virtualNetworks/subnets/networkSecurityGroup.id"
              },
              {
                "field": "name",
                "notIn": [
                  "GatewaySubnet",
                  "AzureFirewallSubnet",
                  "AzureFirewallManagementSubnet"
                ]
              },
              {
                "equals": 0,
                "value": "[length(field('Microsoft.Network/virtualNetworks/subnets/privateEndpoints[*]'))]"
              }
            ]
          },
          {
            "allOf": [
              {
                "equals": "Microsoft.Network/virtualNetworks",
                "field": "type"
              },
              {
                "count": {
                  "field": "Microsoft.Network/virtualNetworks/subnets[*]",
                  "where": {
                    "allOf": [
                      {
                        "exists": "false",
                        "field": "Microsoft.Network/virtualNetworks/subnets[*].networkSecurityGroup.id"
                      },
                      {
                        "field": "Microsoft.Network/virtualNetworks/subnets[*].name",
                        "notIn": [
                          "GatewaySubnet",
                          "AzureFirewallSubnet",
                          "AzureFirewallManagementSubnet"
                        ]
                      },
                      {
                        "equals": 0,
                        "value": "[length(field('Microsoft.Network/virtualNetworks/subnets[*].privateEndpoints[*]'))]"
                      }
                    ]
                  }
                },
                "notEquals": 0
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw74c23fa8-5cea-413e-bf2c-8226f7728a46.bylaw.ps1 · 43 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '74c23fa8-5cea-413e-bf2c-8226f7728a46' {

    Properties @{
        DisplayName = 'Prevent subnets without NSG'
        Description = 'This policy prevents subnets without an NSG attached to them.'
        Category    = 'Network'
        Version     = '1.0.0'
        Mode        = 'All'
    }

    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource * {

        Require 'type, networkSecurityGroup.id, name and 1 more are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks/subnets' -IfPresent
                Field 'Microsoft.Network/virtualNetworks/subnets/networkSecurityGroup.id' | Must -Exist
                Field name | Must -BeIn @('GatewaySubnet', 'AzureFirewallSubnet', 'AzureFirewallManagementSubnet')
                Arm '[length(field(''Microsoft.Network/virtualNetworks/subnets/privateEndpoints[*]''))]' | Must -Not -Be 0
            }
        }

        Require 'type, subnets, networkSecurityGroup.id and 2 more are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks' -IfPresent
                Count 'Microsoft.Network/virtualNetworks/subnets[*]' -Where {
                    Field networkSecurityGroup.id | Must -Not -Exist
                    Field name | Must -Not -BeIn @('GatewaySubnet', 'AzureFirewallSubnet', 'AzureFirewallManagementSubnet')
                    Arm '[length(field(''Microsoft.Network/virtualNetworks/subnets[*].privateEndpoints[*]''))]' | Must -Be 0
                } | Must -Be 0
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/prevent-subnets-without-nsg/azurepolicy.json in Azure/Community-Policy, under the MIT licence.