Bylaw

Gallery › Network › Azure Community Policy

deny-ports-nsg

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 · 97 lines
{
  "name": "8abf7f28-f631-4700-9ff7-26f7e994458a",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "deny-ports-nsg",
    "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": {
        "anyOf": [
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Network/networkSecurityGroups/securityRules"
              },
              {
                "not": {
                  "field": "Microsoft.Network/networkSecurityGroups/securityRules/sourceAddressPrefix",
                  "notEquals": "*"
                }
              },
              {
                "anyOf": [
                  {
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules/destinationPortRange",
                    "equals": "22"
                  },
                  {
                    "field": "Microsoft.Network/networkSecurityGroups/securityRules/destinationPortRange",
                    "equals": "3389"
                  }
                ]
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Network/networkSecurityGroups"
              },
              {
                "count": {
                  "field": "Microsoft.Network/networkSecurityGroups/securityRules[*]",
                  "where": {
                    "allOf": [
                      {
                        "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].sourceAddressPrefix",
                        "equals": "*"
                      },
                      {
                        "anyOf": [
                          {
                            "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].destinationPortRange",
                            "equals": "22"
                          },
                          {
                            "field": "Microsoft.Network/networkSecurityGroups/securityRules[*].destinationPortRange",
                            "equals": "3389"
                          }
                        ]
                      }
                    ]
                  }
                },
                "greater": 0
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw8abf7f28-f631-4700-9ff7-26f7e994458a.bylaw.ps1 · 46 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '8abf7f28-f631-4700-9ff7-26f7e994458a' {

    Properties @{
        DisplayName = 'deny-ports-nsg'
        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 * {

        Require 'type, sourceAddressPrefix and destinationPortRange are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/networkSecurityGroups/securityRules' -IfPresent
                Field 'Microsoft.Network/networkSecurityGroups/securityRules/sourceAddressPrefix' | Must -Not -Be '*' -IfPresent
                AllOf {
                    Field 'Microsoft.Network/networkSecurityGroups/securityRules/destinationPortRange' | Must -Not -Be '22' -IfPresent
                    Field 'Microsoft.Network/networkSecurityGroups/securityRules/destinationPortRange' | Must -Not -Be '3389' -IfPresent
                }
            }
        }

        Require 'type, securityRules, sourceAddressPrefix and 1 more are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Network/networkSecurityGroups' -IfPresent
                Each 'Microsoft.Network/networkSecurityGroups/securityRules[*]' -Where {
                    Field sourceAddressPrefix | Must -Be '*'
                } {
                    Field destinationPortRange | Must -Not -Be '22'
                    Field destinationPortRange | Must -Not -Be '3389'
                }
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/deny-ports-nsg/azurepolicy.json in Azure/Community-Policy, under the MIT licence.