Bylaw

Gallery › Network › Azure Community Policy

Block NSG Creations and Updates

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

need to add description

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

Azure Policy JSONazurepolicy.json · 93 lines
{
  "name": "369e7d90-cb62-4a3c-bd5a-6ba4659a2704",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Block NSG Creations and Updates",
    "description": "need to add description",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "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": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Network/networkSecurityGroups"
          },
          {
            "count": {
              "field": "Microsoft.Network/networkSecurityGroups/securityRules[*]",
              "where": {
                "allOf": [
                  {
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].access",
                    "equals": "Allow"
                  },
                  {
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].direction",
                    "equals": "Inbound"
                  },
                  {
                    "anyOf": [
                      {
                        "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].destinationPortRange",
                        "equals": "*"
                      },
                      {
                        "not": {
                          "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].destinationPortRanges[*]",
                          "notEquals": "*"
                        }
                      }
                    ]
                  },
                  {
                    "anyOf": [
                      {
                        "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].sourceAddressPrefix",
                        "in": [
                          "*",
                          "Internet"
                        ]
                      },
                      {
                        "not": {
                          "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].sourceAddressPrefixes[*]",
                          "notIn": [
                            "*",
                            "Internet"
                          ]
                        }
                      }
                    ]
                  }
                ]
              }
            },
            "greaterOrEquals": 1
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw369e7d90-cb62-4a3c-bd5a-6ba4659a2704.bylaw.ps1 · 42 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '369e7d90-cb62-4a3c-bd5a-6ba4659a2704' {

    Properties @{
        DisplayName = 'Block NSG Creations and Updates'
        Description = 'need to add description'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

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

    Resource 'Microsoft.Network/networkSecurityGroups' {

        Require 'securityRules, access, direction and 4 more are as required' {
            Count 'securityRules[*]' -Where {
                Field access | Must -Be 'Allow'
                Field direction | Must -Be 'Inbound'
                AnyOf {
                    Field destinationPortRange | Must -Be '*'
                    Count 'destinationPortRanges[*]' -Where {
                        Element | Must -Be '*'
                    } | Must -BeGreaterThan 0
                }
                AnyOf {
                    Field sourceAddressPrefix | Must -BeIn @('*', 'Internet')
                    Count 'sourceAddressPrefixes[*]' -Where {
                        Element | Must -BeIn @('*', 'Internet')
                    } | Must -BeGreaterThan 0
                }
            } | Must -Be 0
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/block-nsg-creations-and-updates/azurepolicy.json in Azure/Community-Policy, under the MIT licence.