Gallery › Network › Azure Community Policy
Subnets must have an NSG and that NSG must have the same suffix as the subnet
Converted Policy Audit Deny Disabled Mode All Version 1.0.0
This Policy requires that all subnets have an NSG and that the provisioned NSG shares the same suffix as it's attached Subnet.
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONazurepolicy.json · 106 lines
{
"name": "de1b0589-1706-4262-a4ae-de82aff55300",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Subnets must have an NSG and that NSG must have the same suffix as the subnet",
"description": "This Policy requires that all subnets have an NSG and that the provisioned NSG shares the same suffix as it's attached Subnet.",
"metadata": {
"version": "1.0.0",
"category": "Network"
},
"mode": "All",
"parameters": {
"exceptionList": {
"type": "Array",
"metadata": {
"displayName": "Exempt Subnets",
"description": "Subnets with this name will be exempt from this Policy"
}
},
"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": [
{
"field": "type",
"equals": "Microsoft.Network/virtualNetworks"
},
{
"count": {
"field": "Microsoft.Network/virtualNetworks/subnets[*]",
"where": {
"allOf": [
{
"value": "[if(empty(field('Microsoft.Network/virtualNetworks/subnets[*].networkSecurityGroup.id')), bool('false'), endswith(substring(last(split(string(field('Microsoft.Network/virtualNetworks/subnets[*].networkSecurityGroup.id')), 'networkSecurityGroups/')), 0, sub(length(last(split(string(field('Microsoft.Network/virtualNetworks/subnets[*].networkSecurityGroup.id')), 'networkSecurityGroups/'))), 2)), substring(last(split(string(field('Microsoft.Network/virtualNetworks/subnets[*].name')), '-')), 0, sub(length(last(split(string(field('Microsoft.Network/virtualNetworks/subnets[*].name')), '-'))), 2))))]",
"equals": false
},
{
"not": {
"anyOf": [
{
"field": "Microsoft.Network/virtualNetworks/subnets[*].name",
"equals": "GatewaySubnet"
},
{
"field": "Microsoft.Network/virtualNetworks/subnets[*].name",
"equals": "AzureFirewallSubnet"
}
]
}
}
]
}
},
"greater": 0
}
]
},
{
"allOf": [
{
"field": "type",
"equals": "Microsoft.Network/virtualNetworks/subnets"
},
{
"value": "[if(empty(field('Microsoft.Network/virtualNetworks/subnets/networkSecurityGroup.id')), bool('false'), endswith(last(split(substring(last(split(field('Microsoft.Network/virtualNetworks/subnets/networkSecurityGroup.id'), 'networkSecurityGroups/')), 0, length(last(split(field('Microsoft.Network/virtualNetworks/subnets/networkSecurityGroup.id'), 'networkSecurityGroups/')))), '-')), last(split(field('name'),'-'))))]",
"equals": false
},
{
"not": {
"anyOf": [
{
"field": "name",
"equals": "GatewaySubnet"
},
{
"field": "name",
"equals": "AzureFirewallSubnet"
}
]
}
}
]
}
]
},
"then": {
"effect": "[parameters('effect')]"
}
}
}
}Bylawde1b0589-1706-4262-a4ae-de82aff55300.bylaw.ps1 · 53 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'de1b0589-1706-4262-a4ae-de82aff55300' {
Properties @{
DisplayName = 'Subnets must have an NSG and that NSG must have the same suffix as the subnet'
Description = 'This Policy requires that all subnets have an NSG and that the provisioned NSG shares the same suffix as it''s attached Subnet.'
Version = '1.0.0'
Category = 'Network'
Mode = 'All'
}
Parameter ExceptionList @{
Type = 'Array'
DisplayName = 'Exempt Subnets'
Description = 'Subnets with this name will be exempt from this Policy'
}
Parameter Effect @{
AllowedValues = @('Deny', 'Audit', 'Disabled')
Default = 'Audit'
DisplayName = 'Effect'
Description = 'Deny, Audit or Disabled the execution of the Policy'
}
Resource * {
Require 'type, subnets, name and 2 more are as required' {
AnyOf {
Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks' -IfPresent
Each 'Microsoft.Network/virtualNetworks/subnets[*]' -Where {
Arm '[if(empty(field(''Microsoft.Network/virtualNetworks/subnets[*].networkSecurityGroup.id'')), bool(''false''), endswith(substring(last(split(string(field(''Microsoft.Network/virtualNetworks/subnets[*].networkSecurityGroup.id'')), ''networkSecurityGroups/'')), 0, sub(length(last(split(string(field(''Microsoft.Network/virtualNetworks/subnets[*].networkSecurityGroup.id'')), ''networkSecurityGroups/''))), 2)), substring(last(split(string(field(''Microsoft.Network/virtualNetworks/subnets[*].name'')), ''-'')), 0, sub(length(last(split(string(field(''Microsoft.Network/virtualNetworks/subnets[*].name'')), ''-''))), 2))))]' | Must -Be $false
} {
AnyOf {
Field name | Must -Be 'GatewaySubnet' -Strict
Field name | Must -Be 'AzureFirewallSubnet' -Strict
}
}
}
}
Require 'type, name and networkSecurityGroup.id are as required' {
AnyOf {
Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks/subnets' -IfPresent
Arm '[if(empty(field(''Microsoft.Network/virtualNetworks/subnets/networkSecurityGroup.id'')), bool(''false''), endswith(last(split(substring(last(split(field(''Microsoft.Network/virtualNetworks/subnets/networkSecurityGroup.id''), ''networkSecurityGroups/'')), 0, length(last(split(field(''Microsoft.Network/virtualNetworks/subnets/networkSecurityGroup.id''), ''networkSecurityGroups/'')))), ''-'')), last(split(field(''name''),''-''))))]' | Must -Not -Be $false
AnyOf {
Field name | Must -Be 'GatewaySubnet'
Field name | Must -Be 'AzureFirewallSubnet'
}
}
}
}
Effect (Param Effect)
}Source: policyDefinitions/Network/subnets-must-have-an-nsg-and-that-nsg-must-have-the-same-suffix-as-the-subnet/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw