Bylaw

Gallery › Network › Azure Community Policy

deny-nsgs-with-rules-with-source-any

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 · 77 lines
{
  "name": "274b4f9f-31c1-4ec1-b53e-5f397816392f",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "deny-nsgs-with-rules-with-source-any",
    "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[*].sourceAddressPrefix",
                    "equals": "*"
                  },
                  {
                    "anyOf": [
                      {
                        "not": {
                          "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].sourceAddressPrefixes[*]",
                          "notIn": []
                        }
                      },
                      {
                        "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].sourceAddressPrefixes[*]",
                        "exists": false
                      }
                    ]
                  },
                  {
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].access",
                    "equals": "Allow"
                  },
                  {
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].direction",
                    "equals": "Inbound"
                  }
                ]
              }
            },
            "greater": 0
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw274b4f9f-31c1-4ec1-b53e-5f397816392f.bylaw.ps1 · 38 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '274b4f9f-31c1-4ec1-b53e-5f397816392f' {

    Properties @{
        DisplayName = 'deny-nsgs-with-rules-with-source-any'
        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, sourceAddressPrefix, access and 2 more are as required' {
            Each 'securityRules[*]' -Where {
                Field sourceAddressPrefix | Must -Be '*'
                Field access | Must -Be 'Allow'
                Field direction | Must -Be 'Inbound'
            } {
                Each 'sourceAddressPrefixes[*]' {
                    Element | Must -Not -BeIn @()
                }
                Count 'sourceAddressPrefixes[*]' -Where {
                    Element | Must -Exist
                } | Must -Not -Be 0
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/deny-nsgs-with-rules-with-source-any/azurepolicy.json in Azure/Community-Policy, under the MIT licence.