Bylaw

Gallery › Network › Azure Community Policy

NSG X on every subnet

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This policy enforces a specific NSG on every subnet

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

Azure Policy JSONazurepolicy.json · 72 lines
{
  "name": "c8178b2d-ab54-4c43-9620-7a2aa854049e",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "NSG X on every subnet",
    "description": "This policy enforces a specific NSG on every subnet",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "nsgId": {
        "type": "String",
        "metadata": {
          "description": "Resource Id of the Network Security Group",
          "displayName": "NSG Id"
        }
      },
      "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"
              },
              {
                "not": {
                  "field": "Microsoft.Network/virtualNetworks/subnets[*].networkSecurityGroup.id",
                  "equals": "[parameters('nsgId')]"
                }
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Network/virtualNetworks/subnets"
              },
              {
                "not": {
                  "field": "Microsoft.Network/virtualNetworks/subnets/networkSecurityGroup.id",
                  "equals": "[parameters('nsgId')]"
                }
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylawc8178b2d-ab54-4c43-9620-7a2aa854049e.bylaw.ps1 · 47 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'c8178b2d-ab54-4c43-9620-7a2aa854049e' {

    Properties @{
        DisplayName = 'NSG X on every subnet'
        Description = 'This policy enforces a specific NSG on every subnet'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter NsgId @{
        Type        = 'String'
        DisplayName = 'NSG Id'
        Description = 'Resource Id of the Network Security Group'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource * {

        Require 'type, subnets and networkSecurityGroup.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks' -IfPresent
                AllOf {
                    Field 'Microsoft.Network/virtualNetworks/subnets' | Must -Exist
                    Each 'Microsoft.Network/virtualNetworks/subnets[*]' {
                        Field networkSecurityGroup.id | Must -Be (Param NsgId) -Strict
                    }
                }
            }
        }

        Require 'type and networkSecurityGroup.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/virtualNetworks/subnets' -IfPresent
                Field 'Microsoft.Network/virtualNetworks/subnets/networkSecurityGroup.id' | Must -Be (Param NsgId)
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/nsg-x-on-every-subnet/azurepolicy.json in Azure/Community-Policy, under the MIT licence.