Gallery › Network › Azure Community Policy
Deny subnets missing suffix
Converted Policy Audit Deny Disabled Mode All Version 1.0.0
This Policy will deny the creation of subnets that do not contain one of the suffixes defined within this Policy.
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONazurepolicy.json · 110 lines
{
"name": "82778e1e-cf94-498c-8bd0-234fc9b688d8",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Deny subnets missing suffix",
"description": "This Policy will deny the creation of subnets that do not contain one of the suffixes defined within this Policy.",
"metadata": {
"version": "1.0.0",
"category": "Network"
},
"mode": "All",
"parameters": {
"requiredSuffix": {
"type": "Array",
"metadata": {
"displayName": "Required Suffixes",
"description": "Assessment is performed as such: 'exameple-subnet-suffix', would be split by '-' and the last value would be returned. That would be the word 'suffix' in this example. If that word was not defined in this list the creation of that subnet would be denied."
}
},
"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": [
{
"not": {
"value": "[substring(last(split(string(field('Microsoft.Network/virtualNetworks/subnets[*].name')), '-')), 0, sub(length(last(split(string(field('Microsoft.Network/virtualNetworks/subnets[*].name')), '-'))), 2))]",
"in": "[parameters('requiredSuffix')]"
}
},
{
"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"
},
{
"not": {
"value": "[last(split(field('name'),'-'))]",
"in": "[parameters('requiredSuffix')]"
}
},
{
"not": {
"anyOf": [
{
"field": "name",
"equals": "GatewaySubnet"
},
{
"field": "name",
"equals": "AzureFirewallSubnet"
}
]
}
}
]
}
]
},
"then": {
"effect": "[parameters('effect')]"
}
}
}
}Bylaw82778e1e-cf94-498c-8bd0-234fc9b688d8.bylaw.ps1 · 50 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '82778e1e-cf94-498c-8bd0-234fc9b688d8' {
Properties @{
DisplayName = 'Deny subnets missing suffix'
Description = 'This Policy will deny the creation of subnets that do not contain one of the suffixes defined within this Policy.'
Version = '1.0.0'
Category = 'Network'
Mode = 'All'
}
Parameter RequiredSuffix @{
Type = 'Array'
DisplayName = 'Required Suffixes'
Description = 'Assessment is performed as such: ''exameple-subnet-suffix'', would be split by ''-'' and the last value would be returned. That would be the word ''suffix'' in this example. If that word was not defined in this list the creation of that subnet would be denied.'
}
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 1 more are as required' {
AnyOf {
Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks' -IfPresent
Count 'Microsoft.Network/virtualNetworks/subnets[*]' -Where {
Arm '[substring(last(split(string(field(''Microsoft.Network/virtualNetworks/subnets[*].name'')), ''-'')), 0, sub(length(last(split(string(field(''Microsoft.Network/virtualNetworks/subnets[*].name'')), ''-''))), 2))]' | Must -Not -BeIn (Param RequiredSuffix)
Field name | Must -Not -Be 'GatewaySubnet'
Field name | Must -Not -Be 'AzureFirewallSubnet'
} | Must -Be 0
}
}
Require 'type and name are as required' {
AnyOf {
Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks/subnets' -IfPresent
Arm '[last(split(field(''name''),''-''))]' | Must -BeIn (Param RequiredSuffix)
AnyOf {
Field name | Must -Be 'GatewaySubnet'
Field name | Must -Be 'AzureFirewallSubnet'
}
}
}
}
Effect (Param Effect)
}Source: policyDefinitions/Network/deny-subnets-missing-suffix/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw